blob: b9a6d23364be40e386b4186c051ccd9ddf1c8a75 [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 *
Larry Finger0c817332010-12-08 11:12:31 -060014 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
16 *
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
21 *
22 * Larry Finger <Larry.Finger@lwfinger.net>
23 *
24 *****************************************************************************/
25
Larry Finger0c817332010-12-08 11:12:31 -060026#include "wifi.h"
Larry Fingerd273bb22012-01-27 13:59:25 -060027#include "core.h"
Larry Finger0c817332010-12-08 11:12:31 -060028#include "pci.h"
29#include "base.h"
30#include "ps.h"
Chaoming_Lic7cfe382011-04-25 13:23:15 -050031#include "efuse.h"
Larry Finger38506ec2014-09-22 09:39:19 -050032#include <linux/interrupt.h>
Larry Fingerd273bb22012-01-27 13:59:25 -060033#include <linux/export.h>
Larry Fingerf11bbfd2012-04-13 13:57:43 -050034#include <linux/kmemleak.h>
Larry Finger6f334c22013-07-12 15:32:15 -050035#include <linux/module.h>
36
37MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
38MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
39MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
40MODULE_LICENSE("GPL");
41MODULE_DESCRIPTION("PCI basic driver for rtlwifi");
Larry Finger0c817332010-12-08 11:12:31 -060042
43static const u16 pcibridge_vendors[PCI_BRIDGE_VENDOR_MAX] = {
Larry Finger38506ec2014-09-22 09:39:19 -050044 INTEL_VENDOR_ID,
45 ATI_VENDOR_ID,
46 AMD_VENDOR_ID,
47 SIS_VENDOR_ID
Larry Finger0c817332010-12-08 11:12:31 -060048};
49
Chaoming_Lic7cfe382011-04-25 13:23:15 -050050static const u8 ac_to_hwq[] = {
51 VO_QUEUE,
52 VI_QUEUE,
53 BE_QUEUE,
54 BK_QUEUE
55};
56
Larry Fingerd3bb1422011-04-25 13:23:20 -050057static u8 _rtl_mac_to_hwqueue(struct ieee80211_hw *hw,
Chaoming_Lic7cfe382011-04-25 13:23:15 -050058 struct sk_buff *skb)
59{
60 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Fingerd3bb1422011-04-25 13:23:20 -050061 __le16 fc = rtl_get_fc(skb);
Chaoming_Lic7cfe382011-04-25 13:23:15 -050062 u8 queue_index = skb_get_queue_mapping(skb);
63
64 if (unlikely(ieee80211_is_beacon(fc)))
65 return BEACON_QUEUE;
Larry Finger26634c42013-03-24 22:06:33 -050066 if (ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc))
Chaoming_Lic7cfe382011-04-25 13:23:15 -050067 return MGNT_QUEUE;
68 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE)
69 if (ieee80211_is_nullfunc(fc))
70 return HIGH_QUEUE;
71
72 return ac_to_hwq[queue_index];
73}
74
Larry Finger0c817332010-12-08 11:12:31 -060075/* Update PCI dependent default settings*/
76static void _rtl_pci_update_default_setting(struct ieee80211_hw *hw)
77{
78 struct rtl_priv *rtlpriv = rtl_priv(hw);
79 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
80 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
81 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
82 u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
Chaoming_Lic7cfe382011-04-25 13:23:15 -050083 u8 init_aspm;
Larry Finger0c817332010-12-08 11:12:31 -060084
85 ppsc->reg_rfps_level = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +000086 ppsc->support_aspm = false;
Larry Finger0c817332010-12-08 11:12:31 -060087
88 /*Update PCI ASPM setting */
89 ppsc->const_amdpci_aspm = rtlpci->const_amdpci_aspm;
90 switch (rtlpci->const_pci_aspm) {
91 case 0:
92 /*No ASPM */
93 break;
94
95 case 1:
96 /*ASPM dynamically enabled/disable. */
97 ppsc->reg_rfps_level |= RT_RF_LPS_LEVEL_ASPM;
98 break;
99
100 case 2:
101 /*ASPM with Clock Req dynamically enabled/disable. */
102 ppsc->reg_rfps_level |= (RT_RF_LPS_LEVEL_ASPM |
103 RT_RF_OFF_LEVL_CLK_REQ);
104 break;
105
106 case 3:
107 /*
108 * Always enable ASPM and Clock Req
109 * from initialization to halt.
110 * */
111 ppsc->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM);
112 ppsc->reg_rfps_level |= (RT_RF_PS_LEVEL_ALWAYS_ASPM |
113 RT_RF_OFF_LEVL_CLK_REQ);
114 break;
115
116 case 4:
117 /*
118 * Always enable ASPM without Clock Req
119 * from initialization to halt.
120 * */
121 ppsc->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM |
122 RT_RF_OFF_LEVL_CLK_REQ);
123 ppsc->reg_rfps_level |= RT_RF_PS_LEVEL_ALWAYS_ASPM;
124 break;
125 }
126
127 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
128
129 /*Update Radio OFF setting */
130 switch (rtlpci->const_hwsw_rfoff_d3) {
131 case 1:
132 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
133 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
134 break;
135
136 case 2:
137 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
138 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
139 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
140 break;
141
142 case 3:
143 ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_PCI_D3;
144 break;
145 }
146
147 /*Set HW definition to determine if it supports ASPM. */
148 switch (rtlpci->const_support_pciaspm) {
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500149 case 0:{
150 /*Not support ASPM. */
151 bool support_aspm = false;
152 ppsc->support_aspm = support_aspm;
153 break;
154 }
155 case 1:{
156 /*Support ASPM. */
157 bool support_aspm = true;
158 bool support_backdoor = true;
159 ppsc->support_aspm = support_aspm;
160
161 /*if (priv->oem_id == RT_CID_TOSHIBA &&
162 !priv->ndis_adapter.amd_l1_patch)
163 support_backdoor = false; */
164
165 ppsc->support_backdoor = support_backdoor;
166
167 break;
168 }
Larry Finger0c817332010-12-08 11:12:31 -0600169 case 2:
170 /*ASPM value set by chipset. */
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500171 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_INTEL) {
172 bool support_aspm = true;
173 ppsc->support_aspm = support_aspm;
174 }
Larry Finger0c817332010-12-08 11:12:31 -0600175 break;
176 default:
Larry Fingerb03d9682016-12-15 12:22:58 -0600177 pr_err("switch case %#x not processed\n",
178 rtlpci->const_support_pciaspm);
Larry Finger0c817332010-12-08 11:12:31 -0600179 break;
180 }
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500181
182 /* toshiba aspm issue, toshiba will set aspm selfly
183 * so we should not set aspm in driver */
184 pci_read_config_byte(rtlpci->pdev, 0x80, &init_aspm);
185 if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8192SE &&
186 init_aspm == 0x43)
187 ppsc->support_aspm = false;
188}
189
Larry Finger0c817332010-12-08 11:12:31 -0600190static bool _rtl_pci_platform_switch_device_pci_aspm(
191 struct ieee80211_hw *hw,
192 u8 value)
193{
194 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500195 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600196
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500197 if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE)
198 value |= 0x40;
199
Larry Finger0c817332010-12-08 11:12:31 -0600200 pci_write_config_byte(rtlpci->pdev, 0x80, value);
201
Larry Finger32473282011-03-27 16:19:57 -0500202 return false;
Larry Finger0c817332010-12-08 11:12:31 -0600203}
204
205/*When we set 0x01 to enable clk request. Set 0x0 to disable clk req.*/
Devendra.Naga1d73c512012-01-31 01:28:15 -0500206static void _rtl_pci_switch_clk_req(struct ieee80211_hw *hw, u8 value)
Larry Finger0c817332010-12-08 11:12:31 -0600207{
208 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500209 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600210
Larry Finger0c817332010-12-08 11:12:31 -0600211 pci_write_config_byte(rtlpci->pdev, 0x81, value);
Larry Finger0c817332010-12-08 11:12:31 -0600212
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500213 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE)
214 udelay(100);
Larry Finger0c817332010-12-08 11:12:31 -0600215}
216
217/*Disable RTL8192SE ASPM & Disable Pci Bridge ASPM*/
218static void rtl_pci_disable_aspm(struct ieee80211_hw *hw)
219{
220 struct rtl_priv *rtlpriv = rtl_priv(hw);
221 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
222 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
223 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
224 u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
Larry Finger0c817332010-12-08 11:12:31 -0600225 u8 num4bytes = pcipriv->ndis_adapter.num4bytes;
226 /*Retrieve original configuration settings. */
227 u8 linkctrl_reg = pcipriv->ndis_adapter.linkctrl_reg;
228 u16 pcibridge_linkctrlreg = pcipriv->ndis_adapter.
229 pcibridge_linkctrlreg;
230 u16 aspmlevel = 0;
Larry Finger32473282011-03-27 16:19:57 -0500231 u8 tmp_u1b = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600232
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500233 if (!ppsc->support_aspm)
234 return;
235
Larry Finger0c817332010-12-08 11:12:31 -0600236 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_UNKNOWN) {
237 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800238 "PCI(Bridge) UNKNOWN\n");
Larry Finger0c817332010-12-08 11:12:31 -0600239
240 return;
241 }
242
243 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) {
244 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_CLK_REQ);
245 _rtl_pci_switch_clk_req(hw, 0x0);
246 }
247
Larry Finger32473282011-03-27 16:19:57 -0500248 /*for promising device will in L0 state after an I/O. */
249 pci_read_config_byte(rtlpci->pdev, 0x80, &tmp_u1b);
Larry Finger0c817332010-12-08 11:12:31 -0600250
251 /*Set corresponding value. */
252 aspmlevel |= BIT(0) | BIT(1);
253 linkctrl_reg &= ~aspmlevel;
254 pcibridge_linkctrlreg &= ~(BIT(0) | BIT(1));
255
256 _rtl_pci_platform_switch_device_pci_aspm(hw, linkctrl_reg);
257 udelay(50);
258
259 /*4 Disable Pci Bridge ASPM */
Larry Finger886e14b2011-08-06 05:55:18 -0500260 pci_write_config_byte(rtlpci->pdev, (num4bytes << 2),
261 pcibridge_linkctrlreg);
Larry Finger0c817332010-12-08 11:12:31 -0600262
263 udelay(50);
Larry Finger0c817332010-12-08 11:12:31 -0600264}
265
266/*
267 *Enable RTL8192SE ASPM & Enable Pci Bridge ASPM for
268 *power saving We should follow the sequence to enable
269 *RTL8192SE first then enable Pci Bridge ASPM
270 *or the system will show bluescreen.
271 */
272static void rtl_pci_enable_aspm(struct ieee80211_hw *hw)
273{
274 struct rtl_priv *rtlpriv = rtl_priv(hw);
275 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
276 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
277 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600278 u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
Larry Finger0c817332010-12-08 11:12:31 -0600279 u8 num4bytes = pcipriv->ndis_adapter.num4bytes;
280 u16 aspmlevel;
281 u8 u_pcibridge_aspmsetting;
282 u8 u_device_aspmsetting;
283
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500284 if (!ppsc->support_aspm)
285 return;
286
Larry Finger0c817332010-12-08 11:12:31 -0600287 if (pcibridge_vendor == PCI_BRIDGE_VENDOR_UNKNOWN) {
288 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800289 "PCI(Bridge) UNKNOWN\n");
Larry Finger0c817332010-12-08 11:12:31 -0600290 return;
291 }
292
293 /*4 Enable Pci Bridge ASPM */
Larry Finger0c817332010-12-08 11:12:31 -0600294
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
Larry Finger886e14b2011-08-06 05:55:18 -0500302 pci_write_config_byte(rtlpci->pdev, (num4bytes << 2),
303 u_pcibridge_aspmsetting);
Larry Finger0c817332010-12-08 11:12:31 -0600304
305 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
Larry Finger26634c42013-03-24 22:06:33 -0500306 "PlatformEnableASPM(): Write reg[%x] = %x\n",
Joe Perchesf30d7502012-01-04 19:40:41 -0800307 (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 Finger26634c42013-03-24 22:06:33 -0500352static bool rtl_pci_check_buddy_priv(struct ieee80211_hw *hw,
353 struct rtl_priv **buddy_priv)
354{
355 struct rtl_priv *rtlpriv = rtl_priv(hw);
356 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
357 bool find_buddy_priv = false;
Larry Finger37c52932016-03-17 13:40:56 -0500358 struct rtl_priv *tpriv;
Larry Finger26634c42013-03-24 22:06:33 -0500359 struct rtl_pci_priv *tpcipriv = NULL;
360
361 if (!list_empty(&rtlpriv->glb_var->glb_priv_list)) {
362 list_for_each_entry(tpriv, &rtlpriv->glb_var->glb_priv_list,
363 list) {
Larry Finger37c52932016-03-17 13:40:56 -0500364 tpcipriv = (struct rtl_pci_priv *)tpriv->priv;
365 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
366 "pcipriv->ndis_adapter.funcnumber %x\n",
367 pcipriv->ndis_adapter.funcnumber);
368 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
369 "tpcipriv->ndis_adapter.funcnumber %x\n",
370 tpcipriv->ndis_adapter.funcnumber);
Larry Finger26634c42013-03-24 22:06:33 -0500371
Larry Finger37c52932016-03-17 13:40:56 -0500372 if ((pcipriv->ndis_adapter.busnumber ==
373 tpcipriv->ndis_adapter.busnumber) &&
374 (pcipriv->ndis_adapter.devnumber ==
375 tpcipriv->ndis_adapter.devnumber) &&
376 (pcipriv->ndis_adapter.funcnumber !=
377 tpcipriv->ndis_adapter.funcnumber)) {
378 find_buddy_priv = true;
379 break;
Larry Finger26634c42013-03-24 22:06:33 -0500380 }
381 }
382 }
383
384 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
385 "find_buddy_priv %d\n", find_buddy_priv);
386
387 if (find_buddy_priv)
388 *buddy_priv = tpriv;
389
390 return find_buddy_priv;
391}
392
Larry Fingerd3bb1422011-04-25 13:23:20 -0500393static void rtl_pci_get_linkcontrol_field(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -0600394{
395 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
Larry Finger886e14b2011-08-06 05:55:18 -0500396 struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
Larry Finger0c817332010-12-08 11:12:31 -0600397 u8 capabilityoffset = pcipriv->ndis_adapter.pcibridge_pciehdr_offset;
Larry Finger0c817332010-12-08 11:12:31 -0600398 u8 linkctrl_reg;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500399 u8 num4bbytes;
Larry Finger0c817332010-12-08 11:12:31 -0600400
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500401 num4bbytes = (capabilityoffset + 0x10) / 4;
Larry Finger0c817332010-12-08 11:12:31 -0600402
403 /*Read Link Control Register */
Larry Finger886e14b2011-08-06 05:55:18 -0500404 pci_read_config_byte(rtlpci->pdev, (num4bbytes << 2), &linkctrl_reg);
Larry Finger0c817332010-12-08 11:12:31 -0600405
406 pcipriv->ndis_adapter.pcibridge_linkctrlreg = linkctrl_reg;
407}
408
409static void rtl_pci_parse_configuration(struct pci_dev *pdev,
410 struct ieee80211_hw *hw)
411{
412 struct rtl_priv *rtlpriv = rtl_priv(hw);
413 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
414
415 u8 tmp;
Jiang Liu332badc2012-08-20 14:18:08 -0600416 u16 linkctrl_reg;
Larry Finger0c817332010-12-08 11:12:31 -0600417
418 /*Link Control Register */
Jiang Liu332badc2012-08-20 14:18:08 -0600419 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &linkctrl_reg);
420 pcipriv->ndis_adapter.linkctrl_reg = (u8)linkctrl_reg;
Larry Finger0c817332010-12-08 11:12:31 -0600421
Joe Perchesf30d7502012-01-04 19:40:41 -0800422 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Link Control Register =%x\n",
423 pcipriv->ndis_adapter.linkctrl_reg);
Larry Finger0c817332010-12-08 11:12:31 -0600424
425 pci_read_config_byte(pdev, 0x98, &tmp);
426 tmp |= BIT(4);
427 pci_write_config_byte(pdev, 0x98, tmp);
428
429 tmp = 0x17;
430 pci_write_config_byte(pdev, 0x70f, tmp);
431}
432
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500433static void rtl_pci_init_aspm(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -0600434{
435 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
436
437 _rtl_pci_update_default_setting(hw);
438
439 if (ppsc->reg_rfps_level & RT_RF_PS_LEVEL_ALWAYS_ASPM) {
440 /*Always enable ASPM & Clock Req. */
441 rtl_pci_enable_aspm(hw);
442 RT_SET_PS_LEVEL(ppsc, RT_RF_PS_LEVEL_ALWAYS_ASPM);
443 }
444
445}
446
Larry Finger0c817332010-12-08 11:12:31 -0600447static void _rtl_pci_io_handler_init(struct device *dev,
448 struct ieee80211_hw *hw)
449{
450 struct rtl_priv *rtlpriv = rtl_priv(hw);
451
452 rtlpriv->io.dev = dev;
453
454 rtlpriv->io.write8_async = pci_write8_async;
455 rtlpriv->io.write16_async = pci_write16_async;
456 rtlpriv->io.write32_async = pci_write32_async;
457
458 rtlpriv->io.read8_sync = pci_read8_sync;
459 rtlpriv->io.read16_sync = pci_read16_sync;
460 rtlpriv->io.read32_sync = pci_read32_sync;
461
462}
463
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500464static bool _rtl_update_earlymode_info(struct ieee80211_hw *hw,
465 struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc, u8 tid)
466{
467 struct rtl_priv *rtlpriv = rtl_priv(hw);
468 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Larry Finger26634c42013-03-24 22:06:33 -0500469 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500470 struct sk_buff *next_skb;
Larry Finger26634c42013-03-24 22:06:33 -0500471 u8 additionlen = FCS_LEN;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500472
473 /* here open is 4, wep/tkip is 8, aes is 12*/
474 if (info->control.hw_key)
475 additionlen += info->control.hw_key->icv_len;
476
477 /* The most skb num is 6 */
478 tcb_desc->empkt_num = 0;
479 spin_lock_bh(&rtlpriv->locks.waitq_lock);
480 skb_queue_walk(&rtlpriv->mac80211.skb_waitq[tid], next_skb) {
481 struct ieee80211_tx_info *next_info;
482
483 next_info = IEEE80211_SKB_CB(next_skb);
484 if (next_info->flags & IEEE80211_TX_CTL_AMPDU) {
485 tcb_desc->empkt_len[tcb_desc->empkt_num] =
486 next_skb->len + additionlen;
487 tcb_desc->empkt_num++;
488 } else {
489 break;
490 }
491
492 if (skb_queue_is_last(&rtlpriv->mac80211.skb_waitq[tid],
493 next_skb))
494 break;
495
Larry Finger26634c42013-03-24 22:06:33 -0500496 if (tcb_desc->empkt_num >= rtlhal->max_earlymode_num)
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500497 break;
498 }
499 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
500
501 return true;
502}
503
504/* just for early mode now */
505static void _rtl_pci_tx_chk_waitq(struct ieee80211_hw *hw)
506{
507 struct rtl_priv *rtlpriv = rtl_priv(hw);
508 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
509 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
510 struct sk_buff *skb = NULL;
511 struct ieee80211_tx_info *info = NULL;
Larry Finger26634c42013-03-24 22:06:33 -0500512 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +0300513 int tid;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500514
515 if (!rtlpriv->rtlhal.earlymode_enable)
516 return;
517
Larry Finger26634c42013-03-24 22:06:33 -0500518 if (rtlpriv->dm.supp_phymode_switch &&
519 (rtlpriv->easy_concurrent_ctl.switch_in_process ||
520 (rtlpriv->buddy_priv &&
521 rtlpriv->buddy_priv->easy_concurrent_ctl.switch_in_process)))
522 return;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500523 /* we juse use em for BE/BK/VI/VO */
524 for (tid = 7; tid >= 0; tid--) {
Larry Finger2a00def2012-07-11 14:32:33 -0500525 u8 hw_queue = ac_to_hwq[rtl_tid_to_ac(tid)];
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500526 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
527 while (!mac->act_scanning &&
528 rtlpriv->psc.rfpwr_state == ERFON) {
529 struct rtl_tcb_desc tcb_desc;
530 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
531
532 spin_lock_bh(&rtlpriv->locks.waitq_lock);
533 if (!skb_queue_empty(&mac->skb_waitq[tid]) &&
Larry Finger26634c42013-03-24 22:06:33 -0500534 (ring->entries - skb_queue_len(&ring->queue) >
535 rtlhal->max_earlymode_num)) {
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500536 skb = skb_dequeue(&mac->skb_waitq[tid]);
537 } else {
538 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
539 break;
540 }
541 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
542
543 /* Some macaddr can't do early mode. like
544 * multicast/broadcast/no_qos data */
545 info = IEEE80211_SKB_CB(skb);
546 if (info->flags & IEEE80211_TX_CTL_AMPDU)
547 _rtl_update_earlymode_info(hw, skb,
548 &tcb_desc, tid);
549
Thomas Huehn36323f82012-07-23 21:33:42 +0200550 rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, &tcb_desc);
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500551 }
552 }
553}
554
555
Larry Finger0c817332010-12-08 11:12:31 -0600556static void _rtl_pci_tx_isr(struct ieee80211_hw *hw, int prio)
557{
558 struct rtl_priv *rtlpriv = rtl_priv(hw);
559 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
560
561 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[prio];
562
563 while (skb_queue_len(&ring->queue)) {
Larry Finger0c817332010-12-08 11:12:31 -0600564 struct sk_buff *skb;
565 struct ieee80211_tx_info *info;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500566 __le16 fc;
567 u8 tid;
Larry Finger38506ec2014-09-22 09:39:19 -0500568 u8 *entry;
Larry Finger0c817332010-12-08 11:12:31 -0600569
Larry Finger38506ec2014-09-22 09:39:19 -0500570 if (rtlpriv->use_new_trx_flow)
571 entry = (u8 *)(&ring->buffer_desc[ring->idx]);
572 else
573 entry = (u8 *)(&ring->desc[ring->idx]);
Larry Finger0c817332010-12-08 11:12:31 -0600574
Troy Tand0311312015-02-03 11:15:17 -0600575 if (rtlpriv->cfg->ops->get_available_desc &&
576 rtlpriv->cfg->ops->get_available_desc(hw, prio) <= 1) {
577 RT_TRACE(rtlpriv, (COMP_INTR | COMP_SEND), DBG_DMESG,
578 "no available desc!\n");
579 return;
580 }
581
Larry Finger38506ec2014-09-22 09:39:19 -0500582 if (!rtlpriv->cfg->ops->is_tx_desc_closed(hw, prio, ring->idx))
Larry Finger0c817332010-12-08 11:12:31 -0600583 return;
584 ring->idx = (ring->idx + 1) % ring->entries;
585
586 skb = __skb_dequeue(&ring->queue);
587 pci_unmap_single(rtlpci->pdev,
Larry Fingerd3bb1422011-04-25 13:23:20 -0500588 rtlpriv->cfg->ops->
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -0500589 get_desc(hw, (u8 *)entry, true,
Larry Fingerd3bb1422011-04-25 13:23:20 -0500590 HW_DESC_TXBUFF_ADDR),
Larry Finger0c817332010-12-08 11:12:31 -0600591 skb->len, PCI_DMA_TODEVICE);
592
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500593 /* remove early mode header */
594 if (rtlpriv->rtlhal.earlymode_enable)
595 skb_pull(skb, EM_HDR_LEN);
596
Larry Finger0c817332010-12-08 11:12:31 -0600597 RT_TRACE(rtlpriv, (COMP_INTR | COMP_SEND), DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800598 "new ring->idx:%d, free: skb_queue_len:%d, free: seq:%x\n",
599 ring->idx,
600 skb_queue_len(&ring->queue),
Larry Finger38506ec2014-09-22 09:39:19 -0500601 *(u16 *)(skb->data + 22));
Larry Finger0c817332010-12-08 11:12:31 -0600602
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500603 if (prio == TXCMD_QUEUE) {
604 dev_kfree_skb(skb);
605 goto tx_status_ok;
606
607 }
608
609 /* for sw LPS, just after NULL skb send out, we can
Larry Finger26634c42013-03-24 22:06:33 -0500610 * sure AP knows we are sleeping, we should not let
611 * rf sleep
612 */
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500613 fc = rtl_get_fc(skb);
614 if (ieee80211_is_nullfunc(fc)) {
615 if (ieee80211_has_pm(fc)) {
Mike McCormack9c050442011-06-20 10:44:58 +0900616 rtlpriv->mac80211.offchan_delay = true;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000617 rtlpriv->psc.state_inap = true;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500618 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000619 rtlpriv->psc.state_inap = false;
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500620 }
621 }
Larry Finger26634c42013-03-24 22:06:33 -0500622 if (ieee80211_is_action(fc)) {
623 struct ieee80211_mgmt *action_frame =
624 (struct ieee80211_mgmt *)skb->data;
625 if (action_frame->u.action.u.ht_smps.action ==
626 WLAN_HT_ACTION_SMPS) {
627 dev_kfree_skb(skb);
628 goto tx_status_ok;
629 }
630 }
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500631
632 /* update tid tx pkt num */
633 tid = rtl_get_tid(skb);
634 if (tid <= 7)
635 rtlpriv->link_info.tidtx_inperiod[tid]++;
636
Larry Finger0c817332010-12-08 11:12:31 -0600637 info = IEEE80211_SKB_CB(skb);
638 ieee80211_tx_info_clear_status(info);
639
640 info->flags |= IEEE80211_TX_STAT_ACK;
641 /*info->status.rates[0].count = 1; */
642
643 ieee80211_tx_status_irqsafe(hw, skb);
644
Troy Tand0311312015-02-03 11:15:17 -0600645 if ((ring->entries - skb_queue_len(&ring->queue)) <= 4) {
Larry Finger0c817332010-12-08 11:12:31 -0600646
Troy Tand0311312015-02-03 11:15:17 -0600647 RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
Hans Wennborg4f4378d2014-09-05 20:19:50 -0700648 "more desc left, wake skb_queue@%d, ring->idx = %d, skb_queue_len = 0x%x\n",
Joe Perchesf30d7502012-01-04 19:40:41 -0800649 prio, ring->idx,
650 skb_queue_len(&ring->queue));
Larry Finger0c817332010-12-08 11:12:31 -0600651
652 ieee80211_wake_queue(hw,
653 skb_get_queue_mapping
654 (skb));
655 }
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500656tx_status_ok:
Larry Finger0c817332010-12-08 11:12:31 -0600657 skb = NULL;
658 }
659
660 if (((rtlpriv->link_info.num_rx_inperiod +
Larry Fingerba9f93f2016-11-26 14:43:35 -0600661 rtlpriv->link_info.num_tx_inperiod) > 8) ||
662 (rtlpriv->link_info.num_rx_inperiod > 2))
663 rtl_lps_leave(hw);
Larry Finger0c817332010-12-08 11:12:31 -0600664}
665
Larry Finger38506ec2014-09-22 09:39:19 -0500666static int _rtl_pci_init_one_rxdesc(struct ieee80211_hw *hw,
Larry Fingere9538cf2014-12-30 21:33:07 -0600667 struct sk_buff *new_skb, u8 *entry,
668 int rxring_idx, int desc_idx)
Mike McCormackfd854772011-06-06 23:13:06 +0900669{
670 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger38506ec2014-09-22 09:39:19 -0500671 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
672 u32 bufferaddress;
673 u8 tmp_one = 1;
674 struct sk_buff *skb;
Mike McCormackfd854772011-06-06 23:13:06 +0900675
Larry Fingere9538cf2014-12-30 21:33:07 -0600676 if (likely(new_skb)) {
677 skb = new_skb;
678 goto remap;
679 }
Larry Finger38506ec2014-09-22 09:39:19 -0500680 skb = dev_alloc_skb(rtlpci->rxbuffersize);
681 if (!skb)
682 return 0;
Mike McCormackfd854772011-06-06 23:13:06 +0900683
Larry Fingere9538cf2014-12-30 21:33:07 -0600684remap:
Larry Finger38506ec2014-09-22 09:39:19 -0500685 /* just set skb->cb to mapping addr for pci_unmap_single use */
686 *((dma_addr_t *)skb->cb) =
687 pci_map_single(rtlpci->pdev, skb_tail_pointer(skb),
688 rtlpci->rxbuffersize, PCI_DMA_FROMDEVICE);
689 bufferaddress = *((dma_addr_t *)skb->cb);
690 if (pci_dma_mapping_error(rtlpci->pdev, bufferaddress))
691 return 0;
Larry Fingere9538cf2014-12-30 21:33:07 -0600692 rtlpci->rx_ring[rxring_idx].rx_buf[desc_idx] = skb;
Larry Finger38506ec2014-09-22 09:39:19 -0500693 if (rtlpriv->use_new_trx_flow) {
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -0500694 /* skb->cb may be 64 bit address */
Larry Finger38506ec2014-09-22 09:39:19 -0500695 rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
696 HW_DESC_RX_PREPARE,
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -0500697 (u8 *)(dma_addr_t *)skb->cb);
Mike McCormackfd854772011-06-06 23:13:06 +0900698 } else {
Larry Finger38506ec2014-09-22 09:39:19 -0500699 rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
700 HW_DESC_RXBUFF_ADDR,
701 (u8 *)&bufferaddress);
702 rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
703 HW_DESC_RXPKT_LEN,
704 (u8 *)&rtlpci->rxbuffersize);
705 rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
706 HW_DESC_RXOWN,
707 (u8 *)&tmp_one);
Mike McCormackfd854772011-06-06 23:13:06 +0900708 }
Larry Finger38506ec2014-09-22 09:39:19 -0500709 return 1;
710}
Mike McCormackfd854772011-06-06 23:13:06 +0900711
Larry Finger38506ec2014-09-22 09:39:19 -0500712/* inorder to receive 8K AMSDU we have set skb to
713 * 9100bytes in init rx ring, but if this packet is
714 * not a AMSDU, this large packet will be sent to
715 * TCP/IP directly, this cause big packet ping fail
716 * like: "ping -s 65507", so here we will realloc skb
717 * based on the true size of packet, Mac80211
718 * Probably will do it better, but does not yet.
719 *
720 * Some platform will fail when alloc skb sometimes.
721 * in this condition, we will send the old skb to
722 * mac80211 directly, this will not cause any other
723 * issues, but only this packet will be lost by TCP/IP
724 */
725static void _rtl_pci_rx_to_mac80211(struct ieee80211_hw *hw,
726 struct sk_buff *skb,
727 struct ieee80211_rx_status rx_status)
728{
729 if (unlikely(!rtl_action_proc(hw, skb, false))) {
730 dev_kfree_skb_any(skb);
731 } else {
732 struct sk_buff *uskb = NULL;
Mike McCormackfd854772011-06-06 23:13:06 +0900733
Larry Finger38506ec2014-09-22 09:39:19 -0500734 uskb = dev_alloc_skb(skb->len + 128);
735 if (likely(uskb)) {
736 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
737 sizeof(rx_status));
yuan linyub952f4d2017-06-18 22:52:04 +0800738 skb_put_data(uskb, skb->data, skb->len);
Larry Finger38506ec2014-09-22 09:39:19 -0500739 dev_kfree_skb_any(skb);
740 ieee80211_rx_irqsafe(hw, uskb);
741 } else {
742 ieee80211_rx_irqsafe(hw, skb);
743 }
Mike McCormackfd854772011-06-06 23:13:06 +0900744 }
Larry Finger38506ec2014-09-22 09:39:19 -0500745}
Mike McCormackfd854772011-06-06 23:13:06 +0900746
Larry Finger38506ec2014-09-22 09:39:19 -0500747/*hsisr interrupt handler*/
748static void _rtl_pci_hs_interrupt(struct ieee80211_hw *hw)
749{
750 struct rtl_priv *rtlpriv = rtl_priv(hw);
751 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Larry Finger26634c42013-03-24 22:06:33 -0500752
Larry Finger38506ec2014-09-22 09:39:19 -0500753 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[MAC_HSISR],
754 rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[MAC_HSISR]) |
755 rtlpci->sys_irq_mask);
Mike McCormackfd854772011-06-06 23:13:06 +0900756}
757
Larry Finger0c817332010-12-08 11:12:31 -0600758static void _rtl_pci_rx_interrupt(struct ieee80211_hw *hw)
759{
760 struct rtl_priv *rtlpriv = rtl_priv(hw);
761 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Larry Finger38506ec2014-09-22 09:39:19 -0500762 int rxring_idx = RTL_PCI_RX_MPDU_QUEUE;
Larry Finger0c817332010-12-08 11:12:31 -0600763 struct ieee80211_rx_status rx_status = { 0 };
764 unsigned int count = rtlpci->rxringcount;
765 u8 own;
766 u8 tmp_one;
Larry Finger38506ec2014-09-22 09:39:19 -0500767 bool unicast = false;
768 u8 hw_queue = 0;
769 unsigned int rx_remained_cnt;
Larry Finger0c817332010-12-08 11:12:31 -0600770 struct rtl_stats stats = {
771 .signal = 0,
Larry Finger0c817332010-12-08 11:12:31 -0600772 .rate = 0,
773 };
774
775 /*RX NORMAL PKT */
776 while (count--) {
Larry Finger38506ec2014-09-22 09:39:19 -0500777 struct ieee80211_hdr *hdr;
778 __le16 fc;
779 u16 len;
780 /*rx buffer descriptor */
781 struct rtl_rx_buffer_desc *buffer_desc = NULL;
782 /*if use new trx flow, it means wifi info */
783 struct rtl_rx_desc *pdesc = NULL;
Larry Finger0c817332010-12-08 11:12:31 -0600784 /*rx pkt */
Larry Finger38506ec2014-09-22 09:39:19 -0500785 struct sk_buff *skb = rtlpci->rx_ring[rxring_idx].rx_buf[
786 rtlpci->rx_ring[rxring_idx].idx];
Larry Fingere9538cf2014-12-30 21:33:07 -0600787 struct sk_buff *new_skb;
Larry Finger0c817332010-12-08 11:12:31 -0600788
Larry Finger38506ec2014-09-22 09:39:19 -0500789 if (rtlpriv->use_new_trx_flow) {
790 rx_remained_cnt =
791 rtlpriv->cfg->ops->rx_desc_buff_remained_cnt(hw,
792 hw_queue);
Troy Tand0311312015-02-03 11:15:17 -0600793 if (rx_remained_cnt == 0)
Larry Finger38506ec2014-09-22 09:39:19 -0500794 return;
Larry Fingerf99551a2015-12-21 17:05:08 -0600795 buffer_desc = &rtlpci->rx_ring[rxring_idx].buffer_desc[
796 rtlpci->rx_ring[rxring_idx].idx];
797 pdesc = (struct rtl_rx_desc *)skb->data;
Larry Finger38506ec2014-09-22 09:39:19 -0500798 } else { /* rx descriptor */
799 pdesc = &rtlpci->rx_ring[rxring_idx].desc[
800 rtlpci->rx_ring[rxring_idx].idx];
Larry Finger0c817332010-12-08 11:12:31 -0600801
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -0500802 own = (u8)rtlpriv->cfg->ops->get_desc(hw, (u8 *)pdesc,
Larry Finger38506ec2014-09-22 09:39:19 -0500803 false,
804 HW_DESC_OWN);
805 if (own) /* wait data to be filled by hardware */
806 return;
Mike McCormack2c333362011-06-06 23:12:08 +0900807 }
Mike McCormack6633d642011-06-07 08:58:31 +0900808
Larry Finger38506ec2014-09-22 09:39:19 -0500809 /* Reaching this point means: data is filled already
810 * AAAAAAttention !!!
811 * We can NOT access 'skb' before 'pci_unmap_single'
812 */
813 pci_unmap_single(rtlpci->pdev, *((dma_addr_t *)skb->cb),
814 rtlpci->rxbuffersize, PCI_DMA_FROMDEVICE);
Mike McCormack6633d642011-06-07 08:58:31 +0900815
Larry Fingere9538cf2014-12-30 21:33:07 -0600816 /* get a new skb - if fail, old one will be reused */
817 new_skb = dev_alloc_skb(rtlpci->rxbuffersize);
Larry Fingeraeb2d2a2015-01-20 11:01:20 -0600818 if (unlikely(!new_skb))
Larry Fingere9538cf2014-12-30 21:33:07 -0600819 goto no_new;
Larry Finger38506ec2014-09-22 09:39:19 -0500820 memset(&rx_status , 0 , sizeof(rx_status));
821 rtlpriv->cfg->ops->query_rx_desc(hw, &stats,
822 &rx_status, (u8 *)pdesc, skb);
823
824 if (rtlpriv->use_new_trx_flow)
825 rtlpriv->cfg->ops->rx_check_dma_ok(hw,
826 (u8 *)buffer_desc,
827 hw_queue);
828
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -0500829 len = rtlpriv->cfg->ops->get_desc(hw, (u8 *)pdesc, false,
Larry Finger38506ec2014-09-22 09:39:19 -0500830 HW_DESC_RXPKT_LEN);
831
832 if (skb->end - skb->tail > len) {
833 skb_put(skb, len);
834 if (rtlpriv->use_new_trx_flow)
835 skb_reserve(skb, stats.rx_drvinfo_size +
836 stats.rx_bufshift + 24);
837 else
838 skb_reserve(skb, stats.rx_drvinfo_size +
839 stats.rx_bufshift);
Larry Finger38506ec2014-09-22 09:39:19 -0500840 } else {
841 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
842 "skb->end - skb->tail = %d, len is %d\n",
843 skb->end - skb->tail, len);
Troy Tand0311312015-02-03 11:15:17 -0600844 dev_kfree_skb_any(skb);
845 goto new_trx_end;
Larry Finger38506ec2014-09-22 09:39:19 -0500846 }
847 /* handle command packet here */
Larry Fingerd1cd5ba2014-11-07 10:05:12 -0600848 if (rtlpriv->cfg->ops->rx_command_packet &&
Colin Ian Kingce254242016-02-22 11:35:46 +0000849 rtlpriv->cfg->ops->rx_command_packet(hw, &stats, skb)) {
Larry Finger38506ec2014-09-22 09:39:19 -0500850 dev_kfree_skb_any(skb);
Troy Tand0311312015-02-03 11:15:17 -0600851 goto new_trx_end;
Larry Finger38506ec2014-09-22 09:39:19 -0500852 }
Larry Finger0c817332010-12-08 11:12:31 -0600853
Mike McCormack2c333362011-06-06 23:12:08 +0900854 /*
855 * NOTICE This can not be use for mac80211,
856 * this is done in mac80211 code,
Larry Finger38506ec2014-09-22 09:39:19 -0500857 * if done here sec DHCP will fail
Mike McCormack2c333362011-06-06 23:12:08 +0900858 * skb_trim(skb, skb->len - 4);
859 */
Larry Finger0c817332010-12-08 11:12:31 -0600860
Larry Finger38506ec2014-09-22 09:39:19 -0500861 hdr = rtl_get_hdr(skb);
862 fc = rtl_get_fc(skb);
Larry Finger0c817332010-12-08 11:12:31 -0600863
Larry Finger38506ec2014-09-22 09:39:19 -0500864 if (!stats.crc && !stats.hwerror) {
865 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status,
866 sizeof(rx_status));
867
868 if (is_broadcast_ether_addr(hdr->addr1)) {
869 ;/*TODO*/
870 } else if (is_multicast_ether_addr(hdr->addr1)) {
871 ;/*TODO*/
872 } else {
873 unicast = true;
874 rtlpriv->stats.rxbytesunicast += skb->len;
875 }
Taehee Yoocad737d2015-03-28 00:22:39 +0900876 rtl_is_special_data(hw, skb, false, true);
Larry Finger38506ec2014-09-22 09:39:19 -0500877
878 if (ieee80211_is_data(fc)) {
879 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
880 if (unicast)
881 rtlpriv->link_info.num_rx_inperiod++;
882 }
Ping-Ke Shihc76ab8e2017-06-21 12:15:37 -0500883
884 rtl_collect_scan_list(hw, skb);
885
Larry Finger38506ec2014-09-22 09:39:19 -0500886 /* static bcn for roaming */
887 rtl_beacon_statistic(hw, skb);
888 rtl_p2p_info(hw, (void *)skb->data, skb->len);
889 /* for sw lps */
890 rtl_swlps_beacon(hw, (void *)skb->data, skb->len);
891 rtl_recognize_peer(hw, (void *)skb->data, skb->len);
892 if ((rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP) &&
893 (rtlpriv->rtlhal.current_bandtype ==
894 BAND_ON_2_4G) &&
895 (ieee80211_is_beacon(fc) ||
896 ieee80211_is_probe_resp(fc))) {
897 dev_kfree_skb_any(skb);
898 } else {
899 _rtl_pci_rx_to_mac80211(hw, skb, rx_status);
900 }
901 } else {
902 dev_kfree_skb_any(skb);
903 }
Troy Tand0311312015-02-03 11:15:17 -0600904new_trx_end:
Larry Finger38506ec2014-09-22 09:39:19 -0500905 if (rtlpriv->use_new_trx_flow) {
906 rtlpci->rx_ring[hw_queue].next_rx_rp += 1;
907 rtlpci->rx_ring[hw_queue].next_rx_rp %=
908 RTL_PCI_MAX_RX_COUNT;
909
910 rx_remained_cnt--;
911 rtl_write_word(rtlpriv, 0x3B4,
912 rtlpci->rx_ring[hw_queue].next_rx_rp);
913 }
Mike McCormack2c333362011-06-06 23:12:08 +0900914 if (((rtlpriv->link_info.num_rx_inperiod +
Larry Fingera2699132013-03-24 22:06:41 -0500915 rtlpriv->link_info.num_tx_inperiod) > 8) ||
Larry Fingerba9f93f2016-11-26 14:43:35 -0600916 (rtlpriv->link_info.num_rx_inperiod > 2))
917 rtl_lps_leave(hw);
Larry Fingere9538cf2014-12-30 21:33:07 -0600918 skb = new_skb;
919no_new:
Larry Finger38506ec2014-09-22 09:39:19 -0500920 if (rtlpriv->use_new_trx_flow) {
Larry Fingere9538cf2014-12-30 21:33:07 -0600921 _rtl_pci_init_one_rxdesc(hw, skb, (u8 *)buffer_desc,
Larry Finger38506ec2014-09-22 09:39:19 -0500922 rxring_idx,
Larry Finger38506ec2014-09-22 09:39:19 -0500923 rtlpci->rx_ring[rxring_idx].idx);
Larry Fingere9538cf2014-12-30 21:33:07 -0600924 } else {
925 _rtl_pci_init_one_rxdesc(hw, skb, (u8 *)pdesc,
926 rxring_idx,
927 rtlpci->rx_ring[rxring_idx].idx);
Larry Finger38506ec2014-09-22 09:39:19 -0500928 if (rtlpci->rx_ring[rxring_idx].idx ==
929 rtlpci->rxringcount - 1)
930 rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc,
931 false,
932 HW_DESC_RXERO,
933 (u8 *)&tmp_one);
934 }
935 rtlpci->rx_ring[rxring_idx].idx =
936 (rtlpci->rx_ring[rxring_idx].idx + 1) %
937 rtlpci->rxringcount;
Larry Finger0c817332010-12-08 11:12:31 -0600938 }
Larry Finger0c817332010-12-08 11:12:31 -0600939}
940
Larry Finger0c817332010-12-08 11:12:31 -0600941static irqreturn_t _rtl_pci_interrupt(int irq, void *dev_id)
942{
943 struct ieee80211_hw *hw = dev_id;
Larry Finger38506ec2014-09-22 09:39:19 -0500944 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600945 struct rtl_priv *rtlpriv = rtl_priv(hw);
Chaoming_Lic7cfe382011-04-25 13:23:15 -0500946 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Finger0c817332010-12-08 11:12:31 -0600947 unsigned long flags;
948 u32 inta = 0;
949 u32 intb = 0;
Larry Fingerde2e56c2011-11-23 21:30:19 -0600950 irqreturn_t ret = IRQ_HANDLED;
Larry Finger0c817332010-12-08 11:12:31 -0600951
Larry Finger38506ec2014-09-22 09:39:19 -0500952 if (rtlpci->irq_enabled == 0)
953 return ret;
954
955 spin_lock_irqsave(&rtlpriv->locks.irq_th_lock , flags);
956 rtlpriv->cfg->ops->disable_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -0600957
958 /*read ISR: 4/8bytes */
959 rtlpriv->cfg->ops->interrupt_recognized(hw, &inta, &intb);
960
961 /*Shared IRQ or HW disappared */
Larry Finger0529c6b2014-09-26 16:40:24 -0500962 if (!inta || inta == 0xffff)
Larry Finger0c817332010-12-08 11:12:31 -0600963 goto done;
964
965 /*<1> beacon related */
966 if (inta & rtlpriv->cfg->maps[RTL_IMR_TBDOK]) {
967 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800968 "beacon ok interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600969 }
970
971 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_TBDER])) {
972 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800973 "beacon err interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600974 }
975
976 if (inta & rtlpriv->cfg->maps[RTL_IMR_BDOK]) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800977 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE, "beacon interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600978 }
979
Larry Fingere6deaf82013-03-24 22:06:55 -0500980 if (inta & rtlpriv->cfg->maps[RTL_IMR_BCNINT]) {
Larry Finger0c817332010-12-08 11:12:31 -0600981 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800982 "prepare beacon for interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600983 tasklet_schedule(&rtlpriv->works.irq_prepare_bcn_tasklet);
984 }
985
Larry Finger38506ec2014-09-22 09:39:19 -0500986 /*<2> Tx related */
987 if (unlikely(intb & rtlpriv->cfg->maps[RTL_IMR_TXFOVW]))
Joe Perchesf30d7502012-01-04 19:40:41 -0800988 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, "IMR_TXFOVW!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600989
990 if (inta & rtlpriv->cfg->maps[RTL_IMR_MGNTDOK]) {
991 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800992 "Manage ok interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600993 _rtl_pci_tx_isr(hw, MGNT_QUEUE);
994 }
995
996 if (inta & rtlpriv->cfg->maps[RTL_IMR_HIGHDOK]) {
997 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800998 "HIGH_QUEUE ok interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600999 _rtl_pci_tx_isr(hw, HIGH_QUEUE);
1000 }
1001
1002 if (inta & rtlpriv->cfg->maps[RTL_IMR_BKDOK]) {
1003 rtlpriv->link_info.num_tx_inperiod++;
1004
1005 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -08001006 "BK Tx OK interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -06001007 _rtl_pci_tx_isr(hw, BK_QUEUE);
1008 }
1009
1010 if (inta & rtlpriv->cfg->maps[RTL_IMR_BEDOK]) {
1011 rtlpriv->link_info.num_tx_inperiod++;
1012
1013 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -08001014 "BE TX OK interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -06001015 _rtl_pci_tx_isr(hw, BE_QUEUE);
1016 }
1017
1018 if (inta & rtlpriv->cfg->maps[RTL_IMR_VIDOK]) {
1019 rtlpriv->link_info.num_tx_inperiod++;
1020
1021 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -08001022 "VI TX OK interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -06001023 _rtl_pci_tx_isr(hw, VI_QUEUE);
1024 }
1025
1026 if (inta & rtlpriv->cfg->maps[RTL_IMR_VODOK]) {
1027 rtlpriv->link_info.num_tx_inperiod++;
1028
1029 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -08001030 "Vo TX OK interrupt!\n");
Larry Finger0c817332010-12-08 11:12:31 -06001031 _rtl_pci_tx_isr(hw, VO_QUEUE);
1032 }
1033
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001034 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
1035 if (inta & rtlpriv->cfg->maps[RTL_IMR_COMDOK]) {
1036 rtlpriv->link_info.num_tx_inperiod++;
1037
1038 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -08001039 "CMD TX OK interrupt!\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001040 _rtl_pci_tx_isr(hw, TXCMD_QUEUE);
1041 }
1042 }
1043
Larry Finger38506ec2014-09-22 09:39:19 -05001044 /*<3> Rx related */
Larry Finger0c817332010-12-08 11:12:31 -06001045 if (inta & rtlpriv->cfg->maps[RTL_IMR_ROK]) {
Joe Perchesf30d7502012-01-04 19:40:41 -08001046 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE, "Rx ok interrupt!\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001047 _rtl_pci_rx_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001048 }
1049
1050 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_RDU])) {
1051 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Joe Perchesf30d7502012-01-04 19:40:41 -08001052 "rx descriptor unavailable!\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001053 _rtl_pci_rx_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001054 }
1055
Larry Finger38506ec2014-09-22 09:39:19 -05001056 if (unlikely(intb & rtlpriv->cfg->maps[RTL_IMR_RXFOVW])) {
Joe Perchesf30d7502012-01-04 19:40:41 -08001057 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, "rx overflow !\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001058 _rtl_pci_rx_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001059 }
1060
Larry Finger38506ec2014-09-22 09:39:19 -05001061 /*<4> fw related*/
Larry Finger26634c42013-03-24 22:06:33 -05001062 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723AE) {
1063 if (inta & rtlpriv->cfg->maps[RTL_IMR_C2HCMD]) {
1064 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
1065 "firmware interrupt!\n");
1066 queue_delayed_work(rtlpriv->works.rtl_wq,
1067 &rtlpriv->works.fwevt_wq, 0);
1068 }
1069 }
1070
Larry Finger38506ec2014-09-22 09:39:19 -05001071 /*<5> hsisr related*/
1072 /* Only 8188EE & 8723BE Supported.
1073 * If Other ICs Come in, System will corrupt,
1074 * because maps[RTL_IMR_HSISR_IND] & maps[MAC_HSISR]
1075 * are not initialized
1076 */
1077 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8188EE ||
1078 rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
1079 if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_HSISR_IND])) {
1080 RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
1081 "hsisr interrupt!\n");
1082 _rtl_pci_hs_interrupt(hw);
1083 }
1084 }
1085
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001086 if (rtlpriv->rtlhal.earlymode_enable)
1087 tasklet_schedule(&rtlpriv->works.irq_tasklet);
1088
Larry Finger0c817332010-12-08 11:12:31 -06001089done:
Larry Finger38506ec2014-09-22 09:39:19 -05001090 rtlpriv->cfg->ops->enable_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001091 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
Larry Fingerde2e56c2011-11-23 21:30:19 -06001092 return ret;
Larry Finger0c817332010-12-08 11:12:31 -06001093}
1094
1095static void _rtl_pci_irq_tasklet(struct ieee80211_hw *hw)
1096{
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001097 _rtl_pci_tx_chk_waitq(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001098}
1099
1100static void _rtl_pci_prepare_bcn_tasklet(struct ieee80211_hw *hw)
1101{
1102 struct rtl_priv *rtlpriv = rtl_priv(hw);
1103 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1104 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001105 struct rtl8192_tx_ring *ring = NULL;
Larry Finger0c817332010-12-08 11:12:31 -06001106 struct ieee80211_hdr *hdr = NULL;
1107 struct ieee80211_tx_info *info = NULL;
1108 struct sk_buff *pskb = NULL;
1109 struct rtl_tx_desc *pdesc = NULL;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001110 struct rtl_tcb_desc tcb_desc;
Larry Fingerf3355dd2014-03-04 16:53:47 -06001111 /*This is for new trx flow*/
1112 struct rtl_tx_buffer_desc *pbuffer_desc = NULL;
Larry Finger0c817332010-12-08 11:12:31 -06001113 u8 temp_one = 1;
Larry Fingerbe0b5e62015-03-21 15:16:05 -05001114 u8 *entry;
Larry Finger0c817332010-12-08 11:12:31 -06001115
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001116 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
Larry Finger0c817332010-12-08 11:12:31 -06001117 ring = &rtlpci->tx_ring[BEACON_QUEUE];
1118 pskb = __skb_dequeue(&ring->queue);
Larry Fingerbe0b5e62015-03-21 15:16:05 -05001119 if (rtlpriv->use_new_trx_flow)
1120 entry = (u8 *)(&ring->buffer_desc[ring->idx]);
1121 else
1122 entry = (u8 *)(&ring->desc[ring->idx]);
1123 if (pskb) {
1124 pci_unmap_single(rtlpci->pdev,
1125 rtlpriv->cfg->ops->get_desc(
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -05001126 hw, (u8 *)entry, true, HW_DESC_TXBUFF_ADDR),
Larry Fingerbe0b5e62015-03-21 15:16:05 -05001127 pskb->len, PCI_DMA_TODEVICE);
Larry Finger0c817332010-12-08 11:12:31 -06001128 kfree_skb(pskb);
Larry Fingerbe0b5e62015-03-21 15:16:05 -05001129 }
Larry Finger0c817332010-12-08 11:12:31 -06001130
1131 /*NB: the beacon data buffer must be 32-bit aligned. */
1132 pskb = ieee80211_beacon_get(hw, mac->vif);
1133 if (pskb == NULL)
1134 return;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001135 hdr = rtl_get_hdr(pskb);
Larry Finger0c817332010-12-08 11:12:31 -06001136 info = IEEE80211_SKB_CB(pskb);
Larry Finger0c817332010-12-08 11:12:31 -06001137 pdesc = &ring->desc[0];
Larry Finger38506ec2014-09-22 09:39:19 -05001138 if (rtlpriv->use_new_trx_flow)
1139 pbuffer_desc = &ring->buffer_desc[0];
1140
1141 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc,
Larry Fingerf3355dd2014-03-04 16:53:47 -06001142 (u8 *)pbuffer_desc, info, NULL, pskb,
1143 BEACON_QUEUE, &tcb_desc);
Larry Finger0c817332010-12-08 11:12:31 -06001144
1145 __skb_queue_tail(&ring->queue, pskb);
1146
Larry Fingerfb6eaf22014-11-05 19:10:52 -06001147 if (rtlpriv->use_new_trx_flow) {
1148 temp_one = 4;
1149 rtlpriv->cfg->ops->set_desc(hw, (u8 *)pbuffer_desc, true,
1150 HW_DESC_OWN, (u8 *)&temp_one);
1151 } else {
1152 rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc, true, HW_DESC_OWN,
1153 &temp_one);
1154 }
Larry Finger0c817332010-12-08 11:12:31 -06001155 return;
1156}
1157
1158static void _rtl_pci_init_trx_var(struct ieee80211_hw *hw)
1159{
1160 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Larry Finger38506ec2014-09-22 09:39:19 -05001161 struct rtl_priv *rtlpriv = rtl_priv(hw);
1162 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
Larry Finger0c817332010-12-08 11:12:31 -06001163 u8 i;
Larry Finger38506ec2014-09-22 09:39:19 -05001164 u16 desc_num;
1165
1166 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE)
1167 desc_num = TX_DESC_NUM_92E;
1168 else
1169 desc_num = RT_TXDESC_NUM;
Larry Finger0c817332010-12-08 11:12:31 -06001170
1171 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
Larry Finger38506ec2014-09-22 09:39:19 -05001172 rtlpci->txringcount[i] = desc_num;
Larry Finger0c817332010-12-08 11:12:31 -06001173
1174 /*
1175 *we just alloc 2 desc for beacon queue,
1176 *because we just need first desc in hw beacon.
1177 */
1178 rtlpci->txringcount[BEACON_QUEUE] = 2;
1179
Larry Finger38506ec2014-09-22 09:39:19 -05001180 /*BE queue need more descriptor for performance
Larry Finger0c817332010-12-08 11:12:31 -06001181 *consideration or, No more tx desc will happen,
1182 *and may cause mac80211 mem leakage.
1183 */
Larry Finger38506ec2014-09-22 09:39:19 -05001184 if (!rtl_priv(hw)->use_new_trx_flow)
1185 rtlpci->txringcount[BE_QUEUE] = RT_TXDESC_NUM_BE_QUEUE;
Larry Finger0c817332010-12-08 11:12:31 -06001186
1187 rtlpci->rxbuffersize = 9100; /*2048/1024; */
1188 rtlpci->rxringcount = RTL_PCI_MAX_RX_COUNT; /*64; */
1189}
1190
1191static void _rtl_pci_init_struct(struct ieee80211_hw *hw,
1192 struct pci_dev *pdev)
1193{
1194 struct rtl_priv *rtlpriv = rtl_priv(hw);
1195 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1196 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1197 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Finger0c817332010-12-08 11:12:31 -06001198
1199 rtlpci->up_first_time = true;
1200 rtlpci->being_init_adapter = false;
1201
1202 rtlhal->hw = hw;
1203 rtlpci->pdev = pdev;
1204
Larry Finger0c817332010-12-08 11:12:31 -06001205 /*Tx/Rx related var */
1206 _rtl_pci_init_trx_var(hw);
1207
Larry Finger37c52932016-03-17 13:40:56 -05001208 /*IBSS*/
1209 mac->beacon_interval = 100;
Larry Finger0c817332010-12-08 11:12:31 -06001210
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001211 /*AMPDU*/
1212 mac->min_space_cfg = 0;
Larry Finger0c817332010-12-08 11:12:31 -06001213 mac->max_mss_density = 0;
1214 /*set sane AMPDU defaults */
1215 mac->current_ampdu_density = 7;
1216 mac->current_ampdu_factor = 3;
1217
Ping-Ke Shih8d0d43e2017-02-06 21:29:59 -06001218 /*Retry Limit*/
1219 mac->retry_short = 7;
1220 mac->retry_long = 7;
1221
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001222 /*QOS*/
Larry Finger2cddad32014-02-28 15:16:46 -06001223 rtlpci->acm_method = EACMWAY2_SW;
Larry Finger0c817332010-12-08 11:12:31 -06001224
1225 /*task */
1226 tasklet_init(&rtlpriv->works.irq_tasklet,
1227 (void (*)(unsigned long))_rtl_pci_irq_tasklet,
1228 (unsigned long)hw);
1229 tasklet_init(&rtlpriv->works.irq_prepare_bcn_tasklet,
1230 (void (*)(unsigned long))_rtl_pci_prepare_bcn_tasklet,
1231 (unsigned long)hw);
Larry Fingera2699132013-03-24 22:06:41 -05001232 INIT_WORK(&rtlpriv->works.lps_change_work,
1233 rtl_lps_change_work_callback);
Larry Finger0c817332010-12-08 11:12:31 -06001234}
1235
1236static int _rtl_pci_init_tx_ring(struct ieee80211_hw *hw,
1237 unsigned int prio, unsigned int entries)
1238{
1239 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1240 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger38506ec2014-09-22 09:39:19 -05001241 struct rtl_tx_buffer_desc *buffer_desc;
1242 struct rtl_tx_desc *desc;
1243 dma_addr_t buffer_desc_dma, desc_dma;
Larry Finger0c817332010-12-08 11:12:31 -06001244 u32 nextdescaddress;
1245 int i;
1246
Larry Finger38506ec2014-09-22 09:39:19 -05001247 /* alloc tx buffer desc for new trx flow*/
1248 if (rtlpriv->use_new_trx_flow) {
1249 buffer_desc =
1250 pci_zalloc_consistent(rtlpci->pdev,
1251 sizeof(*buffer_desc) * entries,
1252 &buffer_desc_dma);
1253
1254 if (!buffer_desc || (unsigned long)buffer_desc & 0xFF) {
Larry Fingerb03d9682016-12-15 12:22:58 -06001255 pr_err("Cannot allocate TX ring (prio = %d)\n",
1256 prio);
Larry Finger38506ec2014-09-22 09:39:19 -05001257 return -ENOMEM;
1258 }
1259
1260 rtlpci->tx_ring[prio].buffer_desc = buffer_desc;
1261 rtlpci->tx_ring[prio].buffer_desc_dma = buffer_desc_dma;
1262
1263 rtlpci->tx_ring[prio].cur_tx_rp = 0;
1264 rtlpci->tx_ring[prio].cur_tx_wp = 0;
1265 rtlpci->tx_ring[prio].avl_desc = entries;
1266 }
1267
1268 /* alloc dma for this ring */
1269 desc = pci_zalloc_consistent(rtlpci->pdev,
1270 sizeof(*desc) * entries, &desc_dma);
1271
1272 if (!desc || (unsigned long)desc & 0xFF) {
Larry Fingerb03d9682016-12-15 12:22:58 -06001273 pr_err("Cannot allocate TX ring (prio = %d)\n", prio);
Larry Finger0c817332010-12-08 11:12:31 -06001274 return -ENOMEM;
1275 }
1276
Larry Finger38506ec2014-09-22 09:39:19 -05001277 rtlpci->tx_ring[prio].desc = desc;
1278 rtlpci->tx_ring[prio].dma = desc_dma;
1279
Larry Finger0c817332010-12-08 11:12:31 -06001280 rtlpci->tx_ring[prio].idx = 0;
1281 rtlpci->tx_ring[prio].entries = entries;
1282 skb_queue_head_init(&rtlpci->tx_ring[prio].queue);
1283
Joe Perchesf30d7502012-01-04 19:40:41 -08001284 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "queue:%d, ring_addr:%p\n",
Larry Finger38506ec2014-09-22 09:39:19 -05001285 prio, desc);
Larry Finger0c817332010-12-08 11:12:31 -06001286
Larry Finger38506ec2014-09-22 09:39:19 -05001287 /* init every desc in this ring */
1288 if (!rtlpriv->use_new_trx_flow) {
1289 for (i = 0; i < entries; i++) {
1290 nextdescaddress = (u32)desc_dma +
1291 ((i + 1) % entries) *
1292 sizeof(*desc);
Larry Finger0c817332010-12-08 11:12:31 -06001293
Larry Finger38506ec2014-09-22 09:39:19 -05001294 rtlpriv->cfg->ops->set_desc(hw, (u8 *)&desc[i],
1295 true,
1296 HW_DESC_TX_NEXTDESC_ADDR,
1297 (u8 *)&nextdescaddress);
1298 }
Larry Finger0c817332010-12-08 11:12:31 -06001299 }
Larry Finger0c817332010-12-08 11:12:31 -06001300 return 0;
1301}
1302
Larry Finger38506ec2014-09-22 09:39:19 -05001303static int _rtl_pci_init_rx_ring(struct ieee80211_hw *hw, int rxring_idx)
Larry Finger0c817332010-12-08 11:12:31 -06001304{
1305 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1306 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger38506ec2014-09-22 09:39:19 -05001307 int i;
Larry Finger0c817332010-12-08 11:12:31 -06001308
Larry Finger38506ec2014-09-22 09:39:19 -05001309 if (rtlpriv->use_new_trx_flow) {
1310 struct rtl_rx_buffer_desc *entry = NULL;
1311 /* alloc dma for this ring */
1312 rtlpci->rx_ring[rxring_idx].buffer_desc =
1313 pci_zalloc_consistent(rtlpci->pdev,
1314 sizeof(*rtlpci->rx_ring[rxring_idx].
1315 buffer_desc) *
1316 rtlpci->rxringcount,
1317 &rtlpci->rx_ring[rxring_idx].dma);
1318 if (!rtlpci->rx_ring[rxring_idx].buffer_desc ||
1319 (ulong)rtlpci->rx_ring[rxring_idx].buffer_desc & 0xFF) {
Larry Fingerb03d9682016-12-15 12:22:58 -06001320 pr_err("Cannot allocate RX ring\n");
Larry Finger0c817332010-12-08 11:12:31 -06001321 return -ENOMEM;
1322 }
1323
Larry Finger38506ec2014-09-22 09:39:19 -05001324 /* init every desc in this ring */
1325 rtlpci->rx_ring[rxring_idx].idx = 0;
1326 for (i = 0; i < rtlpci->rxringcount; i++) {
1327 entry = &rtlpci->rx_ring[rxring_idx].buffer_desc[i];
Larry Fingere9538cf2014-12-30 21:33:07 -06001328 if (!_rtl_pci_init_one_rxdesc(hw, NULL, (u8 *)entry,
Larry Finger38506ec2014-09-22 09:39:19 -05001329 rxring_idx, i))
1330 return -ENOMEM;
1331 }
1332 } else {
1333 struct rtl_rx_desc *entry = NULL;
1334 u8 tmp_one = 1;
1335 /* alloc dma for this ring */
1336 rtlpci->rx_ring[rxring_idx].desc =
1337 pci_zalloc_consistent(rtlpci->pdev,
1338 sizeof(*rtlpci->rx_ring[rxring_idx].
1339 desc) * rtlpci->rxringcount,
1340 &rtlpci->rx_ring[rxring_idx].dma);
1341 if (!rtlpci->rx_ring[rxring_idx].desc ||
1342 (unsigned long)rtlpci->rx_ring[rxring_idx].desc & 0xFF) {
Larry Fingerb03d9682016-12-15 12:22:58 -06001343 pr_err("Cannot allocate RX ring\n");
Larry Finger38506ec2014-09-22 09:39:19 -05001344 return -ENOMEM;
1345 }
Larry Finger0c817332010-12-08 11:12:31 -06001346
Larry Finger38506ec2014-09-22 09:39:19 -05001347 /* init every desc in this ring */
1348 rtlpci->rx_ring[rxring_idx].idx = 0;
Larry Finger0019a2c2011-05-19 11:48:45 -05001349
Larry Finger0c817332010-12-08 11:12:31 -06001350 for (i = 0; i < rtlpci->rxringcount; i++) {
Larry Finger38506ec2014-09-22 09:39:19 -05001351 entry = &rtlpci->rx_ring[rxring_idx].desc[i];
Larry Fingere9538cf2014-12-30 21:33:07 -06001352 if (!_rtl_pci_init_one_rxdesc(hw, NULL, (u8 *)entry,
Larry Finger38506ec2014-09-22 09:39:19 -05001353 rxring_idx, i))
1354 return -ENOMEM;
Larry Finger0c817332010-12-08 11:12:31 -06001355 }
1356
Larry Fingerf3355dd2014-03-04 16:53:47 -06001357 rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
Joe Perches2c208892012-06-04 12:44:17 +00001358 HW_DESC_RXERO, &tmp_one);
Larry Finger0c817332010-12-08 11:12:31 -06001359 }
1360 return 0;
1361}
1362
1363static void _rtl_pci_free_tx_ring(struct ieee80211_hw *hw,
1364 unsigned int prio)
1365{
1366 struct rtl_priv *rtlpriv = rtl_priv(hw);
1367 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1368 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[prio];
1369
Larry Finger38506ec2014-09-22 09:39:19 -05001370 /* free every desc in this ring */
Larry Finger0c817332010-12-08 11:12:31 -06001371 while (skb_queue_len(&ring->queue)) {
Larry Finger38506ec2014-09-22 09:39:19 -05001372 u8 *entry;
Larry Finger0c817332010-12-08 11:12:31 -06001373 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1374
Larry Finger38506ec2014-09-22 09:39:19 -05001375 if (rtlpriv->use_new_trx_flow)
1376 entry = (u8 *)(&ring->buffer_desc[ring->idx]);
1377 else
1378 entry = (u8 *)(&ring->desc[ring->idx]);
1379
Larry Finger0c817332010-12-08 11:12:31 -06001380 pci_unmap_single(rtlpci->pdev,
Larry Fingerd3bb1422011-04-25 13:23:20 -05001381 rtlpriv->cfg->
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -05001382 ops->get_desc(hw, (u8 *)entry,
1383 true,
Larry Fingerd3bb1422011-04-25 13:23:20 -05001384 HW_DESC_TXBUFF_ADDR),
Larry Finger0c817332010-12-08 11:12:31 -06001385 skb->len, PCI_DMA_TODEVICE);
1386 kfree_skb(skb);
1387 ring->idx = (ring->idx + 1) % ring->entries;
1388 }
1389
Larry Finger38506ec2014-09-22 09:39:19 -05001390 /* free dma of this ring */
1391 pci_free_consistent(rtlpci->pdev,
1392 sizeof(*ring->desc) * ring->entries,
1393 ring->desc, ring->dma);
1394 ring->desc = NULL;
1395 if (rtlpriv->use_new_trx_flow) {
Simon Graham7f66c2f2012-02-07 18:07:38 -06001396 pci_free_consistent(rtlpci->pdev,
Larry Fingercaea2172014-11-05 19:10:53 -06001397 sizeof(*ring->buffer_desc) * ring->entries,
Larry Finger38506ec2014-09-22 09:39:19 -05001398 ring->buffer_desc, ring->buffer_desc_dma);
Larry Fingercaea2172014-11-05 19:10:53 -06001399 ring->buffer_desc = NULL;
Simon Graham7f66c2f2012-02-07 18:07:38 -06001400 }
Larry Finger0c817332010-12-08 11:12:31 -06001401}
1402
Larry Finger38506ec2014-09-22 09:39:19 -05001403static void _rtl_pci_free_rx_ring(struct ieee80211_hw *hw, int rxring_idx)
Larry Finger0c817332010-12-08 11:12:31 -06001404{
Larry Finger38506ec2014-09-22 09:39:19 -05001405 struct rtl_priv *rtlpriv = rtl_priv(hw);
1406 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1407 int i;
Larry Finger0c817332010-12-08 11:12:31 -06001408
Larry Finger38506ec2014-09-22 09:39:19 -05001409 /* free every desc in this ring */
1410 for (i = 0; i < rtlpci->rxringcount; i++) {
1411 struct sk_buff *skb = rtlpci->rx_ring[rxring_idx].rx_buf[i];
Larry Finger0c817332010-12-08 11:12:31 -06001412
Larry Finger38506ec2014-09-22 09:39:19 -05001413 if (!skb)
1414 continue;
1415 pci_unmap_single(rtlpci->pdev, *((dma_addr_t *)skb->cb),
1416 rtlpci->rxbuffersize, PCI_DMA_FROMDEVICE);
1417 kfree_skb(skb);
1418 }
Larry Finger0c817332010-12-08 11:12:31 -06001419
Larry Finger38506ec2014-09-22 09:39:19 -05001420 /* free dma of this ring */
1421 if (rtlpriv->use_new_trx_flow) {
1422 pci_free_consistent(rtlpci->pdev,
1423 sizeof(*rtlpci->rx_ring[rxring_idx].
1424 buffer_desc) * rtlpci->rxringcount,
1425 rtlpci->rx_ring[rxring_idx].buffer_desc,
1426 rtlpci->rx_ring[rxring_idx].dma);
1427 rtlpci->rx_ring[rxring_idx].buffer_desc = NULL;
1428 } else {
1429 pci_free_consistent(rtlpci->pdev,
1430 sizeof(*rtlpci->rx_ring[rxring_idx].desc) *
1431 rtlpci->rxringcount,
1432 rtlpci->rx_ring[rxring_idx].desc,
1433 rtlpci->rx_ring[rxring_idx].dma);
1434 rtlpci->rx_ring[rxring_idx].desc = NULL;
Larry Finger0c817332010-12-08 11:12:31 -06001435 }
1436}
1437
1438static int _rtl_pci_init_trx_ring(struct ieee80211_hw *hw)
1439{
1440 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1441 int ret;
Larry Finger38506ec2014-09-22 09:39:19 -05001442 int i, rxring_idx;
Larry Finger0c817332010-12-08 11:12:31 -06001443
Larry Finger38506ec2014-09-22 09:39:19 -05001444 /* rxring_idx 0:RX_MPDU_QUEUE
1445 * rxring_idx 1:RX_CMD_QUEUE
1446 */
1447 for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++) {
1448 ret = _rtl_pci_init_rx_ring(hw, rxring_idx);
1449 if (ret)
1450 return ret;
1451 }
Larry Finger0c817332010-12-08 11:12:31 -06001452
1453 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
1454 ret = _rtl_pci_init_tx_ring(hw, i,
1455 rtlpci->txringcount[i]);
1456 if (ret)
1457 goto err_free_rings;
1458 }
1459
1460 return 0;
1461
1462err_free_rings:
Larry Finger38506ec2014-09-22 09:39:19 -05001463 for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++)
1464 _rtl_pci_free_rx_ring(hw, rxring_idx);
Larry Finger0c817332010-12-08 11:12:31 -06001465
1466 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
Larry Finger38506ec2014-09-22 09:39:19 -05001467 if (rtlpci->tx_ring[i].desc ||
1468 rtlpci->tx_ring[i].buffer_desc)
Larry Finger0c817332010-12-08 11:12:31 -06001469 _rtl_pci_free_tx_ring(hw, i);
1470
1471 return 1;
1472}
1473
1474static int _rtl_pci_deinit_trx_ring(struct ieee80211_hw *hw)
1475{
Larry Finger38506ec2014-09-22 09:39:19 -05001476 u32 i, rxring_idx;
Larry Finger0c817332010-12-08 11:12:31 -06001477
1478 /*free rx rings */
Larry Finger38506ec2014-09-22 09:39:19 -05001479 for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++)
1480 _rtl_pci_free_rx_ring(hw, rxring_idx);
Larry Finger0c817332010-12-08 11:12:31 -06001481
1482 /*free tx rings */
1483 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
1484 _rtl_pci_free_tx_ring(hw, i);
1485
1486 return 0;
1487}
1488
1489int rtl_pci_reset_trx_ring(struct ieee80211_hw *hw)
1490{
1491 struct rtl_priv *rtlpriv = rtl_priv(hw);
1492 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
Larry Finger38506ec2014-09-22 09:39:19 -05001493 int i, rxring_idx;
Larry Finger0c817332010-12-08 11:12:31 -06001494 unsigned long flags;
1495 u8 tmp_one = 1;
Larry Finger38506ec2014-09-22 09:39:19 -05001496 u32 bufferaddress;
1497 /* rxring_idx 0:RX_MPDU_QUEUE */
1498 /* rxring_idx 1:RX_CMD_QUEUE */
1499 for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++) {
1500 /* force the rx_ring[RX_MPDU_QUEUE/
1501 * RX_CMD_QUEUE].idx to the first one
1502 *new trx flow, do nothing
1503 */
1504 if (!rtlpriv->use_new_trx_flow &&
1505 rtlpci->rx_ring[rxring_idx].desc) {
Larry Finger0c817332010-12-08 11:12:31 -06001506 struct rtl_rx_desc *entry = NULL;
1507
Larry Finger38506ec2014-09-22 09:39:19 -05001508 rtlpci->rx_ring[rxring_idx].idx = 0;
Larry Finger0c817332010-12-08 11:12:31 -06001509 for (i = 0; i < rtlpci->rxringcount; i++) {
Larry Finger38506ec2014-09-22 09:39:19 -05001510 entry = &rtlpci->rx_ring[rxring_idx].desc[i];
1511 bufferaddress =
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -05001512 rtlpriv->cfg->ops->get_desc(hw, (u8 *)entry,
Larry Finger38506ec2014-09-22 09:39:19 -05001513 false , HW_DESC_RXBUFF_ADDR);
1514 memset((u8 *)entry , 0 ,
1515 sizeof(*rtlpci->rx_ring
1516 [rxring_idx].desc));/*clear one entry*/
1517 if (rtlpriv->use_new_trx_flow) {
1518 rtlpriv->cfg->ops->set_desc(hw,
1519 (u8 *)entry, false,
1520 HW_DESC_RX_PREPARE,
1521 (u8 *)&bufferaddress);
1522 } else {
1523 rtlpriv->cfg->ops->set_desc(hw,
1524 (u8 *)entry, false,
1525 HW_DESC_RXBUFF_ADDR,
1526 (u8 *)&bufferaddress);
1527 rtlpriv->cfg->ops->set_desc(hw,
1528 (u8 *)entry, false,
1529 HW_DESC_RXPKT_LEN,
1530 (u8 *)&rtlpci->rxbuffersize);
1531 rtlpriv->cfg->ops->set_desc(hw,
1532 (u8 *)entry, false,
1533 HW_DESC_RXOWN,
1534 (u8 *)&tmp_one);
1535 }
Larry Finger0c817332010-12-08 11:12:31 -06001536 }
Larry Finger38506ec2014-09-22 09:39:19 -05001537 rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
1538 HW_DESC_RXERO, (u8 *)&tmp_one);
Larry Finger0c817332010-12-08 11:12:31 -06001539 }
Larry Finger38506ec2014-09-22 09:39:19 -05001540 rtlpci->rx_ring[rxring_idx].idx = 0;
Larry Finger0c817332010-12-08 11:12:31 -06001541 }
1542
1543 /*
1544 *after reset, release previous pending packet,
1545 *and force the tx idx to the first one
1546 */
Larry Finger38506ec2014-09-22 09:39:19 -05001547 spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
Larry Finger0c817332010-12-08 11:12:31 -06001548 for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
Larry Finger38506ec2014-09-22 09:39:19 -05001549 if (rtlpci->tx_ring[i].desc ||
1550 rtlpci->tx_ring[i].buffer_desc) {
Larry Finger0c817332010-12-08 11:12:31 -06001551 struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[i];
1552
1553 while (skb_queue_len(&ring->queue)) {
Larry Finger38506ec2014-09-22 09:39:19 -05001554 u8 *entry;
1555 struct sk_buff *skb =
1556 __skb_dequeue(&ring->queue);
1557 if (rtlpriv->use_new_trx_flow)
1558 entry = (u8 *)(&ring->buffer_desc
1559 [ring->idx]);
1560 else
1561 entry = (u8 *)(&ring->desc[ring->idx]);
Larry Finger0c817332010-12-08 11:12:31 -06001562
1563 pci_unmap_single(rtlpci->pdev,
Larry Fingerd3bb1422011-04-25 13:23:20 -05001564 rtlpriv->cfg->ops->
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -05001565 get_desc(hw, (u8 *)
Larry Finger0c817332010-12-08 11:12:31 -06001566 entry,
1567 true,
Larry Fingerd3bb1422011-04-25 13:23:20 -05001568 HW_DESC_TXBUFF_ADDR),
Larry Finger0c817332010-12-08 11:12:31 -06001569 skb->len, PCI_DMA_TODEVICE);
wang yanqingcf968932016-05-07 00:33:53 +08001570 dev_kfree_skb_irq(skb);
Larry Finger38506ec2014-09-22 09:39:19 -05001571 ring->idx = (ring->idx + 1) % ring->entries;
Larry Finger0c817332010-12-08 11:12:31 -06001572 }
1573 ring->idx = 0;
1574 }
1575 }
Larry Finger38506ec2014-09-22 09:39:19 -05001576 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
Larry Finger0c817332010-12-08 11:12:31 -06001577
Larry Finger0c817332010-12-08 11:12:31 -06001578 return 0;
1579}
1580
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001581static bool rtl_pci_tx_chk_waitq_insert(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02001582 struct ieee80211_sta *sta,
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001583 struct sk_buff *skb)
Larry Finger0c817332010-12-08 11:12:31 -06001584{
1585 struct rtl_priv *rtlpriv = rtl_priv(hw);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001586 struct rtl_sta_info *sta_entry = NULL;
1587 u8 tid = rtl_get_tid(skb);
Larry Finger0f015452012-10-25 13:46:46 -05001588 __le16 fc = rtl_get_fc(skb);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001589
1590 if (!sta)
1591 return false;
1592 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1593
1594 if (!rtlpriv->rtlhal.earlymode_enable)
1595 return false;
Larry Finger0f015452012-10-25 13:46:46 -05001596 if (ieee80211_is_nullfunc(fc))
1597 return false;
1598 if (ieee80211_is_qos_nullfunc(fc))
1599 return false;
1600 if (ieee80211_is_pspoll(fc))
1601 return false;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001602 if (sta_entry->tids[tid].agg.agg_state != RTL_AGG_OPERATIONAL)
1603 return false;
1604 if (_rtl_mac_to_hwqueue(hw, skb) > VO_QUEUE)
1605 return false;
1606 if (tid > 7)
1607 return false;
1608
1609 /* maybe every tid should be checked */
1610 if (!rtlpriv->link_info.higher_busytxtraffic[tid])
1611 return false;
1612
1613 spin_lock_bh(&rtlpriv->locks.waitq_lock);
1614 skb_queue_tail(&rtlpriv->mac80211.skb_waitq[tid], skb);
1615 spin_unlock_bh(&rtlpriv->locks.waitq_lock);
1616
1617 return true;
1618}
1619
Thomas Huehn36323f82012-07-23 21:33:42 +02001620static int rtl_pci_tx(struct ieee80211_hw *hw,
1621 struct ieee80211_sta *sta,
1622 struct sk_buff *skb,
1623 struct rtl_tcb_desc *ptcb_desc)
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001624{
1625 struct rtl_priv *rtlpriv = rtl_priv(hw);
1626 struct rtl_sta_info *sta_entry = NULL;
1627 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001628 struct rtl8192_tx_ring *ring;
1629 struct rtl_tx_desc *pdesc;
Larry Fingerf3355dd2014-03-04 16:53:47 -06001630 struct rtl_tx_buffer_desc *ptx_bd_desc = NULL;
Larry Finger38506ec2014-09-22 09:39:19 -05001631 u16 idx;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001632 u8 hw_queue = _rtl_mac_to_hwqueue(hw, skb);
Larry Finger0c817332010-12-08 11:12:31 -06001633 unsigned long flags;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001634 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1635 __le16 fc = rtl_get_fc(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001636 u8 *pda_addr = hdr->addr1;
1637 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1638 /*ssn */
Larry Finger0c817332010-12-08 11:12:31 -06001639 u8 tid = 0;
1640 u16 seq_number = 0;
1641 u8 own;
1642 u8 temp_one = 1;
1643
Larry Finger0f015452012-10-25 13:46:46 -05001644 if (ieee80211_is_mgmt(fc))
1645 rtl_tx_mgmt_proc(hw, skb);
Larry Finger0c817332010-12-08 11:12:31 -06001646
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001647 if (rtlpriv->psc.sw_ps_enabled) {
1648 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
1649 !ieee80211_has_pm(fc))
1650 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1651 }
1652
1653 rtl_action_proc(hw, skb, true);
Larry Finger0c817332010-12-08 11:12:31 -06001654
1655 if (is_multicast_ether_addr(pda_addr))
1656 rtlpriv->stats.txbytesmulticast += skb->len;
1657 else if (is_broadcast_ether_addr(pda_addr))
1658 rtlpriv->stats.txbytesbroadcast += skb->len;
1659 else
1660 rtlpriv->stats.txbytesunicast += skb->len;
1661
1662 spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
Larry Finger0c817332010-12-08 11:12:31 -06001663 ring = &rtlpci->tx_ring[hw_queue];
Larry Finger38506ec2014-09-22 09:39:19 -05001664 if (hw_queue != BEACON_QUEUE) {
1665 if (rtlpriv->use_new_trx_flow)
1666 idx = ring->cur_tx_wp;
1667 else
1668 idx = (ring->idx + skb_queue_len(&ring->queue)) %
1669 ring->entries;
1670 } else {
Larry Finger0c817332010-12-08 11:12:31 -06001671 idx = 0;
Larry Finger38506ec2014-09-22 09:39:19 -05001672 }
Larry Finger0c817332010-12-08 11:12:31 -06001673
1674 pdesc = &ring->desc[idx];
Larry Fingerf3355dd2014-03-04 16:53:47 -06001675 if (rtlpriv->use_new_trx_flow) {
1676 ptx_bd_desc = &ring->buffer_desc[idx];
1677 } else {
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -05001678 own = (u8)rtlpriv->cfg->ops->get_desc(hw, (u8 *)pdesc,
Larry Fingerf3355dd2014-03-04 16:53:47 -06001679 true, HW_DESC_OWN);
Larry Finger0c817332010-12-08 11:12:31 -06001680
Larry Fingerf3355dd2014-03-04 16:53:47 -06001681 if ((own == 1) && (hw_queue != BEACON_QUEUE)) {
1682 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Hans Wennborg4f4378d2014-09-05 20:19:50 -07001683 "No more TX desc@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%x\n",
Larry Fingerf3355dd2014-03-04 16:53:47 -06001684 hw_queue, ring->idx, idx,
1685 skb_queue_len(&ring->queue));
Larry Finger0c817332010-12-08 11:12:31 -06001686
Larry Fingerf3355dd2014-03-04 16:53:47 -06001687 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock,
1688 flags);
1689 return skb->len;
1690 }
Larry Finger0c817332010-12-08 11:12:31 -06001691 }
1692
Troy Tand0311312015-02-03 11:15:17 -06001693 if (rtlpriv->cfg->ops->get_available_desc &&
1694 rtlpriv->cfg->ops->get_available_desc(hw, hw_queue) == 0) {
1695 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1696 "get_available_desc fail\n");
1697 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock,
1698 flags);
1699 return skb->len;
1700 }
1701
Larry Finger0c817332010-12-08 11:12:31 -06001702 if (ieee80211_is_data_qos(fc)) {
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001703 tid = rtl_get_tid(skb);
1704 if (sta) {
1705 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1706 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
1707 IEEE80211_SCTL_SEQ) >> 4;
1708 seq_number += 1;
Larry Finger0c817332010-12-08 11:12:31 -06001709
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001710 if (!ieee80211_has_morefrags(hdr->frame_control))
1711 sta_entry->tids[tid].seq_number = seq_number;
1712 }
Larry Finger0c817332010-12-08 11:12:31 -06001713 }
1714
1715 if (ieee80211_is_data(fc))
1716 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
1717
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001718 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc,
Larry Fingerf3355dd2014-03-04 16:53:47 -06001719 (u8 *)ptx_bd_desc, info, sta, skb, hw_queue, ptcb_desc);
Larry Finger0c817332010-12-08 11:12:31 -06001720
1721 __skb_queue_tail(&ring->queue, skb);
1722
Larry Fingerf3355dd2014-03-04 16:53:47 -06001723 if (rtlpriv->use_new_trx_flow) {
1724 rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc, true,
Joe Perches9cb76aa2014-03-24 10:46:20 -07001725 HW_DESC_OWN, &hw_queue);
Larry Fingerf3355dd2014-03-04 16:53:47 -06001726 } else {
1727 rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc, true,
Joe Perches9cb76aa2014-03-24 10:46:20 -07001728 HW_DESC_OWN, &temp_one);
Larry Fingerf3355dd2014-03-04 16:53:47 -06001729 }
Larry Finger0c817332010-12-08 11:12:31 -06001730
1731 if ((ring->entries - skb_queue_len(&ring->queue)) < 2 &&
1732 hw_queue != BEACON_QUEUE) {
Larry Finger0c817332010-12-08 11:12:31 -06001733 RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
Hans Wennborg4f4378d2014-09-05 20:19:50 -07001734 "less desc left, stop skb_queue@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%x\n",
Joe Perchesf30d7502012-01-04 19:40:41 -08001735 hw_queue, ring->idx, idx,
1736 skb_queue_len(&ring->queue));
Larry Finger0c817332010-12-08 11:12:31 -06001737
1738 ieee80211_stop_queue(hw, skb_get_queue_mapping(skb));
1739 }
1740
1741 spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1742
1743 rtlpriv->cfg->ops->tx_polling(hw, hw_queue);
1744
1745 return 0;
1746}
1747
Larry Finger38506ec2014-09-22 09:39:19 -05001748static void rtl_pci_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001749{
1750 struct rtl_priv *rtlpriv = rtl_priv(hw);
1751 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1752 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
Larry Finger26634c42013-03-24 22:06:33 -05001753 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001754 u16 i = 0;
1755 int queue_id;
1756 struct rtl8192_tx_ring *ring;
1757
Larry Finger26634c42013-03-24 22:06:33 -05001758 if (mac->skip_scan)
1759 return;
1760
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001761 for (queue_id = RTL_PCI_MAX_TX_QUEUE_COUNT - 1; queue_id >= 0;) {
1762 u32 queue_len;
Larry Finger38506ec2014-09-22 09:39:19 -05001763
1764 if (((queues >> queue_id) & 0x1) == 0) {
1765 queue_id--;
1766 continue;
1767 }
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001768 ring = &pcipriv->dev.tx_ring[queue_id];
1769 queue_len = skb_queue_len(&ring->queue);
1770 if (queue_len == 0 || queue_id == BEACON_QUEUE ||
1771 queue_id == TXCMD_QUEUE) {
1772 queue_id--;
1773 continue;
1774 } else {
1775 msleep(20);
1776 i++;
1777 }
1778
1779 /* we just wait 1s for all queues */
1780 if (rtlpriv->psc.rfpwr_state == ERFOFF ||
1781 is_hal_stop(rtlhal) || i >= 200)
1782 return;
1783 }
1784}
1785
Larry Fingerd3bb1422011-04-25 13:23:20 -05001786static void rtl_pci_deinit(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -06001787{
1788 struct rtl_priv *rtlpriv = rtl_priv(hw);
1789 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1790
1791 _rtl_pci_deinit_trx_ring(hw);
1792
1793 synchronize_irq(rtlpci->pdev->irq);
1794 tasklet_kill(&rtlpriv->works.irq_tasklet);
Larry Fingera2699132013-03-24 22:06:41 -05001795 cancel_work_sync(&rtlpriv->works.lps_change_work);
Larry Finger0c817332010-12-08 11:12:31 -06001796
1797 flush_workqueue(rtlpriv->works.rtl_wq);
1798 destroy_workqueue(rtlpriv->works.rtl_wq);
1799
1800}
1801
Larry Fingerd3bb1422011-04-25 13:23:20 -05001802static int rtl_pci_init(struct ieee80211_hw *hw, struct pci_dev *pdev)
Larry Finger0c817332010-12-08 11:12:31 -06001803{
Larry Finger0c817332010-12-08 11:12:31 -06001804 int err;
1805
1806 _rtl_pci_init_struct(hw, pdev);
1807
1808 err = _rtl_pci_init_trx_ring(hw);
1809 if (err) {
Larry Fingerb03d9682016-12-15 12:22:58 -06001810 pr_err("tx ring initialization failed\n");
John W. Linville12325282012-02-09 14:48:25 -05001811 return err;
Larry Finger0c817332010-12-08 11:12:31 -06001812 }
1813
John W. Linville12325282012-02-09 14:48:25 -05001814 return 0;
Larry Finger0c817332010-12-08 11:12:31 -06001815}
1816
Larry Fingerd3bb1422011-04-25 13:23:20 -05001817static int rtl_pci_start(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -06001818{
1819 struct rtl_priv *rtlpriv = rtl_priv(hw);
1820 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1821 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1822 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
Ping-Ke Shih8d0d43e2017-02-06 21:29:59 -06001823 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
Larry Finger0c817332010-12-08 11:12:31 -06001824
1825 int err;
1826
1827 rtl_pci_reset_trx_ring(hw);
1828
1829 rtlpci->driver_is_goingto_unload = false;
Larry Finger08054202014-10-23 11:27:09 -05001830 if (rtlpriv->cfg->ops->get_btc_status &&
1831 rtlpriv->cfg->ops->get_btc_status()) {
Ping-Ke Shihf1cb27e2017-06-21 12:15:36 -05001832 rtlpriv->btcoexist.btc_info.ap_num = 36;
Larry Finger38506ec2014-09-22 09:39:19 -05001833 rtlpriv->btcoexist.btc_ops->btc_init_variables(rtlpriv);
1834 rtlpriv->btcoexist.btc_ops->btc_init_hal_vars(rtlpriv);
1835 }
Larry Finger0c817332010-12-08 11:12:31 -06001836 err = rtlpriv->cfg->ops->hw_init(hw);
1837 if (err) {
1838 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001839 "Failed to config hardware!\n");
Larry Finger0c817332010-12-08 11:12:31 -06001840 return err;
1841 }
Ping-Ke Shih8d0d43e2017-02-06 21:29:59 -06001842 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
1843 &rtlmac->retry_long);
Larry Finger0c817332010-12-08 11:12:31 -06001844
1845 rtlpriv->cfg->ops->enable_interrupt(hw);
Joe Perchesf30d7502012-01-04 19:40:41 -08001846 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "enable_interrupt OK\n");
Larry Finger0c817332010-12-08 11:12:31 -06001847
1848 rtl_init_rx_config(hw);
1849
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03001850 /*should be after adapter start and interrupt enable. */
Larry Finger0c817332010-12-08 11:12:31 -06001851 set_hal_start(rtlhal);
1852
1853 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
1854
1855 rtlpci->up_first_time = false;
1856
Larry Finger38506ec2014-09-22 09:39:19 -05001857 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "rtl_pci_start OK\n");
Larry Finger0c817332010-12-08 11:12:31 -06001858 return 0;
1859}
1860
Larry Fingerd3bb1422011-04-25 13:23:20 -05001861static void rtl_pci_stop(struct ieee80211_hw *hw)
Larry Finger0c817332010-12-08 11:12:31 -06001862{
1863 struct rtl_priv *rtlpriv = rtl_priv(hw);
1864 struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1865 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1866 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1867 unsigned long flags;
1868 u8 RFInProgressTimeOut = 0;
1869
Larry Finger38506ec2014-09-22 09:39:19 -05001870 if (rtlpriv->cfg->ops->get_btc_status())
1871 rtlpriv->btcoexist.btc_ops->btc_halt_notify();
1872
Larry Finger0c817332010-12-08 11:12:31 -06001873 /*
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +03001874 *should be before disable interrupt&adapter
Larry Finger0c817332010-12-08 11:12:31 -06001875 *and will do it immediately.
1876 */
1877 set_hal_stop(rtlhal);
1878
Larry Finger9278db62013-12-11 17:13:10 -06001879 rtlpci->driver_is_goingto_unload = true;
Larry Finger0c817332010-12-08 11:12:31 -06001880 rtlpriv->cfg->ops->disable_interrupt(hw);
Larry Fingera2699132013-03-24 22:06:41 -05001881 cancel_work_sync(&rtlpriv->works.lps_change_work);
Larry Finger0c817332010-12-08 11:12:31 -06001882
1883 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1884 while (ppsc->rfchange_inprogress) {
1885 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1886 if (RFInProgressTimeOut > 100) {
1887 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1888 break;
1889 }
1890 mdelay(1);
1891 RFInProgressTimeOut++;
1892 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1893 }
1894 ppsc->rfchange_inprogress = true;
1895 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1896
Larry Finger0c817332010-12-08 11:12:31 -06001897 rtlpriv->cfg->ops->hw_disable(hw);
Larry Fingerb0302ab2012-01-30 09:54:49 -06001898 /* some things are not needed if firmware not available */
1899 if (!rtlpriv->max_fw_size)
1900 return;
Larry Finger0c817332010-12-08 11:12:31 -06001901 rtlpriv->cfg->ops->led_control(hw, LED_CTL_POWER_OFF);
1902
1903 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1904 ppsc->rfchange_inprogress = false;
1905 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1906
1907 rtl_pci_enable_aspm(hw);
1908}
1909
1910static bool _rtl_pci_find_adapter(struct pci_dev *pdev,
1911 struct ieee80211_hw *hw)
1912{
1913 struct rtl_priv *rtlpriv = rtl_priv(hw);
1914 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1915 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1916 struct pci_dev *bridge_pdev = pdev->bus->self;
1917 u16 venderid;
1918 u16 deviceid;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001919 u8 revisionid;
Larry Finger0c817332010-12-08 11:12:31 -06001920 u16 irqline;
1921 u8 tmp;
1922
Chaoming Lifc7707a2011-05-06 15:32:02 -05001923 pcipriv->ndis_adapter.pcibridge_vendor = PCI_BRIDGE_VENDOR_UNKNOWN;
Larry Finger0c817332010-12-08 11:12:31 -06001924 venderid = pdev->vendor;
1925 deviceid = pdev->device;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001926 pci_read_config_byte(pdev, 0x8, &revisionid);
Larry Finger0c817332010-12-08 11:12:31 -06001927 pci_read_config_word(pdev, 0x3C, &irqline);
1928
Larry Fingerfa7ccfb2011-06-18 22:49:53 -05001929 /* PCI ID 0x10ec:0x8192 occurs for both RTL8192E, which uses
1930 * r8192e_pci, and RTL8192SE, which uses this driver. If the
1931 * revision ID is RTL_PCI_REVISION_ID_8192PCIE (0x01), then
1932 * the correct driver is r8192e_pci, thus this routine should
1933 * return false.
1934 */
1935 if (deviceid == RTL_PCI_8192SE_DID &&
1936 revisionid == RTL_PCI_REVISION_ID_8192PCIE)
1937 return false;
1938
Larry Finger0c817332010-12-08 11:12:31 -06001939 if (deviceid == RTL_PCI_8192_DID ||
1940 deviceid == RTL_PCI_0044_DID ||
1941 deviceid == RTL_PCI_0047_DID ||
1942 deviceid == RTL_PCI_8192SE_DID ||
1943 deviceid == RTL_PCI_8174_DID ||
1944 deviceid == RTL_PCI_8173_DID ||
1945 deviceid == RTL_PCI_8172_DID ||
1946 deviceid == RTL_PCI_8171_DID) {
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001947 switch (revisionid) {
Larry Finger0c817332010-12-08 11:12:31 -06001948 case RTL_PCI_REVISION_ID_8192PCIE:
1949 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001950 "8192 PCI-E is found - vid/did=%x/%x\n",
1951 venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001952 rtlhal->hw_type = HARDWARE_TYPE_RTL8192E;
Larry Finger0f015452012-10-25 13:46:46 -05001953 return false;
Larry Finger0c817332010-12-08 11:12:31 -06001954 case RTL_PCI_REVISION_ID_8192SE:
1955 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001956 "8192SE is found - vid/did=%x/%x\n",
1957 venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001958 rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
1959 break;
1960 default:
1961 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Joe Perchesf30d7502012-01-04 19:40:41 -08001962 "Err: Unknown device - vid/did=%x/%x\n",
1963 venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001964 rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
1965 break;
1966
1967 }
Larry Finger0f015452012-10-25 13:46:46 -05001968 } else if (deviceid == RTL_PCI_8723AE_DID) {
1969 rtlhal->hw_type = HARDWARE_TYPE_RTL8723AE;
1970 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
1971 "8723AE PCI-E is found - "
1972 "vid/did=%x/%x\n", venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06001973 } else if (deviceid == RTL_PCI_8192CET_DID ||
1974 deviceid == RTL_PCI_8192CE_DID ||
1975 deviceid == RTL_PCI_8191CE_DID ||
1976 deviceid == RTL_PCI_8188CE_DID) {
1977 rtlhal->hw_type = HARDWARE_TYPE_RTL8192CE;
1978 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001979 "8192C PCI-E is found - vid/did=%x/%x\n",
1980 venderid, deviceid);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05001981 } else if (deviceid == RTL_PCI_8192DE_DID ||
1982 deviceid == RTL_PCI_8192DE_DID2) {
1983 rtlhal->hw_type = HARDWARE_TYPE_RTL8192DE;
1984 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001985 "8192D PCI-E is found - vid/did=%x/%x\n",
1986 venderid, deviceid);
Larry Finger5c691772013-03-24 22:06:56 -05001987 } else if (deviceid == RTL_PCI_8188EE_DID) {
1988 rtlhal->hw_type = HARDWARE_TYPE_RTL8188EE;
1989 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1990 "Find adapter, Hardware type is 8188EE\n");
Larry Finger38506ec2014-09-22 09:39:19 -05001991 } else if (deviceid == RTL_PCI_8723BE_DID) {
1992 rtlhal->hw_type = HARDWARE_TYPE_RTL8723BE;
1993 RT_TRACE(rtlpriv, COMP_INIT , DBG_LOUD,
1994 "Find adapter, Hardware type is 8723BE\n");
1995 } else if (deviceid == RTL_PCI_8192EE_DID) {
1996 rtlhal->hw_type = HARDWARE_TYPE_RTL8192EE;
1997 RT_TRACE(rtlpriv, COMP_INIT , DBG_LOUD,
1998 "Find adapter, Hardware type is 8192EE\n");
1999 } else if (deviceid == RTL_PCI_8821AE_DID) {
2000 rtlhal->hw_type = HARDWARE_TYPE_RTL8821AE;
2001 RT_TRACE(rtlpriv, COMP_INIT , DBG_LOUD,
2002 "Find adapter, Hardware type is 8821AE\n");
2003 } else if (deviceid == RTL_PCI_8812AE_DID) {
2004 rtlhal->hw_type = HARDWARE_TYPE_RTL8812AE;
2005 RT_TRACE(rtlpriv, COMP_INIT , DBG_LOUD,
2006 "Find adapter, Hardware type is 8812AE\n");
Larry Finger0c817332010-12-08 11:12:31 -06002007 } else {
2008 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Joe Perchesf30d7502012-01-04 19:40:41 -08002009 "Err: Unknown device - vid/did=%x/%x\n",
2010 venderid, deviceid);
Larry Finger0c817332010-12-08 11:12:31 -06002011
2012 rtlhal->hw_type = RTL_DEFAULT_HARDWARE_TYPE;
2013 }
2014
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002015 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192DE) {
2016 if (revisionid == 0 || revisionid == 1) {
2017 if (revisionid == 0) {
Joe Perchesf30d7502012-01-04 19:40:41 -08002018 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2019 "Find 92DE MAC0\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002020 rtlhal->interfaceindex = 0;
2021 } else if (revisionid == 1) {
2022 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -08002023 "Find 92DE MAC1\n");
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002024 rtlhal->interfaceindex = 1;
2025 }
2026 } else {
2027 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -08002028 "Unknown device - VendorID/DeviceID=%x/%x, Revision=%x\n",
2029 venderid, deviceid, revisionid);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002030 rtlhal->interfaceindex = 0;
2031 }
2032 }
Larry Finger38506ec2014-09-22 09:39:19 -05002033
2034 /* 92ee use new trx flow */
2035 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE)
2036 rtlpriv->use_new_trx_flow = true;
2037 else
2038 rtlpriv->use_new_trx_flow = false;
2039
Larry Finger0c817332010-12-08 11:12:31 -06002040 /*find bus info */
2041 pcipriv->ndis_adapter.busnumber = pdev->bus->number;
2042 pcipriv->ndis_adapter.devnumber = PCI_SLOT(pdev->devfn);
2043 pcipriv->ndis_adapter.funcnumber = PCI_FUNC(pdev->devfn);
2044
Larry Finger38506ec2014-09-22 09:39:19 -05002045 /*find bridge info */
2046 pcipriv->ndis_adapter.pcibridge_vendor = PCI_BRIDGE_VENDOR_UNKNOWN;
Larry Finger26634c42013-03-24 22:06:33 -05002047 /* some ARM have no bridge_pdev and will crash here
2048 * so we should check if bridge_pdev is NULL
2049 */
Larry Fingerb6b67df2011-07-29 10:53:12 -05002050 if (bridge_pdev) {
2051 /*find bridge info if available */
2052 pcipriv->ndis_adapter.pcibridge_vendorid = bridge_pdev->vendor;
2053 for (tmp = 0; tmp < PCI_BRIDGE_VENDOR_MAX; tmp++) {
2054 if (bridge_pdev->vendor == pcibridge_vendors[tmp]) {
2055 pcipriv->ndis_adapter.pcibridge_vendor = tmp;
2056 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08002057 "Pci Bridge Vendor is found index: %d\n",
2058 tmp);
Larry Fingerb6b67df2011-07-29 10:53:12 -05002059 break;
2060 }
Larry Finger0c817332010-12-08 11:12:31 -06002061 }
2062 }
2063
2064 if (pcipriv->ndis_adapter.pcibridge_vendor !=
2065 PCI_BRIDGE_VENDOR_UNKNOWN) {
2066 pcipriv->ndis_adapter.pcibridge_busnum =
2067 bridge_pdev->bus->number;
2068 pcipriv->ndis_adapter.pcibridge_devnum =
2069 PCI_SLOT(bridge_pdev->devfn);
2070 pcipriv->ndis_adapter.pcibridge_funcnum =
2071 PCI_FUNC(bridge_pdev->devfn);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002072 pcipriv->ndis_adapter.pcibridge_pciehdr_offset =
2073 pci_pcie_cap(bridge_pdev);
Larry Finger0c817332010-12-08 11:12:31 -06002074 pcipriv->ndis_adapter.num4bytes =
2075 (pcipriv->ndis_adapter.pcibridge_pciehdr_offset + 0x10) / 4;
2076
2077 rtl_pci_get_linkcontrol_field(hw);
2078
2079 if (pcipriv->ndis_adapter.pcibridge_vendor ==
2080 PCI_BRIDGE_VENDOR_AMD) {
2081 pcipriv->ndis_adapter.amd_l1_patch =
2082 rtl_pci_get_amd_l1_patch(hw);
2083 }
2084 }
2085
2086 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08002087 "pcidev busnumber:devnumber:funcnumber:vendor:link_ctl %d:%d:%d:%x:%x\n",
2088 pcipriv->ndis_adapter.busnumber,
2089 pcipriv->ndis_adapter.devnumber,
2090 pcipriv->ndis_adapter.funcnumber,
2091 pdev->vendor, pcipriv->ndis_adapter.linkctrl_reg);
Larry Finger0c817332010-12-08 11:12:31 -06002092
2093 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08002094 "pci_bridge busnumber:devnumber:funcnumber:vendor:pcie_cap:link_ctl_reg:amd %d:%d:%d:%x:%x:%x:%x\n",
2095 pcipriv->ndis_adapter.pcibridge_busnum,
2096 pcipriv->ndis_adapter.pcibridge_devnum,
2097 pcipriv->ndis_adapter.pcibridge_funcnum,
2098 pcibridge_vendors[pcipriv->ndis_adapter.pcibridge_vendor],
2099 pcipriv->ndis_adapter.pcibridge_pciehdr_offset,
2100 pcipriv->ndis_adapter.pcibridge_linkctrlreg,
2101 pcipriv->ndis_adapter.amd_l1_patch);
Larry Finger0c817332010-12-08 11:12:31 -06002102
2103 rtl_pci_parse_configuration(pdev, hw);
Larry Finger26634c42013-03-24 22:06:33 -05002104 list_add_tail(&rtlpriv->list, &rtlpriv->glb_var->glb_priv_list);
Larry Finger0c817332010-12-08 11:12:31 -06002105
2106 return true;
2107}
2108
Adam Lee94010fa2014-03-28 11:36:18 +08002109static int rtl_pci_intr_mode_msi(struct ieee80211_hw *hw)
2110{
2111 struct rtl_priv *rtlpriv = rtl_priv(hw);
2112 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
2113 struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
2114 int ret;
2115
2116 ret = pci_enable_msi(rtlpci->pdev);
2117 if (ret < 0)
2118 return ret;
2119
2120 ret = request_irq(rtlpci->pdev->irq, &_rtl_pci_interrupt,
2121 IRQF_SHARED, KBUILD_MODNAME, hw);
2122 if (ret < 0) {
2123 pci_disable_msi(rtlpci->pdev);
2124 return ret;
2125 }
2126
2127 rtlpci->using_msi = true;
2128
2129 RT_TRACE(rtlpriv, COMP_INIT|COMP_INTR, DBG_DMESG,
2130 "MSI Interrupt Mode!\n");
2131 return 0;
2132}
2133
2134static int rtl_pci_intr_mode_legacy(struct ieee80211_hw *hw)
2135{
2136 struct rtl_priv *rtlpriv = rtl_priv(hw);
2137 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
2138 struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
2139 int ret;
2140
2141 ret = request_irq(rtlpci->pdev->irq, &_rtl_pci_interrupt,
2142 IRQF_SHARED, KBUILD_MODNAME, hw);
2143 if (ret < 0)
2144 return ret;
2145
2146 rtlpci->using_msi = false;
2147 RT_TRACE(rtlpriv, COMP_INIT|COMP_INTR, DBG_DMESG,
2148 "Pin-based Interrupt Mode!\n");
2149 return 0;
2150}
2151
2152static int rtl_pci_intr_mode_decide(struct ieee80211_hw *hw)
2153{
2154 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
2155 struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
2156 int ret;
2157
2158 if (rtlpci->msi_support) {
2159 ret = rtl_pci_intr_mode_msi(hw);
2160 if (ret < 0)
2161 ret = rtl_pci_intr_mode_legacy(hw);
2162 } else {
2163 ret = rtl_pci_intr_mode_legacy(hw);
2164 }
2165 return ret;
2166}
2167
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -05002168static void platform_enable_dma64(struct pci_dev *pdev, bool dma64)
2169{
2170 u8 value;
2171
2172 pci_read_config_byte(pdev, 0x719, &value);
2173
2174 /* 0x719 Bit5 is DMA64 bit fetch. */
2175 if (dma64)
2176 value |= BIT(5);
2177 else
2178 value &= ~BIT(5);
2179
2180 pci_write_config_byte(pdev, 0x719, value);
2181}
2182
Bill Pemberton9e2ff362012-12-03 09:56:43 -05002183int rtl_pci_probe(struct pci_dev *pdev,
Larry Finger0c817332010-12-08 11:12:31 -06002184 const struct pci_device_id *id)
2185{
2186 struct ieee80211_hw *hw = NULL;
2187
2188 struct rtl_priv *rtlpriv = NULL;
2189 struct rtl_pci_priv *pcipriv = NULL;
2190 struct rtl_pci *rtlpci;
2191 unsigned long pmem_start, pmem_len, pmem_flags;
2192 int err;
2193
2194 err = pci_enable_device(pdev);
2195 if (err) {
Larry Finger531940f2016-12-15 12:22:57 -06002196 WARN_ONCE(true, "%s : Cannot enable new PCI device\n",
Joe Perches9d833ed2012-01-04 19:40:43 -08002197 pci_name(pdev));
Larry Finger0c817332010-12-08 11:12:31 -06002198 return err;
2199 }
2200
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -05002201 if (((struct rtl_hal_cfg *)id->driver_data)->mod_params->dma64 &&
2202 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
2203 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
2204 WARN_ONCE(true,
2205 "Unable to obtain 64bit DMA for consistent allocations\n");
2206 err = -ENOMEM;
2207 goto fail1;
2208 }
2209
2210 platform_enable_dma64(pdev, true);
2211 } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Larry Finger0c817332010-12-08 11:12:31 -06002212 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
Larry Finger531940f2016-12-15 12:22:57 -06002213 WARN_ONCE(true,
2214 "rtlwifi: Unable to obtain 32bit DMA for consistent allocations\n");
Tim Gardner3d86b932012-02-02 13:48:06 -07002215 err = -ENOMEM;
2216 goto fail1;
Larry Finger0c817332010-12-08 11:12:31 -06002217 }
Ping-Ke Shih0c07bd72017-09-29 14:47:53 -05002218
2219 platform_enable_dma64(pdev, false);
Larry Finger0c817332010-12-08 11:12:31 -06002220 }
2221
2222 pci_set_master(pdev);
2223
2224 hw = ieee80211_alloc_hw(sizeof(struct rtl_pci_priv) +
2225 sizeof(struct rtl_priv), &rtl_ops);
2226 if (!hw) {
Larry Finger531940f2016-12-15 12:22:57 -06002227 WARN_ONCE(true,
Joe Perches9d833ed2012-01-04 19:40:43 -08002228 "%s : ieee80211 alloc failed\n", pci_name(pdev));
Larry Finger0c817332010-12-08 11:12:31 -06002229 err = -ENOMEM;
2230 goto fail1;
2231 }
2232
2233 SET_IEEE80211_DEV(hw, &pdev->dev);
2234 pci_set_drvdata(pdev, hw);
2235
2236 rtlpriv = hw->priv;
Larry Finger26634c42013-03-24 22:06:33 -05002237 rtlpriv->hw = hw;
Larry Finger0c817332010-12-08 11:12:31 -06002238 pcipriv = (void *)rtlpriv->priv;
2239 pcipriv->dev.pdev = pdev;
Larry Fingerb0302ab2012-01-30 09:54:49 -06002240 init_completion(&rtlpriv->firmware_loading_complete);
Larry Finger38506ec2014-09-22 09:39:19 -05002241 /*proximity init here*/
2242 rtlpriv->proximity.proxim_on = false;
2243
2244 pcipriv = (void *)rtlpriv->priv;
2245 pcipriv->dev.pdev = pdev;
Larry Finger0c817332010-12-08 11:12:31 -06002246
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002247 /* init cfg & intf_ops */
2248 rtlpriv->rtlhal.interface = INTF_PCI;
2249 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_data);
2250 rtlpriv->intf_ops = &rtl_pci_ops;
Larry Finger6f334c22013-07-12 15:32:15 -05002251 rtlpriv->glb_var = &rtl_global_var;
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002252
Larry Finger0c817332010-12-08 11:12:31 -06002253 /* MEM map */
2254 err = pci_request_regions(pdev, KBUILD_MODNAME);
2255 if (err) {
Larry Finger531940f2016-12-15 12:22:57 -06002256 WARN_ONCE(true, "rtlwifi: Can't obtain PCI resources\n");
Tim Gardner3d86b932012-02-02 13:48:06 -07002257 goto fail1;
Larry Finger0c817332010-12-08 11:12:31 -06002258 }
2259
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002260 pmem_start = pci_resource_start(pdev, rtlpriv->cfg->bar_id);
2261 pmem_len = pci_resource_len(pdev, rtlpriv->cfg->bar_id);
2262 pmem_flags = pci_resource_flags(pdev, rtlpriv->cfg->bar_id);
Larry Finger0c817332010-12-08 11:12:31 -06002263
2264 /*shared mem start */
2265 rtlpriv->io.pci_mem_start =
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002266 (unsigned long)pci_iomap(pdev,
2267 rtlpriv->cfg->bar_id, pmem_len);
Larry Finger0c817332010-12-08 11:12:31 -06002268 if (rtlpriv->io.pci_mem_start == 0) {
Larry Finger531940f2016-12-15 12:22:57 -06002269 WARN_ONCE(true, "rtlwifi: Can't map PCI mem\n");
Tim Gardner3d86b932012-02-02 13:48:06 -07002270 err = -ENOMEM;
Larry Finger0c817332010-12-08 11:12:31 -06002271 goto fail2;
2272 }
2273
2274 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08002275 "mem mapped space: start: 0x%08lx len:%08lx flags:%08lx, after map:0x%08lx\n",
2276 pmem_start, pmem_len, pmem_flags,
2277 rtlpriv->io.pci_mem_start);
Larry Finger0c817332010-12-08 11:12:31 -06002278
2279 /* Disable Clk Request */
2280 pci_write_config_byte(pdev, 0x81, 0);
2281 /* leave D3 mode */
2282 pci_write_config_byte(pdev, 0x44, 0);
2283 pci_write_config_byte(pdev, 0x04, 0x06);
2284 pci_write_config_byte(pdev, 0x04, 0x07);
2285
Larry Finger0c817332010-12-08 11:12:31 -06002286 /* find adapter */
Tim Gardner3d86b932012-02-02 13:48:06 -07002287 if (!_rtl_pci_find_adapter(pdev, hw)) {
2288 err = -ENODEV;
Malcolm Priestleyfc81bab2017-07-30 09:02:19 +01002289 goto fail2;
Tim Gardner3d86b932012-02-02 13:48:06 -07002290 }
Larry Finger0c817332010-12-08 11:12:31 -06002291
2292 /* Init IO handler */
2293 _rtl_pci_io_handler_init(&pdev->dev, hw);
2294
2295 /*like read eeprom and so on */
2296 rtlpriv->cfg->ops->read_eeprom_info(hw);
2297
Larry Finger7d63a5f2014-11-25 10:32:07 -06002298 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
Larry Fingerb03d9682016-12-15 12:22:58 -06002299 pr_err("Can't init_sw_vars\n");
Larry Finger7d63a5f2014-11-25 10:32:07 -06002300 err = -ENODEV;
2301 goto fail3;
2302 }
2303 rtlpriv->cfg->ops->init_sw_leds(hw);
2304
2305 /*aspm */
2306 rtl_pci_init_aspm(hw);
2307
Larry Finger0c817332010-12-08 11:12:31 -06002308 /* Init mac80211 sw */
2309 err = rtl_init_core(hw);
2310 if (err) {
Larry Fingerb03d9682016-12-15 12:22:58 -06002311 pr_err("Can't allocate sw for mac80211\n");
Larry Finger0c817332010-12-08 11:12:31 -06002312 goto fail3;
2313 }
2314
2315 /* Init PCI sw */
John W. Linville12325282012-02-09 14:48:25 -05002316 err = rtl_pci_init(hw, pdev);
Larry Finger0c817332010-12-08 11:12:31 -06002317 if (err) {
Larry Fingerb03d9682016-12-15 12:22:58 -06002318 pr_err("Failed to init PCI\n");
Larry Finger0c817332010-12-08 11:12:31 -06002319 goto fail3;
2320 }
2321
Larry Finger38506ec2014-09-22 09:39:19 -05002322 err = ieee80211_register_hw(hw);
2323 if (err) {
Larry Fingerb03d9682016-12-15 12:22:58 -06002324 pr_err("Can't register mac80211 hw.\n");
Larry Finger574e02a2012-05-04 08:27:43 -05002325 err = -ENODEV;
2326 goto fail3;
2327 }
Larry Finger38506ec2014-09-22 09:39:19 -05002328 rtlpriv->mac80211.mac80211_registered = 1;
Larry Finger574e02a2012-05-04 08:27:43 -05002329
Larry Finger38506ec2014-09-22 09:39:19 -05002330 /*init rfkill */
2331 rtl_init_rfkill(hw); /* Init PCI sw */
2332
Larry Finger0c817332010-12-08 11:12:31 -06002333 rtlpci = rtl_pcidev(pcipriv);
Adam Lee94010fa2014-03-28 11:36:18 +08002334 err = rtl_pci_intr_mode_decide(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002335 if (err) {
2336 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08002337 "%s: failed to register IRQ handler\n",
2338 wiphy_name(hw->wiphy));
Larry Finger0c817332010-12-08 11:12:31 -06002339 goto fail3;
Larry Finger0c817332010-12-08 11:12:31 -06002340 }
Larry Fingerb0302ab2012-01-30 09:54:49 -06002341 rtlpci->irq_alloc = 1;
Larry Finger0c817332010-12-08 11:12:31 -06002342
Larry Finger38506ec2014-09-22 09:39:19 -05002343 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
Larry Finger0c817332010-12-08 11:12:31 -06002344 return 0;
2345
2346fail3:
Larry Finger38506ec2014-09-22 09:39:19 -05002347 pci_set_drvdata(pdev, NULL);
Larry Finger0c817332010-12-08 11:12:31 -06002348 rtl_deinit_core(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002349
Malcolm Priestleyfc81bab2017-07-30 09:02:19 +01002350fail2:
Larry Finger0c817332010-12-08 11:12:31 -06002351 if (rtlpriv->io.pci_mem_start != 0)
Larry Finger62e63972011-02-11 14:27:46 -06002352 pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
Larry Finger0c817332010-12-08 11:12:31 -06002353
Larry Finger0c817332010-12-08 11:12:31 -06002354 pci_release_regions(pdev);
Larry Fingerb0302ab2012-01-30 09:54:49 -06002355 complete(&rtlpriv->firmware_loading_complete);
Larry Finger0c817332010-12-08 11:12:31 -06002356
2357fail1:
Tim Gardner3d86b932012-02-02 13:48:06 -07002358 if (hw)
2359 ieee80211_free_hw(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002360 pci_disable_device(pdev);
2361
Tim Gardner3d86b932012-02-02 13:48:06 -07002362 return err;
Larry Finger0c817332010-12-08 11:12:31 -06002363
2364}
2365EXPORT_SYMBOL(rtl_pci_probe);
2366
2367void rtl_pci_disconnect(struct pci_dev *pdev)
2368{
2369 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2370 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
2371 struct rtl_priv *rtlpriv = rtl_priv(hw);
2372 struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
2373 struct rtl_mac *rtlmac = rtl_mac(rtlpriv);
2374
Larry Fingerb0302ab2012-01-30 09:54:49 -06002375 /* just in case driver is removed before firmware callback */
2376 wait_for_completion(&rtlpriv->firmware_loading_complete);
Larry Finger0c817332010-12-08 11:12:31 -06002377 clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
2378
Larry Finger0c817332010-12-08 11:12:31 -06002379 /*ieee80211_unregister_hw will call ops_stop */
2380 if (rtlmac->mac80211_registered == 1) {
2381 ieee80211_unregister_hw(hw);
2382 rtlmac->mac80211_registered = 0;
2383 } else {
2384 rtl_deinit_deferred_work(hw);
2385 rtlpriv->intf_ops->adapter_stop(hw);
2386 }
Larry Finger44eb65cf2012-04-19 21:39:06 -05002387 rtlpriv->cfg->ops->disable_interrupt(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002388
2389 /*deinit rfkill */
2390 rtl_deinit_rfkill(hw);
2391
2392 rtl_pci_deinit(hw);
2393 rtl_deinit_core(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002394 rtlpriv->cfg->ops->deinit_sw_vars(hw);
2395
2396 if (rtlpci->irq_alloc) {
2397 free_irq(rtlpci->pdev->irq, hw);
2398 rtlpci->irq_alloc = 0;
2399 }
2400
Adam Lee94010fa2014-03-28 11:36:18 +08002401 if (rtlpci->using_msi)
2402 pci_disable_msi(rtlpci->pdev);
2403
Larry Finger26634c42013-03-24 22:06:33 -05002404 list_del(&rtlpriv->list);
Larry Finger0c817332010-12-08 11:12:31 -06002405 if (rtlpriv->io.pci_mem_start != 0) {
Larry Finger62e63972011-02-11 14:27:46 -06002406 pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
Larry Finger0c817332010-12-08 11:12:31 -06002407 pci_release_regions(pdev);
2408 }
2409
2410 pci_disable_device(pdev);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002411
2412 rtl_pci_disable_aspm(hw);
2413
Larry Finger38506ec2014-09-22 09:39:19 -05002414 pci_set_drvdata(pdev, NULL);
2415
Larry Finger0c817332010-12-08 11:12:31 -06002416 ieee80211_free_hw(hw);
2417}
2418EXPORT_SYMBOL(rtl_pci_disconnect);
2419
Hauke Mehrtens244a77e2012-11-29 23:27:17 +01002420#ifdef CONFIG_PM_SLEEP
Larry Finger0c817332010-12-08 11:12:31 -06002421/***************************************
2422kernel pci power state define:
2423PCI_D0 ((pci_power_t __force) 0)
2424PCI_D1 ((pci_power_t __force) 1)
2425PCI_D2 ((pci_power_t __force) 2)
2426PCI_D3hot ((pci_power_t __force) 3)
2427PCI_D3cold ((pci_power_t __force) 4)
2428PCI_UNKNOWN ((pci_power_t __force) 5)
2429
2430This function is called when system
2431goes into suspend state mac80211 will
2432call rtl_mac_stop() from the mac80211
2433suspend function first, So there is
2434no need to call hw_disable here.
2435****************************************/
Larry Finger603be382011-10-11 21:28:47 -05002436int rtl_pci_suspend(struct device *dev)
Larry Finger0c817332010-12-08 11:12:31 -06002437{
Larry Finger603be382011-10-11 21:28:47 -05002438 struct pci_dev *pdev = to_pci_dev(dev);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002439 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2440 struct rtl_priv *rtlpriv = rtl_priv(hw);
2441
2442 rtlpriv->cfg->ops->hw_suspend(hw);
2443 rtl_deinit_rfkill(hw);
2444
Larry Finger0c817332010-12-08 11:12:31 -06002445 return 0;
2446}
2447EXPORT_SYMBOL(rtl_pci_suspend);
2448
Larry Finger603be382011-10-11 21:28:47 -05002449int rtl_pci_resume(struct device *dev)
Larry Finger0c817332010-12-08 11:12:31 -06002450{
Larry Finger603be382011-10-11 21:28:47 -05002451 struct pci_dev *pdev = to_pci_dev(dev);
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002452 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2453 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002454
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002455 rtlpriv->cfg->ops->hw_resume(hw);
2456 rtl_init_rfkill(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002457 return 0;
2458}
2459EXPORT_SYMBOL(rtl_pci_resume);
Hauke Mehrtens244a77e2012-11-29 23:27:17 +01002460#endif /* CONFIG_PM_SLEEP */
Larry Finger0c817332010-12-08 11:12:31 -06002461
Julia Lawall1bfcfdc2016-05-01 21:57:44 +02002462const struct rtl_intf_ops rtl_pci_ops = {
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002463 .read_efuse_byte = read_efuse_byte,
Larry Finger0c817332010-12-08 11:12:31 -06002464 .adapter_start = rtl_pci_start,
2465 .adapter_stop = rtl_pci_stop,
Larry Finger26634c42013-03-24 22:06:33 -05002466 .check_buddy_priv = rtl_pci_check_buddy_priv,
Larry Finger0c817332010-12-08 11:12:31 -06002467 .adapter_tx = rtl_pci_tx,
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002468 .flush = rtl_pci_flush,
Larry Finger0c817332010-12-08 11:12:31 -06002469 .reset_trx_ring = rtl_pci_reset_trx_ring,
Chaoming_Lic7cfe382011-04-25 13:23:15 -05002470 .waitq_insert = rtl_pci_tx_chk_waitq_insert,
Larry Finger0c817332010-12-08 11:12:31 -06002471
2472 .disable_aspm = rtl_pci_disable_aspm,
2473 .enable_aspm = rtl_pci_enable_aspm,
2474};