rtlwifi: Convert RT_TRACE macro to use ##__VA_ARGS__
[deliverable/linux.git] / drivers / net / wireless / rtlwifi / pci.c
CommitLineData
0c817338
LF
1/******************************************************************************
2 *
3 * Copyright(c) 2009-2010 Realtek Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
25 *
26 * Larry Finger <Larry.Finger@lwfinger.net>
27 *
28 *****************************************************************************/
29
ee40fa06 30#include <linux/export.h>
0c817338
LF
31#include "core.h"
32#include "wifi.h"
33#include "pci.h"
34#include "base.h"
35#include "ps.h"
c7cfe38e 36#include "efuse.h"
0c817338
LF
37
38static const u16 pcibridge_vendors[PCI_BRIDGE_VENDOR_MAX] = {
f01dce94
JM
39 PCI_VENDOR_ID_INTEL,
40 PCI_VENDOR_ID_ATI,
41 PCI_VENDOR_ID_AMD,
42 PCI_VENDOR_ID_SI
0c817338
LF
43};
44
c7cfe38e
C
45static const u8 ac_to_hwq[] = {
46 VO_QUEUE,
47 VI_QUEUE,
48 BE_QUEUE,
49 BK_QUEUE
50};
51
d3bb1429 52static u8 _rtl_mac_to_hwqueue(struct ieee80211_hw *hw,
c7cfe38e
C
53 struct sk_buff *skb)
54{
55 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
d3bb1429 56 __le16 fc = rtl_get_fc(skb);
c7cfe38e
C
57 u8 queue_index = skb_get_queue_mapping(skb);
58
59 if (unlikely(ieee80211_is_beacon(fc)))
60 return BEACON_QUEUE;
61 if (ieee80211_is_mgmt(fc))
62 return MGNT_QUEUE;
63 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE)
64 if (ieee80211_is_nullfunc(fc))
65 return HIGH_QUEUE;
66
67 return ac_to_hwq[queue_index];
68}
69
0c817338
LF
70/* Update PCI dependent default settings*/
71static void _rtl_pci_update_default_setting(struct ieee80211_hw *hw)
72{
73 struct rtl_priv *rtlpriv = rtl_priv(hw);
74 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
75 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
76 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
77 u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
c7cfe38e 78 u8 init_aspm;
0c817338
LF
79
80 ppsc->reg_rfps_level = 0;
3db1cd5c 81 ppsc->support_aspm = false;
0c817338
LF
82
83 /*Update PCI ASPM setting */
84 ppsc->const_amdpci_aspm = rtlpci->const_amdpci_aspm;
85 switch (rtlpci->const_pci_aspm) {
86 case 0:
87 /*No ASPM */
88 break;
89
90 case 1:
91 /*ASPM dynamically enabled/disable. */
92 ppsc->reg_rfps_level |= RT_RF_LPS_LEVEL_ASPM;
93 break;
94
95 case 2:
96 /*ASPM with Clock Req dynamically enabled/disable. */
97 ppsc->reg_rfps_level |= (RT_RF_LPS_LEVEL_ASPM |
98 RT_RF_OFF_LEVL_CLK_REQ);
99 break;
100
101 case 3:
102 /*
103 * Always enable ASPM and Clock Req
104 * from initialization to halt.
105 * */
106 ppsc->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM);
107 ppsc->reg_rfps_level |= (RT_RF_PS_LEVEL_ALWAYS_ASPM |
108 RT_RF_OFF_LEVL_CLK_REQ);
109 break;
110
111 case 4:
112 /*
113 * Always enable ASPM without Clock Req
114 * from initialization to halt.
115 * */
116 ppsc->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM |
117 RT_RF_OFF_LEVL_CLK_REQ);
118 ppsc->reg_rfps_level |= RT_RF_PS_LEVEL_ALWAYS_ASPM;
119 break;
120 }
121
122 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
123
124 /*Update Radio OFF setting */
125 switch (rtlpci->const_hwsw_rfoff_d3) {
126 case 1:
127 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
128 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
129 break;
130
131 case 2:
132 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
133 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
134 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
135 break;
136
137 case 3:
138 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_PCI_D3;
139 break;
140 }
141
142 /*Set HW definition to determine if it supports ASPM. */
143 switch (rtlpci->const_support_pciaspm) {
c7cfe38e
C
144 case 0:{
145 /*Not support ASPM. */
146 bool support_aspm = false;
147 ppsc->support_aspm = support_aspm;
148 break;
149 }
150 case 1:{
151 /*Support ASPM. */
152 bool support_aspm = true;
153 bool support_backdoor = true;
154 ppsc->support_aspm = support_aspm;
155
156 /*if (priv->oem_id == RT_CID_TOSHIBA &&
157 !priv->ndis_adapter.amd_l1_patch)
158 support_backdoor = false; */
159
160 ppsc->support_backdoor = support_backdoor;
161
162 break;
163 }
0c817338
LF
164 case 2:
165 /*ASPM value set by chipset. */
c7cfe38e
C
166 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_INTEL) {
167 bool support_aspm = true;
168 ppsc->support_aspm = support_aspm;
169 }
0c817338
LF
170 break;
171 default:
172 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
f30d7507 173 "switch case not processed\n");
0c817338
LF
174 break;
175 }
c7cfe38e
C
176
177 /* toshiba aspm issue, toshiba will set aspm selfly
178 * so we should not set aspm in driver */
179 pci_read_config_byte(rtlpci->pdev, 0x80, &init_aspm);
180 if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8192SE &&
181 init_aspm == 0x43)
182 ppsc->support_aspm = false;
183}
184
0c817338
LF
185static bool _rtl_pci_platform_switch_device_pci_aspm(
186 struct ieee80211_hw *hw,
187 u8 value)
188{
189 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
c7cfe38e
C
190 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
191
192 if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE)
193 value |= 0x40;
0c817338 194
0c817338
LF
195 pci_write_config_byte(rtlpci->pdev, 0x80, value);
196
32473284 197 return false;
0c817338
LF
198}
199
200/*When we set 0x01 to enable clk request. Set 0x0 to disable clk req.*/
201static bool _rtl_pci_switch_clk_req(struct ieee80211_hw *hw, u8 value)
202{
203 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
c7cfe38e 204 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0c817338 205
0c817338 206 pci_write_config_byte(rtlpci->pdev, 0x81, value);
0c817338 207
c7cfe38e
C
208 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE)
209 udelay(100);
210
32473284 211 return true;
0c817338
LF
212}
213
214/*Disable RTL8192SE ASPM & Disable Pci Bridge ASPM*/
215static void rtl_pci_disable_aspm(struct ieee80211_hw *hw)
216{
217 struct rtl_priv *rtlpriv = rtl_priv(hw);
218 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
219 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
220 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
221 u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
0c817338
LF
222 u8 num4bytes = pcipriv->ndis_adapter.num4bytes;
223 /*Retrieve original configuration settings. */
224 u8 linkctrl_reg = pcipriv->ndis_adapter.linkctrl_reg;
225 u16 pcibridge_linkctrlreg = pcipriv->ndis_adapter.
226 pcibridge_linkctrlreg;
227 u16 aspmlevel = 0;
32473284 228 u8 tmp_u1b = 0;
0c817338 229
c7cfe38e
C
230 if (!ppsc->support_aspm)
231 return;
232
0c817338
LF
233 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_UNKNOWN) {
234 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
f30d7507 235 "PCI(Bridge) UNKNOWN\n");
0c817338
LF
236
237 return;
238 }
239
240 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) {
241 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_CLK_REQ);
242 _rtl_pci_switch_clk_req(hw, 0x0);
243 }
244
32473284
LF
245 /*for promising device will in L0 state after an I/O. */
246 pci_read_config_byte(rtlpci->pdev, 0x80, &tmp_u1b);
0c817338
LF
247
248 /*Set corresponding value. */
249 aspmlevel |= BIT(0) | BIT(1);
250 linkctrl_reg &= ~aspmlevel;
251 pcibridge_linkctrlreg &= ~(BIT(0) | BIT(1));
252
253 _rtl_pci_platform_switch_device_pci_aspm(hw, linkctrl_reg);
254 udelay(50);
255
256 /*4 Disable Pci Bridge ASPM */
886e14b6
LF
257 pci_write_config_byte(rtlpci->pdev, (num4bytes << 2),
258 pcibridge_linkctrlreg);
0c817338
LF
259
260 udelay(50);
0c817338
LF
261}
262
263/*
264 *Enable RTL8192SE ASPM & Enable Pci Bridge ASPM for
265 *power saving We should follow the sequence to enable
266 *RTL8192SE first then enable Pci Bridge ASPM
267 *or the system will show bluescreen.
268 */
269static void rtl_pci_enable_aspm(struct ieee80211_hw *hw)
270{
271 struct rtl_priv *rtlpriv = rtl_priv(hw);
272 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
273 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
274 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
275 u8 pcibridge_busnum = pcipriv->ndis_adapter.pcibridge_busnum;
276 u8 pcibridge_devnum = pcipriv->ndis_adapter.pcibridge_devnum;
277 u8 pcibridge_funcnum = pcipriv->ndis_adapter.pcibridge_funcnum;
278 u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
0c817338
LF
279 u8 num4bytes = pcipriv->ndis_adapter.num4bytes;
280 u16 aspmlevel;
281 u8 u_pcibridge_aspmsetting;
282 u8 u_device_aspmsetting;
283
c7cfe38e
C
284 if (!ppsc->support_aspm)
285 return;
286
0c817338
LF
287 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_UNKNOWN) {
288 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
f30d7507 289 "PCI(Bridge) UNKNOWN\n");
0c817338
LF
290 return;
291 }
292
293 /*4 Enable Pci Bridge ASPM */
0c817338
LF
294
295 u_pcibridge_aspmsetting =
296 pcipriv->ndis_adapter.pcibridge_linkctrlreg |
297 rtlpci->const_hostpci_aspm_setting;
298
299 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_INTEL)
300 u_pcibridge_aspmsetting &= ~BIT(0);
301
886e14b6
LF
302 pci_write_config_byte(rtlpci->pdev, (num4bytes << 2),
303 u_pcibridge_aspmsetting);
0c817338
LF
304
305 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
f30d7507
JP
306 "PlatformEnableASPM():PciBridge busnumber[%x], DevNumbe[%x], funcnumber[%x], Write reg[%x] = %x\n",
307 pcibridge_busnum, pcibridge_devnum, pcibridge_funcnum,
308 (pcipriv->ndis_adapter.pcibridge_pciehdr_offset + 0x10),
309 u_pcibridge_aspmsetting);
0c817338
LF
310
311 udelay(50);
312
313 /*Get ASPM level (with/without Clock Req) */
314 aspmlevel = rtlpci->const_devicepci_aspm_setting;
315 u_device_aspmsetting = pcipriv->ndis_adapter.linkctrl_reg;
316
317 /*_rtl_pci_platform_switch_device_pci_aspm(dev,*/
318 /*(priv->ndis_adapter.linkctrl_reg | ASPMLevel)); */
319
320 u_device_aspmsetting |= aspmlevel;
321
322 _rtl_pci_platform_switch_device_pci_aspm(hw, u_device_aspmsetting);
323
324 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) {
325 _rtl_pci_switch_clk_req(hw, (ppsc->reg_rfps_level &
326 RT_RF_OFF_LEVL_CLK_REQ) ? 1 : 0);
327 RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_CLK_REQ);
328 }
c7cfe38e 329 udelay(100);
0c817338
LF
330}
331
332static bool rtl_pci_get_amd_l1_patch(struct ieee80211_hw *hw)
333{
886e14b6 334 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
0c817338
LF
335
336 bool status = false;
337 u8 offset_e0;
338 unsigned offset_e4;
339
886e14b6 340 pci_write_config_byte(rtlpci->pdev, 0xe0, 0xa0);
0c817338 341
886e14b6 342 pci_read_config_byte(rtlpci->pdev, 0xe0, &offset_e0);
0c817338
LF
343
344 if (offset_e0 == 0xA0) {
886e14b6 345 pci_read_config_dword(rtlpci->pdev, 0xe4, &offset_e4);
0c817338
LF
346 if (offset_e4 & BIT(23))
347 status = true;
348 }
349
350 return status;
351}
352
d3bb1429 353static void rtl_pci_get_linkcontrol_field(struct ieee80211_hw *hw)
0c817338
LF
354{
355 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
886e14b6 356 struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
0c817338 357 u8 capabilityoffset = pcipriv->ndis_adapter.pcibridge_pciehdr_offset;
0c817338 358 u8 linkctrl_reg;
c7cfe38e 359 u8 num4bbytes;
0c817338 360
c7cfe38e 361 num4bbytes = (capabilityoffset + 0x10) / 4;
0c817338
LF
362
363 /*Read Link Control Register */
886e14b6 364 pci_read_config_byte(rtlpci->pdev, (num4bbytes << 2), &linkctrl_reg);
0c817338
LF
365
366 pcipriv->ndis_adapter.pcibridge_linkctrlreg = linkctrl_reg;
367}
368
369static void rtl_pci_parse_configuration(struct pci_dev *pdev,
370 struct ieee80211_hw *hw)
371{
372 struct rtl_priv *rtlpriv = rtl_priv(hw);
373 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
374
375 u8 tmp;
376 int pos;
377 u8 linkctrl_reg;
378
379 /*Link Control Register */
6a4ecc29 380 pos = pci_pcie_cap(pdev);
0c817338
LF
381 pci_read_config_byte(pdev, pos + PCI_EXP_LNKCTL, &linkctrl_reg);
382 pcipriv->ndis_adapter.linkctrl_reg = linkctrl_reg;
383
f30d7507
JP
384 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Link Control Register =%x\n",
385 pcipriv->ndis_adapter.linkctrl_reg);
0c817338
LF
386
387 pci_read_config_byte(pdev, 0x98, &tmp);
388 tmp |= BIT(4);
389 pci_write_config_byte(pdev, 0x98, tmp);
390
391 tmp = 0x17;
392 pci_write_config_byte(pdev, 0x70f, tmp);
393}
394
c7cfe38e 395static void rtl_pci_init_aspm(struct ieee80211_hw *hw)
0c817338
LF
396{
397 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
398
399 _rtl_pci_update_default_setting(hw);
400
401 if (ppsc->reg_rfps_level & RT_RF_PS_LEVEL_ALWAYS_ASPM) {
402 /*Always enable ASPM & Clock Req. */
403 rtl_pci_enable_aspm(hw);
404 RT_SET_PS_LEVEL(ppsc, RT_RF_PS_LEVEL_ALWAYS_ASPM);
405 }
406
407}
408
0c817338
LF
409static void _rtl_pci_io_handler_init(struct device *dev,
410 struct ieee80211_hw *hw)
411{
412 struct rtl_priv *rtlpriv = rtl_priv(hw);
413
414 rtlpriv->io.dev = dev;
415
416 rtlpriv->io.write8_async = pci_write8_async;
417 rtlpriv->io.write16_async = pci_write16_async;
418 rtlpriv->io.write32_async = pci_write32_async;
419
420 rtlpriv->io.read8_sync = pci_read8_sync;
421 rtlpriv->io.read16_sync = pci_read16_sync;
422 rtlpriv->io.read32_sync = pci_read32_sync;
423
424}
425
426static void _rtl_pci_io_handler_release(struct ieee80211_hw *hw)
427{
428}
429
c7cfe38e
C
430static bool _rtl_update_earlymode_info(struct ieee80211_hw *hw,
431 struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc, u8 tid)
432{
433 struct rtl_priv *rtlpriv = rtl_priv(hw);
434 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
435 u8 additionlen = FCS_LEN;
436 struct sk_buff *next_skb;
437
438 /* here open is 4, wep/tkip is 8, aes is 12*/
439 if (info->control.hw_key)
440 additionlen += info->control.hw_key->icv_len;
441
442 /* The most skb num is 6 */
443 tcb_desc->empkt_num = 0;
444 spin_lock_bh(&rtlpriv->locks.waitq_lock);
445 skb_queue_walk(&rtlpriv->mac80211.skb_waitq[tid], next_skb) {
446 struct ieee80211_tx_info *next_info;
447
448 next_info = IEEE80211_SKB_CB(next_skb);
449 if (next_info->flags & IEEE80211_TX_CTL_AMPDU) {
450 tcb_desc->empkt_len[tcb_desc->empkt_num] =
451 next_skb->len + additionlen;
452 tcb_desc->empkt_num++;
453 } else {
454 break;
455 }
456
457 if (skb_queue_is_last(&rtlpriv->mac80211.skb_waitq[tid],
458 next_skb))
459 break;
460
461 if (tcb_desc->empkt_num >= 5)
462 break;
463 }
464 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
465
466 return true;
467}
468
469/* just for early mode now */
470static void _rtl_pci_tx_chk_waitq(struct ieee80211_hw *hw)
471{
472 struct rtl_priv *rtlpriv = rtl_priv(hw);
473 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
474 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
475 struct sk_buff *skb = NULL;
476 struct ieee80211_tx_info *info = NULL;
fb914ebf 477 int tid;
c7cfe38e
C
478
479 if (!rtlpriv->rtlhal.earlymode_enable)
480 return;
481
482 /* we juse use em for BE/BK/VI/VO */
483 for (tid = 7; tid >= 0; tid--) {
484 u8 hw_queue = ac_to_hwq[rtl_tid_to_ac(hw, tid)];
485 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
486 while (!mac->act_scanning &&
487 rtlpriv->psc.rfpwr_state == ERFON) {
488 struct rtl_tcb_desc tcb_desc;
489 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
490
491 spin_lock_bh(&rtlpriv->locks.waitq_lock);
492 if (!skb_queue_empty(&mac->skb_waitq[tid]) &&
493 (ring->entries - skb_queue_len(&ring->queue) > 5)) {
494 skb = skb_dequeue(&mac->skb_waitq[tid]);
495 } else {
496 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
497 break;
498 }
499 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
500
501 /* Some macaddr can't do early mode. like
502 * multicast/broadcast/no_qos data */
503 info = IEEE80211_SKB_CB(skb);
504 if (info->flags & IEEE80211_TX_CTL_AMPDU)
505 _rtl_update_earlymode_info(hw, skb,
506 &tcb_desc, tid);
507
c7cfe38e 508 rtlpriv->intf_ops->adapter_tx(hw, skb, &tcb_desc);
c7cfe38e
C
509 }
510 }
511}
512
513
0c817338
LF
514static void _rtl_pci_tx_isr(struct ieee80211_hw *hw, int prio)
515{
516 struct rtl_priv *rtlpriv = rtl_priv(hw);
517 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
518
519 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[prio];
520
521 while (skb_queue_len(&ring->queue)) {
522 struct rtl_tx_desc *entry = &ring->desc[ring->idx];
523 struct sk_buff *skb;
524 struct ieee80211_tx_info *info;
c7cfe38e
C
525 __le16 fc;
526 u8 tid;
0c817338
LF
527
528 u8 own = (u8) rtlpriv->cfg->ops->get_desc((u8 *) entry, true,
529 HW_DESC_OWN);
530
531 /*
532 *beacon packet will only use the first
533 *descriptor defautly,and the own may not
534 *be cleared by the hardware
535 */
536 if (own)
537 return;
538 ring->idx = (ring->idx + 1) % ring->entries;
539
540 skb = __skb_dequeue(&ring->queue);
541 pci_unmap_single(rtlpci->pdev,
d3bb1429 542 rtlpriv->cfg->ops->
0c817338 543 get_desc((u8 *) entry, true,
d3bb1429 544 HW_DESC_TXBUFF_ADDR),
0c817338
LF
545 skb->len, PCI_DMA_TODEVICE);
546
c7cfe38e
C
547 /* remove early mode header */
548 if (rtlpriv->rtlhal.earlymode_enable)
549 skb_pull(skb, EM_HDR_LEN);
550
0c817338 551 RT_TRACE(rtlpriv, (COMP_INTR | COMP_SEND), DBG_TRACE,
f30d7507
JP
552 "new ring->idx:%d, free: skb_queue_len:%d, free: seq:%x\n",
553 ring->idx,
554 skb_queue_len(&ring->queue),
555 *(u16 *) (skb->data + 22));
0c817338 556
c7cfe38e
C
557 if (prio == TXCMD_QUEUE) {
558 dev_kfree_skb(skb);
559 goto tx_status_ok;
560
561 }
562
563 /* for sw LPS, just after NULL skb send out, we can
564 * sure AP kown we are sleeped, our we should not let
565 * rf to sleep*/
566 fc = rtl_get_fc(skb);
567 if (ieee80211_is_nullfunc(fc)) {
568 if (ieee80211_has_pm(fc)) {
9c050440 569 rtlpriv->mac80211.offchan_delay = true;
3db1cd5c 570 rtlpriv->psc.state_inap = true;
c7cfe38e 571 } else {
3db1cd5c 572 rtlpriv->psc.state_inap = false;
c7cfe38e
C
573 }
574 }
575
576 /* update tid tx pkt num */
577 tid = rtl_get_tid(skb);
578 if (tid <= 7)
579 rtlpriv->link_info.tidtx_inperiod[tid]++;
580
0c817338
LF
581 info = IEEE80211_SKB_CB(skb);
582 ieee80211_tx_info_clear_status(info);
583
584 info->flags |= IEEE80211_TX_STAT_ACK;
585 /*info->status.rates[0].count = 1; */
586
587 ieee80211_tx_status_irqsafe(hw, skb);
588
589 if ((ring->entries - skb_queue_len(&ring->queue))
590 == 2) {
591
592 RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
f30d7507
JP
593 "more desc left, wake skb_queue@%d, ring->idx = %d, skb_queue_len = 0x%d\n",
594 prio, ring->idx,
595 skb_queue_len(&ring->queue));
0c817338
LF
596
597 ieee80211_wake_queue(hw,
598 skb_get_queue_mapping
599 (skb));
600 }
c7cfe38e 601tx_status_ok:
0c817338
LF
602 skb = NULL;
603 }
604
605 if (((rtlpriv->link_info.num_rx_inperiod +
606 rtlpriv->link_info.num_tx_inperiod) > 8) ||
607 (rtlpriv->link_info.num_rx_inperiod > 2)) {
41affd52 608 schedule_work(&rtlpriv->works.lps_leave_work);
0c817338
LF
609 }
610}
611
fd854772
MM
612static void _rtl_receive_one(struct ieee80211_hw *hw, struct sk_buff *skb,
613 struct ieee80211_rx_status rx_status)
614{
615 struct rtl_priv *rtlpriv = rtl_priv(hw);
616 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
617 __le16 fc = rtl_get_fc(skb);
618 bool unicast = false;
619 struct sk_buff *uskb = NULL;
620 u8 *pdata;
621
622
623 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
624
625 if (is_broadcast_ether_addr(hdr->addr1)) {
626 ;/*TODO*/
627 } else if (is_multicast_ether_addr(hdr->addr1)) {
628 ;/*TODO*/
629 } else {
630 unicast = true;
631 rtlpriv->stats.rxbytesunicast += skb->len;
632 }
633
634 rtl_is_special_data(hw, skb, false);
635
636 if (ieee80211_is_data(fc)) {
637 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
638
639 if (unicast)
640 rtlpriv->link_info.num_rx_inperiod++;
641 }
642
643 /* for sw lps */
644 rtl_swlps_beacon(hw, (void *)skb->data, skb->len);
645 rtl_recognize_peer(hw, (void *)skb->data, skb->len);
646 if ((rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP) &&
647 (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G) &&
648 (ieee80211_is_beacon(fc) || ieee80211_is_probe_resp(fc)))
649 return;
650
651 if (unlikely(!rtl_action_proc(hw, skb, false)))
652 return;
653
654 uskb = dev_alloc_skb(skb->len + 128);
655 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status, sizeof(rx_status));
656 pdata = (u8 *)skb_put(uskb, skb->len);
657 memcpy(pdata, skb->data, skb->len);
658
659 ieee80211_rx_irqsafe(hw, uskb);
660}
661
0c817338
LF
662static void _rtl_pci_rx_interrupt(struct ieee80211_hw *hw)
663{
664 struct rtl_priv *rtlpriv = rtl_priv(hw);
665 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
666 int rx_queue_idx = RTL_PCI_RX_MPDU_QUEUE;
667
668 struct ieee80211_rx_status rx_status = { 0 };
669 unsigned int count = rtlpci->rxringcount;
670 u8 own;
671 u8 tmp_one;
672 u32 bufferaddress;
0c817338
LF
673
674 struct rtl_stats stats = {
675 .signal = 0,
676 .noise = -98,
677 .rate = 0,
678 };
34ddb207 679 int index = rtlpci->rx_ring[rx_queue_idx].idx;
0c817338
LF
680
681 /*RX NORMAL PKT */
682 while (count--) {
683 /*rx descriptor */
684 struct rtl_rx_desc *pdesc = &rtlpci->rx_ring[rx_queue_idx].desc[
34ddb207 685 index];
0c817338
LF
686 /*rx pkt */
687 struct sk_buff *skb = rtlpci->rx_ring[rx_queue_idx].rx_buf[
34ddb207 688 index];
2c333366 689 struct sk_buff *new_skb = NULL;
0c817338
LF
690
691 own = (u8) rtlpriv->cfg->ops->get_desc((u8 *) pdesc,
692 false, HW_DESC_OWN);
693
2c333366
MM
694 /*wait data to be filled by hardware */
695 if (own)
34ddb207 696 break;
6633d649 697
2c333366
MM
698 rtlpriv->cfg->ops->query_rx_desc(hw, &stats,
699 &rx_status,
700 (u8 *) pdesc, skb);
701
8db8ddf1
MM
702 if (stats.crc || stats.hwerror)
703 goto done;
704
2c333366
MM
705 new_skb = dev_alloc_skb(rtlpci->rxbuffersize);
706 if (unlikely(!new_skb)) {
f30d7507
JP
707 RT_TRACE(rtlpriv, (COMP_INTR | COMP_RECV), DBG_DMESG,
708 "can't alloc skb for rx\n");
2c333366
MM
709 goto done;
710 }
711
712 pci_unmap_single(rtlpci->pdev,
713 *((dma_addr_t *) skb->cb),
714 rtlpci->rxbuffersize,
715 PCI_DMA_FROMDEVICE);
716
717 skb_put(skb, rtlpriv->cfg->ops->get_desc((u8 *) pdesc, false,
718 HW_DESC_RXPKT_LEN));
719 skb_reserve(skb, stats.rx_drvinfo_size + stats.rx_bufshift);
720
721 /*
722 * NOTICE This can not be use for mac80211,
723 * this is done in mac80211 code,
724 * if you done here sec DHCP will fail
725 * skb_trim(skb, skb->len - 4);
726 */
727
fd854772 728 _rtl_receive_one(hw, skb, rx_status);
0c817338 729
2c333366
MM
730 if (((rtlpriv->link_info.num_rx_inperiod +
731 rtlpriv->link_info.num_tx_inperiod) > 8) ||
732 (rtlpriv->link_info.num_rx_inperiod > 2)) {
41affd52 733 schedule_work(&rtlpriv->works.lps_leave_work);
2c333366 734 }
0c817338 735
14058add 736 dev_kfree_skb_any(skb);
2c333366 737 skb = new_skb;
0c817338 738
2c333366
MM
739 rtlpci->rx_ring[rx_queue_idx].rx_buf[index] = skb;
740 *((dma_addr_t *) skb->cb) =
0c817338
LF
741 pci_map_single(rtlpci->pdev, skb_tail_pointer(skb),
742 rtlpci->rxbuffersize,
743 PCI_DMA_FROMDEVICE);
744
0c817338 745done:
d3bb1429 746 bufferaddress = (*((dma_addr_t *)skb->cb));
0c817338
LF
747 tmp_one = 1;
748 rtlpriv->cfg->ops->set_desc((u8 *) pdesc, false,
749 HW_DESC_RXBUFF_ADDR,
750 (u8 *)&bufferaddress);
0c817338
LF
751 rtlpriv->cfg->ops->set_desc((u8 *)pdesc, false,
752 HW_DESC_RXPKT_LEN,
753 (u8 *)&rtlpci->rxbuffersize);
754
34ddb207 755 if (index == rtlpci->rxringcount - 1)
0c817338
LF
756 rtlpriv->cfg->ops->set_desc((u8 *)pdesc, false,
757 HW_DESC_RXERO,
758 (u8 *)&tmp_one);
759
febc9fe5
MM
760 rtlpriv->cfg->ops->set_desc((u8 *)pdesc, false, HW_DESC_RXOWN,
761 (u8 *)&tmp_one);
762
34ddb207 763 index = (index + 1) % rtlpci->rxringcount;
0c817338
LF
764 }
765
34ddb207 766 rtlpci->rx_ring[rx_queue_idx].idx = index;
0c817338
LF
767}
768
0c817338
LF
769static irqreturn_t _rtl_pci_interrupt(int irq, void *dev_id)
770{
771 struct ieee80211_hw *hw = dev_id;
772 struct rtl_priv *rtlpriv = rtl_priv(hw);
c7cfe38e 773 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0c817338
LF
774 unsigned long flags;
775 u32 inta = 0;
776 u32 intb = 0;
de2e56ce 777 irqreturn_t ret = IRQ_HANDLED;
0c817338 778
0c817338
LF
779 spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
780
781 /*read ISR: 4/8bytes */
782 rtlpriv->cfg->ops->interrupt_recognized(hw, &inta, &intb);
783
784 /*Shared IRQ or HW disappared */
de2e56ce
LF
785 if (!inta || inta == 0xffff) {
786 ret = IRQ_NONE;
0c817338 787 goto done;
de2e56ce 788 }
0c817338
LF
789
790 /*<1> beacon related */
791 if (inta & rtlpriv->cfg->maps[RTL_IMR_TBDOK]) {
792 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 793 "beacon ok interrupt!\n");
0c817338
LF
794 }
795
796 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_TBDER])) {
797 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 798 "beacon err interrupt!\n");
0c817338
LF
799 }
800
801 if (inta & rtlpriv->cfg->maps[RTL_IMR_BDOK]) {
f30d7507 802 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE, "beacon interrupt!\n");
0c817338
LF
803 }
804
805 if (inta & rtlpriv->cfg->maps[RTL_IMR_BcnInt]) {
806 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 807 "prepare beacon for interrupt!\n");
0c817338
LF
808 tasklet_schedule(&rtlpriv->works.irq_prepare_bcn_tasklet);
809 }
810
811 /*<3> Tx related */
812 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_TXFOVW]))
f30d7507 813 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, "IMR_TXFOVW!\n");
0c817338
LF
814
815 if (inta & rtlpriv->cfg->maps[RTL_IMR_MGNTDOK]) {
816 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 817 "Manage ok interrupt!\n");
0c817338
LF
818 _rtl_pci_tx_isr(hw, MGNT_QUEUE);
819 }
820
821 if (inta & rtlpriv->cfg->maps[RTL_IMR_HIGHDOK]) {
822 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 823 "HIGH_QUEUE ok interrupt!\n");
0c817338
LF
824 _rtl_pci_tx_isr(hw, HIGH_QUEUE);
825 }
826
827 if (inta & rtlpriv->cfg->maps[RTL_IMR_BKDOK]) {
828 rtlpriv->link_info.num_tx_inperiod++;
829
830 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 831 "BK Tx OK interrupt!\n");
0c817338
LF
832 _rtl_pci_tx_isr(hw, BK_QUEUE);
833 }
834
835 if (inta & rtlpriv->cfg->maps[RTL_IMR_BEDOK]) {
836 rtlpriv->link_info.num_tx_inperiod++;
837
838 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 839 "BE TX OK interrupt!\n");
0c817338
LF
840 _rtl_pci_tx_isr(hw, BE_QUEUE);
841 }
842
843 if (inta & rtlpriv->cfg->maps[RTL_IMR_VIDOK]) {
844 rtlpriv->link_info.num_tx_inperiod++;
845
846 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 847 "VI TX OK interrupt!\n");
0c817338
LF
848 _rtl_pci_tx_isr(hw, VI_QUEUE);
849 }
850
851 if (inta & rtlpriv->cfg->maps[RTL_IMR_VODOK]) {
852 rtlpriv->link_info.num_tx_inperiod++;
853
854 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 855 "Vo TX OK interrupt!\n");
0c817338
LF
856 _rtl_pci_tx_isr(hw, VO_QUEUE);
857 }
858
c7cfe38e
C
859 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
860 if (inta & rtlpriv->cfg->maps[RTL_IMR_COMDOK]) {
861 rtlpriv->link_info.num_tx_inperiod++;
862
863 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
f30d7507 864 "CMD TX OK interrupt!\n");
c7cfe38e
C
865 _rtl_pci_tx_isr(hw, TXCMD_QUEUE);
866 }
867 }
868
0c817338
LF
869 /*<2> Rx related */
870 if (inta & rtlpriv->cfg->maps[RTL_IMR_ROK]) {
f30d7507 871 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE, "Rx ok interrupt!\n");
c7cfe38e 872 _rtl_pci_rx_interrupt(hw);
0c817338
LF
873 }
874
875 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_RDU])) {
876 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
f30d7507 877 "rx descriptor unavailable!\n");
c7cfe38e 878 _rtl_pci_rx_interrupt(hw);
0c817338
LF
879 }
880
881 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_RXFOVW])) {
f30d7507 882 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, "rx overflow !\n");
c7cfe38e 883 _rtl_pci_rx_interrupt(hw);
0c817338
LF
884 }
885
c7cfe38e
C
886 if (rtlpriv->rtlhal.earlymode_enable)
887 tasklet_schedule(&rtlpriv->works.irq_tasklet);
888
0c817338
LF
889done:
890 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
de2e56ce 891 return ret;
0c817338
LF
892}
893
894static void _rtl_pci_irq_tasklet(struct ieee80211_hw *hw)
895{
c7cfe38e 896 _rtl_pci_tx_chk_waitq(hw);
0c817338
LF
897}
898
899static void _rtl_pci_prepare_bcn_tasklet(struct ieee80211_hw *hw)
900{
901 struct rtl_priv *rtlpriv = rtl_priv(hw);
902 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
903 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
c7cfe38e 904 struct rtl8192_tx_ring *ring = NULL;
0c817338
LF
905 struct ieee80211_hdr *hdr = NULL;
906 struct ieee80211_tx_info *info = NULL;
907 struct sk_buff *pskb = NULL;
908 struct rtl_tx_desc *pdesc = NULL;
c7cfe38e 909 struct rtl_tcb_desc tcb_desc;
0c817338
LF
910 u8 temp_one = 1;
911
c7cfe38e 912 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
0c817338
LF
913 ring = &rtlpci->tx_ring[BEACON_QUEUE];
914 pskb = __skb_dequeue(&ring->queue);
915 if (pskb)
916 kfree_skb(pskb);
917
918 /*NB: the beacon data buffer must be 32-bit aligned. */
919 pskb = ieee80211_beacon_get(hw, mac->vif);
920 if (pskb == NULL)
921 return;
c7cfe38e 922 hdr = rtl_get_hdr(pskb);
0c817338 923 info = IEEE80211_SKB_CB(pskb);
0c817338
LF
924 pdesc = &ring->desc[0];
925 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *) pdesc,
c7cfe38e 926 info, pskb, BEACON_QUEUE, &tcb_desc);
0c817338
LF
927
928 __skb_queue_tail(&ring->queue, pskb);
929
930 rtlpriv->cfg->ops->set_desc((u8 *) pdesc, true, HW_DESC_OWN,
931 (u8 *)&temp_one);
932
933 return;
934}
935
41affd52
SG
936static void rtl_lps_leave_work_callback(struct work_struct *work)
937{
938 struct rtl_works *rtlworks =
939 container_of(work, struct rtl_works, lps_leave_work);
940 struct ieee80211_hw *hw = rtlworks->hw;
941
942 rtl_lps_leave(hw);
943}
944
0c817338
LF
945static void _rtl_pci_init_trx_var(struct ieee80211_hw *hw)
946{
947 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
948 u8 i;
949
950 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
951 rtlpci->txringcount[i] = RT_TXDESC_NUM;
952
953 /*
954 *we just alloc 2 desc for beacon queue,
955 *because we just need first desc in hw beacon.
956 */
957 rtlpci->txringcount[BEACON_QUEUE] = 2;
958
959 /*
960 *BE queue need more descriptor for performance
961 *consideration or, No more tx desc will happen,
962 *and may cause mac80211 mem leakage.
963 */
964 rtlpci->txringcount[BE_QUEUE] = RT_TXDESC_NUM_BE_QUEUE;
965
966 rtlpci->rxbuffersize = 9100; /*2048/1024; */
967 rtlpci->rxringcount = RTL_PCI_MAX_RX_COUNT; /*64; */
968}
969
970static void _rtl_pci_init_struct(struct ieee80211_hw *hw,
971 struct pci_dev *pdev)
972{
973 struct rtl_priv *rtlpriv = rtl_priv(hw);
974 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
975 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
976 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0c817338
LF
977
978 rtlpci->up_first_time = true;
979 rtlpci->being_init_adapter = false;
980
981 rtlhal->hw = hw;
982 rtlpci->pdev = pdev;
983
0c817338
LF
984 /*Tx/Rx related var */
985 _rtl_pci_init_trx_var(hw);
986
c7cfe38e 987 /*IBSS*/ mac->beacon_interval = 100;
0c817338 988
c7cfe38e
C
989 /*AMPDU*/
990 mac->min_space_cfg = 0;
0c817338
LF
991 mac->max_mss_density = 0;
992 /*set sane AMPDU defaults */
993 mac->current_ampdu_density = 7;
994 mac->current_ampdu_factor = 3;
995
c7cfe38e
C
996 /*QOS*/
997 rtlpci->acm_method = eAcmWay2_SW;
0c817338
LF
998
999 /*task */
1000 tasklet_init(&rtlpriv->works.irq_tasklet,
1001 (void (*)(unsigned long))_rtl_pci_irq_tasklet,
1002 (unsigned long)hw);
1003 tasklet_init(&rtlpriv->works.irq_prepare_bcn_tasklet,
1004 (void (*)(unsigned long))_rtl_pci_prepare_bcn_tasklet,
1005 (unsigned long)hw);
41affd52 1006 INIT_WORK(&rtlpriv->works.lps_leave_work, rtl_lps_leave_work_callback);
0c817338
LF
1007}
1008
1009static int _rtl_pci_init_tx_ring(struct ieee80211_hw *hw,
1010 unsigned int prio, unsigned int entries)
1011{
1012 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1013 struct rtl_priv *rtlpriv = rtl_priv(hw);
1014 struct rtl_tx_desc *ring;
1015 dma_addr_t dma;
1016 u32 nextdescaddress;
1017 int i;
1018
1019 ring = pci_alloc_consistent(rtlpci->pdev,
1020 sizeof(*ring) * entries, &dma);
1021
1022 if (!ring || (unsigned long)ring & 0xFF) {
1023 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
f30d7507 1024 "Cannot allocate TX ring (prio = %d)\n", prio);
0c817338
LF
1025 return -ENOMEM;
1026 }
1027
1028 memset(ring, 0, sizeof(*ring) * entries);
1029 rtlpci->tx_ring[prio].desc = ring;
1030 rtlpci->tx_ring[prio].dma = dma;
1031 rtlpci->tx_ring[prio].idx = 0;
1032 rtlpci->tx_ring[prio].entries = entries;
1033 skb_queue_head_init(&rtlpci->tx_ring[prio].queue);
1034
f30d7507
JP
1035 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "queue:%d, ring_addr:%p\n",
1036 prio, ring);
0c817338
LF
1037
1038 for (i = 0; i < entries; i++) {
d3bb1429 1039 nextdescaddress = (u32) dma +
982d96bb 1040 ((i + 1) % entries) *
d3bb1429 1041 sizeof(*ring);
0c817338
LF
1042
1043 rtlpriv->cfg->ops->set_desc((u8 *)&(ring[i]),
1044 true, HW_DESC_TX_NEXTDESC_ADDR,
1045 (u8 *)&nextdescaddress);
1046 }
1047
1048 return 0;
1049}
1050
1051static int _rtl_pci_init_rx_ring(struct ieee80211_hw *hw)
1052{
1053 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1054 struct rtl_priv *rtlpriv = rtl_priv(hw);
1055 struct rtl_rx_desc *entry = NULL;
1056 int i, rx_queue_idx;
1057 u8 tmp_one = 1;
1058
1059 /*
1060 *rx_queue_idx 0:RX_MPDU_QUEUE
1061 *rx_queue_idx 1:RX_CMD_QUEUE
1062 */
1063 for (rx_queue_idx = 0; rx_queue_idx < RTL_PCI_MAX_RX_QUEUE;
1064 rx_queue_idx++) {
1065 rtlpci->rx_ring[rx_queue_idx].desc =
1066 pci_alloc_consistent(rtlpci->pdev,
1067 sizeof(*rtlpci->rx_ring[rx_queue_idx].
1068 desc) * rtlpci->rxringcount,
1069 &rtlpci->rx_ring[rx_queue_idx].dma);
1070
1071 if (!rtlpci->rx_ring[rx_queue_idx].desc ||
1072 (unsigned long)rtlpci->rx_ring[rx_queue_idx].desc & 0xFF) {
1073 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
f30d7507 1074 "Cannot allocate RX ring\n");
0c817338
LF
1075 return -ENOMEM;
1076 }
1077
1078 memset(rtlpci->rx_ring[rx_queue_idx].desc, 0,
1079 sizeof(*rtlpci->rx_ring[rx_queue_idx].desc) *
1080 rtlpci->rxringcount);
1081
1082 rtlpci->rx_ring[rx_queue_idx].idx = 0;
1083
0019a2c9
LF
1084 /* If amsdu_8k is disabled, set buffersize to 4096. This
1085 * change will reduce memory fragmentation.
1086 */
1087 if (rtlpci->rxbuffersize > 4096 &&
1088 rtlpriv->rtlhal.disable_amsdu_8k)
1089 rtlpci->rxbuffersize = 4096;
1090
0c817338
LF
1091 for (i = 0; i < rtlpci->rxringcount; i++) {
1092 struct sk_buff *skb =
1093 dev_alloc_skb(rtlpci->rxbuffersize);
1094 u32 bufferaddress;
0c817338
LF
1095 if (!skb)
1096 return 0;
bdc4bf65 1097 entry = &rtlpci->rx_ring[rx_queue_idx].desc[i];
0c817338
LF
1098
1099 /*skb->dev = dev; */
1100
1101 rtlpci->rx_ring[rx_queue_idx].rx_buf[i] = skb;
1102
1103 /*
1104 *just set skb->cb to mapping addr
1105 *for pci_unmap_single use
1106 */
1107 *((dma_addr_t *) skb->cb) =
1108 pci_map_single(rtlpci->pdev, skb_tail_pointer(skb),
1109 rtlpci->rxbuffersize,
1110 PCI_DMA_FROMDEVICE);
1111
d3bb1429 1112 bufferaddress = (*((dma_addr_t *)skb->cb));
0c817338
LF
1113 rtlpriv->cfg->ops->set_desc((u8 *)entry, false,
1114 HW_DESC_RXBUFF_ADDR,
1115 (u8 *)&bufferaddress);
1116 rtlpriv->cfg->ops->set_desc((u8 *)entry, false,
1117 HW_DESC_RXPKT_LEN,
1118 (u8 *)&rtlpci->
1119 rxbuffersize);
1120 rtlpriv->cfg->ops->set_desc((u8 *) entry, false,
1121 HW_DESC_RXOWN,
1122 (u8 *)&tmp_one);
1123 }
1124
1125 rtlpriv->cfg->ops->set_desc((u8 *) entry, false,
1126 HW_DESC_RXERO, (u8 *)&tmp_one);
1127 }
1128 return 0;
1129}
1130
1131static void _rtl_pci_free_tx_ring(struct ieee80211_hw *hw,
1132 unsigned int prio)
1133{
1134 struct rtl_priv *rtlpriv = rtl_priv(hw);
1135 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1136 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[prio];
1137
1138 while (skb_queue_len(&ring->queue)) {
1139 struct rtl_tx_desc *entry = &ring->desc[ring->idx];
1140 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1141
1142 pci_unmap_single(rtlpci->pdev,
d3bb1429 1143 rtlpriv->cfg->
0c817338 1144 ops->get_desc((u8 *) entry, true,
d3bb1429 1145 HW_DESC_TXBUFF_ADDR),
0c817338
LF
1146 skb->len, PCI_DMA_TODEVICE);
1147 kfree_skb(skb);
1148 ring->idx = (ring->idx + 1) % ring->entries;
1149 }
1150
1151 pci_free_consistent(rtlpci->pdev,
1152 sizeof(*ring->desc) * ring->entries,
1153 ring->desc, ring->dma);
1154 ring->desc = NULL;
1155}
1156
1157static void _rtl_pci_free_rx_ring(struct rtl_pci *rtlpci)
1158{
1159 int i, rx_queue_idx;
1160
1161 /*rx_queue_idx 0:RX_MPDU_QUEUE */
1162 /*rx_queue_idx 1:RX_CMD_QUEUE */
1163 for (rx_queue_idx = 0; rx_queue_idx < RTL_PCI_MAX_RX_QUEUE;
1164 rx_queue_idx++) {
1165 for (i = 0; i < rtlpci->rxringcount; i++) {
1166 struct sk_buff *skb =
1167 rtlpci->rx_ring[rx_queue_idx].rx_buf[i];
1168 if (!skb)
1169 continue;
1170
1171 pci_unmap_single(rtlpci->pdev,
1172 *((dma_addr_t *) skb->cb),
1173 rtlpci->rxbuffersize,
1174 PCI_DMA_FROMDEVICE);
1175 kfree_skb(skb);
1176 }
1177
1178 pci_free_consistent(rtlpci->pdev,
1179 sizeof(*rtlpci->rx_ring[rx_queue_idx].
1180 desc) * rtlpci->rxringcount,
1181 rtlpci->rx_ring[rx_queue_idx].desc,
1182 rtlpci->rx_ring[rx_queue_idx].dma);
1183 rtlpci->rx_ring[rx_queue_idx].desc = NULL;
1184 }
1185}
1186
1187static int _rtl_pci_init_trx_ring(struct ieee80211_hw *hw)
1188{
1189 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1190 int ret;
1191 int i;
1192
1193 ret = _rtl_pci_init_rx_ring(hw);
1194 if (ret)
1195 return ret;
1196
1197 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
1198 ret = _rtl_pci_init_tx_ring(hw, i,
1199 rtlpci->txringcount[i]);
1200 if (ret)
1201 goto err_free_rings;
1202 }
1203
1204 return 0;
1205
1206err_free_rings:
1207 _rtl_pci_free_rx_ring(rtlpci);
1208
1209 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
1210 if (rtlpci->tx_ring[i].desc)
1211 _rtl_pci_free_tx_ring(hw, i);
1212
1213 return 1;
1214}
1215
1216static int _rtl_pci_deinit_trx_ring(struct ieee80211_hw *hw)
1217{
1218 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1219 u32 i;
1220
1221 /*free rx rings */
1222 _rtl_pci_free_rx_ring(rtlpci);
1223
1224 /*free tx rings */
1225 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
1226 _rtl_pci_free_tx_ring(hw, i);
1227
1228 return 0;
1229}
1230
1231int rtl_pci_reset_trx_ring(struct ieee80211_hw *hw)
1232{
1233 struct rtl_priv *rtlpriv = rtl_priv(hw);
1234 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1235 int i, rx_queue_idx;
1236 unsigned long flags;
1237 u8 tmp_one = 1;
1238
1239 /*rx_queue_idx 0:RX_MPDU_QUEUE */
1240 /*rx_queue_idx 1:RX_CMD_QUEUE */
1241 for (rx_queue_idx = 0; rx_queue_idx < RTL_PCI_MAX_RX_QUEUE;
1242 rx_queue_idx++) {
1243 /*
1244 *force the rx_ring[RX_MPDU_QUEUE/
1245 *RX_CMD_QUEUE].idx to the first one
1246 */
1247 if (rtlpci->rx_ring[rx_queue_idx].desc) {
1248 struct rtl_rx_desc *entry = NULL;
1249
1250 for (i = 0; i < rtlpci->rxringcount; i++) {
1251 entry = &rtlpci->rx_ring[rx_queue_idx].desc[i];
1252 rtlpriv->cfg->ops->set_desc((u8 *) entry,
1253 false,
1254 HW_DESC_RXOWN,
1255 (u8 *)&tmp_one);
1256 }
1257 rtlpci->rx_ring[rx_queue_idx].idx = 0;
1258 }
1259 }
1260
1261 /*
1262 *after reset, release previous pending packet,
1263 *and force the tx idx to the first one
1264 */
1265 spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
1266 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
1267 if (rtlpci->tx_ring[i].desc) {
1268 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[i];
1269
1270 while (skb_queue_len(&ring->queue)) {
1271 struct rtl_tx_desc *entry =
1272 &ring->desc[ring->idx];
1273 struct sk_buff *skb =
1274 __skb_dequeue(&ring->queue);
1275
1276 pci_unmap_single(rtlpci->pdev,
d3bb1429 1277 rtlpriv->cfg->ops->
0c817338
LF
1278 get_desc((u8 *)
1279 entry,
1280 true,
d3bb1429 1281 HW_DESC_TXBUFF_ADDR),
0c817338
LF
1282 skb->len, PCI_DMA_TODEVICE);
1283 kfree_skb(skb);
1284 ring->idx = (ring->idx + 1) % ring->entries;
1285 }
1286 ring->idx = 0;
1287 }
1288 }
1289
1290 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1291
1292 return 0;
1293}
1294
c7cfe38e
C
1295static bool rtl_pci_tx_chk_waitq_insert(struct ieee80211_hw *hw,
1296 struct sk_buff *skb)
0c817338 1297{
c7cfe38e
C
1298 struct rtl_priv *rtlpriv = rtl_priv(hw);
1299 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1300 struct ieee80211_sta *sta = info->control.sta;
1301 struct rtl_sta_info *sta_entry = NULL;
1302 u8 tid = rtl_get_tid(skb);
1303
1304 if (!sta)
1305 return false;
1306 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1307
1308 if (!rtlpriv->rtlhal.earlymode_enable)
1309 return false;
1310 if (sta_entry->tids[tid].agg.agg_state != RTL_AGG_OPERATIONAL)
1311 return false;
1312 if (_rtl_mac_to_hwqueue(hw, skb) > VO_QUEUE)
1313 return false;
1314 if (tid > 7)
1315 return false;
1316
1317 /* maybe every tid should be checked */
1318 if (!rtlpriv->link_info.higher_busytxtraffic[tid])
1319 return false;
1320
1321 spin_lock_bh(&rtlpriv->locks.waitq_lock);
1322 skb_queue_tail(&rtlpriv->mac80211.skb_waitq[tid], skb);
1323 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
0c817338 1324
c7cfe38e 1325 return true;
0c817338
LF
1326}
1327
d3bb1429 1328static int rtl_pci_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
c7cfe38e 1329 struct rtl_tcb_desc *ptcb_desc)
0c817338
LF
1330{
1331 struct rtl_priv *rtlpriv = rtl_priv(hw);
c7cfe38e 1332 struct rtl_sta_info *sta_entry = NULL;
0c817338 1333 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
c7cfe38e 1334 struct ieee80211_sta *sta = info->control.sta;
0c817338
LF
1335 struct rtl8192_tx_ring *ring;
1336 struct rtl_tx_desc *pdesc;
1337 u8 idx;
c7cfe38e 1338 u8 hw_queue = _rtl_mac_to_hwqueue(hw, skb);
0c817338 1339 unsigned long flags;
c7cfe38e
C
1340 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1341 __le16 fc = rtl_get_fc(skb);
0c817338
LF
1342 u8 *pda_addr = hdr->addr1;
1343 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1344 /*ssn */
0c817338
LF
1345 u8 tid = 0;
1346 u16 seq_number = 0;
1347 u8 own;
1348 u8 temp_one = 1;
1349
c7cfe38e 1350 if (ieee80211_is_auth(fc)) {
f30d7507 1351 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
c7cfe38e
C
1352 rtl_ips_nic_on(hw);
1353 }
1354
1355 if (rtlpriv->psc.sw_ps_enabled) {
1356 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
1357 !ieee80211_has_pm(fc))
1358 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1359 }
0c817338 1360
c7cfe38e 1361 rtl_action_proc(hw, skb, true);
0c817338
LF
1362
1363 if (is_multicast_ether_addr(pda_addr))
1364 rtlpriv->stats.txbytesmulticast += skb->len;
1365 else if (is_broadcast_ether_addr(pda_addr))
1366 rtlpriv->stats.txbytesbroadcast += skb->len;
1367 else
1368 rtlpriv->stats.txbytesunicast += skb->len;
1369
1370 spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
0c817338
LF
1371 ring = &rtlpci->tx_ring[hw_queue];
1372 if (hw_queue != BEACON_QUEUE)
1373 idx = (ring->idx + skb_queue_len(&ring->queue)) %
1374 ring->entries;
1375 else
1376 idx = 0;
1377
1378 pdesc = &ring->desc[idx];
1379 own = (u8) rtlpriv->cfg->ops->get_desc((u8 *) pdesc,
1380 true, HW_DESC_OWN);
1381
1382 if ((own == 1) && (hw_queue != BEACON_QUEUE)) {
1383 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
f30d7507
JP
1384 "No more TX desc@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%d\n",
1385 hw_queue, ring->idx, idx,
1386 skb_queue_len(&ring->queue));
0c817338
LF
1387
1388 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1389 return skb->len;
1390 }
1391
0c817338 1392 if (ieee80211_is_data_qos(fc)) {
c7cfe38e
C
1393 tid = rtl_get_tid(skb);
1394 if (sta) {
1395 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1396 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
1397 IEEE80211_SCTL_SEQ) >> 4;
1398 seq_number += 1;
1399
1400 if (!ieee80211_has_morefrags(hdr->frame_control))
1401 sta_entry->tids[tid].seq_number = seq_number;
1402 }
0c817338
LF
1403 }
1404
1405 if (ieee80211_is_data(fc))
1406 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
1407
c7cfe38e
C
1408 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc,
1409 info, skb, hw_queue, ptcb_desc);
0c817338
LF
1410
1411 __skb_queue_tail(&ring->queue, skb);
1412
c7cfe38e 1413 rtlpriv->cfg->ops->set_desc((u8 *)pdesc, true,
0c817338
LF
1414 HW_DESC_OWN, (u8 *)&temp_one);
1415
0c817338
LF
1416
1417 if ((ring->entries - skb_queue_len(&ring->queue)) < 2 &&
1418 hw_queue != BEACON_QUEUE) {
1419
1420 RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
f30d7507
JP
1421 "less desc left, stop skb_queue@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%d\n",
1422 hw_queue, ring->idx, idx,
1423 skb_queue_len(&ring->queue));
0c817338
LF
1424
1425 ieee80211_stop_queue(hw, skb_get_queue_mapping(skb));
1426 }
1427
1428 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1429
1430 rtlpriv->cfg->ops->tx_polling(hw, hw_queue);
1431
1432 return 0;
1433}
1434
c7cfe38e
C
1435static void rtl_pci_flush(struct ieee80211_hw *hw, bool drop)
1436{
1437 struct rtl_priv *rtlpriv = rtl_priv(hw);
1438 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1439 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1440 u16 i = 0;
1441 int queue_id;
1442 struct rtl8192_tx_ring *ring;
1443
1444 for (queue_id = RTL_PCI_MAX_TX_QUEUE_COUNT - 1; queue_id >= 0;) {
1445 u32 queue_len;
1446 ring = &pcipriv->dev.tx_ring[queue_id];
1447 queue_len = skb_queue_len(&ring->queue);
1448 if (queue_len == 0 || queue_id == BEACON_QUEUE ||
1449 queue_id == TXCMD_QUEUE) {
1450 queue_id--;
1451 continue;
1452 } else {
1453 msleep(20);
1454 i++;
1455 }
1456
1457 /* we just wait 1s for all queues */
1458 if (rtlpriv->psc.rfpwr_state == ERFOFF ||
1459 is_hal_stop(rtlhal) || i >= 200)
1460 return;
1461 }
1462}
1463
d3bb1429 1464static void rtl_pci_deinit(struct ieee80211_hw *hw)
0c817338
LF
1465{
1466 struct rtl_priv *rtlpriv = rtl_priv(hw);
1467 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1468
1469 _rtl_pci_deinit_trx_ring(hw);
1470
1471 synchronize_irq(rtlpci->pdev->irq);
1472 tasklet_kill(&rtlpriv->works.irq_tasklet);
41affd52 1473 cancel_work_sync(&rtlpriv->works.lps_leave_work);
0c817338
LF
1474
1475 flush_workqueue(rtlpriv->works.rtl_wq);
1476 destroy_workqueue(rtlpriv->works.rtl_wq);
1477
1478}
1479
d3bb1429 1480static int rtl_pci_init(struct ieee80211_hw *hw, struct pci_dev *pdev)
0c817338
LF
1481{
1482 struct rtl_priv *rtlpriv = rtl_priv(hw);
1483 int err;
1484
1485 _rtl_pci_init_struct(hw, pdev);
1486
1487 err = _rtl_pci_init_trx_ring(hw);
1488 if (err) {
1489 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
f30d7507 1490 "tx ring initialization failed\n");
0c817338
LF
1491 return err;
1492 }
1493
1494 return 1;
1495}
1496
d3bb1429 1497static int rtl_pci_start(struct ieee80211_hw *hw)
0c817338
LF
1498{
1499 struct rtl_priv *rtlpriv = rtl_priv(hw);
1500 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1501 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1502 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1503
1504 int err;
1505
1506 rtl_pci_reset_trx_ring(hw);
1507
1508 rtlpci->driver_is_goingto_unload = false;
1509 err = rtlpriv->cfg->ops->hw_init(hw);
1510 if (err) {
1511 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507 1512 "Failed to config hardware!\n");
0c817338
LF
1513 return err;
1514 }
1515
1516 rtlpriv->cfg->ops->enable_interrupt(hw);
f30d7507 1517 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "enable_interrupt OK\n");
0c817338
LF
1518
1519 rtl_init_rx_config(hw);
1520
fb914ebf 1521 /*should be after adapter start and interrupt enable. */
0c817338
LF
1522 set_hal_start(rtlhal);
1523
1524 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
1525
1526 rtlpci->up_first_time = false;
1527
f30d7507 1528 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "OK\n");
0c817338
LF
1529 return 0;
1530}
1531
d3bb1429 1532static void rtl_pci_stop(struct ieee80211_hw *hw)
0c817338
LF
1533{
1534 struct rtl_priv *rtlpriv = rtl_priv(hw);
1535 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1536 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1537 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1538 unsigned long flags;
1539 u8 RFInProgressTimeOut = 0;
1540
1541 /*
fb914ebf 1542 *should be before disable interrupt&adapter
0c817338
LF
1543 *and will do it immediately.
1544 */
1545 set_hal_stop(rtlhal);
1546
1547 rtlpriv->cfg->ops->disable_interrupt(hw);
41affd52 1548 cancel_work_sync(&rtlpriv->works.lps_leave_work);
0c817338
LF
1549
1550 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1551 while (ppsc->rfchange_inprogress) {
1552 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1553 if (RFInProgressTimeOut > 100) {
1554 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1555 break;
1556 }
1557 mdelay(1);
1558 RFInProgressTimeOut++;
1559 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1560 }
1561 ppsc->rfchange_inprogress = true;
1562 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1563
1564 rtlpci->driver_is_goingto_unload = true;
1565 rtlpriv->cfg->ops->hw_disable(hw);
1566 rtlpriv->cfg->ops->led_control(hw, LED_CTL_POWER_OFF);
1567
1568 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1569 ppsc->rfchange_inprogress = false;
1570 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1571
1572 rtl_pci_enable_aspm(hw);
1573}
1574
1575static bool _rtl_pci_find_adapter(struct pci_dev *pdev,
1576 struct ieee80211_hw *hw)
1577{
1578 struct rtl_priv *rtlpriv = rtl_priv(hw);
1579 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1580 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1581 struct pci_dev *bridge_pdev = pdev->bus->self;
1582 u16 venderid;
1583 u16 deviceid;
c7cfe38e 1584 u8 revisionid;
0c817338
LF
1585 u16 irqline;
1586 u8 tmp;
1587
fc7707a4 1588 pcipriv->ndis_adapter.pcibridge_vendor = PCI_BRIDGE_VENDOR_UNKNOWN;
0c817338
LF
1589 venderid = pdev->vendor;
1590 deviceid = pdev->device;
c7cfe38e 1591 pci_read_config_byte(pdev, 0x8, &revisionid);
0c817338
LF
1592 pci_read_config_word(pdev, 0x3C, &irqline);
1593
fa7ccfb1
LF
1594 /* PCI ID 0x10ec:0x8192 occurs for both RTL8192E, which uses
1595 * r8192e_pci, and RTL8192SE, which uses this driver. If the
1596 * revision ID is RTL_PCI_REVISION_ID_8192PCIE (0x01), then
1597 * the correct driver is r8192e_pci, thus this routine should
1598 * return false.
1599 */
1600 if (deviceid == RTL_PCI_8192SE_DID &&
1601 revisionid == RTL_PCI_REVISION_ID_8192PCIE)
1602 return false;
1603
0c817338
LF
1604 if (deviceid == RTL_PCI_8192_DID ||
1605 deviceid == RTL_PCI_0044_DID ||
1606 deviceid == RTL_PCI_0047_DID ||
1607 deviceid == RTL_PCI_8192SE_DID ||
1608 deviceid == RTL_PCI_8174_DID ||
1609 deviceid == RTL_PCI_8173_DID ||
1610 deviceid == RTL_PCI_8172_DID ||
1611 deviceid == RTL_PCI_8171_DID) {
c7cfe38e 1612 switch (revisionid) {
0c817338
LF
1613 case RTL_PCI_REVISION_ID_8192PCIE:
1614 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507
JP
1615 "8192 PCI-E is found - vid/did=%x/%x\n",
1616 venderid, deviceid);
0c817338
LF
1617 rtlhal->hw_type = HARDWARE_TYPE_RTL8192E;
1618 break;
1619 case RTL_PCI_REVISION_ID_8192SE:
1620 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507
JP
1621 "8192SE is found - vid/did=%x/%x\n",
1622 venderid, deviceid);
0c817338
LF
1623 rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
1624 break;
1625 default:
1626 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
f30d7507
JP
1627 "Err: Unknown device - vid/did=%x/%x\n",
1628 venderid, deviceid);
0c817338
LF
1629 rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
1630 break;
1631
1632 }
1633 } else if (deviceid == RTL_PCI_8192CET_DID ||
1634 deviceid == RTL_PCI_8192CE_DID ||
1635 deviceid == RTL_PCI_8191CE_DID ||
1636 deviceid == RTL_PCI_8188CE_DID) {
1637 rtlhal->hw_type = HARDWARE_TYPE_RTL8192CE;
1638 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507
JP
1639 "8192C PCI-E is found - vid/did=%x/%x\n",
1640 venderid, deviceid);
c7cfe38e
C
1641 } else if (deviceid == RTL_PCI_8192DE_DID ||
1642 deviceid == RTL_PCI_8192DE_DID2) {
1643 rtlhal->hw_type = HARDWARE_TYPE_RTL8192DE;
1644 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507
JP
1645 "8192D PCI-E is found - vid/did=%x/%x\n",
1646 venderid, deviceid);
0c817338
LF
1647 } else {
1648 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
f30d7507
JP
1649 "Err: Unknown device - vid/did=%x/%x\n",
1650 venderid, deviceid);
0c817338
LF
1651
1652 rtlhal->hw_type = RTL_DEFAULT_HARDWARE_TYPE;
1653 }
1654
c7cfe38e
C
1655 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192DE) {
1656 if (revisionid == 0 || revisionid == 1) {
1657 if (revisionid == 0) {
f30d7507
JP
1658 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1659 "Find 92DE MAC0\n");
c7cfe38e
C
1660 rtlhal->interfaceindex = 0;
1661 } else if (revisionid == 1) {
1662 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
f30d7507 1663 "Find 92DE MAC1\n");
c7cfe38e
C
1664 rtlhal->interfaceindex = 1;
1665 }
1666 } else {
1667 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
f30d7507
JP
1668 "Unknown device - VendorID/DeviceID=%x/%x, Revision=%x\n",
1669 venderid, deviceid, revisionid);
c7cfe38e
C
1670 rtlhal->interfaceindex = 0;
1671 }
1672 }
0c817338
LF
1673 /*find bus info */
1674 pcipriv->ndis_adapter.busnumber = pdev->bus->number;
1675 pcipriv->ndis_adapter.devnumber = PCI_SLOT(pdev->devfn);
1676 pcipriv->ndis_adapter.funcnumber = PCI_FUNC(pdev->devfn);
1677
b6b67df3
LF
1678 if (bridge_pdev) {
1679 /*find bridge info if available */
1680 pcipriv->ndis_adapter.pcibridge_vendorid = bridge_pdev->vendor;
1681 for (tmp = 0; tmp < PCI_BRIDGE_VENDOR_MAX; tmp++) {
1682 if (bridge_pdev->vendor == pcibridge_vendors[tmp]) {
1683 pcipriv->ndis_adapter.pcibridge_vendor = tmp;
1684 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507
JP
1685 "Pci Bridge Vendor is found index: %d\n",
1686 tmp);
b6b67df3
LF
1687 break;
1688 }
0c817338
LF
1689 }
1690 }
1691
1692 if (pcipriv->ndis_adapter.pcibridge_vendor !=
1693 PCI_BRIDGE_VENDOR_UNKNOWN) {
1694 pcipriv->ndis_adapter.pcibridge_busnum =
1695 bridge_pdev->bus->number;
1696 pcipriv->ndis_adapter.pcibridge_devnum =
1697 PCI_SLOT(bridge_pdev->devfn);
1698 pcipriv->ndis_adapter.pcibridge_funcnum =
1699 PCI_FUNC(bridge_pdev->devfn);
c7cfe38e
C
1700 pcipriv->ndis_adapter.pcibridge_pciehdr_offset =
1701 pci_pcie_cap(bridge_pdev);
0c817338
LF
1702 pcipriv->ndis_adapter.num4bytes =
1703 (pcipriv->ndis_adapter.pcibridge_pciehdr_offset + 0x10) / 4;
1704
1705 rtl_pci_get_linkcontrol_field(hw);
1706
1707 if (pcipriv->ndis_adapter.pcibridge_vendor ==
1708 PCI_BRIDGE_VENDOR_AMD) {
1709 pcipriv->ndis_adapter.amd_l1_patch =
1710 rtl_pci_get_amd_l1_patch(hw);
1711 }
1712 }
1713
1714 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507
JP
1715 "pcidev busnumber:devnumber:funcnumber:vendor:link_ctl %d:%d:%d:%x:%x\n",
1716 pcipriv->ndis_adapter.busnumber,
1717 pcipriv->ndis_adapter.devnumber,
1718 pcipriv->ndis_adapter.funcnumber,
1719 pdev->vendor, pcipriv->ndis_adapter.linkctrl_reg);
0c817338
LF
1720
1721 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507
JP
1722 "pci_bridge busnumber:devnumber:funcnumber:vendor:pcie_cap:link_ctl_reg:amd %d:%d:%d:%x:%x:%x:%x\n",
1723 pcipriv->ndis_adapter.pcibridge_busnum,
1724 pcipriv->ndis_adapter.pcibridge_devnum,
1725 pcipriv->ndis_adapter.pcibridge_funcnum,
1726 pcibridge_vendors[pcipriv->ndis_adapter.pcibridge_vendor],
1727 pcipriv->ndis_adapter.pcibridge_pciehdr_offset,
1728 pcipriv->ndis_adapter.pcibridge_linkctrlreg,
1729 pcipriv->ndis_adapter.amd_l1_patch);
0c817338
LF
1730
1731 rtl_pci_parse_configuration(pdev, hw);
1732
1733 return true;
1734}
1735
1736int __devinit rtl_pci_probe(struct pci_dev *pdev,
1737 const struct pci_device_id *id)
1738{
1739 struct ieee80211_hw *hw = NULL;
1740
1741 struct rtl_priv *rtlpriv = NULL;
1742 struct rtl_pci_priv *pcipriv = NULL;
1743 struct rtl_pci *rtlpci;
1744 unsigned long pmem_start, pmem_len, pmem_flags;
1745 int err;
1746
1747 err = pci_enable_device(pdev);
1748 if (err) {
1749 RT_ASSERT(false,
1750 ("%s : Cannot enable new PCI device\n",
1751 pci_name(pdev)));
1752 return err;
1753 }
1754
1755 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
1756 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1757 RT_ASSERT(false, ("Unable to obtain 32bit DMA "
1758 "for consistent allocations\n"));
1759 pci_disable_device(pdev);
1760 return -ENOMEM;
1761 }
1762 }
1763
1764 pci_set_master(pdev);
1765
1766 hw = ieee80211_alloc_hw(sizeof(struct rtl_pci_priv) +
1767 sizeof(struct rtl_priv), &rtl_ops);
1768 if (!hw) {
1769 RT_ASSERT(false,
1770 ("%s : ieee80211 alloc failed\n", pci_name(pdev)));
1771 err = -ENOMEM;
1772 goto fail1;
1773 }
1774
1775 SET_IEEE80211_DEV(hw, &pdev->dev);
1776 pci_set_drvdata(pdev, hw);
1777
1778 rtlpriv = hw->priv;
1779 pcipriv = (void *)rtlpriv->priv;
1780 pcipriv->dev.pdev = pdev;
1781
c7cfe38e
C
1782 /* init cfg & intf_ops */
1783 rtlpriv->rtlhal.interface = INTF_PCI;
1784 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_data);
1785 rtlpriv->intf_ops = &rtl_pci_ops;
1786
0c817338
LF
1787 /*
1788 *init dbgp flags before all
1789 *other functions, because we will
1790 *use it in other funtions like
1791 *RT_TRACE/RT_PRINT/RTL_PRINT_DATA
1792 *you can not use these macro
1793 *before this
1794 */
1795 rtl_dbgp_flag_init(hw);
1796
1797 /* MEM map */
1798 err = pci_request_regions(pdev, KBUILD_MODNAME);
1799 if (err) {
1800 RT_ASSERT(false, ("Can't obtain PCI resources\n"));
1801 return err;
1802 }
1803
c7cfe38e
C
1804 pmem_start = pci_resource_start(pdev, rtlpriv->cfg->bar_id);
1805 pmem_len = pci_resource_len(pdev, rtlpriv->cfg->bar_id);
1806 pmem_flags = pci_resource_flags(pdev, rtlpriv->cfg->bar_id);
0c817338
LF
1807
1808 /*shared mem start */
1809 rtlpriv->io.pci_mem_start =
c7cfe38e
C
1810 (unsigned long)pci_iomap(pdev,
1811 rtlpriv->cfg->bar_id, pmem_len);
0c817338
LF
1812 if (rtlpriv->io.pci_mem_start == 0) {
1813 RT_ASSERT(false, ("Can't map PCI mem\n"));
1814 goto fail2;
1815 }
1816
1817 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507
JP
1818 "mem mapped space: start: 0x%08lx len:%08lx flags:%08lx, after map:0x%08lx\n",
1819 pmem_start, pmem_len, pmem_flags,
1820 rtlpriv->io.pci_mem_start);
0c817338
LF
1821
1822 /* Disable Clk Request */
1823 pci_write_config_byte(pdev, 0x81, 0);
1824 /* leave D3 mode */
1825 pci_write_config_byte(pdev, 0x44, 0);
1826 pci_write_config_byte(pdev, 0x04, 0x06);
1827 pci_write_config_byte(pdev, 0x04, 0x07);
1828
0c817338 1829 /* find adapter */
fa7ccfb1
LF
1830 if (!_rtl_pci_find_adapter(pdev, hw))
1831 goto fail3;
0c817338
LF
1832
1833 /* Init IO handler */
1834 _rtl_pci_io_handler_init(&pdev->dev, hw);
1835
1836 /*like read eeprom and so on */
1837 rtlpriv->cfg->ops->read_eeprom_info(hw);
1838
1839 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
f30d7507 1840 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
0c817338
LF
1841 goto fail3;
1842 }
1843
1844 rtlpriv->cfg->ops->init_sw_leds(hw);
1845
1846 /*aspm */
1847 rtl_pci_init_aspm(hw);
1848
1849 /* Init mac80211 sw */
1850 err = rtl_init_core(hw);
1851 if (err) {
1852 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
f30d7507 1853 "Can't allocate sw for mac80211\n");
0c817338
LF
1854 goto fail3;
1855 }
1856
1857 /* Init PCI sw */
1858 err = !rtl_pci_init(hw, pdev);
1859 if (err) {
f30d7507 1860 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Failed to init PCI\n");
0c817338
LF
1861 goto fail3;
1862 }
1863
1864 err = ieee80211_register_hw(hw);
1865 if (err) {
1866 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
f30d7507 1867 "Can't register mac80211 hw\n");
0c817338
LF
1868 goto fail3;
1869 } else {
1870 rtlpriv->mac80211.mac80211_registered = 1;
1871 }
1872
1873 err = sysfs_create_group(&pdev->dev.kobj, &rtl_attribute_group);
1874 if (err) {
1875 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
f30d7507 1876 "failed to create sysfs device attributes\n");
0c817338
LF
1877 goto fail3;
1878 }
1879
1880 /*init rfkill */
1881 rtl_init_rfkill(hw);
1882
1883 rtlpci = rtl_pcidev(pcipriv);
1884 err = request_irq(rtlpci->pdev->irq, &_rtl_pci_interrupt,
1885 IRQF_SHARED, KBUILD_MODNAME, hw);
1886 if (err) {
1887 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
f30d7507
JP
1888 "%s: failed to register IRQ handler\n",
1889 wiphy_name(hw->wiphy));
0c817338
LF
1890 goto fail3;
1891 } else {
1892 rtlpci->irq_alloc = 1;
1893 }
1894
1895 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1896 return 0;
1897
1898fail3:
1899 pci_set_drvdata(pdev, NULL);
1900 rtl_deinit_core(hw);
1901 _rtl_pci_io_handler_release(hw);
1902 ieee80211_free_hw(hw);
1903
1904 if (rtlpriv->io.pci_mem_start != 0)
62e63975 1905 pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
0c817338
LF
1906
1907fail2:
1908 pci_release_regions(pdev);
1909
1910fail1:
1911
1912 pci_disable_device(pdev);
1913
1914 return -ENODEV;
1915
1916}
1917EXPORT_SYMBOL(rtl_pci_probe);
1918
1919void rtl_pci_disconnect(struct pci_dev *pdev)
1920{
1921 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1922 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1923 struct rtl_priv *rtlpriv = rtl_priv(hw);
1924 struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
1925 struct rtl_mac *rtlmac = rtl_mac(rtlpriv);
1926
1927 clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1928
1929 sysfs_remove_group(&pdev->dev.kobj, &rtl_attribute_group);
1930
1931 /*ieee80211_unregister_hw will call ops_stop */
1932 if (rtlmac->mac80211_registered == 1) {
1933 ieee80211_unregister_hw(hw);
1934 rtlmac->mac80211_registered = 0;
1935 } else {
1936 rtl_deinit_deferred_work(hw);
1937 rtlpriv->intf_ops->adapter_stop(hw);
1938 }
1939
1940 /*deinit rfkill */
1941 rtl_deinit_rfkill(hw);
1942
1943 rtl_pci_deinit(hw);
1944 rtl_deinit_core(hw);
0c817338
LF
1945 _rtl_pci_io_handler_release(hw);
1946 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1947
1948 if (rtlpci->irq_alloc) {
1949 free_irq(rtlpci->pdev->irq, hw);
1950 rtlpci->irq_alloc = 0;
1951 }
1952
1953 if (rtlpriv->io.pci_mem_start != 0) {
62e63975 1954 pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
0c817338
LF
1955 pci_release_regions(pdev);
1956 }
1957
1958 pci_disable_device(pdev);
c7cfe38e
C
1959
1960 rtl_pci_disable_aspm(hw);
1961
0c817338
LF
1962 pci_set_drvdata(pdev, NULL);
1963
1964 ieee80211_free_hw(hw);
1965}
1966EXPORT_SYMBOL(rtl_pci_disconnect);
1967
1968/***************************************
1969kernel pci power state define:
1970PCI_D0 ((pci_power_t __force) 0)
1971PCI_D1 ((pci_power_t __force) 1)
1972PCI_D2 ((pci_power_t __force) 2)
1973PCI_D3hot ((pci_power_t __force) 3)
1974PCI_D3cold ((pci_power_t __force) 4)
1975PCI_UNKNOWN ((pci_power_t __force) 5)
1976
1977This function is called when system
1978goes into suspend state mac80211 will
1979call rtl_mac_stop() from the mac80211
1980suspend function first, So there is
1981no need to call hw_disable here.
1982****************************************/
603be388 1983int rtl_pci_suspend(struct device *dev)
0c817338 1984{
603be388 1985 struct pci_dev *pdev = to_pci_dev(dev);
c7cfe38e
C
1986 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1987 struct rtl_priv *rtlpriv = rtl_priv(hw);
1988
1989 rtlpriv->cfg->ops->hw_suspend(hw);
1990 rtl_deinit_rfkill(hw);
1991
0c817338
LF
1992 return 0;
1993}
1994EXPORT_SYMBOL(rtl_pci_suspend);
1995
603be388 1996int rtl_pci_resume(struct device *dev)
0c817338 1997{
603be388 1998 struct pci_dev *pdev = to_pci_dev(dev);
c7cfe38e
C
1999 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2000 struct rtl_priv *rtlpriv = rtl_priv(hw);
0c817338 2001
c7cfe38e
C
2002 rtlpriv->cfg->ops->hw_resume(hw);
2003 rtl_init_rfkill(hw);
0c817338
LF
2004 return 0;
2005}
2006EXPORT_SYMBOL(rtl_pci_resume);
2007
2008struct rtl_intf_ops rtl_pci_ops = {
c7cfe38e 2009 .read_efuse_byte = read_efuse_byte,
0c817338
LF
2010 .adapter_start = rtl_pci_start,
2011 .adapter_stop = rtl_pci_stop,
2012 .adapter_tx = rtl_pci_tx,
c7cfe38e 2013 .flush = rtl_pci_flush,
0c817338 2014 .reset_trx_ring = rtl_pci_reset_trx_ring,
c7cfe38e 2015 .waitq_insert = rtl_pci_tx_chk_waitq_insert,
0c817338
LF
2016
2017 .disable_aspm = rtl_pci_disable_aspm,
2018 .enable_aspm = rtl_pci_enable_aspm,
2019};
This page took 0.25522 seconds and 5 git commands to generate.