blob: 4261e8ecc4c382d84a9b19c40d8fed2b463d5a5a [file] [log] [blame]
Larry Finger0c817332010-12-08 11:12:31 -06001/******************************************************************************
2 *
Larry Fingera8d76062012-01-07 20:46:42 -06003 * Copyright(c) 2009-2012 Realtek Corporation.
Larry Finger0c817332010-12-08 11:12:31 -06004 *
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
Larry Finger0c817332010-12-08 11:12:31 -060030#include "wifi.h"
Larry Fingerd273bb22012-01-27 13:59:25 -060031#include "core.h"
Larry Finger0c817332010-12-08 11:12:31 -060032#include "pci.h"
33#include "base.h"
34#include "ps.h"
Chaoming_Lic7cfe382011-04-25 13:23:15 -050035#include "efuse.h"
Larry Fingerd273bb22012-01-27 13:59:25 -060036#include <linux/export.h>
Larry Fingerf11bbfd2012-04-13 13:57:43 -050037#include <linux/kmemleak.h>
Larry Finger0c817332010-12-08 11:12:31 -060038
39static const u16 pcibridge_vendors[PCI_BRIDGE_VENDOR_MAX] = {
Jon Masonf01dce92011-06-27 13:03:44 -050040 PCI_VENDOR_ID_INTEL,
41 PCI_VENDOR_ID_ATI,
42 PCI_VENDOR_ID_AMD,
43 PCI_VENDOR_ID_SI
Larry Finger0c817332010-12-08 11:12:31 -060044};
45
Chaoming_Lic7cfe382011-04-25 13:23:15 -050046static const u8 ac_to_hwq[] = {
47 VO_QUEUE,
48 VI_QUEUE,
49 BE_QUEUE,
50 BK_QUEUE
51};
52
Larry Fingerd3bb1422011-04-25 13:23:20 -050053static u8 _rtl_mac_to_hwqueue(struct ieee80211_hw *hw,
Chaoming_Lic7cfe382011-04-25 13:23:15 -050054 struct sk_buff *skb)
55{
56 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Fingerd3bb1422011-04-25 13:23:20 -050057 __le16 fc = rtl_get_fc(skb);
Chaoming_Lic7cfe382011-04-25 13:23:15 -050058 u8 queue_index = skb_get_queue_mapping(skb);
59
60 if (unlikely(ieee80211_is_beacon(fc)))
61 return BEACON_QUEUE;
62 if (ieee80211_is_mgmt(fc))
63 return MGNT_QUEUE;
64 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE)
65 if (ieee80211_is_nullfunc(fc))
66 return HIGH_QUEUE;
67
68 return ac_to_hwq[queue_index];
69}
70
Larry Finger0c817332010-12-08 11:12:31 -060071/* Update PCI dependent default settings*/
72static void _rtl_pci_update_default_setting(struct ieee80211_hw *hw)
73{
74 struct rtl_priv *rtlpriv = rtl_priv(hw);
75 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
76 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
77 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
78 u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
Chaoming_Lic7cfe382011-04-25 13:23:15 -050079 u8 init_aspm;
Larry Finger0c817332010-12-08 11:12:31 -060080
81 ppsc->reg_rfps_level = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +000082 ppsc->support_aspm = false;
Larry Finger0c817332010-12-08 11:12:31 -060083
84 /*Update PCI ASPM setting */
85 ppsc->const_amdpci_aspm = rtlpci->const_amdpci_aspm;
86 switch (rtlpci->const_pci_aspm) {
87 case 0:
88 /*No ASPM */
89 break;
90
91 case 1:
92 /*ASPM dynamically enabled/disable. */
93 ppsc->reg_rfps_level |= RT_RF_LPS_LEVEL_ASPM;
94 break;
95
96 case 2:
97 /*ASPM with Clock Req dynamically enabled/disable. */
98 ppsc->reg_rfps_level |= (RT_RF_LPS_LEVEL_ASPM |
99 RT_RF_OFF_LEVL_CLK_REQ);
100 break;
101
102 case 3:
103 /*
104 * Always enable ASPM and Clock Req
105 * from initialization to halt.
106 * */
107 ppsc->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM);
108 ppsc->reg_rfps_level |= (RT_RF_PS_LEVEL_ALWAYS_ASPM |
109 RT_RF_OFF_LEVL_CLK_REQ);
110 break;
111
112 case 4:
113 /*
114 * Always enable ASPM without Clock Req
115 * from initialization to halt.
116 * */
117 ppsc->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM |
118 RT_RF_OFF_LEVL_CLK_REQ);
119 ppsc->reg_rfps_level |= RT_RF_PS_LEVEL_ALWAYS_ASPM;
120 break;
121 }
122
123 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
124
125 /*Update Radio OFF setting */
126 switch (rtlpci->const_hwsw_rfoff_d3) {
127 case 1:
128 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
129 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
130 break;
131
132 case 2:
133 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
134 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
135 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
136 break;
137
138 case 3:
139 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_PCI_D3;
140 break;
141 }
142
143 /*Set HW definition to determine if it supports ASPM. */
144 switch (rtlpci->const_support_pciaspm) {
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500145 case 0:{
146 /*Not support ASPM. */
147 bool support_aspm = false;
148 ppsc->support_aspm = support_aspm;
149 break;
150 }
151 case 1:{
152 /*Support ASPM. */
153 bool support_aspm = true;
154 bool support_backdoor = true;
155 ppsc->support_aspm = support_aspm;
156
157 /*if (priv->oem_id == RT_CID_TOSHIBA &&
158 !priv->ndis_adapter.amd_l1_patch)
159 support_backdoor = false; */
160
161 ppsc->support_backdoor = support_backdoor;
162
163 break;
164 }
Larry Finger0c817332010-12-08 11:12:31 -0600165 case 2:
166 /*ASPM value set by chipset. */
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500167 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_INTEL) {
168 bool support_aspm = true;
169 ppsc->support_aspm = support_aspm;
170 }
Larry Finger0c817332010-12-08 11:12:31 -0600171 break;
172 default:
173 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800174 "switch case not processed\n");
Larry Finger0c817332010-12-08 11:12:31 -0600175 break;
176 }
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500177
178 /* toshiba aspm issue, toshiba will set aspm selfly
179 * so we should not set aspm in driver */
180 pci_read_config_byte(rtlpci->pdev, 0x80, &init_aspm);
181 if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8192SE &&
182 init_aspm == 0x43)
183 ppsc->support_aspm = false;
184}
185
Larry Finger0c817332010-12-08 11:12:31 -0600186static bool _rtl_pci_platform_switch_device_pci_aspm(
187 struct ieee80211_hw *hw,
188 u8 value)
189{
190 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500191 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600192
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500193 if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE)
194 value |= 0x40;
195
Larry Finger0c817332010-12-08 11:12:31 -0600196 pci_write_config_byte(rtlpci->pdev, 0x80, value);
197
Larry Finger32473282011-03-27 16:19:57 -0500198 return false;
Larry Finger0c817332010-12-08 11:12:31 -0600199}
200
201/*When we set 0x01 to enable clk request. Set 0x0 to disable clk req.*/
Devendra.Naga1d73c512012-01-31 01:28:15 -0500202static void _rtl_pci_switch_clk_req(struct ieee80211_hw *hw, u8 value)
Larry Finger0c817332010-12-08 11:12:31 -0600203{
204 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500205 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600206
Larry Finger0c817332010-12-08 11:12:31 -0600207 pci_write_config_byte(rtlpci->pdev, 0x81, value);
Larry Finger0c817332010-12-08 11:12:31 -0600208
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500209 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE)
210 udelay(100);
Larry Finger0c817332010-12-08 11:12:31 -0600211}
212
213/*Disable RTL8192SE ASPM & Disable Pci Bridge ASPM*/
214static void rtl_pci_disable_aspm(struct ieee80211_hw *hw)
215{
216 struct rtl_priv *rtlpriv = rtl_priv(hw);
217 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
218 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
219 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
220 u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
Larry Finger0c817332010-12-08 11:12:31 -0600221 u8 num4bytes = pcipriv->ndis_adapter.num4bytes;
222 /*Retrieve original configuration settings. */
223 u8 linkctrl_reg = pcipriv->ndis_adapter.linkctrl_reg;
224 u16 pcibridge_linkctrlreg = pcipriv->ndis_adapter.
225 pcibridge_linkctrlreg;
226 u16 aspmlevel = 0;
Larry Finger32473282011-03-27 16:19:57 -0500227 u8 tmp_u1b = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600228
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500229 if (!ppsc->support_aspm)
230 return;
231
Larry Finger0c817332010-12-08 11:12:31 -0600232 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_UNKNOWN) {
233 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800234 "PCI(Bridge) UNKNOWN\n");
Larry Finger0c817332010-12-08 11:12:31 -0600235
236 return;
237 }
238
239 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) {
240 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_CLK_REQ);
241 _rtl_pci_switch_clk_req(hw, 0x0);
242 }
243
Larry Finger32473282011-03-27 16:19:57 -0500244 /*for promising device will in L0 state after an I/O. */
245 pci_read_config_byte(rtlpci->pdev, 0x80, &tmp_u1b);
Larry Finger0c817332010-12-08 11:12:31 -0600246
247 /*Set corresponding value. */
248 aspmlevel |= BIT(0) | BIT(1);
249 linkctrl_reg &= ~aspmlevel;
250 pcibridge_linkctrlreg &= ~(BIT(0) | BIT(1));
251
252 _rtl_pci_platform_switch_device_pci_aspm(hw, linkctrl_reg);
253 udelay(50);
254
255 /*4 Disable Pci Bridge ASPM */
Larry Finger886e14b2011-08-06 05:55:18 -0500256 pci_write_config_byte(rtlpci->pdev, (num4bytes << 2),
257 pcibridge_linkctrlreg);
Larry Finger0c817332010-12-08 11:12:31 -0600258
259 udelay(50);
Larry Finger0c817332010-12-08 11:12:31 -0600260}
261
262/*
263 *Enable RTL8192SE ASPM & Enable Pci Bridge ASPM for
264 *power saving We should follow the sequence to enable
265 *RTL8192SE first then enable Pci Bridge ASPM
266 *or the system will show bluescreen.
267 */
268static void rtl_pci_enable_aspm(struct ieee80211_hw *hw)
269{
270 struct rtl_priv *rtlpriv = rtl_priv(hw);
271 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
272 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
273 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
274 u8 pcibridge_busnum = pcipriv->ndis_adapter.pcibridge_busnum;
275 u8 pcibridge_devnum = pcipriv->ndis_adapter.pcibridge_devnum;
276 u8 pcibridge_funcnum = pcipriv->ndis_adapter.pcibridge_funcnum;
277 u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
Larry Finger0c817332010-12-08 11:12:31 -0600278 u8 num4bytes = pcipriv->ndis_adapter.num4bytes;
279 u16 aspmlevel;
280 u8 u_pcibridge_aspmsetting;
281 u8 u_device_aspmsetting;
282
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500283 if (!ppsc->support_aspm)
284 return;
285
Larry Finger0c817332010-12-08 11:12:31 -0600286 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_UNKNOWN) {
287 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800288 "PCI(Bridge) UNKNOWN\n");
Larry Finger0c817332010-12-08 11:12:31 -0600289 return;
290 }
291
292 /*4 Enable Pci Bridge ASPM */
Larry Finger0c817332010-12-08 11:12:31 -0600293
294 u_pcibridge_aspmsetting =
295 pcipriv->ndis_adapter.pcibridge_linkctrlreg |
296 rtlpci->const_hostpci_aspm_setting;
297
298 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_INTEL)
299 u_pcibridge_aspmsetting &= ~BIT(0);
300
Larry Finger886e14b2011-08-06 05:55:18 -0500301 pci_write_config_byte(rtlpci->pdev, (num4bytes << 2),
302 u_pcibridge_aspmsetting);
Larry Finger0c817332010-12-08 11:12:31 -0600303
304 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800305 "PlatformEnableASPM():PciBridge busnumber[%x], DevNumbe[%x], funcnumber[%x], Write reg[%x] = %x\n",
306 pcibridge_busnum, pcibridge_devnum, pcibridge_funcnum,
307 (pcipriv->ndis_adapter.pcibridge_pciehdr_offset + 0x10),
308 u_pcibridge_aspmsetting);
Larry Finger0c817332010-12-08 11:12:31 -0600309
310 udelay(50);
311
312 /*Get ASPM level (with/without Clock Req) */
313 aspmlevel = rtlpci->const_devicepci_aspm_setting;
314 u_device_aspmsetting = pcipriv->ndis_adapter.linkctrl_reg;
315
316 /*_rtl_pci_platform_switch_device_pci_aspm(dev,*/
317 /*(priv->ndis_adapter.linkctrl_reg | ASPMLevel)); */
318
319 u_device_aspmsetting |= aspmlevel;
320
321 _rtl_pci_platform_switch_device_pci_aspm(hw, u_device_aspmsetting);
322
323 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) {
324 _rtl_pci_switch_clk_req(hw, (ppsc->reg_rfps_level &
325 RT_RF_OFF_LEVL_CLK_REQ) ? 1 : 0);
326 RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_CLK_REQ);
327 }
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500328 udelay(100);
Larry Finger0c817332010-12-08 11:12:31 -0600329}
330
331static bool rtl_pci_get_amd_l1_patch(struct ieee80211_hw *hw)
332{
Larry Finger886e14b2011-08-06 05:55:18 -0500333 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600334
335 bool status = false;
336 u8 offset_e0;
337 unsigned offset_e4;
338
Larry Finger886e14b2011-08-06 05:55:18 -0500339 pci_write_config_byte(rtlpci->pdev, 0xe0, 0xa0);
Larry Finger0c817332010-12-08 11:12:31 -0600340
Larry Finger886e14b2011-08-06 05:55:18 -0500341 pci_read_config_byte(rtlpci->pdev, 0xe0, &offset_e0);
Larry Finger0c817332010-12-08 11:12:31 -0600342
343 if (offset_e0 == 0xA0) {
Larry Finger886e14b2011-08-06 05:55:18 -0500344 pci_read_config_dword(rtlpci->pdev, 0xe4, &offset_e4);
Larry Finger0c817332010-12-08 11:12:31 -0600345 if (offset_e4 & BIT(23))
346 status = true;
347 }
348
349 return status;
350}
351
Larry Fingerd3bb1422011-04-25 13:23:20 -0500352static void rtl_pci_get_linkcontrol_field(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -0600353{
354 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
Larry Finger886e14b2011-08-06 05:55:18 -0500355 struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
Larry Finger0c817332010-12-08 11:12:31 -0600356 u8 capabilityoffset = pcipriv->ndis_adapter.pcibridge_pciehdr_offset;
Larry Finger0c817332010-12-08 11:12:31 -0600357 u8 linkctrl_reg;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500358 u8 num4bbytes;
Larry Finger0c817332010-12-08 11:12:31 -0600359
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500360 num4bbytes = (capabilityoffset + 0x10) / 4;
Larry Finger0c817332010-12-08 11:12:31 -0600361
362 /*Read Link Control Register */
Larry Finger886e14b2011-08-06 05:55:18 -0500363 pci_read_config_byte(rtlpci->pdev, (num4bbytes << 2), &linkctrl_reg);
Larry Finger0c817332010-12-08 11:12:31 -0600364
365 pcipriv->ndis_adapter.pcibridge_linkctrlreg = linkctrl_reg;
366}
367
368static void rtl_pci_parse_configuration(struct pci_dev *pdev,
369 struct ieee80211_hw *hw)
370{
371 struct rtl_priv *rtlpriv = rtl_priv(hw);
372 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
373
374 u8 tmp;
Jiang Liu332badc2012-08-20 14:18:08 -0600375 u16 linkctrl_reg;
Larry Finger0c817332010-12-08 11:12:31 -0600376
377 /*Link Control Register */
Jiang Liu332badc2012-08-20 14:18:08 -0600378 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &linkctrl_reg);
379 pcipriv->ndis_adapter.linkctrl_reg = (u8)linkctrl_reg;
Larry Finger0c817332010-12-08 11:12:31 -0600380
Joe Perchesf30d7502012-01-04 19:40:41 -0800381 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Link Control Register =%x\n",
382 pcipriv->ndis_adapter.linkctrl_reg);
Larry Finger0c817332010-12-08 11:12:31 -0600383
384 pci_read_config_byte(pdev, 0x98, &tmp);
385 tmp |= BIT(4);
386 pci_write_config_byte(pdev, 0x98, tmp);
387
388 tmp = 0x17;
389 pci_write_config_byte(pdev, 0x70f, tmp);
390}
391
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500392static void rtl_pci_init_aspm(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -0600393{
394 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
395
396 _rtl_pci_update_default_setting(hw);
397
398 if (ppsc->reg_rfps_level & RT_RF_PS_LEVEL_ALWAYS_ASPM) {
399 /*Always enable ASPM & Clock Req. */
400 rtl_pci_enable_aspm(hw);
401 RT_SET_PS_LEVEL(ppsc, RT_RF_PS_LEVEL_ALWAYS_ASPM);
402 }
403
404}
405
Larry Finger0c817332010-12-08 11:12:31 -0600406static void _rtl_pci_io_handler_init(struct device *dev,
407 struct ieee80211_hw *hw)
408{
409 struct rtl_priv *rtlpriv = rtl_priv(hw);
410
411 rtlpriv->io.dev = dev;
412
413 rtlpriv->io.write8_async = pci_write8_async;
414 rtlpriv->io.write16_async = pci_write16_async;
415 rtlpriv->io.write32_async = pci_write32_async;
416
417 rtlpriv->io.read8_sync = pci_read8_sync;
418 rtlpriv->io.read16_sync = pci_read16_sync;
419 rtlpriv->io.read32_sync = pci_read32_sync;
420
421}
422
423static void _rtl_pci_io_handler_release(struct ieee80211_hw *hw)
424{
425}
426
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500427static bool _rtl_update_earlymode_info(struct ieee80211_hw *hw,
428 struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc, u8 tid)
429{
430 struct rtl_priv *rtlpriv = rtl_priv(hw);
431 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
432 u8 additionlen = FCS_LEN;
433 struct sk_buff *next_skb;
434
435 /* here open is 4, wep/tkip is 8, aes is 12*/
436 if (info->control.hw_key)
437 additionlen += info->control.hw_key->icv_len;
438
439 /* The most skb num is 6 */
440 tcb_desc->empkt_num = 0;
441 spin_lock_bh(&rtlpriv->locks.waitq_lock);
442 skb_queue_walk(&rtlpriv->mac80211.skb_waitq[tid], next_skb) {
443 struct ieee80211_tx_info *next_info;
444
445 next_info = IEEE80211_SKB_CB(next_skb);
446 if (next_info->flags & IEEE80211_TX_CTL_AMPDU) {
447 tcb_desc->empkt_len[tcb_desc->empkt_num] =
448 next_skb->len + additionlen;
449 tcb_desc->empkt_num++;
450 } else {
451 break;
452 }
453
454 if (skb_queue_is_last(&rtlpriv->mac80211.skb_waitq[tid],
455 next_skb))
456 break;
457
458 if (tcb_desc->empkt_num >= 5)
459 break;
460 }
461 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
462
463 return true;
464}
465
466/* just for early mode now */
467static void _rtl_pci_tx_chk_waitq(struct ieee80211_hw *hw)
468{
469 struct rtl_priv *rtlpriv = rtl_priv(hw);
470 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
471 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
472 struct sk_buff *skb = NULL;
473 struct ieee80211_tx_info *info = NULL;
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +0300474 int tid;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500475
476 if (!rtlpriv->rtlhal.earlymode_enable)
477 return;
478
479 /* we juse use em for BE/BK/VI/VO */
480 for (tid = 7; tid >= 0; tid--) {
Larry Finger2a00def2012-07-11 14:32:33 -0500481 u8 hw_queue = ac_to_hwq[rtl_tid_to_ac(tid)];
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500482 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
483 while (!mac->act_scanning &&
484 rtlpriv->psc.rfpwr_state == ERFON) {
485 struct rtl_tcb_desc tcb_desc;
486 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
487
488 spin_lock_bh(&rtlpriv->locks.waitq_lock);
489 if (!skb_queue_empty(&mac->skb_waitq[tid]) &&
490 (ring->entries - skb_queue_len(&ring->queue) > 5)) {
491 skb = skb_dequeue(&mac->skb_waitq[tid]);
492 } else {
493 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
494 break;
495 }
496 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
497
498 /* Some macaddr can't do early mode. like
499 * multicast/broadcast/no_qos data */
500 info = IEEE80211_SKB_CB(skb);
501 if (info->flags & IEEE80211_TX_CTL_AMPDU)
502 _rtl_update_earlymode_info(hw, skb,
503 &tcb_desc, tid);
504
Thomas Huehn36323f82012-07-23 21:33:42 +0200505 rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, &tcb_desc);
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500506 }
507 }
508}
509
510
Larry Finger0c817332010-12-08 11:12:31 -0600511static void _rtl_pci_tx_isr(struct ieee80211_hw *hw, int prio)
512{
513 struct rtl_priv *rtlpriv = rtl_priv(hw);
514 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
515
516 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[prio];
517
518 while (skb_queue_len(&ring->queue)) {
519 struct rtl_tx_desc *entry = &ring->desc[ring->idx];
520 struct sk_buff *skb;
521 struct ieee80211_tx_info *info;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500522 __le16 fc;
523 u8 tid;
Larry Finger0c817332010-12-08 11:12:31 -0600524
525 u8 own = (u8) rtlpriv->cfg->ops->get_desc((u8 *) entry, true,
526 HW_DESC_OWN);
527
528 /*
529 *beacon packet will only use the first
530 *descriptor defautly,and the own may not
531 *be cleared by the hardware
532 */
533 if (own)
534 return;
535 ring->idx = (ring->idx + 1) % ring->entries;
536
537 skb = __skb_dequeue(&ring->queue);
538 pci_unmap_single(rtlpci->pdev,
Larry Fingerd3bb1422011-04-25 13:23:20 -0500539 rtlpriv->cfg->ops->
Larry Finger0c817332010-12-08 11:12:31 -0600540 get_desc((u8 *) entry, true,
Larry Fingerd3bb1422011-04-25 13:23:20 -0500541 HW_DESC_TXBUFF_ADDR),
Larry Finger0c817332010-12-08 11:12:31 -0600542 skb->len, PCI_DMA_TODEVICE);
543
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500544 /* remove early mode header */
545 if (rtlpriv->rtlhal.earlymode_enable)
546 skb_pull(skb, EM_HDR_LEN);
547
Larry Finger0c817332010-12-08 11:12:31 -0600548 RT_TRACE(rtlpriv, (COMP_INTR | COMP_SEND), DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800549 "new ring->idx:%d, free: skb_queue_len:%d, free: seq:%x\n",
550 ring->idx,
551 skb_queue_len(&ring->queue),
552 *(u16 *) (skb->data + 22));
Larry Finger0c817332010-12-08 11:12:31 -0600553
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500554 if (prio == TXCMD_QUEUE) {
555 dev_kfree_skb(skb);
556 goto tx_status_ok;
557
558 }
559
560 /* for sw LPS, just after NULL skb send out, we can
561 * sure AP kown we are sleeped, our we should not let
562 * rf to sleep*/
563 fc = rtl_get_fc(skb);
564 if (ieee80211_is_nullfunc(fc)) {
565 if (ieee80211_has_pm(fc)) {
Mike McCormack9c050442011-06-20 10:44:58 +0900566 rtlpriv->mac80211.offchan_delay = true;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000567 rtlpriv->psc.state_inap = true;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500568 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000569 rtlpriv->psc.state_inap = false;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500570 }
571 }
572
573 /* update tid tx pkt num */
574 tid = rtl_get_tid(skb);
575 if (tid <= 7)
576 rtlpriv->link_info.tidtx_inperiod[tid]++;
577
Larry Finger0c817332010-12-08 11:12:31 -0600578 info = IEEE80211_SKB_CB(skb);
579 ieee80211_tx_info_clear_status(info);
580
581 info->flags |= IEEE80211_TX_STAT_ACK;
582 /*info->status.rates[0].count = 1; */
583
584 ieee80211_tx_status_irqsafe(hw, skb);
585
586 if ((ring->entries - skb_queue_len(&ring->queue))
587 == 2) {
588
589 RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800590 "more desc left, wake skb_queue@%d, ring->idx = %d, skb_queue_len = 0x%d\n",
591 prio, ring->idx,
592 skb_queue_len(&ring->queue));
Larry Finger0c817332010-12-08 11:12:31 -0600593
594 ieee80211_wake_queue(hw,
595 skb_get_queue_mapping
596 (skb));
597 }
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500598tx_status_ok:
Larry Finger0c817332010-12-08 11:12:31 -0600599 skb = NULL;
600 }
601
602 if (((rtlpriv->link_info.num_rx_inperiod +
603 rtlpriv->link_info.num_tx_inperiod) > 8) ||
604 (rtlpriv->link_info.num_rx_inperiod > 2)) {
Stanislaw Gruszka41affd52011-12-12 12:43:23 +0100605 schedule_work(&rtlpriv->works.lps_leave_work);
Larry Finger0c817332010-12-08 11:12:31 -0600606 }
607}
608
Mike McCormackfd854772011-06-06 23:13:06 +0900609static void _rtl_receive_one(struct ieee80211_hw *hw, struct sk_buff *skb,
610 struct ieee80211_rx_status rx_status)
611{
612 struct rtl_priv *rtlpriv = rtl_priv(hw);
613 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
614 __le16 fc = rtl_get_fc(skb);
615 bool unicast = false;
616 struct sk_buff *uskb = NULL;
617 u8 *pdata;
618
619
620 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
621
622 if (is_broadcast_ether_addr(hdr->addr1)) {
623 ;/*TODO*/
624 } else if (is_multicast_ether_addr(hdr->addr1)) {
625 ;/*TODO*/
626 } else {
627 unicast = true;
628 rtlpriv->stats.rxbytesunicast += skb->len;
629 }
630
631 rtl_is_special_data(hw, skb, false);
632
633 if (ieee80211_is_data(fc)) {
634 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
635
636 if (unicast)
637 rtlpriv->link_info.num_rx_inperiod++;
638 }
639
640 /* for sw lps */
641 rtl_swlps_beacon(hw, (void *)skb->data, skb->len);
642 rtl_recognize_peer(hw, (void *)skb->data, skb->len);
643 if ((rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP) &&
644 (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G) &&
645 (ieee80211_is_beacon(fc) || ieee80211_is_probe_resp(fc)))
646 return;
647
648 if (unlikely(!rtl_action_proc(hw, skb, false)))
649 return;
650
651 uskb = dev_alloc_skb(skb->len + 128);
Larry Finger76a92be2012-01-07 20:46:40 -0600652 if (!uskb)
653 return; /* exit if allocation failed */
Mike McCormackfd854772011-06-06 23:13:06 +0900654 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status, sizeof(rx_status));
655 pdata = (u8 *)skb_put(uskb, skb->len);
656 memcpy(pdata, skb->data, skb->len);
657
658 ieee80211_rx_irqsafe(hw, uskb);
659}
660
Larry Finger0c817332010-12-08 11:12:31 -0600661static void _rtl_pci_rx_interrupt(struct ieee80211_hw *hw)
662{
663 struct rtl_priv *rtlpriv = rtl_priv(hw);
664 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
665 int rx_queue_idx = RTL_PCI_RX_MPDU_QUEUE;
666
667 struct ieee80211_rx_status rx_status = { 0 };
668 unsigned int count = rtlpci->rxringcount;
669 u8 own;
670 u8 tmp_one;
671 u32 bufferaddress;
Larry Finger0c817332010-12-08 11:12:31 -0600672
673 struct rtl_stats stats = {
674 .signal = 0,
675 .noise = -98,
676 .rate = 0,
677 };
Mike McCormack34ddb202011-05-31 08:49:07 +0900678 int index = rtlpci->rx_ring[rx_queue_idx].idx;
Larry Finger0c817332010-12-08 11:12:31 -0600679
680 /*RX NORMAL PKT */
681 while (count--) {
682 /*rx descriptor */
683 struct rtl_rx_desc *pdesc = &rtlpci->rx_ring[rx_queue_idx].desc[
Mike McCormack34ddb202011-05-31 08:49:07 +0900684 index];
Larry Finger0c817332010-12-08 11:12:31 -0600685 /*rx pkt */
686 struct sk_buff *skb = rtlpci->rx_ring[rx_queue_idx].rx_buf[
Mike McCormack34ddb202011-05-31 08:49:07 +0900687 index];
Mike McCormack2c333362011-06-06 23:12:08 +0900688 struct sk_buff *new_skb = NULL;
Larry Finger0c817332010-12-08 11:12:31 -0600689
690 own = (u8) rtlpriv->cfg->ops->get_desc((u8 *) pdesc,
691 false, HW_DESC_OWN);
692
Mike McCormack2c333362011-06-06 23:12:08 +0900693 /*wait data to be filled by hardware */
694 if (own)
Mike McCormack34ddb202011-05-31 08:49:07 +0900695 break;
Larry Finger0c817332010-12-08 11:12:31 -0600696
Mike McCormack2c333362011-06-06 23:12:08 +0900697 rtlpriv->cfg->ops->query_rx_desc(hw, &stats,
698 &rx_status,
699 (u8 *) pdesc, skb);
Larry Finger0c817332010-12-08 11:12:31 -0600700
Mike McCormack8db8ddf2011-06-06 23:12:42 +0900701 if (stats.crc || stats.hwerror)
702 goto done;
703
Mike McCormack2c333362011-06-06 23:12:08 +0900704 new_skb = dev_alloc_skb(rtlpci->rxbuffersize);
705 if (unlikely(!new_skb)) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800706 RT_TRACE(rtlpriv, (COMP_INTR | COMP_RECV), DBG_DMESG,
707 "can't alloc skb for rx\n");
Mike McCormack2c333362011-06-06 23:12:08 +0900708 goto done;
709 }
Mike McCormack6633d642011-06-07 08:58:31 +0900710
Mike McCormack2c333362011-06-06 23:12:08 +0900711 pci_unmap_single(rtlpci->pdev,
712 *((dma_addr_t *) skb->cb),
713 rtlpci->rxbuffersize,
714 PCI_DMA_FROMDEVICE);
Mike McCormack6633d642011-06-07 08:58:31 +0900715
Mike McCormack2c333362011-06-06 23:12:08 +0900716 skb_put(skb, rtlpriv->cfg->ops->get_desc((u8 *) pdesc, false,
717 HW_DESC_RXPKT_LEN));
718 skb_reserve(skb, stats.rx_drvinfo_size + stats.rx_bufshift);
Larry Finger0c817332010-12-08 11:12:31 -0600719
Mike McCormack2c333362011-06-06 23:12:08 +0900720 /*
721 * NOTICE This can not be use for mac80211,
722 * this is done in mac80211 code,
723 * if you done here sec DHCP will fail
724 * skb_trim(skb, skb->len - 4);
725 */
Larry Finger0c817332010-12-08 11:12:31 -0600726
Mike McCormackfd854772011-06-06 23:13:06 +0900727 _rtl_receive_one(hw, skb, rx_status);
Larry Finger0c817332010-12-08 11:12:31 -0600728
Mike McCormack2c333362011-06-06 23:12:08 +0900729 if (((rtlpriv->link_info.num_rx_inperiod +
730 rtlpriv->link_info.num_tx_inperiod) > 8) ||
731 (rtlpriv->link_info.num_rx_inperiod > 2)) {
Stanislaw Gruszka41affd52011-12-12 12:43:23 +0100732 schedule_work(&rtlpriv->works.lps_leave_work);
Mike McCormack2c333362011-06-06 23:12:08 +0900733 }
Larry Finger0c817332010-12-08 11:12:31 -0600734
Mike McCormack14058ad2011-06-06 23:12:53 +0900735 dev_kfree_skb_any(skb);
Mike McCormack2c333362011-06-06 23:12:08 +0900736 skb = new_skb;
Larry Finger0c817332010-12-08 11:12:31 -0600737
Mike McCormack2c333362011-06-06 23:12:08 +0900738 rtlpci->rx_ring[rx_queue_idx].rx_buf[index] = skb;
739 *((dma_addr_t *) skb->cb) =
Larry Finger0c817332010-12-08 11:12:31 -0600740 pci_map_single(rtlpci->pdev, skb_tail_pointer(skb),
741 rtlpci->rxbuffersize,
742 PCI_DMA_FROMDEVICE);
743
Larry Finger0c817332010-12-08 11:12:31 -0600744done:
Larry Fingerd3bb1422011-04-25 13:23:20 -0500745 bufferaddress = (*((dma_addr_t *)skb->cb));
Larry Finger86bfec82012-12-27 10:37:28 -0600746 if (pci_dma_mapping_error(rtlpci->pdev, bufferaddress))
747 return;
Larry Finger0c817332010-12-08 11:12:31 -0600748 tmp_one = 1;
749 rtlpriv->cfg->ops->set_desc((u8 *) pdesc, false,
750 HW_DESC_RXBUFF_ADDR,
751 (u8 *)&bufferaddress);
Larry Finger0c817332010-12-08 11:12:31 -0600752 rtlpriv->cfg->ops->set_desc((u8 *)pdesc, false,
753 HW_DESC_RXPKT_LEN,
754 (u8 *)&rtlpci->rxbuffersize);
755
Mike McCormack34ddb202011-05-31 08:49:07 +0900756 if (index == rtlpci->rxringcount - 1)
Larry Finger0c817332010-12-08 11:12:31 -0600757 rtlpriv->cfg->ops->set_desc((u8 *)pdesc, false,
758 HW_DESC_RXERO,
Joe Perches2c208892012-06-04 12:44:17 +0000759 &tmp_one);
Larry Finger0c817332010-12-08 11:12:31 -0600760
Mike McCormackfebc9fe2011-05-31 08:49:51 +0900761 rtlpriv->cfg->ops->set_desc((u8 *)pdesc, false, HW_DESC_RXOWN,
Joe Perches2c208892012-06-04 12:44:17 +0000762 &tmp_one);
Mike McCormackfebc9fe2011-05-31 08:49:51 +0900763
Mike McCormack34ddb202011-05-31 08:49:07 +0900764 index = (index + 1) % rtlpci->rxringcount;
Larry Finger0c817332010-12-08 11:12:31 -0600765 }
766
Mike McCormack34ddb202011-05-31 08:49:07 +0900767 rtlpci->rx_ring[rx_queue_idx].idx = index;
Larry Finger0c817332010-12-08 11:12:31 -0600768}
769
Larry Finger0c817332010-12-08 11:12:31 -0600770static irqreturn_t _rtl_pci_interrupt(int irq, void *dev_id)
771{
772 struct ieee80211_hw *hw = dev_id;
773 struct rtl_priv *rtlpriv = rtl_priv(hw);
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500774 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600775 unsigned long flags;
776 u32 inta = 0;
777 u32 intb = 0;
Larry Fingerde2e56c2011-11-23 21:30:19 -0600778 irqreturn_t ret = IRQ_HANDLED;
Larry Finger0c817332010-12-08 11:12:31 -0600779
Larry Finger0c817332010-12-08 11:12:31 -0600780 spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
781
782 /*read ISR: 4/8bytes */
783 rtlpriv->cfg->ops->interrupt_recognized(hw, &inta, &intb);
784
785 /*Shared IRQ or HW disappared */
Larry Fingerde2e56c2011-11-23 21:30:19 -0600786 if (!inta || inta == 0xffff) {
787 ret = IRQ_NONE;
Larry Finger0c817332010-12-08 11:12:31 -0600788 goto done;
Larry Fingerde2e56c2011-11-23 21:30:19 -0600789 }
Larry Finger0c817332010-12-08 11:12:31 -0600790
791 /*<1> beacon related */
792 if (inta & rtlpriv->cfg->maps[RTL_IMR_TBDOK]) {
793 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800794 "beacon ok interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600795 }
796
797 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_TBDER])) {
798 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800799 "beacon err interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600800 }
801
802 if (inta & rtlpriv->cfg->maps[RTL_IMR_BDOK]) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800803 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE, "beacon interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600804 }
805
806 if (inta & rtlpriv->cfg->maps[RTL_IMR_BcnInt]) {
807 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800808 "prepare beacon for interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600809 tasklet_schedule(&rtlpriv->works.irq_prepare_bcn_tasklet);
810 }
811
812 /*<3> Tx related */
813 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_TXFOVW]))
Joe Perchesf30d7502012-01-04 19:40:41 -0800814 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, "IMR_TXFOVW!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600815
816 if (inta & rtlpriv->cfg->maps[RTL_IMR_MGNTDOK]) {
817 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800818 "Manage ok interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600819 _rtl_pci_tx_isr(hw, MGNT_QUEUE);
820 }
821
822 if (inta & rtlpriv->cfg->maps[RTL_IMR_HIGHDOK]) {
823 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800824 "HIGH_QUEUE ok interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600825 _rtl_pci_tx_isr(hw, HIGH_QUEUE);
826 }
827
828 if (inta & rtlpriv->cfg->maps[RTL_IMR_BKDOK]) {
829 rtlpriv->link_info.num_tx_inperiod++;
830
831 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800832 "BK Tx OK interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600833 _rtl_pci_tx_isr(hw, BK_QUEUE);
834 }
835
836 if (inta & rtlpriv->cfg->maps[RTL_IMR_BEDOK]) {
837 rtlpriv->link_info.num_tx_inperiod++;
838
839 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800840 "BE TX OK interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600841 _rtl_pci_tx_isr(hw, BE_QUEUE);
842 }
843
844 if (inta & rtlpriv->cfg->maps[RTL_IMR_VIDOK]) {
845 rtlpriv->link_info.num_tx_inperiod++;
846
847 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800848 "VI TX OK interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600849 _rtl_pci_tx_isr(hw, VI_QUEUE);
850 }
851
852 if (inta & rtlpriv->cfg->maps[RTL_IMR_VODOK]) {
853 rtlpriv->link_info.num_tx_inperiod++;
854
855 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800856 "Vo TX OK interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600857 _rtl_pci_tx_isr(hw, VO_QUEUE);
858 }
859
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500860 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
861 if (inta & rtlpriv->cfg->maps[RTL_IMR_COMDOK]) {
862 rtlpriv->link_info.num_tx_inperiod++;
863
864 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800865 "CMD TX OK interrupt!\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500866 _rtl_pci_tx_isr(hw, TXCMD_QUEUE);
867 }
868 }
869
Larry Finger0c817332010-12-08 11:12:31 -0600870 /*<2> Rx related */
871 if (inta & rtlpriv->cfg->maps[RTL_IMR_ROK]) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800872 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE, "Rx ok interrupt!\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500873 _rtl_pci_rx_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -0600874 }
875
876 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_RDU])) {
877 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Joe Perchesf30d7502012-01-04 19:40:41 -0800878 "rx descriptor unavailable!\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500879 _rtl_pci_rx_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -0600880 }
881
882 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_RXFOVW])) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800883 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, "rx overflow !\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500884 _rtl_pci_rx_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -0600885 }
886
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500887 if (rtlpriv->rtlhal.earlymode_enable)
888 tasklet_schedule(&rtlpriv->works.irq_tasklet);
889
Larry Finger0c817332010-12-08 11:12:31 -0600890done:
891 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
Larry Fingerde2e56c2011-11-23 21:30:19 -0600892 return ret;
Larry Finger0c817332010-12-08 11:12:31 -0600893}
894
895static void _rtl_pci_irq_tasklet(struct ieee80211_hw *hw)
896{
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500897 _rtl_pci_tx_chk_waitq(hw);
Larry Finger0c817332010-12-08 11:12:31 -0600898}
899
900static void _rtl_pci_prepare_bcn_tasklet(struct ieee80211_hw *hw)
901{
902 struct rtl_priv *rtlpriv = rtl_priv(hw);
903 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
904 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500905 struct rtl8192_tx_ring *ring = NULL;
Larry Finger0c817332010-12-08 11:12:31 -0600906 struct ieee80211_hdr *hdr = NULL;
907 struct ieee80211_tx_info *info = NULL;
908 struct sk_buff *pskb = NULL;
909 struct rtl_tx_desc *pdesc = NULL;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500910 struct rtl_tcb_desc tcb_desc;
Larry Finger0c817332010-12-08 11:12:31 -0600911 u8 temp_one = 1;
912
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500913 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
Larry Finger0c817332010-12-08 11:12:31 -0600914 ring = &rtlpci->tx_ring[BEACON_QUEUE];
915 pskb = __skb_dequeue(&ring->queue);
Larry Fingera75e2ad2012-03-26 10:48:20 -0500916 if (pskb) {
917 struct rtl_tx_desc *entry = &ring->desc[ring->idx];
918 pci_unmap_single(rtlpci->pdev, rtlpriv->cfg->ops->get_desc(
919 (u8 *) entry, true, HW_DESC_TXBUFF_ADDR),
920 pskb->len, PCI_DMA_TODEVICE);
Larry Finger0c817332010-12-08 11:12:31 -0600921 kfree_skb(pskb);
Larry Fingera75e2ad2012-03-26 10:48:20 -0500922 }
Larry Finger0c817332010-12-08 11:12:31 -0600923
924 /*NB: the beacon data buffer must be 32-bit aligned. */
925 pskb = ieee80211_beacon_get(hw, mac->vif);
926 if (pskb == NULL)
927 return;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500928 hdr = rtl_get_hdr(pskb);
Larry Finger0c817332010-12-08 11:12:31 -0600929 info = IEEE80211_SKB_CB(pskb);
Larry Finger0c817332010-12-08 11:12:31 -0600930 pdesc = &ring->desc[0];
931 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *) pdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +0200932 info, NULL, pskb, BEACON_QUEUE, &tcb_desc);
Larry Finger0c817332010-12-08 11:12:31 -0600933
934 __skb_queue_tail(&ring->queue, pskb);
935
936 rtlpriv->cfg->ops->set_desc((u8 *) pdesc, true, HW_DESC_OWN,
Joe Perches2c208892012-06-04 12:44:17 +0000937 &temp_one);
Larry Finger0c817332010-12-08 11:12:31 -0600938
939 return;
940}
941
Stanislaw Gruszka41affd52011-12-12 12:43:23 +0100942static void rtl_lps_leave_work_callback(struct work_struct *work)
943{
944 struct rtl_works *rtlworks =
945 container_of(work, struct rtl_works, lps_leave_work);
946 struct ieee80211_hw *hw = rtlworks->hw;
947
948 rtl_lps_leave(hw);
949}
950
Larry Finger0c817332010-12-08 11:12:31 -0600951static void _rtl_pci_init_trx_var(struct ieee80211_hw *hw)
952{
953 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
954 u8 i;
955
956 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
957 rtlpci->txringcount[i] = RT_TXDESC_NUM;
958
959 /*
960 *we just alloc 2 desc for beacon queue,
961 *because we just need first desc in hw beacon.
962 */
963 rtlpci->txringcount[BEACON_QUEUE] = 2;
964
965 /*
966 *BE queue need more descriptor for performance
967 *consideration or, No more tx desc will happen,
968 *and may cause mac80211 mem leakage.
969 */
970 rtlpci->txringcount[BE_QUEUE] = RT_TXDESC_NUM_BE_QUEUE;
971
972 rtlpci->rxbuffersize = 9100; /*2048/1024; */
973 rtlpci->rxringcount = RTL_PCI_MAX_RX_COUNT; /*64; */
974}
975
976static void _rtl_pci_init_struct(struct ieee80211_hw *hw,
977 struct pci_dev *pdev)
978{
979 struct rtl_priv *rtlpriv = rtl_priv(hw);
980 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
981 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
982 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600983
984 rtlpci->up_first_time = true;
985 rtlpci->being_init_adapter = false;
986
987 rtlhal->hw = hw;
988 rtlpci->pdev = pdev;
989
Larry Finger0c817332010-12-08 11:12:31 -0600990 /*Tx/Rx related var */
991 _rtl_pci_init_trx_var(hw);
992
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500993 /*IBSS*/ mac->beacon_interval = 100;
Larry Finger0c817332010-12-08 11:12:31 -0600994
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500995 /*AMPDU*/
996 mac->min_space_cfg = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600997 mac->max_mss_density = 0;
998 /*set sane AMPDU defaults */
999 mac->current_ampdu_density = 7;
1000 mac->current_ampdu_factor = 3;
1001
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001002 /*QOS*/
1003 rtlpci->acm_method = eAcmWay2_SW;
Larry Finger0c817332010-12-08 11:12:31 -06001004
1005 /*task */
1006 tasklet_init(&rtlpriv->works.irq_tasklet,
1007 (void (*)(unsigned long))_rtl_pci_irq_tasklet,
1008 (unsigned long)hw);
1009 tasklet_init(&rtlpriv->works.irq_prepare_bcn_tasklet,
1010 (void (*)(unsigned long))_rtl_pci_prepare_bcn_tasklet,
1011 (unsigned long)hw);
Stanislaw Gruszka41affd52011-12-12 12:43:23 +01001012 INIT_WORK(&rtlpriv->works.lps_leave_work, rtl_lps_leave_work_callback);
Larry Finger0c817332010-12-08 11:12:31 -06001013}
1014
1015static int _rtl_pci_init_tx_ring(struct ieee80211_hw *hw,
1016 unsigned int prio, unsigned int entries)
1017{
1018 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1019 struct rtl_priv *rtlpriv = rtl_priv(hw);
1020 struct rtl_tx_desc *ring;
1021 dma_addr_t dma;
1022 u32 nextdescaddress;
1023 int i;
1024
1025 ring = pci_alloc_consistent(rtlpci->pdev,
1026 sizeof(*ring) * entries, &dma);
1027
1028 if (!ring || (unsigned long)ring & 0xFF) {
1029 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001030 "Cannot allocate TX ring (prio = %d)\n", prio);
Larry Finger0c817332010-12-08 11:12:31 -06001031 return -ENOMEM;
1032 }
1033
1034 memset(ring, 0, sizeof(*ring) * entries);
1035 rtlpci->tx_ring[prio].desc = ring;
1036 rtlpci->tx_ring[prio].dma = dma;
1037 rtlpci->tx_ring[prio].idx = 0;
1038 rtlpci->tx_ring[prio].entries = entries;
1039 skb_queue_head_init(&rtlpci->tx_ring[prio].queue);
1040
Joe Perchesf30d7502012-01-04 19:40:41 -08001041 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "queue:%d, ring_addr:%p\n",
1042 prio, ring);
Larry Finger0c817332010-12-08 11:12:31 -06001043
1044 for (i = 0; i < entries; i++) {
Larry Fingerd3bb1422011-04-25 13:23:20 -05001045 nextdescaddress = (u32) dma +
Larry Finger982d96b2011-05-01 22:30:54 -05001046 ((i + 1) % entries) *
Larry Fingerd3bb1422011-04-25 13:23:20 -05001047 sizeof(*ring);
Larry Finger0c817332010-12-08 11:12:31 -06001048
1049 rtlpriv->cfg->ops->set_desc((u8 *)&(ring[i]),
1050 true, HW_DESC_TX_NEXTDESC_ADDR,
1051 (u8 *)&nextdescaddress);
1052 }
1053
1054 return 0;
1055}
1056
1057static int _rtl_pci_init_rx_ring(struct ieee80211_hw *hw)
1058{
1059 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1060 struct rtl_priv *rtlpriv = rtl_priv(hw);
1061 struct rtl_rx_desc *entry = NULL;
1062 int i, rx_queue_idx;
1063 u8 tmp_one = 1;
1064
1065 /*
1066 *rx_queue_idx 0:RX_MPDU_QUEUE
1067 *rx_queue_idx 1:RX_CMD_QUEUE
1068 */
1069 for (rx_queue_idx = 0; rx_queue_idx < RTL_PCI_MAX_RX_QUEUE;
1070 rx_queue_idx++) {
1071 rtlpci->rx_ring[rx_queue_idx].desc =
1072 pci_alloc_consistent(rtlpci->pdev,
1073 sizeof(*rtlpci->rx_ring[rx_queue_idx].
1074 desc) * rtlpci->rxringcount,
1075 &rtlpci->rx_ring[rx_queue_idx].dma);
1076
1077 if (!rtlpci->rx_ring[rx_queue_idx].desc ||
1078 (unsigned long)rtlpci->rx_ring[rx_queue_idx].desc & 0xFF) {
1079 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001080 "Cannot allocate RX ring\n");
Larry Finger0c817332010-12-08 11:12:31 -06001081 return -ENOMEM;
1082 }
1083
1084 memset(rtlpci->rx_ring[rx_queue_idx].desc, 0,
1085 sizeof(*rtlpci->rx_ring[rx_queue_idx].desc) *
1086 rtlpci->rxringcount);
1087
1088 rtlpci->rx_ring[rx_queue_idx].idx = 0;
1089
Larry Finger0019a2c2011-05-19 11:48:45 -05001090 /* If amsdu_8k is disabled, set buffersize to 4096. This
1091 * change will reduce memory fragmentation.
1092 */
1093 if (rtlpci->rxbuffersize > 4096 &&
1094 rtlpriv->rtlhal.disable_amsdu_8k)
1095 rtlpci->rxbuffersize = 4096;
1096
Larry Finger0c817332010-12-08 11:12:31 -06001097 for (i = 0; i < rtlpci->rxringcount; i++) {
1098 struct sk_buff *skb =
1099 dev_alloc_skb(rtlpci->rxbuffersize);
1100 u32 bufferaddress;
Larry Finger0c817332010-12-08 11:12:31 -06001101 if (!skb)
1102 return 0;
Larry Fingerf11bbfd2012-04-13 13:57:43 -05001103 kmemleak_not_leak(skb);
Jesper Juhlbdc4bf652011-01-21 13:40:54 -06001104 entry = &rtlpci->rx_ring[rx_queue_idx].desc[i];
Larry Finger0c817332010-12-08 11:12:31 -06001105
1106 /*skb->dev = dev; */
1107
1108 rtlpci->rx_ring[rx_queue_idx].rx_buf[i] = skb;
1109
1110 /*
1111 *just set skb->cb to mapping addr
1112 *for pci_unmap_single use
1113 */
1114 *((dma_addr_t *) skb->cb) =
1115 pci_map_single(rtlpci->pdev, skb_tail_pointer(skb),
1116 rtlpci->rxbuffersize,
1117 PCI_DMA_FROMDEVICE);
1118
Larry Fingerd3bb1422011-04-25 13:23:20 -05001119 bufferaddress = (*((dma_addr_t *)skb->cb));
Larry Finger86bfec82012-12-27 10:37:28 -06001120 if (pci_dma_mapping_error(rtlpci->pdev, bufferaddress)) {
1121 dev_kfree_skb_any(skb);
1122 return 1;
1123 }
Larry Finger0c817332010-12-08 11:12:31 -06001124 rtlpriv->cfg->ops->set_desc((u8 *)entry, false,
1125 HW_DESC_RXBUFF_ADDR,
1126 (u8 *)&bufferaddress);
1127 rtlpriv->cfg->ops->set_desc((u8 *)entry, false,
1128 HW_DESC_RXPKT_LEN,
1129 (u8 *)&rtlpci->
1130 rxbuffersize);
1131 rtlpriv->cfg->ops->set_desc((u8 *) entry, false,
1132 HW_DESC_RXOWN,
Joe Perches2c208892012-06-04 12:44:17 +00001133 &tmp_one);
Larry Finger0c817332010-12-08 11:12:31 -06001134 }
1135
1136 rtlpriv->cfg->ops->set_desc((u8 *) entry, false,
Joe Perches2c208892012-06-04 12:44:17 +00001137 HW_DESC_RXERO, &tmp_one);
Larry Finger0c817332010-12-08 11:12:31 -06001138 }
1139 return 0;
1140}
1141
1142static void _rtl_pci_free_tx_ring(struct ieee80211_hw *hw,
1143 unsigned int prio)
1144{
1145 struct rtl_priv *rtlpriv = rtl_priv(hw);
1146 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1147 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[prio];
1148
1149 while (skb_queue_len(&ring->queue)) {
1150 struct rtl_tx_desc *entry = &ring->desc[ring->idx];
1151 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1152
1153 pci_unmap_single(rtlpci->pdev,
Larry Fingerd3bb1422011-04-25 13:23:20 -05001154 rtlpriv->cfg->
Larry Finger0c817332010-12-08 11:12:31 -06001155 ops->get_desc((u8 *) entry, true,
Larry Fingerd3bb1422011-04-25 13:23:20 -05001156 HW_DESC_TXBUFF_ADDR),
Larry Finger0c817332010-12-08 11:12:31 -06001157 skb->len, PCI_DMA_TODEVICE);
1158 kfree_skb(skb);
1159 ring->idx = (ring->idx + 1) % ring->entries;
1160 }
1161
Simon Graham7f66c2f2012-02-07 18:07:38 -06001162 if (ring->desc) {
1163 pci_free_consistent(rtlpci->pdev,
1164 sizeof(*ring->desc) * ring->entries,
1165 ring->desc, ring->dma);
1166 ring->desc = NULL;
1167 }
Larry Finger0c817332010-12-08 11:12:31 -06001168}
1169
1170static void _rtl_pci_free_rx_ring(struct rtl_pci *rtlpci)
1171{
1172 int i, rx_queue_idx;
1173
1174 /*rx_queue_idx 0:RX_MPDU_QUEUE */
1175 /*rx_queue_idx 1:RX_CMD_QUEUE */
1176 for (rx_queue_idx = 0; rx_queue_idx < RTL_PCI_MAX_RX_QUEUE;
1177 rx_queue_idx++) {
1178 for (i = 0; i < rtlpci->rxringcount; i++) {
1179 struct sk_buff *skb =
1180 rtlpci->rx_ring[rx_queue_idx].rx_buf[i];
1181 if (!skb)
1182 continue;
1183
1184 pci_unmap_single(rtlpci->pdev,
1185 *((dma_addr_t *) skb->cb),
1186 rtlpci->rxbuffersize,
1187 PCI_DMA_FROMDEVICE);
1188 kfree_skb(skb);
1189 }
1190
Simon Graham7f66c2f2012-02-07 18:07:38 -06001191 if (rtlpci->rx_ring[rx_queue_idx].desc) {
1192 pci_free_consistent(rtlpci->pdev,
Larry Finger0c817332010-12-08 11:12:31 -06001193 sizeof(*rtlpci->rx_ring[rx_queue_idx].
1194 desc) * rtlpci->rxringcount,
1195 rtlpci->rx_ring[rx_queue_idx].desc,
1196 rtlpci->rx_ring[rx_queue_idx].dma);
Simon Graham7f66c2f2012-02-07 18:07:38 -06001197 rtlpci->rx_ring[rx_queue_idx].desc = NULL;
1198 }
Larry Finger0c817332010-12-08 11:12:31 -06001199 }
1200}
1201
1202static int _rtl_pci_init_trx_ring(struct ieee80211_hw *hw)
1203{
1204 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1205 int ret;
1206 int i;
1207
1208 ret = _rtl_pci_init_rx_ring(hw);
1209 if (ret)
1210 return ret;
1211
1212 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
1213 ret = _rtl_pci_init_tx_ring(hw, i,
1214 rtlpci->txringcount[i]);
1215 if (ret)
1216 goto err_free_rings;
1217 }
1218
1219 return 0;
1220
1221err_free_rings:
1222 _rtl_pci_free_rx_ring(rtlpci);
1223
1224 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
1225 if (rtlpci->tx_ring[i].desc)
1226 _rtl_pci_free_tx_ring(hw, i);
1227
1228 return 1;
1229}
1230
1231static int _rtl_pci_deinit_trx_ring(struct ieee80211_hw *hw)
1232{
1233 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1234 u32 i;
1235
1236 /*free rx rings */
1237 _rtl_pci_free_rx_ring(rtlpci);
1238
1239 /*free tx rings */
1240 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
1241 _rtl_pci_free_tx_ring(hw, i);
1242
1243 return 0;
1244}
1245
1246int rtl_pci_reset_trx_ring(struct ieee80211_hw *hw)
1247{
1248 struct rtl_priv *rtlpriv = rtl_priv(hw);
1249 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1250 int i, rx_queue_idx;
1251 unsigned long flags;
1252 u8 tmp_one = 1;
1253
1254 /*rx_queue_idx 0:RX_MPDU_QUEUE */
1255 /*rx_queue_idx 1:RX_CMD_QUEUE */
1256 for (rx_queue_idx = 0; rx_queue_idx < RTL_PCI_MAX_RX_QUEUE;
1257 rx_queue_idx++) {
1258 /*
1259 *force the rx_ring[RX_MPDU_QUEUE/
1260 *RX_CMD_QUEUE].idx to the first one
1261 */
1262 if (rtlpci->rx_ring[rx_queue_idx].desc) {
1263 struct rtl_rx_desc *entry = NULL;
1264
1265 for (i = 0; i < rtlpci->rxringcount; i++) {
1266 entry = &rtlpci->rx_ring[rx_queue_idx].desc[i];
1267 rtlpriv->cfg->ops->set_desc((u8 *) entry,
1268 false,
1269 HW_DESC_RXOWN,
Joe Perches2c208892012-06-04 12:44:17 +00001270 &tmp_one);
Larry Finger0c817332010-12-08 11:12:31 -06001271 }
1272 rtlpci->rx_ring[rx_queue_idx].idx = 0;
1273 }
1274 }
1275
1276 /*
1277 *after reset, release previous pending packet,
1278 *and force the tx idx to the first one
1279 */
Larry Finger0c817332010-12-08 11:12:31 -06001280 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
1281 if (rtlpci->tx_ring[i].desc) {
1282 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[i];
1283
1284 while (skb_queue_len(&ring->queue)) {
Larry Finger5a2766a2012-06-24 11:06:29 -05001285 struct rtl_tx_desc *entry;
1286 struct sk_buff *skb;
Larry Finger0c817332010-12-08 11:12:31 -06001287
Larry Finger5a2766a2012-06-24 11:06:29 -05001288 spin_lock_irqsave(&rtlpriv->locks.irq_th_lock,
1289 flags);
1290 entry = &ring->desc[ring->idx];
1291 skb = __skb_dequeue(&ring->queue);
Larry Finger0c817332010-12-08 11:12:31 -06001292 pci_unmap_single(rtlpci->pdev,
Larry Fingerd3bb1422011-04-25 13:23:20 -05001293 rtlpriv->cfg->ops->
Larry Finger0c817332010-12-08 11:12:31 -06001294 get_desc((u8 *)
1295 entry,
1296 true,
Larry Fingerd3bb1422011-04-25 13:23:20 -05001297 HW_DESC_TXBUFF_ADDR),
Larry Finger0c817332010-12-08 11:12:31 -06001298 skb->len, PCI_DMA_TODEVICE);
Larry Finger0c817332010-12-08 11:12:31 -06001299 ring->idx = (ring->idx + 1) % ring->entries;
Larry Finger5a2766a2012-06-24 11:06:29 -05001300 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock,
1301 flags);
1302 kfree_skb(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001303 }
1304 ring->idx = 0;
1305 }
1306 }
1307
Larry Finger0c817332010-12-08 11:12:31 -06001308 return 0;
1309}
1310
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001311static bool rtl_pci_tx_chk_waitq_insert(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02001312 struct ieee80211_sta *sta,
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001313 struct sk_buff *skb)
Larry Finger0c817332010-12-08 11:12:31 -06001314{
1315 struct rtl_priv *rtlpriv = rtl_priv(hw);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001316 struct rtl_sta_info *sta_entry = NULL;
1317 u8 tid = rtl_get_tid(skb);
Larry Finger0f015452012-10-25 13:46:46 -05001318 __le16 fc = rtl_get_fc(skb);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001319
1320 if (!sta)
1321 return false;
1322 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1323
1324 if (!rtlpriv->rtlhal.earlymode_enable)
1325 return false;
Larry Finger0f015452012-10-25 13:46:46 -05001326 if (ieee80211_is_nullfunc(fc))
1327 return false;
1328 if (ieee80211_is_qos_nullfunc(fc))
1329 return false;
1330 if (ieee80211_is_pspoll(fc))
1331 return false;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001332 if (sta_entry->tids[tid].agg.agg_state != RTL_AGG_OPERATIONAL)
1333 return false;
1334 if (_rtl_mac_to_hwqueue(hw, skb) > VO_QUEUE)
1335 return false;
1336 if (tid > 7)
1337 return false;
1338
1339 /* maybe every tid should be checked */
1340 if (!rtlpriv->link_info.higher_busytxtraffic[tid])
1341 return false;
1342
1343 spin_lock_bh(&rtlpriv->locks.waitq_lock);
1344 skb_queue_tail(&rtlpriv->mac80211.skb_waitq[tid], skb);
1345 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
1346
1347 return true;
1348}
1349
Thomas Huehn36323f82012-07-23 21:33:42 +02001350static int rtl_pci_tx(struct ieee80211_hw *hw,
1351 struct ieee80211_sta *sta,
1352 struct sk_buff *skb,
1353 struct rtl_tcb_desc *ptcb_desc)
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001354{
1355 struct rtl_priv *rtlpriv = rtl_priv(hw);
1356 struct rtl_sta_info *sta_entry = NULL;
1357 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001358 struct rtl8192_tx_ring *ring;
1359 struct rtl_tx_desc *pdesc;
1360 u8 idx;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001361 u8 hw_queue = _rtl_mac_to_hwqueue(hw, skb);
Larry Finger0c817332010-12-08 11:12:31 -06001362 unsigned long flags;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001363 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1364 __le16 fc = rtl_get_fc(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001365 u8 *pda_addr = hdr->addr1;
1366 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1367 /*ssn */
Larry Finger0c817332010-12-08 11:12:31 -06001368 u8 tid = 0;
1369 u16 seq_number = 0;
1370 u8 own;
1371 u8 temp_one = 1;
1372
Larry Finger0f015452012-10-25 13:46:46 -05001373 if (ieee80211_is_mgmt(fc))
1374 rtl_tx_mgmt_proc(hw, skb);
Larry Finger0c817332010-12-08 11:12:31 -06001375
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001376 if (rtlpriv->psc.sw_ps_enabled) {
1377 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
1378 !ieee80211_has_pm(fc))
1379 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1380 }
1381
1382 rtl_action_proc(hw, skb, true);
Larry Finger0c817332010-12-08 11:12:31 -06001383
1384 if (is_multicast_ether_addr(pda_addr))
1385 rtlpriv->stats.txbytesmulticast += skb->len;
1386 else if (is_broadcast_ether_addr(pda_addr))
1387 rtlpriv->stats.txbytesbroadcast += skb->len;
1388 else
1389 rtlpriv->stats.txbytesunicast += skb->len;
1390
1391 spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
Larry Finger0c817332010-12-08 11:12:31 -06001392 ring = &rtlpci->tx_ring[hw_queue];
1393 if (hw_queue != BEACON_QUEUE)
1394 idx = (ring->idx + skb_queue_len(&ring->queue)) %
1395 ring->entries;
1396 else
1397 idx = 0;
1398
1399 pdesc = &ring->desc[idx];
1400 own = (u8) rtlpriv->cfg->ops->get_desc((u8 *) pdesc,
1401 true, HW_DESC_OWN);
1402
1403 if ((own == 1) && (hw_queue != BEACON_QUEUE)) {
1404 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Joe Perchesf30d7502012-01-04 19:40:41 -08001405 "No more TX desc@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%d\n",
1406 hw_queue, ring->idx, idx,
1407 skb_queue_len(&ring->queue));
Larry Finger0c817332010-12-08 11:12:31 -06001408
1409 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1410 return skb->len;
1411 }
1412
Larry Finger0c817332010-12-08 11:12:31 -06001413 if (ieee80211_is_data_qos(fc)) {
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001414 tid = rtl_get_tid(skb);
1415 if (sta) {
1416 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1417 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
1418 IEEE80211_SCTL_SEQ) >> 4;
1419 seq_number += 1;
Larry Finger0c817332010-12-08 11:12:31 -06001420
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001421 if (!ieee80211_has_morefrags(hdr->frame_control))
1422 sta_entry->tids[tid].seq_number = seq_number;
1423 }
Larry Finger0c817332010-12-08 11:12:31 -06001424 }
1425
1426 if (ieee80211_is_data(fc))
1427 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
1428
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001429 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +02001430 info, sta, skb, hw_queue, ptcb_desc);
Larry Finger0c817332010-12-08 11:12:31 -06001431
1432 __skb_queue_tail(&ring->queue, skb);
1433
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001434 rtlpriv->cfg->ops->set_desc((u8 *)pdesc, true,
Joe Perches2c208892012-06-04 12:44:17 +00001435 HW_DESC_OWN, &temp_one);
Larry Finger0c817332010-12-08 11:12:31 -06001436
Larry Finger0c817332010-12-08 11:12:31 -06001437
1438 if ((ring->entries - skb_queue_len(&ring->queue)) < 2 &&
1439 hw_queue != BEACON_QUEUE) {
1440
1441 RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -08001442 "less desc left, stop skb_queue@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%d\n",
1443 hw_queue, ring->idx, idx,
1444 skb_queue_len(&ring->queue));
Larry Finger0c817332010-12-08 11:12:31 -06001445
1446 ieee80211_stop_queue(hw, skb_get_queue_mapping(skb));
1447 }
1448
1449 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1450
1451 rtlpriv->cfg->ops->tx_polling(hw, hw_queue);
1452
1453 return 0;
1454}
1455
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001456static void rtl_pci_flush(struct ieee80211_hw *hw, bool drop)
1457{
1458 struct rtl_priv *rtlpriv = rtl_priv(hw);
1459 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1460 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1461 u16 i = 0;
1462 int queue_id;
1463 struct rtl8192_tx_ring *ring;
1464
1465 for (queue_id = RTL_PCI_MAX_TX_QUEUE_COUNT - 1; queue_id >= 0;) {
1466 u32 queue_len;
1467 ring = &pcipriv->dev.tx_ring[queue_id];
1468 queue_len = skb_queue_len(&ring->queue);
1469 if (queue_len == 0 || queue_id == BEACON_QUEUE ||
1470 queue_id == TXCMD_QUEUE) {
1471 queue_id--;
1472 continue;
1473 } else {
1474 msleep(20);
1475 i++;
1476 }
1477
1478 /* we just wait 1s for all queues */
1479 if (rtlpriv->psc.rfpwr_state == ERFOFF ||
1480 is_hal_stop(rtlhal) || i >= 200)
1481 return;
1482 }
1483}
1484
Larry Fingerd3bb1422011-04-25 13:23:20 -05001485static void rtl_pci_deinit(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -06001486{
1487 struct rtl_priv *rtlpriv = rtl_priv(hw);
1488 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1489
1490 _rtl_pci_deinit_trx_ring(hw);
1491
1492 synchronize_irq(rtlpci->pdev->irq);
1493 tasklet_kill(&rtlpriv->works.irq_tasklet);
Stanislaw Gruszka41affd52011-12-12 12:43:23 +01001494 cancel_work_sync(&rtlpriv->works.lps_leave_work);
Larry Finger0c817332010-12-08 11:12:31 -06001495
1496 flush_workqueue(rtlpriv->works.rtl_wq);
1497 destroy_workqueue(rtlpriv->works.rtl_wq);
1498
1499}
1500
Larry Fingerd3bb1422011-04-25 13:23:20 -05001501static int rtl_pci_init(struct ieee80211_hw *hw, struct pci_dev *pdev)
Larry Finger0c817332010-12-08 11:12:31 -06001502{
1503 struct rtl_priv *rtlpriv = rtl_priv(hw);
1504 int err;
1505
1506 _rtl_pci_init_struct(hw, pdev);
1507
1508 err = _rtl_pci_init_trx_ring(hw);
1509 if (err) {
1510 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001511 "tx ring initialization failed\n");
John W. Linville12325282012-02-09 14:48:25 -05001512 return err;
Larry Finger0c817332010-12-08 11:12:31 -06001513 }
1514
John W. Linville12325282012-02-09 14:48:25 -05001515 return 0;
Larry Finger0c817332010-12-08 11:12:31 -06001516}
1517
Larry Fingerd3bb1422011-04-25 13:23:20 -05001518static int rtl_pci_start(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -06001519{
1520 struct rtl_priv *rtlpriv = rtl_priv(hw);
1521 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1522 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1523 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1524
1525 int err;
1526
1527 rtl_pci_reset_trx_ring(hw);
1528
1529 rtlpci->driver_is_goingto_unload = false;
1530 err = rtlpriv->cfg->ops->hw_init(hw);
1531 if (err) {
1532 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001533 "Failed to config hardware!\n");
Larry Finger0c817332010-12-08 11:12:31 -06001534 return err;
1535 }
1536
1537 rtlpriv->cfg->ops->enable_interrupt(hw);
Joe Perchesf30d7502012-01-04 19:40:41 -08001538 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "enable_interrupt OK\n");
Larry Finger0c817332010-12-08 11:12:31 -06001539
1540 rtl_init_rx_config(hw);
1541
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03001542 /*should be after adapter start and interrupt enable. */
Larry Finger0c817332010-12-08 11:12:31 -06001543 set_hal_start(rtlhal);
1544
1545 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
1546
1547 rtlpci->up_first_time = false;
1548
Joe Perchesf30d7502012-01-04 19:40:41 -08001549 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "OK\n");
Larry Finger0c817332010-12-08 11:12:31 -06001550 return 0;
1551}
1552
Larry Fingerd3bb1422011-04-25 13:23:20 -05001553static void rtl_pci_stop(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -06001554{
1555 struct rtl_priv *rtlpriv = rtl_priv(hw);
1556 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1557 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1558 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1559 unsigned long flags;
1560 u8 RFInProgressTimeOut = 0;
1561
1562 /*
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03001563 *should be before disable interrupt&adapter
Larry Finger0c817332010-12-08 11:12:31 -06001564 *and will do it immediately.
1565 */
1566 set_hal_stop(rtlhal);
1567
1568 rtlpriv->cfg->ops->disable_interrupt(hw);
Stanislaw Gruszka41affd52011-12-12 12:43:23 +01001569 cancel_work_sync(&rtlpriv->works.lps_leave_work);
Larry Finger0c817332010-12-08 11:12:31 -06001570
1571 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1572 while (ppsc->rfchange_inprogress) {
1573 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1574 if (RFInProgressTimeOut > 100) {
1575 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1576 break;
1577 }
1578 mdelay(1);
1579 RFInProgressTimeOut++;
1580 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1581 }
1582 ppsc->rfchange_inprogress = true;
1583 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1584
1585 rtlpci->driver_is_goingto_unload = true;
1586 rtlpriv->cfg->ops->hw_disable(hw);
Larry Fingerb0302ab2012-01-30 09:54:49 -06001587 /* some things are not needed if firmware not available */
1588 if (!rtlpriv->max_fw_size)
1589 return;
Larry Finger0c817332010-12-08 11:12:31 -06001590 rtlpriv->cfg->ops->led_control(hw, LED_CTL_POWER_OFF);
1591
1592 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1593 ppsc->rfchange_inprogress = false;
1594 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1595
1596 rtl_pci_enable_aspm(hw);
1597}
1598
1599static bool _rtl_pci_find_adapter(struct pci_dev *pdev,
1600 struct ieee80211_hw *hw)
1601{
1602 struct rtl_priv *rtlpriv = rtl_priv(hw);
1603 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1604 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1605 struct pci_dev *bridge_pdev = pdev->bus->self;
1606 u16 venderid;
1607 u16 deviceid;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001608 u8 revisionid;
Larry Finger0c817332010-12-08 11:12:31 -06001609 u16 irqline;
1610 u8 tmp;
1611
Chaoming Lifc7707a2011-05-06 15:32:02 -05001612 pcipriv->ndis_adapter.pcibridge_vendor = PCI_BRIDGE_VENDOR_UNKNOWN;
Larry Finger0c817332010-12-08 11:12:31 -06001613 venderid = pdev->vendor;
1614 deviceid = pdev->device;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001615 pci_read_config_byte(pdev, 0x8, &revisionid);
Larry Finger0c817332010-12-08 11:12:31 -06001616 pci_read_config_word(pdev, 0x3C, &irqline);
1617
Larry Fingerfa7ccfb2011-06-18 22:49:53 -05001618 /* PCI ID 0x10ec:0x8192 occurs for both RTL8192E, which uses
1619 * r8192e_pci, and RTL8192SE, which uses this driver. If the
1620 * revision ID is RTL_PCI_REVISION_ID_8192PCIE (0x01), then
1621 * the correct driver is r8192e_pci, thus this routine should
1622 * return false.
1623 */
1624 if (deviceid == RTL_PCI_8192SE_DID &&
1625 revisionid == RTL_PCI_REVISION_ID_8192PCIE)
1626 return false;
1627
Larry Finger0c817332010-12-08 11:12:31 -06001628 if (deviceid == RTL_PCI_8192_DID ||
1629 deviceid == RTL_PCI_0044_DID ||
1630 deviceid == RTL_PCI_0047_DID ||
1631 deviceid == RTL_PCI_8192SE_DID ||
1632 deviceid == RTL_PCI_8174_DID ||
1633 deviceid == RTL_PCI_8173_DID ||
1634 deviceid == RTL_PCI_8172_DID ||
1635 deviceid == RTL_PCI_8171_DID) {
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001636 switch (revisionid) {
Larry Finger0c817332010-12-08 11:12:31 -06001637 case RTL_PCI_REVISION_ID_8192PCIE:
1638 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001639 "8192 PCI-E is found - vid/did=%x/%x\n",
1640 venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001641 rtlhal->hw_type = HARDWARE_TYPE_RTL8192E;
Larry Finger0f015452012-10-25 13:46:46 -05001642 return false;
Larry Finger0c817332010-12-08 11:12:31 -06001643 case RTL_PCI_REVISION_ID_8192SE:
1644 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001645 "8192SE is found - vid/did=%x/%x\n",
1646 venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001647 rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
1648 break;
1649 default:
1650 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Joe Perchesf30d7502012-01-04 19:40:41 -08001651 "Err: Unknown device - vid/did=%x/%x\n",
1652 venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001653 rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
1654 break;
1655
1656 }
Larry Finger0f015452012-10-25 13:46:46 -05001657 } else if (deviceid == RTL_PCI_8723AE_DID) {
1658 rtlhal->hw_type = HARDWARE_TYPE_RTL8723AE;
1659 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
1660 "8723AE PCI-E is found - "
1661 "vid/did=%x/%x\n", venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001662 } else if (deviceid == RTL_PCI_8192CET_DID ||
1663 deviceid == RTL_PCI_8192CE_DID ||
1664 deviceid == RTL_PCI_8191CE_DID ||
1665 deviceid == RTL_PCI_8188CE_DID) {
1666 rtlhal->hw_type = HARDWARE_TYPE_RTL8192CE;
1667 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001668 "8192C PCI-E is found - vid/did=%x/%x\n",
1669 venderid, deviceid);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001670 } else if (deviceid == RTL_PCI_8192DE_DID ||
1671 deviceid == RTL_PCI_8192DE_DID2) {
1672 rtlhal->hw_type = HARDWARE_TYPE_RTL8192DE;
1673 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001674 "8192D PCI-E is found - vid/did=%x/%x\n",
1675 venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001676 } else {
1677 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Joe Perchesf30d7502012-01-04 19:40:41 -08001678 "Err: Unknown device - vid/did=%x/%x\n",
1679 venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001680
1681 rtlhal->hw_type = RTL_DEFAULT_HARDWARE_TYPE;
1682 }
1683
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001684 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192DE) {
1685 if (revisionid == 0 || revisionid == 1) {
1686 if (revisionid == 0) {
Joe Perchesf30d7502012-01-04 19:40:41 -08001687 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1688 "Find 92DE MAC0\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001689 rtlhal->interfaceindex = 0;
1690 } else if (revisionid == 1) {
1691 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -08001692 "Find 92DE MAC1\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001693 rtlhal->interfaceindex = 1;
1694 }
1695 } else {
1696 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -08001697 "Unknown device - VendorID/DeviceID=%x/%x, Revision=%x\n",
1698 venderid, deviceid, revisionid);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001699 rtlhal->interfaceindex = 0;
1700 }
1701 }
Larry Finger0c817332010-12-08 11:12:31 -06001702 /*find bus info */
1703 pcipriv->ndis_adapter.busnumber = pdev->bus->number;
1704 pcipriv->ndis_adapter.devnumber = PCI_SLOT(pdev->devfn);
1705 pcipriv->ndis_adapter.funcnumber = PCI_FUNC(pdev->devfn);
1706
Larry Fingerb6b67df2011-07-29 10:53:12 -05001707 if (bridge_pdev) {
1708 /*find bridge info if available */
1709 pcipriv->ndis_adapter.pcibridge_vendorid = bridge_pdev->vendor;
1710 for (tmp = 0; tmp < PCI_BRIDGE_VENDOR_MAX; tmp++) {
1711 if (bridge_pdev->vendor == pcibridge_vendors[tmp]) {
1712 pcipriv->ndis_adapter.pcibridge_vendor = tmp;
1713 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001714 "Pci Bridge Vendor is found index: %d\n",
1715 tmp);
Larry Fingerb6b67df2011-07-29 10:53:12 -05001716 break;
1717 }
Larry Finger0c817332010-12-08 11:12:31 -06001718 }
1719 }
1720
1721 if (pcipriv->ndis_adapter.pcibridge_vendor !=
1722 PCI_BRIDGE_VENDOR_UNKNOWN) {
1723 pcipriv->ndis_adapter.pcibridge_busnum =
1724 bridge_pdev->bus->number;
1725 pcipriv->ndis_adapter.pcibridge_devnum =
1726 PCI_SLOT(bridge_pdev->devfn);
1727 pcipriv->ndis_adapter.pcibridge_funcnum =
1728 PCI_FUNC(bridge_pdev->devfn);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001729 pcipriv->ndis_adapter.pcibridge_pciehdr_offset =
1730 pci_pcie_cap(bridge_pdev);
Larry Finger0c817332010-12-08 11:12:31 -06001731 pcipriv->ndis_adapter.num4bytes =
1732 (pcipriv->ndis_adapter.pcibridge_pciehdr_offset + 0x10) / 4;
1733
1734 rtl_pci_get_linkcontrol_field(hw);
1735
1736 if (pcipriv->ndis_adapter.pcibridge_vendor ==
1737 PCI_BRIDGE_VENDOR_AMD) {
1738 pcipriv->ndis_adapter.amd_l1_patch =
1739 rtl_pci_get_amd_l1_patch(hw);
1740 }
1741 }
1742
1743 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001744 "pcidev busnumber:devnumber:funcnumber:vendor:link_ctl %d:%d:%d:%x:%x\n",
1745 pcipriv->ndis_adapter.busnumber,
1746 pcipriv->ndis_adapter.devnumber,
1747 pcipriv->ndis_adapter.funcnumber,
1748 pdev->vendor, pcipriv->ndis_adapter.linkctrl_reg);
Larry Finger0c817332010-12-08 11:12:31 -06001749
1750 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001751 "pci_bridge busnumber:devnumber:funcnumber:vendor:pcie_cap:link_ctl_reg:amd %d:%d:%d:%x:%x:%x:%x\n",
1752 pcipriv->ndis_adapter.pcibridge_busnum,
1753 pcipriv->ndis_adapter.pcibridge_devnum,
1754 pcipriv->ndis_adapter.pcibridge_funcnum,
1755 pcibridge_vendors[pcipriv->ndis_adapter.pcibridge_vendor],
1756 pcipriv->ndis_adapter.pcibridge_pciehdr_offset,
1757 pcipriv->ndis_adapter.pcibridge_linkctrlreg,
1758 pcipriv->ndis_adapter.amd_l1_patch);
Larry Finger0c817332010-12-08 11:12:31 -06001759
1760 rtl_pci_parse_configuration(pdev, hw);
1761
1762 return true;
1763}
1764
Bill Pemberton9e2ff362012-12-03 09:56:43 -05001765int rtl_pci_probe(struct pci_dev *pdev,
Larry Finger0c817332010-12-08 11:12:31 -06001766 const struct pci_device_id *id)
1767{
1768 struct ieee80211_hw *hw = NULL;
1769
1770 struct rtl_priv *rtlpriv = NULL;
1771 struct rtl_pci_priv *pcipriv = NULL;
1772 struct rtl_pci *rtlpci;
1773 unsigned long pmem_start, pmem_len, pmem_flags;
1774 int err;
1775
1776 err = pci_enable_device(pdev);
1777 if (err) {
Joe Perches9d833ed2012-01-04 19:40:43 -08001778 RT_ASSERT(false, "%s : Cannot enable new PCI device\n",
1779 pci_name(pdev));
Larry Finger0c817332010-12-08 11:12:31 -06001780 return err;
1781 }
1782
1783 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
1784 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
Joe Perches9d833ed2012-01-04 19:40:43 -08001785 RT_ASSERT(false,
1786 "Unable to obtain 32bit DMA for consistent allocations\n");
Tim Gardner3d86b932012-02-02 13:48:06 -07001787 err = -ENOMEM;
1788 goto fail1;
Larry Finger0c817332010-12-08 11:12:31 -06001789 }
1790 }
1791
1792 pci_set_master(pdev);
1793
1794 hw = ieee80211_alloc_hw(sizeof(struct rtl_pci_priv) +
1795 sizeof(struct rtl_priv), &rtl_ops);
1796 if (!hw) {
1797 RT_ASSERT(false,
Joe Perches9d833ed2012-01-04 19:40:43 -08001798 "%s : ieee80211 alloc failed\n", pci_name(pdev));
Larry Finger0c817332010-12-08 11:12:31 -06001799 err = -ENOMEM;
1800 goto fail1;
1801 }
1802
1803 SET_IEEE80211_DEV(hw, &pdev->dev);
1804 pci_set_drvdata(pdev, hw);
1805
1806 rtlpriv = hw->priv;
1807 pcipriv = (void *)rtlpriv->priv;
1808 pcipriv->dev.pdev = pdev;
Larry Fingerb0302ab2012-01-30 09:54:49 -06001809 init_completion(&rtlpriv->firmware_loading_complete);
Larry Finger0c817332010-12-08 11:12:31 -06001810
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001811 /* init cfg & intf_ops */
1812 rtlpriv->rtlhal.interface = INTF_PCI;
1813 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_data);
1814 rtlpriv->intf_ops = &rtl_pci_ops;
1815
Larry Finger0c817332010-12-08 11:12:31 -06001816 /*
1817 *init dbgp flags before all
1818 *other functions, because we will
1819 *use it in other funtions like
1820 *RT_TRACE/RT_PRINT/RTL_PRINT_DATA
1821 *you can not use these macro
1822 *before this
1823 */
1824 rtl_dbgp_flag_init(hw);
1825
1826 /* MEM map */
1827 err = pci_request_regions(pdev, KBUILD_MODNAME);
1828 if (err) {
Joe Perches9d833ed2012-01-04 19:40:43 -08001829 RT_ASSERT(false, "Can't obtain PCI resources\n");
Tim Gardner3d86b932012-02-02 13:48:06 -07001830 goto fail1;
Larry Finger0c817332010-12-08 11:12:31 -06001831 }
1832
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001833 pmem_start = pci_resource_start(pdev, rtlpriv->cfg->bar_id);
1834 pmem_len = pci_resource_len(pdev, rtlpriv->cfg->bar_id);
1835 pmem_flags = pci_resource_flags(pdev, rtlpriv->cfg->bar_id);
Larry Finger0c817332010-12-08 11:12:31 -06001836
1837 /*shared mem start */
1838 rtlpriv->io.pci_mem_start =
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001839 (unsigned long)pci_iomap(pdev,
1840 rtlpriv->cfg->bar_id, pmem_len);
Larry Finger0c817332010-12-08 11:12:31 -06001841 if (rtlpriv->io.pci_mem_start == 0) {
Joe Perches9d833ed2012-01-04 19:40:43 -08001842 RT_ASSERT(false, "Can't map PCI mem\n");
Tim Gardner3d86b932012-02-02 13:48:06 -07001843 err = -ENOMEM;
Larry Finger0c817332010-12-08 11:12:31 -06001844 goto fail2;
1845 }
1846
1847 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001848 "mem mapped space: start: 0x%08lx len:%08lx flags:%08lx, after map:0x%08lx\n",
1849 pmem_start, pmem_len, pmem_flags,
1850 rtlpriv->io.pci_mem_start);
Larry Finger0c817332010-12-08 11:12:31 -06001851
1852 /* Disable Clk Request */
1853 pci_write_config_byte(pdev, 0x81, 0);
1854 /* leave D3 mode */
1855 pci_write_config_byte(pdev, 0x44, 0);
1856 pci_write_config_byte(pdev, 0x04, 0x06);
1857 pci_write_config_byte(pdev, 0x04, 0x07);
1858
Larry Finger0c817332010-12-08 11:12:31 -06001859 /* find adapter */
Tim Gardner3d86b932012-02-02 13:48:06 -07001860 if (!_rtl_pci_find_adapter(pdev, hw)) {
1861 err = -ENODEV;
Larry Fingerfa7ccfb2011-06-18 22:49:53 -05001862 goto fail3;
Tim Gardner3d86b932012-02-02 13:48:06 -07001863 }
Larry Finger0c817332010-12-08 11:12:31 -06001864
1865 /* Init IO handler */
1866 _rtl_pci_io_handler_init(&pdev->dev, hw);
1867
1868 /*like read eeprom and so on */
1869 rtlpriv->cfg->ops->read_eeprom_info(hw);
1870
Larry Finger0c817332010-12-08 11:12:31 -06001871 /*aspm */
1872 rtl_pci_init_aspm(hw);
1873
1874 /* Init mac80211 sw */
1875 err = rtl_init_core(hw);
1876 if (err) {
1877 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001878 "Can't allocate sw for mac80211\n");
Larry Finger0c817332010-12-08 11:12:31 -06001879 goto fail3;
1880 }
1881
1882 /* Init PCI sw */
John W. Linville12325282012-02-09 14:48:25 -05001883 err = rtl_pci_init(hw, pdev);
Larry Finger0c817332010-12-08 11:12:31 -06001884 if (err) {
Joe Perchesf30d7502012-01-04 19:40:41 -08001885 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Failed to init PCI\n");
Larry Finger0c817332010-12-08 11:12:31 -06001886 goto fail3;
1887 }
1888
Larry Finger574e02a2012-05-04 08:27:43 -05001889 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
1890 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
1891 err = -ENODEV;
1892 goto fail3;
1893 }
1894
1895 rtlpriv->cfg->ops->init_sw_leds(hw);
1896
Larry Finger0c817332010-12-08 11:12:31 -06001897 err = sysfs_create_group(&pdev->dev.kobj, &rtl_attribute_group);
1898 if (err) {
1899 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001900 "failed to create sysfs device attributes\n");
Larry Finger0c817332010-12-08 11:12:31 -06001901 goto fail3;
1902 }
1903
Larry Finger0c817332010-12-08 11:12:31 -06001904 rtlpci = rtl_pcidev(pcipriv);
1905 err = request_irq(rtlpci->pdev->irq, &_rtl_pci_interrupt,
1906 IRQF_SHARED, KBUILD_MODNAME, hw);
1907 if (err) {
1908 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001909 "%s: failed to register IRQ handler\n",
1910 wiphy_name(hw->wiphy));
Larry Finger0c817332010-12-08 11:12:31 -06001911 goto fail3;
Larry Finger0c817332010-12-08 11:12:31 -06001912 }
Larry Fingerb0302ab2012-01-30 09:54:49 -06001913 rtlpci->irq_alloc = 1;
Larry Finger0c817332010-12-08 11:12:31 -06001914
Larry Finger0c817332010-12-08 11:12:31 -06001915 return 0;
1916
1917fail3:
Larry Finger0c817332010-12-08 11:12:31 -06001918 rtl_deinit_core(hw);
1919 _rtl_pci_io_handler_release(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001920
1921 if (rtlpriv->io.pci_mem_start != 0)
Larry Finger62e63972011-02-11 14:27:46 -06001922 pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
Larry Finger0c817332010-12-08 11:12:31 -06001923
1924fail2:
1925 pci_release_regions(pdev);
Larry Fingerb0302ab2012-01-30 09:54:49 -06001926 complete(&rtlpriv->firmware_loading_complete);
Larry Finger0c817332010-12-08 11:12:31 -06001927
1928fail1:
Tim Gardner3d86b932012-02-02 13:48:06 -07001929 if (hw)
1930 ieee80211_free_hw(hw);
1931 pci_set_drvdata(pdev, NULL);
Larry Finger0c817332010-12-08 11:12:31 -06001932 pci_disable_device(pdev);
1933
Tim Gardner3d86b932012-02-02 13:48:06 -07001934 return err;
Larry Finger0c817332010-12-08 11:12:31 -06001935
1936}
1937EXPORT_SYMBOL(rtl_pci_probe);
1938
1939void rtl_pci_disconnect(struct pci_dev *pdev)
1940{
1941 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1942 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1943 struct rtl_priv *rtlpriv = rtl_priv(hw);
1944 struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
1945 struct rtl_mac *rtlmac = rtl_mac(rtlpriv);
1946
Larry Fingerb0302ab2012-01-30 09:54:49 -06001947 /* just in case driver is removed before firmware callback */
1948 wait_for_completion(&rtlpriv->firmware_loading_complete);
Larry Finger0c817332010-12-08 11:12:31 -06001949 clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1950
1951 sysfs_remove_group(&pdev->dev.kobj, &rtl_attribute_group);
1952
1953 /*ieee80211_unregister_hw will call ops_stop */
1954 if (rtlmac->mac80211_registered == 1) {
1955 ieee80211_unregister_hw(hw);
1956 rtlmac->mac80211_registered = 0;
1957 } else {
1958 rtl_deinit_deferred_work(hw);
1959 rtlpriv->intf_ops->adapter_stop(hw);
1960 }
Larry Finger44eb65cf2012-04-19 21:39:06 -05001961 rtlpriv->cfg->ops->disable_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001962
1963 /*deinit rfkill */
1964 rtl_deinit_rfkill(hw);
1965
1966 rtl_pci_deinit(hw);
1967 rtl_deinit_core(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001968 _rtl_pci_io_handler_release(hw);
1969 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1970
1971 if (rtlpci->irq_alloc) {
1972 free_irq(rtlpci->pdev->irq, hw);
1973 rtlpci->irq_alloc = 0;
1974 }
1975
1976 if (rtlpriv->io.pci_mem_start != 0) {
Larry Finger62e63972011-02-11 14:27:46 -06001977 pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
Larry Finger0c817332010-12-08 11:12:31 -06001978 pci_release_regions(pdev);
1979 }
1980
1981 pci_disable_device(pdev);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001982
1983 rtl_pci_disable_aspm(hw);
1984
Larry Finger0c817332010-12-08 11:12:31 -06001985 pci_set_drvdata(pdev, NULL);
1986
1987 ieee80211_free_hw(hw);
1988}
1989EXPORT_SYMBOL(rtl_pci_disconnect);
1990
Hauke Mehrtens244a77e2012-11-29 23:27:17 +01001991#ifdef CONFIG_PM_SLEEP
Larry Finger0c817332010-12-08 11:12:31 -06001992/***************************************
1993kernel pci power state define:
1994PCI_D0 ((pci_power_t __force) 0)
1995PCI_D1 ((pci_power_t __force) 1)
1996PCI_D2 ((pci_power_t __force) 2)
1997PCI_D3hot ((pci_power_t __force) 3)
1998PCI_D3cold ((pci_power_t __force) 4)
1999PCI_UNKNOWN ((pci_power_t __force) 5)
2000
2001This function is called when system
2002goes into suspend state mac80211 will
2003call rtl_mac_stop() from the mac80211
2004suspend function first, So there is
2005no need to call hw_disable here.
2006****************************************/
Larry Finger603be382011-10-11 21:28:47 -05002007int rtl_pci_suspend(struct device *dev)
Larry Finger0c817332010-12-08 11:12:31 -06002008{
Larry Finger603be382011-10-11 21:28:47 -05002009 struct pci_dev *pdev = to_pci_dev(dev);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002010 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2011 struct rtl_priv *rtlpriv = rtl_priv(hw);
2012
2013 rtlpriv->cfg->ops->hw_suspend(hw);
2014 rtl_deinit_rfkill(hw);
2015
Larry Finger0c817332010-12-08 11:12:31 -06002016 return 0;
2017}
2018EXPORT_SYMBOL(rtl_pci_suspend);
2019
Larry Finger603be382011-10-11 21:28:47 -05002020int rtl_pci_resume(struct device *dev)
Larry Finger0c817332010-12-08 11:12:31 -06002021{
Larry Finger603be382011-10-11 21:28:47 -05002022 struct pci_dev *pdev = to_pci_dev(dev);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002023 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2024 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002025
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002026 rtlpriv->cfg->ops->hw_resume(hw);
2027 rtl_init_rfkill(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002028 return 0;
2029}
2030EXPORT_SYMBOL(rtl_pci_resume);
Hauke Mehrtens244a77e2012-11-29 23:27:17 +01002031#endif /* CONFIG_PM_SLEEP */
Larry Finger0c817332010-12-08 11:12:31 -06002032
2033struct rtl_intf_ops rtl_pci_ops = {
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002034 .read_efuse_byte = read_efuse_byte,
Larry Finger0c817332010-12-08 11:12:31 -06002035 .adapter_start = rtl_pci_start,
2036 .adapter_stop = rtl_pci_stop,
2037 .adapter_tx = rtl_pci_tx,
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002038 .flush = rtl_pci_flush,
Larry Finger0c817332010-12-08 11:12:31 -06002039 .reset_trx_ring = rtl_pci_reset_trx_ring,
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002040 .waitq_insert = rtl_pci_tx_chk_waitq_insert,
Larry Finger0c817332010-12-08 11:12:31 -06002041
2042 .disable_aspm = rtl_pci_disable_aspm,
2043 .enable_aspm = rtl_pci_enable_aspm,
2044};