xref: /openbmc/linux/drivers/net/wireless/realtek/rtlwifi/pci.c (revision 060f35a317ef09101b128f399dce7ed13d019461)
1d27a76faSLarry Finger // SPDX-License-Identifier: GPL-2.0
2d27a76faSLarry Finger /* Copyright(c) 2009-2012  Realtek Corporation.*/
3f1d2b4d3SLarry Finger 
4f1d2b4d3SLarry Finger #include "wifi.h"
5f1d2b4d3SLarry Finger #include "core.h"
6f1d2b4d3SLarry Finger #include "pci.h"
7f1d2b4d3SLarry Finger #include "base.h"
8f1d2b4d3SLarry Finger #include "ps.h"
9f1d2b4d3SLarry Finger #include "efuse.h"
10f1d2b4d3SLarry Finger #include <linux/interrupt.h>
11f1d2b4d3SLarry Finger #include <linux/export.h>
12f1d2b4d3SLarry Finger #include <linux/module.h>
13f1d2b4d3SLarry Finger 
14f1d2b4d3SLarry Finger MODULE_AUTHOR("lizhaoming	<chaoming_li@realsil.com.cn>");
15f1d2b4d3SLarry Finger MODULE_AUTHOR("Realtek WlanFAE	<wlanfae@realtek.com>");
16f1d2b4d3SLarry Finger MODULE_AUTHOR("Larry Finger	<Larry.FInger@lwfinger.net>");
17f1d2b4d3SLarry Finger MODULE_LICENSE("GPL");
18f1d2b4d3SLarry Finger MODULE_DESCRIPTION("PCI basic driver for rtlwifi");
19f1d2b4d3SLarry Finger 
20f1d2b4d3SLarry Finger static const u16 pcibridge_vendors[PCI_BRIDGE_VENDOR_MAX] = {
21f1d2b4d3SLarry Finger 	INTEL_VENDOR_ID,
22f1d2b4d3SLarry Finger 	ATI_VENDOR_ID,
23f1d2b4d3SLarry Finger 	AMD_VENDOR_ID,
24f1d2b4d3SLarry Finger 	SIS_VENDOR_ID
25f1d2b4d3SLarry Finger };
26f1d2b4d3SLarry Finger 
27f1d2b4d3SLarry Finger static const u8 ac_to_hwq[] = {
28f1d2b4d3SLarry Finger 	VO_QUEUE,
29f1d2b4d3SLarry Finger 	VI_QUEUE,
30f1d2b4d3SLarry Finger 	BE_QUEUE,
31f1d2b4d3SLarry Finger 	BK_QUEUE
32f1d2b4d3SLarry Finger };
33f1d2b4d3SLarry Finger 
_rtl_mac_to_hwqueue(struct ieee80211_hw * hw,struct sk_buff * skb)34ae0122b6SLarry Finger static u8 _rtl_mac_to_hwqueue(struct ieee80211_hw *hw, struct sk_buff *skb)
35f1d2b4d3SLarry Finger {
36f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
37f1d2b4d3SLarry Finger 	__le16 fc = rtl_get_fc(skb);
38f1d2b4d3SLarry Finger 	u8 queue_index = skb_get_queue_mapping(skb);
39e298be2aSPing-Ke Shih 	struct ieee80211_hdr *hdr;
40f1d2b4d3SLarry Finger 
41f1d2b4d3SLarry Finger 	if (unlikely(ieee80211_is_beacon(fc)))
42f1d2b4d3SLarry Finger 		return BEACON_QUEUE;
43f1d2b4d3SLarry Finger 	if (ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc))
44f1d2b4d3SLarry Finger 		return MGNT_QUEUE;
45f1d2b4d3SLarry Finger 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE)
46f1d2b4d3SLarry Finger 		if (ieee80211_is_nullfunc(fc))
47f1d2b4d3SLarry Finger 			return HIGH_QUEUE;
48e298be2aSPing-Ke Shih 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8822BE) {
49e298be2aSPing-Ke Shih 		hdr = rtl_get_hdr(skb);
50e298be2aSPing-Ke Shih 
51e298be2aSPing-Ke Shih 		if (is_multicast_ether_addr(hdr->addr1) ||
52e298be2aSPing-Ke Shih 		    is_broadcast_ether_addr(hdr->addr1))
53e298be2aSPing-Ke Shih 			return HIGH_QUEUE;
54e298be2aSPing-Ke Shih 	}
55f1d2b4d3SLarry Finger 
56f1d2b4d3SLarry Finger 	return ac_to_hwq[queue_index];
57f1d2b4d3SLarry Finger }
58f1d2b4d3SLarry Finger 
59f1d2b4d3SLarry Finger /* Update PCI dependent default settings*/
_rtl_pci_update_default_setting(struct ieee80211_hw * hw)60f1d2b4d3SLarry Finger static void _rtl_pci_update_default_setting(struct ieee80211_hw *hw)
61f1d2b4d3SLarry Finger {
62f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
63f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
64f1d2b4d3SLarry Finger 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
65f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
66f1d2b4d3SLarry Finger 	u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
67f1d2b4d3SLarry Finger 	u8 init_aspm;
68f1d2b4d3SLarry Finger 
69f1d2b4d3SLarry Finger 	ppsc->reg_rfps_level = 0;
70f1d2b4d3SLarry Finger 	ppsc->support_aspm = false;
71f1d2b4d3SLarry Finger 
72f1d2b4d3SLarry Finger 	/*Update PCI ASPM setting */
73f1d2b4d3SLarry Finger 	ppsc->const_amdpci_aspm = rtlpci->const_amdpci_aspm;
74f1d2b4d3SLarry Finger 	switch (rtlpci->const_pci_aspm) {
75f1d2b4d3SLarry Finger 	case 0:
76f1d2b4d3SLarry Finger 		/*No ASPM */
77f1d2b4d3SLarry Finger 		break;
78f1d2b4d3SLarry Finger 
79f1d2b4d3SLarry Finger 	case 1:
80f1d2b4d3SLarry Finger 		/*ASPM dynamically enabled/disable. */
81f1d2b4d3SLarry Finger 		ppsc->reg_rfps_level |= RT_RF_LPS_LEVEL_ASPM;
82f1d2b4d3SLarry Finger 		break;
83f1d2b4d3SLarry Finger 
84f1d2b4d3SLarry Finger 	case 2:
85f1d2b4d3SLarry Finger 		/*ASPM with Clock Req dynamically enabled/disable. */
86f1d2b4d3SLarry Finger 		ppsc->reg_rfps_level |= (RT_RF_LPS_LEVEL_ASPM |
87f1d2b4d3SLarry Finger 					 RT_RF_OFF_LEVL_CLK_REQ);
88f1d2b4d3SLarry Finger 		break;
89f1d2b4d3SLarry Finger 
90f1d2b4d3SLarry Finger 	case 3:
91ae0122b6SLarry Finger 		/* Always enable ASPM and Clock Req
92f1d2b4d3SLarry Finger 		 * from initialization to halt.
93ae0122b6SLarry Finger 		 */
94f1d2b4d3SLarry Finger 		ppsc->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM);
95f1d2b4d3SLarry Finger 		ppsc->reg_rfps_level |= (RT_RF_PS_LEVEL_ALWAYS_ASPM |
96f1d2b4d3SLarry Finger 					 RT_RF_OFF_LEVL_CLK_REQ);
97f1d2b4d3SLarry Finger 		break;
98f1d2b4d3SLarry Finger 
99f1d2b4d3SLarry Finger 	case 4:
100ae0122b6SLarry Finger 		/* Always enable ASPM without Clock Req
101f1d2b4d3SLarry Finger 		 * from initialization to halt.
102ae0122b6SLarry Finger 		 */
103f1d2b4d3SLarry Finger 		ppsc->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM |
104f1d2b4d3SLarry Finger 					  RT_RF_OFF_LEVL_CLK_REQ);
105f1d2b4d3SLarry Finger 		ppsc->reg_rfps_level |= RT_RF_PS_LEVEL_ALWAYS_ASPM;
106f1d2b4d3SLarry Finger 		break;
107f1d2b4d3SLarry Finger 	}
108f1d2b4d3SLarry Finger 
109f1d2b4d3SLarry Finger 	ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
110f1d2b4d3SLarry Finger 
111f1d2b4d3SLarry Finger 	/*Update Radio OFF setting */
112f1d2b4d3SLarry Finger 	switch (rtlpci->const_hwsw_rfoff_d3) {
113f1d2b4d3SLarry Finger 	case 1:
114f1d2b4d3SLarry Finger 		if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
115f1d2b4d3SLarry Finger 			ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
116f1d2b4d3SLarry Finger 		break;
117f1d2b4d3SLarry Finger 
118f1d2b4d3SLarry Finger 	case 2:
119f1d2b4d3SLarry Finger 		if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
120f1d2b4d3SLarry Finger 			ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
121f1d2b4d3SLarry Finger 		ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
122f1d2b4d3SLarry Finger 		break;
123f1d2b4d3SLarry Finger 
124f1d2b4d3SLarry Finger 	case 3:
125f1d2b4d3SLarry Finger 		ppsc->reg_rfps_level |= RT_RF_OFF_LEVL_PCI_D3;
126f1d2b4d3SLarry Finger 		break;
127f1d2b4d3SLarry Finger 	}
128f1d2b4d3SLarry Finger 
129f1d2b4d3SLarry Finger 	/*Set HW definition to determine if it supports ASPM. */
130f1d2b4d3SLarry Finger 	switch (rtlpci->const_support_pciaspm) {
1315f647f4dSLarry Finger 	case 0:
132f1d2b4d3SLarry Finger 		/*Not support ASPM. */
1335f647f4dSLarry Finger 		ppsc->support_aspm = false;
134f1d2b4d3SLarry Finger 		break;
1355f647f4dSLarry Finger 	case 1:
136f1d2b4d3SLarry Finger 		/*Support ASPM. */
1375f647f4dSLarry Finger 		ppsc->support_aspm = true;
1385f647f4dSLarry Finger 		ppsc->support_backdoor = true;
139f1d2b4d3SLarry Finger 		break;
140f1d2b4d3SLarry Finger 	case 2:
141f1d2b4d3SLarry Finger 		/*ASPM value set by chipset. */
1425f647f4dSLarry Finger 		if (pcibridge_vendor == PCI_BRIDGE_VENDOR_INTEL)
1435f647f4dSLarry Finger 			ppsc->support_aspm = true;
144f1d2b4d3SLarry Finger 		break;
145f1d2b4d3SLarry Finger 	default:
146b03d968bSLarry Finger 		pr_err("switch case %#x not processed\n",
147ad574889SJoe Perches 		       rtlpci->const_support_pciaspm);
148f1d2b4d3SLarry Finger 		break;
149f1d2b4d3SLarry Finger 	}
150f1d2b4d3SLarry Finger 
151f1d2b4d3SLarry Finger 	/* toshiba aspm issue, toshiba will set aspm selfly
152ae0122b6SLarry Finger 	 * so we should not set aspm in driver
153ae0122b6SLarry Finger 	 */
154f1d2b4d3SLarry Finger 	pci_read_config_byte(rtlpci->pdev, 0x80, &init_aspm);
155f1d2b4d3SLarry Finger 	if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8192SE &&
156f1d2b4d3SLarry Finger 	    init_aspm == 0x43)
157f1d2b4d3SLarry Finger 		ppsc->support_aspm = false;
158f1d2b4d3SLarry Finger }
159f1d2b4d3SLarry Finger 
_rtl_pci_platform_switch_device_pci_aspm(struct ieee80211_hw * hw,u8 value)160f1d2b4d3SLarry Finger static bool _rtl_pci_platform_switch_device_pci_aspm(
161f1d2b4d3SLarry Finger 			struct ieee80211_hw *hw,
162f1d2b4d3SLarry Finger 			u8 value)
163f1d2b4d3SLarry Finger {
164f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
165f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
166f1d2b4d3SLarry Finger 
167516ec800SIlpo Järvinen 	value &= PCI_EXP_LNKCTL_ASPMC;
168f1d2b4d3SLarry Finger 
169516ec800SIlpo Järvinen 	if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE)
170516ec800SIlpo Järvinen 		value |= PCI_EXP_LNKCTL_CCC;
171516ec800SIlpo Järvinen 
172516ec800SIlpo Järvinen 	pcie_capability_clear_and_set_word(rtlpci->pdev, PCI_EXP_LNKCTL,
173516ec800SIlpo Järvinen 					   PCI_EXP_LNKCTL_ASPMC | value,
174516ec800SIlpo Järvinen 					   value);
175f1d2b4d3SLarry Finger 
176f1d2b4d3SLarry Finger 	return false;
177f1d2b4d3SLarry Finger }
178f1d2b4d3SLarry Finger 
179516ec800SIlpo Järvinen /* @value is PCI_EXP_LNKCTL_CLKREQ_EN or 0 to enable/disable clk request. */
_rtl_pci_switch_clk_req(struct ieee80211_hw * hw,u16 value)180516ec800SIlpo Järvinen static void _rtl_pci_switch_clk_req(struct ieee80211_hw *hw, u16 value)
181f1d2b4d3SLarry Finger {
182f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
183f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
184f1d2b4d3SLarry Finger 
185516ec800SIlpo Järvinen 	value &= PCI_EXP_LNKCTL_CLKREQ_EN;
186516ec800SIlpo Järvinen 
187516ec800SIlpo Järvinen 	pcie_capability_clear_and_set_word(rtlpci->pdev, PCI_EXP_LNKCTL,
188516ec800SIlpo Järvinen 					   PCI_EXP_LNKCTL_CLKREQ_EN,
189516ec800SIlpo Järvinen 					   value);
190f1d2b4d3SLarry Finger 
191f1d2b4d3SLarry Finger 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE)
192f1d2b4d3SLarry Finger 		udelay(100);
193f1d2b4d3SLarry Finger }
194f1d2b4d3SLarry Finger 
195f1d2b4d3SLarry Finger /*Disable RTL8192SE ASPM & Disable Pci Bridge ASPM*/
rtl_pci_disable_aspm(struct ieee80211_hw * hw)196f1d2b4d3SLarry Finger static void rtl_pci_disable_aspm(struct ieee80211_hw *hw)
197f1d2b4d3SLarry Finger {
198f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
199f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
200f1d2b4d3SLarry Finger 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
201f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
202f1d2b4d3SLarry Finger 	u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
203f1d2b4d3SLarry Finger 	/*Retrieve original configuration settings. */
204f1d2b4d3SLarry Finger 	u8 linkctrl_reg = pcipriv->ndis_adapter.linkctrl_reg;
205f1d2b4d3SLarry Finger 	u16 aspmlevel = 0;
206f1d2b4d3SLarry Finger 	u8 tmp_u1b = 0;
207f1d2b4d3SLarry Finger 
208f1d2b4d3SLarry Finger 	if (!ppsc->support_aspm)
209f1d2b4d3SLarry Finger 		return;
210f1d2b4d3SLarry Finger 
211f1d2b4d3SLarry Finger 	if (pcibridge_vendor == PCI_BRIDGE_VENDOR_UNKNOWN) {
212f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_POWER, DBG_TRACE,
213f1d2b4d3SLarry Finger 			"PCI(Bridge) UNKNOWN\n");
214f1d2b4d3SLarry Finger 
215f1d2b4d3SLarry Finger 		return;
216f1d2b4d3SLarry Finger 	}
217f1d2b4d3SLarry Finger 
218f1d2b4d3SLarry Finger 	if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) {
219f1d2b4d3SLarry Finger 		RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_CLK_REQ);
220f1d2b4d3SLarry Finger 		_rtl_pci_switch_clk_req(hw, 0x0);
221f1d2b4d3SLarry Finger 	}
222f1d2b4d3SLarry Finger 
223f1d2b4d3SLarry Finger 	/*for promising device will in L0 state after an I/O. */
224f1d2b4d3SLarry Finger 	pci_read_config_byte(rtlpci->pdev, 0x80, &tmp_u1b);
225f1d2b4d3SLarry Finger 
226f1d2b4d3SLarry Finger 	/*Set corresponding value. */
227f1d2b4d3SLarry Finger 	aspmlevel |= BIT(0) | BIT(1);
228f1d2b4d3SLarry Finger 	linkctrl_reg &= ~aspmlevel;
229f1d2b4d3SLarry Finger 
230f1d2b4d3SLarry Finger 	_rtl_pci_platform_switch_device_pci_aspm(hw, linkctrl_reg);
231f1d2b4d3SLarry Finger }
232f1d2b4d3SLarry Finger 
233ae0122b6SLarry Finger /*Enable RTL8192SE ASPM & Enable Pci Bridge ASPM for
234f1d2b4d3SLarry Finger  *power saving We should follow the sequence to enable
235f1d2b4d3SLarry Finger  *RTL8192SE first then enable Pci Bridge ASPM
236f1d2b4d3SLarry Finger  *or the system will show bluescreen.
237f1d2b4d3SLarry Finger  */
rtl_pci_enable_aspm(struct ieee80211_hw * hw)238f1d2b4d3SLarry Finger static void rtl_pci_enable_aspm(struct ieee80211_hw *hw)
239f1d2b4d3SLarry Finger {
240f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
241f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
242f1d2b4d3SLarry Finger 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
243f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
244f1d2b4d3SLarry Finger 	u8 pcibridge_vendor = pcipriv->ndis_adapter.pcibridge_vendor;
245f1d2b4d3SLarry Finger 	u16 aspmlevel;
246f1d2b4d3SLarry Finger 	u8 u_device_aspmsetting;
247f1d2b4d3SLarry Finger 
248f1d2b4d3SLarry Finger 	if (!ppsc->support_aspm)
249f1d2b4d3SLarry Finger 		return;
250f1d2b4d3SLarry Finger 
251f1d2b4d3SLarry Finger 	if (pcibridge_vendor == PCI_BRIDGE_VENDOR_UNKNOWN) {
252f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_POWER, DBG_TRACE,
253f1d2b4d3SLarry Finger 			"PCI(Bridge) UNKNOWN\n");
254f1d2b4d3SLarry Finger 		return;
255f1d2b4d3SLarry Finger 	}
256f1d2b4d3SLarry Finger 
257f1d2b4d3SLarry Finger 	/*Get ASPM level (with/without Clock Req) */
258f1d2b4d3SLarry Finger 	aspmlevel = rtlpci->const_devicepci_aspm_setting;
259f1d2b4d3SLarry Finger 	u_device_aspmsetting = pcipriv->ndis_adapter.linkctrl_reg;
260f1d2b4d3SLarry Finger 
261f1d2b4d3SLarry Finger 	/*_rtl_pci_platform_switch_device_pci_aspm(dev,*/
262f1d2b4d3SLarry Finger 	/*(priv->ndis_adapter.linkctrl_reg | ASPMLevel)); */
263f1d2b4d3SLarry Finger 
264f1d2b4d3SLarry Finger 	u_device_aspmsetting |= aspmlevel;
265f1d2b4d3SLarry Finger 
266f1d2b4d3SLarry Finger 	_rtl_pci_platform_switch_device_pci_aspm(hw, u_device_aspmsetting);
267f1d2b4d3SLarry Finger 
268f1d2b4d3SLarry Finger 	if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) {
269f1d2b4d3SLarry Finger 		_rtl_pci_switch_clk_req(hw, (ppsc->reg_rfps_level &
270516ec800SIlpo Järvinen 					     RT_RF_OFF_LEVL_CLK_REQ) ?
271516ec800SIlpo Järvinen 					     PCI_EXP_LNKCTL_CLKREQ_EN : 0);
272f1d2b4d3SLarry Finger 		RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_CLK_REQ);
273f1d2b4d3SLarry Finger 	}
274f1d2b4d3SLarry Finger 	udelay(100);
275f1d2b4d3SLarry Finger }
276f1d2b4d3SLarry Finger 
rtl_pci_get_amd_l1_patch(struct ieee80211_hw * hw)277f1d2b4d3SLarry Finger static bool rtl_pci_get_amd_l1_patch(struct ieee80211_hw *hw)
278f1d2b4d3SLarry Finger {
279f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
280f1d2b4d3SLarry Finger 
281f1d2b4d3SLarry Finger 	bool status = false;
282f1d2b4d3SLarry Finger 	u8 offset_e0;
283ae0122b6SLarry Finger 	unsigned int offset_e4;
284f1d2b4d3SLarry Finger 
285f1d2b4d3SLarry Finger 	pci_write_config_byte(rtlpci->pdev, 0xe0, 0xa0);
286f1d2b4d3SLarry Finger 
287f1d2b4d3SLarry Finger 	pci_read_config_byte(rtlpci->pdev, 0xe0, &offset_e0);
288f1d2b4d3SLarry Finger 
289f1d2b4d3SLarry Finger 	if (offset_e0 == 0xA0) {
290f1d2b4d3SLarry Finger 		pci_read_config_dword(rtlpci->pdev, 0xe4, &offset_e4);
291f1d2b4d3SLarry Finger 		if (offset_e4 & BIT(23))
292f1d2b4d3SLarry Finger 			status = true;
293f1d2b4d3SLarry Finger 	}
294f1d2b4d3SLarry Finger 
295f1d2b4d3SLarry Finger 	return status;
296f1d2b4d3SLarry Finger }
297f1d2b4d3SLarry Finger 
rtl_pci_parse_configuration(struct pci_dev * pdev,struct ieee80211_hw * hw)298f1d2b4d3SLarry Finger static void rtl_pci_parse_configuration(struct pci_dev *pdev,
299f1d2b4d3SLarry Finger 					struct ieee80211_hw *hw)
300f1d2b4d3SLarry Finger {
301f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
302f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
303f1d2b4d3SLarry Finger 
304f1d2b4d3SLarry Finger 	u8 tmp;
305f1d2b4d3SLarry Finger 	u16 linkctrl_reg;
306f1d2b4d3SLarry Finger 
307f1d2b4d3SLarry Finger 	/*Link Control Register */
308f1d2b4d3SLarry Finger 	pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &linkctrl_reg);
309f1d2b4d3SLarry Finger 	pcipriv->ndis_adapter.linkctrl_reg = (u8)linkctrl_reg;
310f1d2b4d3SLarry Finger 
311f108a420SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "Link Control Register =%x\n",
312f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.linkctrl_reg);
313f1d2b4d3SLarry Finger 
314f1d2b4d3SLarry Finger 	pci_read_config_byte(pdev, 0x98, &tmp);
315f1d2b4d3SLarry Finger 	tmp |= BIT(4);
316f1d2b4d3SLarry Finger 	pci_write_config_byte(pdev, 0x98, tmp);
317f1d2b4d3SLarry Finger 
318f1d2b4d3SLarry Finger 	tmp = 0x17;
319f1d2b4d3SLarry Finger 	pci_write_config_byte(pdev, 0x70f, tmp);
320f1d2b4d3SLarry Finger }
321f1d2b4d3SLarry Finger 
rtl_pci_init_aspm(struct ieee80211_hw * hw)322f1d2b4d3SLarry Finger static void rtl_pci_init_aspm(struct ieee80211_hw *hw)
323f1d2b4d3SLarry Finger {
324f1d2b4d3SLarry Finger 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
325f1d2b4d3SLarry Finger 
326f1d2b4d3SLarry Finger 	_rtl_pci_update_default_setting(hw);
327f1d2b4d3SLarry Finger 
328f1d2b4d3SLarry Finger 	if (ppsc->reg_rfps_level & RT_RF_PS_LEVEL_ALWAYS_ASPM) {
329f1d2b4d3SLarry Finger 		/*Always enable ASPM & Clock Req. */
330f1d2b4d3SLarry Finger 		rtl_pci_enable_aspm(hw);
331f1d2b4d3SLarry Finger 		RT_SET_PS_LEVEL(ppsc, RT_RF_PS_LEVEL_ALWAYS_ASPM);
332f1d2b4d3SLarry Finger 	}
333f1d2b4d3SLarry Finger }
334f1d2b4d3SLarry Finger 
_rtl_pci_io_handler_init(struct device * dev,struct ieee80211_hw * hw)335f1d2b4d3SLarry Finger static void _rtl_pci_io_handler_init(struct device *dev,
336f1d2b4d3SLarry Finger 				     struct ieee80211_hw *hw)
337f1d2b4d3SLarry Finger {
338f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
339f1d2b4d3SLarry Finger 
340f1d2b4d3SLarry Finger 	rtlpriv->io.dev = dev;
341f1d2b4d3SLarry Finger 
342f1d2b4d3SLarry Finger 	rtlpriv->io.write8_async = pci_write8_async;
343f1d2b4d3SLarry Finger 	rtlpriv->io.write16_async = pci_write16_async;
344f1d2b4d3SLarry Finger 	rtlpriv->io.write32_async = pci_write32_async;
345f1d2b4d3SLarry Finger 
346f1d2b4d3SLarry Finger 	rtlpriv->io.read8_sync = pci_read8_sync;
347f1d2b4d3SLarry Finger 	rtlpriv->io.read16_sync = pci_read16_sync;
348f1d2b4d3SLarry Finger 	rtlpriv->io.read32_sync = pci_read32_sync;
349f1d2b4d3SLarry Finger }
350f1d2b4d3SLarry Finger 
_rtl_update_earlymode_info(struct ieee80211_hw * hw,struct sk_buff * skb,struct rtl_tcb_desc * tcb_desc,u8 tid)351f1d2b4d3SLarry Finger static bool _rtl_update_earlymode_info(struct ieee80211_hw *hw,
352ae0122b6SLarry Finger 				       struct sk_buff *skb,
353ae0122b6SLarry Finger 				       struct rtl_tcb_desc *tcb_desc, u8 tid)
354f1d2b4d3SLarry Finger {
355f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
356f1d2b4d3SLarry Finger 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
357f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
358f1d2b4d3SLarry Finger 	struct sk_buff *next_skb;
359f1d2b4d3SLarry Finger 	u8 additionlen = FCS_LEN;
360f1d2b4d3SLarry Finger 
361f1d2b4d3SLarry Finger 	/* here open is 4, wep/tkip is 8, aes is 12*/
362f1d2b4d3SLarry Finger 	if (info->control.hw_key)
363f1d2b4d3SLarry Finger 		additionlen += info->control.hw_key->icv_len;
364f1d2b4d3SLarry Finger 
365f1d2b4d3SLarry Finger 	/* The most skb num is 6 */
366f1d2b4d3SLarry Finger 	tcb_desc->empkt_num = 0;
367f1d2b4d3SLarry Finger 	spin_lock_bh(&rtlpriv->locks.waitq_lock);
368f1d2b4d3SLarry Finger 	skb_queue_walk(&rtlpriv->mac80211.skb_waitq[tid], next_skb) {
369f1d2b4d3SLarry Finger 		struct ieee80211_tx_info *next_info;
370f1d2b4d3SLarry Finger 
371f1d2b4d3SLarry Finger 		next_info = IEEE80211_SKB_CB(next_skb);
372f1d2b4d3SLarry Finger 		if (next_info->flags & IEEE80211_TX_CTL_AMPDU) {
373f1d2b4d3SLarry Finger 			tcb_desc->empkt_len[tcb_desc->empkt_num] =
374f1d2b4d3SLarry Finger 				next_skb->len + additionlen;
375f1d2b4d3SLarry Finger 			tcb_desc->empkt_num++;
376f1d2b4d3SLarry Finger 		} else {
377f1d2b4d3SLarry Finger 			break;
378f1d2b4d3SLarry Finger 		}
379f1d2b4d3SLarry Finger 
380f1d2b4d3SLarry Finger 		if (skb_queue_is_last(&rtlpriv->mac80211.skb_waitq[tid],
381f1d2b4d3SLarry Finger 				      next_skb))
382f1d2b4d3SLarry Finger 			break;
383f1d2b4d3SLarry Finger 
384f1d2b4d3SLarry Finger 		if (tcb_desc->empkt_num >= rtlhal->max_earlymode_num)
385f1d2b4d3SLarry Finger 			break;
386f1d2b4d3SLarry Finger 	}
387f1d2b4d3SLarry Finger 	spin_unlock_bh(&rtlpriv->locks.waitq_lock);
388f1d2b4d3SLarry Finger 
389f1d2b4d3SLarry Finger 	return true;
390f1d2b4d3SLarry Finger }
391f1d2b4d3SLarry Finger 
392f1d2b4d3SLarry Finger /* just for early mode now */
_rtl_pci_tx_chk_waitq(struct ieee80211_hw * hw)393f1d2b4d3SLarry Finger static void _rtl_pci_tx_chk_waitq(struct ieee80211_hw *hw)
394f1d2b4d3SLarry Finger {
395f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
396f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
397f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
398f1d2b4d3SLarry Finger 	struct sk_buff *skb = NULL;
399f1d2b4d3SLarry Finger 	struct ieee80211_tx_info *info = NULL;
400f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
401f1d2b4d3SLarry Finger 	int tid;
402f1d2b4d3SLarry Finger 
403f1d2b4d3SLarry Finger 	if (!rtlpriv->rtlhal.earlymode_enable)
404f1d2b4d3SLarry Finger 		return;
405f1d2b4d3SLarry Finger 
406ae0122b6SLarry Finger 	/* we just use em for BE/BK/VI/VO */
407f1d2b4d3SLarry Finger 	for (tid = 7; tid >= 0; tid--) {
408f1d2b4d3SLarry Finger 		u8 hw_queue = ac_to_hwq[rtl_tid_to_ac(tid)];
409f1d2b4d3SLarry Finger 		struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
410ae0122b6SLarry Finger 
411f1d2b4d3SLarry Finger 		while (!mac->act_scanning &&
412f1d2b4d3SLarry Finger 		       rtlpriv->psc.rfpwr_state == ERFON) {
413f1d2b4d3SLarry Finger 			struct rtl_tcb_desc tcb_desc;
414ae0122b6SLarry Finger 
415f1d2b4d3SLarry Finger 			memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
416f1d2b4d3SLarry Finger 
41738bb0baeSJeff Xie 			spin_lock(&rtlpriv->locks.waitq_lock);
418f1d2b4d3SLarry Finger 			if (!skb_queue_empty(&mac->skb_waitq[tid]) &&
419f1d2b4d3SLarry Finger 			    (ring->entries - skb_queue_len(&ring->queue) >
420f1d2b4d3SLarry Finger 			     rtlhal->max_earlymode_num)) {
421f1d2b4d3SLarry Finger 				skb = skb_dequeue(&mac->skb_waitq[tid]);
422f1d2b4d3SLarry Finger 			} else {
42338bb0baeSJeff Xie 				spin_unlock(&rtlpriv->locks.waitq_lock);
424f1d2b4d3SLarry Finger 				break;
425f1d2b4d3SLarry Finger 			}
42638bb0baeSJeff Xie 			spin_unlock(&rtlpriv->locks.waitq_lock);
427f1d2b4d3SLarry Finger 
428f1d2b4d3SLarry Finger 			/* Some macaddr can't do early mode. like
429ae0122b6SLarry Finger 			 * multicast/broadcast/no_qos data
430ae0122b6SLarry Finger 			 */
431f1d2b4d3SLarry Finger 			info = IEEE80211_SKB_CB(skb);
432f1d2b4d3SLarry Finger 			if (info->flags & IEEE80211_TX_CTL_AMPDU)
433f1d2b4d3SLarry Finger 				_rtl_update_earlymode_info(hw, skb,
434f1d2b4d3SLarry Finger 							   &tcb_desc, tid);
435f1d2b4d3SLarry Finger 
436f1d2b4d3SLarry Finger 			rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, &tcb_desc);
437f1d2b4d3SLarry Finger 		}
438f1d2b4d3SLarry Finger 	}
439f1d2b4d3SLarry Finger }
440f1d2b4d3SLarry Finger 
_rtl_pci_tx_isr(struct ieee80211_hw * hw,int prio)441f1d2b4d3SLarry Finger static void _rtl_pci_tx_isr(struct ieee80211_hw *hw, int prio)
442f1d2b4d3SLarry Finger {
443f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
444f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
445f1d2b4d3SLarry Finger 
446f1d2b4d3SLarry Finger 	struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[prio];
447f1d2b4d3SLarry Finger 
448f1d2b4d3SLarry Finger 	while (skb_queue_len(&ring->queue)) {
449f1d2b4d3SLarry Finger 		struct sk_buff *skb;
450f1d2b4d3SLarry Finger 		struct ieee80211_tx_info *info;
451f1d2b4d3SLarry Finger 		__le16 fc;
452f1d2b4d3SLarry Finger 		u8 tid;
453f1d2b4d3SLarry Finger 		u8 *entry;
454f1d2b4d3SLarry Finger 
455f1d2b4d3SLarry Finger 		if (rtlpriv->use_new_trx_flow)
456f1d2b4d3SLarry Finger 			entry = (u8 *)(&ring->buffer_desc[ring->idx]);
457f1d2b4d3SLarry Finger 		else
458f1d2b4d3SLarry Finger 			entry = (u8 *)(&ring->desc[ring->idx]);
459f1d2b4d3SLarry Finger 
460f1d2b4d3SLarry Finger 		if (!rtlpriv->cfg->ops->is_tx_desc_closed(hw, prio, ring->idx))
461f1d2b4d3SLarry Finger 			return;
462f1d2b4d3SLarry Finger 		ring->idx = (ring->idx + 1) % ring->entries;
463f1d2b4d3SLarry Finger 
464f1d2b4d3SLarry Finger 		skb = __skb_dequeue(&ring->queue);
4650dc0b5c2SChristophe JAILLET 		dma_unmap_single(&rtlpci->pdev->dev,
4660dc0b5c2SChristophe JAILLET 				 rtlpriv->cfg->ops->get_desc(hw, (u8 *)entry,
4670dc0b5c2SChristophe JAILLET 						true, HW_DESC_TXBUFF_ADDR),
4680dc0b5c2SChristophe JAILLET 				 skb->len, DMA_TO_DEVICE);
469f1d2b4d3SLarry Finger 
470f1d2b4d3SLarry Finger 		/* remove early mode header */
471f1d2b4d3SLarry Finger 		if (rtlpriv->rtlhal.earlymode_enable)
472f1d2b4d3SLarry Finger 			skb_pull(skb, EM_HDR_LEN);
473f1d2b4d3SLarry Finger 
474f108a420SLarry Finger 		rtl_dbg(rtlpriv, (COMP_INTR | COMP_SEND), DBG_TRACE,
475f1d2b4d3SLarry Finger 			"new ring->idx:%d, free: skb_queue_len:%d, free: seq:%x\n",
476f1d2b4d3SLarry Finger 			ring->idx,
477f1d2b4d3SLarry Finger 			skb_queue_len(&ring->queue),
478f1d2b4d3SLarry Finger 			*(u16 *)(skb->data + 22));
479f1d2b4d3SLarry Finger 
480f1d2b4d3SLarry Finger 		if (prio == TXCMD_QUEUE) {
481f1d2b4d3SLarry Finger 			dev_kfree_skb(skb);
482f1d2b4d3SLarry Finger 			goto tx_status_ok;
483f1d2b4d3SLarry Finger 		}
484f1d2b4d3SLarry Finger 
485f1d2b4d3SLarry Finger 		/* for sw LPS, just after NULL skb send out, we can
486f1d2b4d3SLarry Finger 		 * sure AP knows we are sleeping, we should not let
487f1d2b4d3SLarry Finger 		 * rf sleep
488f1d2b4d3SLarry Finger 		 */
489f1d2b4d3SLarry Finger 		fc = rtl_get_fc(skb);
490f1d2b4d3SLarry Finger 		if (ieee80211_is_nullfunc(fc)) {
491f1d2b4d3SLarry Finger 			if (ieee80211_has_pm(fc)) {
492f1d2b4d3SLarry Finger 				rtlpriv->mac80211.offchan_delay = true;
493f1d2b4d3SLarry Finger 				rtlpriv->psc.state_inap = true;
494f1d2b4d3SLarry Finger 			} else {
495f1d2b4d3SLarry Finger 				rtlpriv->psc.state_inap = false;
496f1d2b4d3SLarry Finger 			}
497f1d2b4d3SLarry Finger 		}
498f1d2b4d3SLarry Finger 		if (ieee80211_is_action(fc)) {
499f1d2b4d3SLarry Finger 			struct ieee80211_mgmt *action_frame =
500f1d2b4d3SLarry Finger 				(struct ieee80211_mgmt *)skb->data;
501f1d2b4d3SLarry Finger 			if (action_frame->u.action.u.ht_smps.action ==
502f1d2b4d3SLarry Finger 			    WLAN_HT_ACTION_SMPS) {
503f1d2b4d3SLarry Finger 				dev_kfree_skb(skb);
504f1d2b4d3SLarry Finger 				goto tx_status_ok;
505f1d2b4d3SLarry Finger 			}
506f1d2b4d3SLarry Finger 		}
507f1d2b4d3SLarry Finger 
508f1d2b4d3SLarry Finger 		/* update tid tx pkt num */
509f1d2b4d3SLarry Finger 		tid = rtl_get_tid(skb);
510f1d2b4d3SLarry Finger 		if (tid <= 7)
511f1d2b4d3SLarry Finger 			rtlpriv->link_info.tidtx_inperiod[tid]++;
512f1d2b4d3SLarry Finger 
513f1d2b4d3SLarry Finger 		info = IEEE80211_SKB_CB(skb);
514f1d2b4d3SLarry Finger 
5156acfbb81STzu-En Huang 		if (likely(!ieee80211_is_nullfunc(fc))) {
5166acfbb81STzu-En Huang 			ieee80211_tx_info_clear_status(info);
517f1d2b4d3SLarry Finger 			info->flags |= IEEE80211_TX_STAT_ACK;
518f1d2b4d3SLarry Finger 			/*info->status.rates[0].count = 1; */
519f1d2b4d3SLarry Finger 			ieee80211_tx_status_irqsafe(hw, skb);
5206acfbb81STzu-En Huang 		} else {
5216acfbb81STzu-En Huang 			rtl_tx_ackqueue(hw, skb);
5226acfbb81STzu-En Huang 		}
523f1d2b4d3SLarry Finger 
524f1d2b4d3SLarry Finger 		if ((ring->entries - skb_queue_len(&ring->queue)) <= 4) {
525f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_ERR, DBG_DMESG,
526f1d2b4d3SLarry Finger 				"more desc left, wake skb_queue@%d, ring->idx = %d, skb_queue_len = 0x%x\n",
527f1d2b4d3SLarry Finger 				prio, ring->idx,
528f1d2b4d3SLarry Finger 				skb_queue_len(&ring->queue));
529f1d2b4d3SLarry Finger 
530ae0122b6SLarry Finger 			ieee80211_wake_queue(hw, skb_get_queue_mapping(skb));
531f1d2b4d3SLarry Finger 		}
532f1d2b4d3SLarry Finger tx_status_ok:
533f1d2b4d3SLarry Finger 		skb = NULL;
534f1d2b4d3SLarry Finger 	}
535f1d2b4d3SLarry Finger 
536f1d2b4d3SLarry Finger 	if (((rtlpriv->link_info.num_rx_inperiod +
537f1d2b4d3SLarry Finger 	      rtlpriv->link_info.num_tx_inperiod) > 8) ||
538ae0122b6SLarry Finger 	      rtlpriv->link_info.num_rx_inperiod > 2)
539920872e0SSebastian Andrzej Siewior 		rtl_lps_leave(hw, false);
540f1d2b4d3SLarry Finger }
541f1d2b4d3SLarry Finger 
_rtl_pci_init_one_rxdesc(struct ieee80211_hw * hw,struct sk_buff * new_skb,u8 * entry,int rxring_idx,int desc_idx)542f1d2b4d3SLarry Finger static int _rtl_pci_init_one_rxdesc(struct ieee80211_hw *hw,
543f1d2b4d3SLarry Finger 				    struct sk_buff *new_skb, u8 *entry,
544f1d2b4d3SLarry Finger 				    int rxring_idx, int desc_idx)
545f1d2b4d3SLarry Finger {
546f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
547f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
548f1d2b4d3SLarry Finger 	u32 bufferaddress;
549f1d2b4d3SLarry Finger 	u8 tmp_one = 1;
550f1d2b4d3SLarry Finger 	struct sk_buff *skb;
551f1d2b4d3SLarry Finger 
552f1d2b4d3SLarry Finger 	if (likely(new_skb)) {
553f1d2b4d3SLarry Finger 		skb = new_skb;
554f1d2b4d3SLarry Finger 		goto remap;
555f1d2b4d3SLarry Finger 	}
556f1d2b4d3SLarry Finger 	skb = dev_alloc_skb(rtlpci->rxbuffersize);
557f1d2b4d3SLarry Finger 	if (!skb)
558f1d2b4d3SLarry Finger 		return 0;
559f1d2b4d3SLarry Finger 
560f1d2b4d3SLarry Finger remap:
561f1d2b4d3SLarry Finger 	/* just set skb->cb to mapping addr for pci_unmap_single use */
562f1d2b4d3SLarry Finger 	*((dma_addr_t *)skb->cb) =
5630dc0b5c2SChristophe JAILLET 		dma_map_single(&rtlpci->pdev->dev, skb_tail_pointer(skb),
5640dc0b5c2SChristophe JAILLET 			       rtlpci->rxbuffersize, DMA_FROM_DEVICE);
565f1d2b4d3SLarry Finger 	bufferaddress = *((dma_addr_t *)skb->cb);
5660dc0b5c2SChristophe JAILLET 	if (dma_mapping_error(&rtlpci->pdev->dev, bufferaddress))
567f1d2b4d3SLarry Finger 		return 0;
568f1d2b4d3SLarry Finger 	rtlpci->rx_ring[rxring_idx].rx_buf[desc_idx] = skb;
569f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow) {
5700c07bd74SPing-Ke Shih 		/* skb->cb may be 64 bit address */
571f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
572f1d2b4d3SLarry Finger 					    HW_DESC_RX_PREPARE,
5730c07bd74SPing-Ke Shih 					    (u8 *)(dma_addr_t *)skb->cb);
574f1d2b4d3SLarry Finger 	} else {
575f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
576f1d2b4d3SLarry Finger 					    HW_DESC_RXBUFF_ADDR,
577f1d2b4d3SLarry Finger 					    (u8 *)&bufferaddress);
578f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
579f1d2b4d3SLarry Finger 					    HW_DESC_RXPKT_LEN,
580f1d2b4d3SLarry Finger 					    (u8 *)&rtlpci->rxbuffersize);
581f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
582f1d2b4d3SLarry Finger 					    HW_DESC_RXOWN,
583f1d2b4d3SLarry Finger 					    (u8 *)&tmp_one);
584f1d2b4d3SLarry Finger 	}
585f1d2b4d3SLarry Finger 	return 1;
586f1d2b4d3SLarry Finger }
587f1d2b4d3SLarry Finger 
588f1d2b4d3SLarry Finger /* inorder to receive 8K AMSDU we have set skb to
589f1d2b4d3SLarry Finger  * 9100bytes in init rx ring, but if this packet is
590f1d2b4d3SLarry Finger  * not a AMSDU, this large packet will be sent to
591f1d2b4d3SLarry Finger  * TCP/IP directly, this cause big packet ping fail
592f1d2b4d3SLarry Finger  * like: "ping -s 65507", so here we will realloc skb
593f1d2b4d3SLarry Finger  * based on the true size of packet, Mac80211
594f1d2b4d3SLarry Finger  * Probably will do it better, but does not yet.
595f1d2b4d3SLarry Finger  *
596f1d2b4d3SLarry Finger  * Some platform will fail when alloc skb sometimes.
597f1d2b4d3SLarry Finger  * in this condition, we will send the old skb to
598f1d2b4d3SLarry Finger  * mac80211 directly, this will not cause any other
599f1d2b4d3SLarry Finger  * issues, but only this packet will be lost by TCP/IP
600f1d2b4d3SLarry Finger  */
_rtl_pci_rx_to_mac80211(struct ieee80211_hw * hw,struct sk_buff * skb,struct ieee80211_rx_status rx_status)601f1d2b4d3SLarry Finger static void _rtl_pci_rx_to_mac80211(struct ieee80211_hw *hw,
602f1d2b4d3SLarry Finger 				    struct sk_buff *skb,
603f1d2b4d3SLarry Finger 				    struct ieee80211_rx_status rx_status)
604f1d2b4d3SLarry Finger {
605f1d2b4d3SLarry Finger 	if (unlikely(!rtl_action_proc(hw, skb, false))) {
606f1d2b4d3SLarry Finger 		dev_kfree_skb_any(skb);
607f1d2b4d3SLarry Finger 	} else {
608f1d2b4d3SLarry Finger 		struct sk_buff *uskb = NULL;
609f1d2b4d3SLarry Finger 
610f1d2b4d3SLarry Finger 		uskb = dev_alloc_skb(skb->len + 128);
611f1d2b4d3SLarry Finger 		if (likely(uskb)) {
612f1d2b4d3SLarry Finger 			memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
613f1d2b4d3SLarry Finger 			       sizeof(rx_status));
614b952f4dfSyuan linyu 			skb_put_data(uskb, skb->data, skb->len);
615f1d2b4d3SLarry Finger 			dev_kfree_skb_any(skb);
616f1d2b4d3SLarry Finger 			ieee80211_rx_irqsafe(hw, uskb);
617f1d2b4d3SLarry Finger 		} else {
618f1d2b4d3SLarry Finger 			ieee80211_rx_irqsafe(hw, skb);
619f1d2b4d3SLarry Finger 		}
620f1d2b4d3SLarry Finger 	}
621f1d2b4d3SLarry Finger }
622f1d2b4d3SLarry Finger 
623f1d2b4d3SLarry Finger /*hsisr interrupt handler*/
_rtl_pci_hs_interrupt(struct ieee80211_hw * hw)624f1d2b4d3SLarry Finger static void _rtl_pci_hs_interrupt(struct ieee80211_hw *hw)
625f1d2b4d3SLarry Finger {
626f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
627f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
628f1d2b4d3SLarry Finger 
629f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[MAC_HSISR],
630f1d2b4d3SLarry Finger 		       rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[MAC_HSISR]) |
631f1d2b4d3SLarry Finger 		       rtlpci->sys_irq_mask);
632f1d2b4d3SLarry Finger }
633f1d2b4d3SLarry Finger 
_rtl_pci_rx_interrupt(struct ieee80211_hw * hw)634f1d2b4d3SLarry Finger static void _rtl_pci_rx_interrupt(struct ieee80211_hw *hw)
635f1d2b4d3SLarry Finger {
636f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
637f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
638f1d2b4d3SLarry Finger 	int rxring_idx = RTL_PCI_RX_MPDU_QUEUE;
639f1d2b4d3SLarry Finger 	struct ieee80211_rx_status rx_status = { 0 };
640f1d2b4d3SLarry Finger 	unsigned int count = rtlpci->rxringcount;
641f1d2b4d3SLarry Finger 	u8 own;
642f1d2b4d3SLarry Finger 	u8 tmp_one;
643f1d2b4d3SLarry Finger 	bool unicast = false;
644f1d2b4d3SLarry Finger 	u8 hw_queue = 0;
645fb9829e9SPing-Ke Shih 	unsigned int rx_remained_cnt = 0;
646f1d2b4d3SLarry Finger 	struct rtl_stats stats = {
647f1d2b4d3SLarry Finger 		.signal = 0,
648f1d2b4d3SLarry Finger 		.rate = 0,
649f1d2b4d3SLarry Finger 	};
650f1d2b4d3SLarry Finger 
651f1d2b4d3SLarry Finger 	/*RX NORMAL PKT */
652f1d2b4d3SLarry Finger 	while (count--) {
653f1d2b4d3SLarry Finger 		struct ieee80211_hdr *hdr;
654f1d2b4d3SLarry Finger 		__le16 fc;
655f1d2b4d3SLarry Finger 		u16 len;
656f1d2b4d3SLarry Finger 		/*rx buffer descriptor */
657f1d2b4d3SLarry Finger 		struct rtl_rx_buffer_desc *buffer_desc = NULL;
658f1d2b4d3SLarry Finger 		/*if use new trx flow, it means wifi info */
659f1d2b4d3SLarry Finger 		struct rtl_rx_desc *pdesc = NULL;
660f1d2b4d3SLarry Finger 		/*rx pkt */
661f1d2b4d3SLarry Finger 		struct sk_buff *skb = rtlpci->rx_ring[rxring_idx].rx_buf[
662f1d2b4d3SLarry Finger 				      rtlpci->rx_ring[rxring_idx].idx];
663f1d2b4d3SLarry Finger 		struct sk_buff *new_skb;
664f1d2b4d3SLarry Finger 
665f1d2b4d3SLarry Finger 		if (rtlpriv->use_new_trx_flow) {
666fb9829e9SPing-Ke Shih 			if (rx_remained_cnt == 0)
667f1d2b4d3SLarry Finger 				rx_remained_cnt =
668f1d2b4d3SLarry Finger 				rtlpriv->cfg->ops->rx_desc_buff_remained_cnt(hw,
669f1d2b4d3SLarry Finger 								      hw_queue);
670f1d2b4d3SLarry Finger 			if (rx_remained_cnt == 0)
671f1d2b4d3SLarry Finger 				return;
672f99551a2SLarry Finger 			buffer_desc = &rtlpci->rx_ring[rxring_idx].buffer_desc[
673f99551a2SLarry Finger 				rtlpci->rx_ring[rxring_idx].idx];
674f99551a2SLarry Finger 			pdesc = (struct rtl_rx_desc *)skb->data;
675f1d2b4d3SLarry Finger 		} else {	/* rx descriptor */
676f1d2b4d3SLarry Finger 			pdesc = &rtlpci->rx_ring[rxring_idx].desc[
677f1d2b4d3SLarry Finger 				rtlpci->rx_ring[rxring_idx].idx];
678f1d2b4d3SLarry Finger 
6790c07bd74SPing-Ke Shih 			own = (u8)rtlpriv->cfg->ops->get_desc(hw, (u8 *)pdesc,
680f1d2b4d3SLarry Finger 							      false,
681f1d2b4d3SLarry Finger 							      HW_DESC_OWN);
682f1d2b4d3SLarry Finger 			if (own) /* wait data to be filled by hardware */
683f1d2b4d3SLarry Finger 				return;
684f1d2b4d3SLarry Finger 		}
685f1d2b4d3SLarry Finger 
686f1d2b4d3SLarry Finger 		/* Reaching this point means: data is filled already
687f1d2b4d3SLarry Finger 		 * AAAAAAttention !!!
688f1d2b4d3SLarry Finger 		 * We can NOT access 'skb' before 'pci_unmap_single'
689f1d2b4d3SLarry Finger 		 */
6900dc0b5c2SChristophe JAILLET 		dma_unmap_single(&rtlpci->pdev->dev, *((dma_addr_t *)skb->cb),
6910dc0b5c2SChristophe JAILLET 				 rtlpci->rxbuffersize, DMA_FROM_DEVICE);
692f1d2b4d3SLarry Finger 
693f1d2b4d3SLarry Finger 		/* get a new skb - if fail, old one will be reused */
694f1d2b4d3SLarry Finger 		new_skb = dev_alloc_skb(rtlpci->rxbuffersize);
695f1d2b4d3SLarry Finger 		if (unlikely(!new_skb))
696f1d2b4d3SLarry Finger 			goto no_new;
697f1d2b4d3SLarry Finger 		memset(&rx_status, 0, sizeof(rx_status));
698f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->query_rx_desc(hw, &stats,
699f1d2b4d3SLarry Finger 						 &rx_status, (u8 *)pdesc, skb);
700f1d2b4d3SLarry Finger 
701f1d2b4d3SLarry Finger 		if (rtlpriv->use_new_trx_flow)
702f1d2b4d3SLarry Finger 			rtlpriv->cfg->ops->rx_check_dma_ok(hw,
703f1d2b4d3SLarry Finger 							   (u8 *)buffer_desc,
704f1d2b4d3SLarry Finger 							   hw_queue);
705f1d2b4d3SLarry Finger 
7060c07bd74SPing-Ke Shih 		len = rtlpriv->cfg->ops->get_desc(hw, (u8 *)pdesc, false,
707f1d2b4d3SLarry Finger 						  HW_DESC_RXPKT_LEN);
708f1d2b4d3SLarry Finger 
709f1d2b4d3SLarry Finger 		if (skb->end - skb->tail > len) {
710f1d2b4d3SLarry Finger 			skb_put(skb, len);
711f1d2b4d3SLarry Finger 			if (rtlpriv->use_new_trx_flow)
712f1d2b4d3SLarry Finger 				skb_reserve(skb, stats.rx_drvinfo_size +
713f1d2b4d3SLarry Finger 					    stats.rx_bufshift + 24);
714f1d2b4d3SLarry Finger 			else
715f1d2b4d3SLarry Finger 				skb_reserve(skb, stats.rx_drvinfo_size +
716f1d2b4d3SLarry Finger 					    stats.rx_bufshift);
717f1d2b4d3SLarry Finger 		} else {
718f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
719f1d2b4d3SLarry Finger 				"skb->end - skb->tail = %d, len is %d\n",
720f1d2b4d3SLarry Finger 				skb->end - skb->tail, len);
721f1d2b4d3SLarry Finger 			dev_kfree_skb_any(skb);
722f1d2b4d3SLarry Finger 			goto new_trx_end;
723f1d2b4d3SLarry Finger 		}
724f1d2b4d3SLarry Finger 		/* handle command packet here */
72523ffab17SPing-Ke Shih 		if (stats.packet_report_type == C2H_PACKET) {
7269ae6ed27SPing-Ke Shih 			rtl_c2hcmd_enqueue(hw, skb);
727f1d2b4d3SLarry Finger 			goto new_trx_end;
728f1d2b4d3SLarry Finger 		}
729f1d2b4d3SLarry Finger 
730ae0122b6SLarry Finger 		/* NOTICE This can not be use for mac80211,
731f1d2b4d3SLarry Finger 		 * this is done in mac80211 code,
732f1d2b4d3SLarry Finger 		 * if done here sec DHCP will fail
733f1d2b4d3SLarry Finger 		 * skb_trim(skb, skb->len - 4);
734f1d2b4d3SLarry Finger 		 */
735f1d2b4d3SLarry Finger 
736f1d2b4d3SLarry Finger 		hdr = rtl_get_hdr(skb);
737f1d2b4d3SLarry Finger 		fc = rtl_get_fc(skb);
738f1d2b4d3SLarry Finger 
739b43f4a16SLarry Finger 		if (!stats.crc && !stats.hwerror && (skb->len > FCS_LEN)) {
740f1d2b4d3SLarry Finger 			memcpy(IEEE80211_SKB_RXCB(skb), &rx_status,
741f1d2b4d3SLarry Finger 			       sizeof(rx_status));
742f1d2b4d3SLarry Finger 
743f1d2b4d3SLarry Finger 			if (is_broadcast_ether_addr(hdr->addr1)) {
744f1d2b4d3SLarry Finger 				;/*TODO*/
745f1d2b4d3SLarry Finger 			} else if (is_multicast_ether_addr(hdr->addr1)) {
746f1d2b4d3SLarry Finger 				;/*TODO*/
747f1d2b4d3SLarry Finger 			} else {
748f1d2b4d3SLarry Finger 				unicast = true;
749f1d2b4d3SLarry Finger 				rtlpriv->stats.rxbytesunicast += skb->len;
750f1d2b4d3SLarry Finger 			}
751f1d2b4d3SLarry Finger 			rtl_is_special_data(hw, skb, false, true);
752f1d2b4d3SLarry Finger 
753f1d2b4d3SLarry Finger 			if (ieee80211_is_data(fc)) {
754f1d2b4d3SLarry Finger 				rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
755f1d2b4d3SLarry Finger 				if (unicast)
756f1d2b4d3SLarry Finger 					rtlpriv->link_info.num_rx_inperiod++;
757f1d2b4d3SLarry Finger 			}
758c76ab8e7SPing-Ke Shih 
759c76ab8e7SPing-Ke Shih 			rtl_collect_scan_list(hw, skb);
760c76ab8e7SPing-Ke Shih 
761f1d2b4d3SLarry Finger 			/* static bcn for roaming */
762f1d2b4d3SLarry Finger 			rtl_beacon_statistic(hw, skb);
763f1d2b4d3SLarry Finger 			rtl_p2p_info(hw, (void *)skb->data, skb->len);
764f1d2b4d3SLarry Finger 			/* for sw lps */
765f1d2b4d3SLarry Finger 			rtl_swlps_beacon(hw, (void *)skb->data, skb->len);
766f1d2b4d3SLarry Finger 			rtl_recognize_peer(hw, (void *)skb->data, skb->len);
767ae0122b6SLarry Finger 			if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP &&
768ae0122b6SLarry Finger 			    rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G &&
769f1d2b4d3SLarry Finger 			    (ieee80211_is_beacon(fc) ||
770f1d2b4d3SLarry Finger 			     ieee80211_is_probe_resp(fc))) {
771f1d2b4d3SLarry Finger 				dev_kfree_skb_any(skb);
772f1d2b4d3SLarry Finger 			} else {
773f1d2b4d3SLarry Finger 				_rtl_pci_rx_to_mac80211(hw, skb, rx_status);
774f1d2b4d3SLarry Finger 			}
775f1d2b4d3SLarry Finger 		} else {
776b43f4a16SLarry Finger 			/* drop packets with errors or those too short */
777f1d2b4d3SLarry Finger 			dev_kfree_skb_any(skb);
778f1d2b4d3SLarry Finger 		}
779f1d2b4d3SLarry Finger new_trx_end:
780f1d2b4d3SLarry Finger 		if (rtlpriv->use_new_trx_flow) {
781f1d2b4d3SLarry Finger 			rtlpci->rx_ring[hw_queue].next_rx_rp += 1;
782f1d2b4d3SLarry Finger 			rtlpci->rx_ring[hw_queue].next_rx_rp %=
783f1d2b4d3SLarry Finger 					RTL_PCI_MAX_RX_COUNT;
784f1d2b4d3SLarry Finger 
785f1d2b4d3SLarry Finger 			rx_remained_cnt--;
786f1d2b4d3SLarry Finger 			rtl_write_word(rtlpriv, 0x3B4,
787f1d2b4d3SLarry Finger 				       rtlpci->rx_ring[hw_queue].next_rx_rp);
788f1d2b4d3SLarry Finger 		}
789f1d2b4d3SLarry Finger 		if (((rtlpriv->link_info.num_rx_inperiod +
790f1d2b4d3SLarry Finger 		      rtlpriv->link_info.num_tx_inperiod) > 8) ||
791ae0122b6SLarry Finger 		      rtlpriv->link_info.num_rx_inperiod > 2)
792920872e0SSebastian Andrzej Siewior 			rtl_lps_leave(hw, false);
793f1d2b4d3SLarry Finger 		skb = new_skb;
794f1d2b4d3SLarry Finger no_new:
795f1d2b4d3SLarry Finger 		if (rtlpriv->use_new_trx_flow) {
796f1d2b4d3SLarry Finger 			_rtl_pci_init_one_rxdesc(hw, skb, (u8 *)buffer_desc,
797f1d2b4d3SLarry Finger 						 rxring_idx,
798f1d2b4d3SLarry Finger 						 rtlpci->rx_ring[rxring_idx].idx);
799f1d2b4d3SLarry Finger 		} else {
800f1d2b4d3SLarry Finger 			_rtl_pci_init_one_rxdesc(hw, skb, (u8 *)pdesc,
801f1d2b4d3SLarry Finger 						 rxring_idx,
802f1d2b4d3SLarry Finger 						 rtlpci->rx_ring[rxring_idx].idx);
803f1d2b4d3SLarry Finger 			if (rtlpci->rx_ring[rxring_idx].idx ==
804f1d2b4d3SLarry Finger 			    rtlpci->rxringcount - 1)
805f1d2b4d3SLarry Finger 				rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc,
806f1d2b4d3SLarry Finger 							    false,
807f1d2b4d3SLarry Finger 							    HW_DESC_RXERO,
808f1d2b4d3SLarry Finger 							    (u8 *)&tmp_one);
809f1d2b4d3SLarry Finger 		}
810f1d2b4d3SLarry Finger 		rtlpci->rx_ring[rxring_idx].idx =
811f1d2b4d3SLarry Finger 				(rtlpci->rx_ring[rxring_idx].idx + 1) %
812f1d2b4d3SLarry Finger 				rtlpci->rxringcount;
813f1d2b4d3SLarry Finger 	}
814f1d2b4d3SLarry Finger }
815f1d2b4d3SLarry Finger 
_rtl_pci_interrupt(int irq,void * dev_id)816f1d2b4d3SLarry Finger static irqreturn_t _rtl_pci_interrupt(int irq, void *dev_id)
817f1d2b4d3SLarry Finger {
818f1d2b4d3SLarry Finger 	struct ieee80211_hw *hw = dev_id;
819f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
820f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
821f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
822f1d2b4d3SLarry Finger 	unsigned long flags;
82378aa6012SLarry Finger 	struct rtl_int intvec = {0};
82478aa6012SLarry Finger 
825f1d2b4d3SLarry Finger 	irqreturn_t ret = IRQ_HANDLED;
826f1d2b4d3SLarry Finger 
827f1d2b4d3SLarry Finger 	if (rtlpci->irq_enabled == 0)
828f1d2b4d3SLarry Finger 		return ret;
829f1d2b4d3SLarry Finger 
830f1d2b4d3SLarry Finger 	spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
831f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->disable_interrupt(hw);
832f1d2b4d3SLarry Finger 
833f1d2b4d3SLarry Finger 	/*read ISR: 4/8bytes */
83478aa6012SLarry Finger 	rtlpriv->cfg->ops->interrupt_recognized(hw, &intvec);
835f1d2b4d3SLarry Finger 
836ae0122b6SLarry Finger 	/*Shared IRQ or HW disappeared */
83778aa6012SLarry Finger 	if (!intvec.inta || intvec.inta == 0xffff)
838f1d2b4d3SLarry Finger 		goto done;
839f1d2b4d3SLarry Finger 
840f1d2b4d3SLarry Finger 	/*<1> beacon related */
84178aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_TBDOK])
842f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
843f1d2b4d3SLarry Finger 			"beacon ok interrupt!\n");
844f1d2b4d3SLarry Finger 
84578aa6012SLarry Finger 	if (unlikely(intvec.inta & rtlpriv->cfg->maps[RTL_IMR_TBDER]))
846f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
847f1d2b4d3SLarry Finger 			"beacon err interrupt!\n");
848f1d2b4d3SLarry Finger 
84978aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_BDOK])
850f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE, "beacon interrupt!\n");
851f1d2b4d3SLarry Finger 
85278aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_BCNINT]) {
853f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
854f1d2b4d3SLarry Finger 			"prepare beacon for interrupt!\n");
855f1d2b4d3SLarry Finger 		tasklet_schedule(&rtlpriv->works.irq_prepare_bcn_tasklet);
856f1d2b4d3SLarry Finger 	}
857f1d2b4d3SLarry Finger 
858f1d2b4d3SLarry Finger 	/*<2> Tx related */
85978aa6012SLarry Finger 	if (unlikely(intvec.intb & rtlpriv->cfg->maps[RTL_IMR_TXFOVW]))
860f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING, "IMR_TXFOVW!\n");
861f1d2b4d3SLarry Finger 
86278aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_MGNTDOK]) {
863f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
864f1d2b4d3SLarry Finger 			"Manage ok interrupt!\n");
865f1d2b4d3SLarry Finger 		_rtl_pci_tx_isr(hw, MGNT_QUEUE);
866f1d2b4d3SLarry Finger 	}
867f1d2b4d3SLarry Finger 
86878aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_HIGHDOK]) {
869f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
870f1d2b4d3SLarry Finger 			"HIGH_QUEUE ok interrupt!\n");
871f1d2b4d3SLarry Finger 		_rtl_pci_tx_isr(hw, HIGH_QUEUE);
872f1d2b4d3SLarry Finger 	}
873f1d2b4d3SLarry Finger 
87478aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_BKDOK]) {
875f1d2b4d3SLarry Finger 		rtlpriv->link_info.num_tx_inperiod++;
876f1d2b4d3SLarry Finger 
877f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
878f1d2b4d3SLarry Finger 			"BK Tx OK interrupt!\n");
879f1d2b4d3SLarry Finger 		_rtl_pci_tx_isr(hw, BK_QUEUE);
880f1d2b4d3SLarry Finger 	}
881f1d2b4d3SLarry Finger 
88278aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_BEDOK]) {
883f1d2b4d3SLarry Finger 		rtlpriv->link_info.num_tx_inperiod++;
884f1d2b4d3SLarry Finger 
885f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
886f1d2b4d3SLarry Finger 			"BE TX OK interrupt!\n");
887f1d2b4d3SLarry Finger 		_rtl_pci_tx_isr(hw, BE_QUEUE);
888f1d2b4d3SLarry Finger 	}
889f1d2b4d3SLarry Finger 
89078aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_VIDOK]) {
891f1d2b4d3SLarry Finger 		rtlpriv->link_info.num_tx_inperiod++;
892f1d2b4d3SLarry Finger 
893f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
894f1d2b4d3SLarry Finger 			"VI TX OK interrupt!\n");
895f1d2b4d3SLarry Finger 		_rtl_pci_tx_isr(hw, VI_QUEUE);
896f1d2b4d3SLarry Finger 	}
897f1d2b4d3SLarry Finger 
89878aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_VODOK]) {
899f1d2b4d3SLarry Finger 		rtlpriv->link_info.num_tx_inperiod++;
900f1d2b4d3SLarry Finger 
901f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
902f1d2b4d3SLarry Finger 			"Vo TX OK interrupt!\n");
903f1d2b4d3SLarry Finger 		_rtl_pci_tx_isr(hw, VO_QUEUE);
904f1d2b4d3SLarry Finger 	}
905f1d2b4d3SLarry Finger 
90689d3e8abSPing-Ke Shih 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8822BE) {
90778aa6012SLarry Finger 		if (intvec.intd & rtlpriv->cfg->maps[RTL_IMR_H2CDOK]) {
90889d3e8abSPing-Ke Shih 			rtlpriv->link_info.num_tx_inperiod++;
90989d3e8abSPing-Ke Shih 
910f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
91189d3e8abSPing-Ke Shih 				"H2C TX OK interrupt!\n");
91289d3e8abSPing-Ke Shih 			_rtl_pci_tx_isr(hw, H2C_QUEUE);
91389d3e8abSPing-Ke Shih 		}
91489d3e8abSPing-Ke Shih 	}
91589d3e8abSPing-Ke Shih 
916f1d2b4d3SLarry Finger 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
91778aa6012SLarry Finger 		if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_COMDOK]) {
918f1d2b4d3SLarry Finger 			rtlpriv->link_info.num_tx_inperiod++;
919f1d2b4d3SLarry Finger 
920f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
921f1d2b4d3SLarry Finger 				"CMD TX OK interrupt!\n");
922f1d2b4d3SLarry Finger 			_rtl_pci_tx_isr(hw, TXCMD_QUEUE);
923f1d2b4d3SLarry Finger 		}
924f1d2b4d3SLarry Finger 	}
925f1d2b4d3SLarry Finger 
926f1d2b4d3SLarry Finger 	/*<3> Rx related */
92778aa6012SLarry Finger 	if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_ROK]) {
928f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE, "Rx ok interrupt!\n");
929f1d2b4d3SLarry Finger 		_rtl_pci_rx_interrupt(hw);
930f1d2b4d3SLarry Finger 	}
931f1d2b4d3SLarry Finger 
93278aa6012SLarry Finger 	if (unlikely(intvec.inta & rtlpriv->cfg->maps[RTL_IMR_RDU])) {
933f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
934f1d2b4d3SLarry Finger 			"rx descriptor unavailable!\n");
935f1d2b4d3SLarry Finger 		_rtl_pci_rx_interrupt(hw);
936f1d2b4d3SLarry Finger 	}
937f1d2b4d3SLarry Finger 
93878aa6012SLarry Finger 	if (unlikely(intvec.intb & rtlpriv->cfg->maps[RTL_IMR_RXFOVW])) {
939f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING, "rx overflow !\n");
940f1d2b4d3SLarry Finger 		_rtl_pci_rx_interrupt(hw);
941f1d2b4d3SLarry Finger 	}
942f1d2b4d3SLarry Finger 
943f1d2b4d3SLarry Finger 	/*<4> fw related*/
944f1d2b4d3SLarry Finger 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723AE) {
94578aa6012SLarry Finger 		if (intvec.inta & rtlpriv->cfg->maps[RTL_IMR_C2HCMD]) {
946f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
947f1d2b4d3SLarry Finger 				"firmware interrupt!\n");
948f1d2b4d3SLarry Finger 			queue_delayed_work(rtlpriv->works.rtl_wq,
949f1d2b4d3SLarry Finger 					   &rtlpriv->works.fwevt_wq, 0);
950f1d2b4d3SLarry Finger 		}
951f1d2b4d3SLarry Finger 	}
952f1d2b4d3SLarry Finger 
953f1d2b4d3SLarry Finger 	/*<5> hsisr related*/
954f1d2b4d3SLarry Finger 	/* Only 8188EE & 8723BE Supported.
955f1d2b4d3SLarry Finger 	 * If Other ICs Come in, System will corrupt,
956f1d2b4d3SLarry Finger 	 * because maps[RTL_IMR_HSISR_IND] & maps[MAC_HSISR]
957f1d2b4d3SLarry Finger 	 * are not initialized
958f1d2b4d3SLarry Finger 	 */
959f1d2b4d3SLarry Finger 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8188EE ||
960f1d2b4d3SLarry Finger 	    rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
96178aa6012SLarry Finger 		if (unlikely(intvec.inta &
96278aa6012SLarry Finger 		    rtlpriv->cfg->maps[RTL_IMR_HSISR_IND])) {
963f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_INTR, DBG_TRACE,
964f1d2b4d3SLarry Finger 				"hsisr interrupt!\n");
965f1d2b4d3SLarry Finger 			_rtl_pci_hs_interrupt(hw);
966f1d2b4d3SLarry Finger 		}
967f1d2b4d3SLarry Finger 	}
968f1d2b4d3SLarry Finger 
969f1d2b4d3SLarry Finger 	if (rtlpriv->rtlhal.earlymode_enable)
970f1d2b4d3SLarry Finger 		tasklet_schedule(&rtlpriv->works.irq_tasklet);
971f1d2b4d3SLarry Finger 
972f1d2b4d3SLarry Finger done:
973f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->enable_interrupt(hw);
974f1d2b4d3SLarry Finger 	spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
975f1d2b4d3SLarry Finger 	return ret;
976f1d2b4d3SLarry Finger }
977f1d2b4d3SLarry Finger 
_rtl_pci_irq_tasklet(struct tasklet_struct * t)978d3ccc14dSAllen Pais static void _rtl_pci_irq_tasklet(struct tasklet_struct *t)
979f1d2b4d3SLarry Finger {
980d3ccc14dSAllen Pais 	struct rtl_priv *rtlpriv = from_tasklet(rtlpriv, t, works.irq_tasklet);
981d3ccc14dSAllen Pais 	struct ieee80211_hw *hw = rtlpriv->hw;
982f1d2b4d3SLarry Finger 	_rtl_pci_tx_chk_waitq(hw);
983f1d2b4d3SLarry Finger }
984f1d2b4d3SLarry Finger 
_rtl_pci_prepare_bcn_tasklet(struct tasklet_struct * t)985d3ccc14dSAllen Pais static void _rtl_pci_prepare_bcn_tasklet(struct tasklet_struct *t)
986f1d2b4d3SLarry Finger {
987d3ccc14dSAllen Pais 	struct rtl_priv *rtlpriv = from_tasklet(rtlpriv, t,
988d3ccc14dSAllen Pais 						works.irq_prepare_bcn_tasklet);
989d3ccc14dSAllen Pais 	struct ieee80211_hw *hw = rtlpriv->hw;
990f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
991f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
992f1d2b4d3SLarry Finger 	struct rtl8192_tx_ring *ring = NULL;
993f1d2b4d3SLarry Finger 	struct ieee80211_hdr *hdr = NULL;
994f1d2b4d3SLarry Finger 	struct ieee80211_tx_info *info = NULL;
995f1d2b4d3SLarry Finger 	struct sk_buff *pskb = NULL;
996f1d2b4d3SLarry Finger 	struct rtl_tx_desc *pdesc = NULL;
997f1d2b4d3SLarry Finger 	struct rtl_tcb_desc tcb_desc;
998f1d2b4d3SLarry Finger 	/*This is for new trx flow*/
999f1d2b4d3SLarry Finger 	struct rtl_tx_buffer_desc *pbuffer_desc = NULL;
1000f1d2b4d3SLarry Finger 	u8 temp_one = 1;
1001f1d2b4d3SLarry Finger 	u8 *entry;
1002f1d2b4d3SLarry Finger 
1003f1d2b4d3SLarry Finger 	memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1004f1d2b4d3SLarry Finger 	ring = &rtlpci->tx_ring[BEACON_QUEUE];
1005f1d2b4d3SLarry Finger 	pskb = __skb_dequeue(&ring->queue);
1006f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow)
1007f1d2b4d3SLarry Finger 		entry = (u8 *)(&ring->buffer_desc[ring->idx]);
1008f1d2b4d3SLarry Finger 	else
1009f1d2b4d3SLarry Finger 		entry = (u8 *)(&ring->desc[ring->idx]);
1010f1d2b4d3SLarry Finger 	if (pskb) {
10110dc0b5c2SChristophe JAILLET 		dma_unmap_single(&rtlpci->pdev->dev,
10120dc0b5c2SChristophe JAILLET 				 rtlpriv->cfg->ops->get_desc(hw, (u8 *)entry,
10130dc0b5c2SChristophe JAILLET 						true, HW_DESC_TXBUFF_ADDR),
10140dc0b5c2SChristophe JAILLET 				 pskb->len, DMA_TO_DEVICE);
1015f1d2b4d3SLarry Finger 		kfree_skb(pskb);
1016f1d2b4d3SLarry Finger 	}
1017f1d2b4d3SLarry Finger 
1018f1d2b4d3SLarry Finger 	/*NB: the beacon data buffer must be 32-bit aligned. */
10196e8912a5SShaul Triebitz 	pskb = ieee80211_beacon_get(hw, mac->vif, 0);
1020ae0122b6SLarry Finger 	if (!pskb)
1021f1d2b4d3SLarry Finger 		return;
1022f1d2b4d3SLarry Finger 	hdr = rtl_get_hdr(pskb);
1023f1d2b4d3SLarry Finger 	info = IEEE80211_SKB_CB(pskb);
1024f1d2b4d3SLarry Finger 	pdesc = &ring->desc[0];
1025f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow)
1026f1d2b4d3SLarry Finger 		pbuffer_desc = &ring->buffer_desc[0];
1027f1d2b4d3SLarry Finger 
1028f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc,
1029f1d2b4d3SLarry Finger 					(u8 *)pbuffer_desc, info, NULL, pskb,
1030f1d2b4d3SLarry Finger 					BEACON_QUEUE, &tcb_desc);
1031f1d2b4d3SLarry Finger 
1032f1d2b4d3SLarry Finger 	__skb_queue_tail(&ring->queue, pskb);
1033f1d2b4d3SLarry Finger 
1034f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow) {
1035f1d2b4d3SLarry Finger 		temp_one = 4;
1036f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->set_desc(hw, (u8 *)pbuffer_desc, true,
1037f1d2b4d3SLarry Finger 					    HW_DESC_OWN, (u8 *)&temp_one);
1038f1d2b4d3SLarry Finger 	} else {
1039f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc, true, HW_DESC_OWN,
1040f1d2b4d3SLarry Finger 					    &temp_one);
1041f1d2b4d3SLarry Finger 	}
1042f1d2b4d3SLarry Finger }
1043f1d2b4d3SLarry Finger 
_rtl_pci_init_trx_var(struct ieee80211_hw * hw)1044f1d2b4d3SLarry Finger static void _rtl_pci_init_trx_var(struct ieee80211_hw *hw)
1045f1d2b4d3SLarry Finger {
1046f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1047f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1048f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1049f1d2b4d3SLarry Finger 	u8 i;
1050f1d2b4d3SLarry Finger 	u16 desc_num;
1051f1d2b4d3SLarry Finger 
1052f1d2b4d3SLarry Finger 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE)
1053f1d2b4d3SLarry Finger 		desc_num = TX_DESC_NUM_92E;
105457869e4bSPing-Ke Shih 	else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8822BE)
105557869e4bSPing-Ke Shih 		desc_num = TX_DESC_NUM_8822B;
1056f1d2b4d3SLarry Finger 	else
1057f1d2b4d3SLarry Finger 		desc_num = RT_TXDESC_NUM;
1058f1d2b4d3SLarry Finger 
1059f1d2b4d3SLarry Finger 	for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
1060f1d2b4d3SLarry Finger 		rtlpci->txringcount[i] = desc_num;
1061f1d2b4d3SLarry Finger 
1062ae0122b6SLarry Finger 	/*we just alloc 2 desc for beacon queue,
1063f1d2b4d3SLarry Finger 	 *because we just need first desc in hw beacon.
1064f1d2b4d3SLarry Finger 	 */
1065f1d2b4d3SLarry Finger 	rtlpci->txringcount[BEACON_QUEUE] = 2;
1066f1d2b4d3SLarry Finger 
1067f1d2b4d3SLarry Finger 	/*BE queue need more descriptor for performance
1068f1d2b4d3SLarry Finger 	 *consideration or, No more tx desc will happen,
1069f1d2b4d3SLarry Finger 	 *and may cause mac80211 mem leakage.
1070f1d2b4d3SLarry Finger 	 */
1071f1d2b4d3SLarry Finger 	if (!rtl_priv(hw)->use_new_trx_flow)
1072f1d2b4d3SLarry Finger 		rtlpci->txringcount[BE_QUEUE] = RT_TXDESC_NUM_BE_QUEUE;
1073f1d2b4d3SLarry Finger 
1074f1d2b4d3SLarry Finger 	rtlpci->rxbuffersize = 9100;	/*2048/1024; */
1075f1d2b4d3SLarry Finger 	rtlpci->rxringcount = RTL_PCI_MAX_RX_COUNT;	/*64; */
1076f1d2b4d3SLarry Finger }
1077f1d2b4d3SLarry Finger 
_rtl_pci_init_struct(struct ieee80211_hw * hw,struct pci_dev * pdev)1078f1d2b4d3SLarry Finger static void _rtl_pci_init_struct(struct ieee80211_hw *hw,
1079f1d2b4d3SLarry Finger 				 struct pci_dev *pdev)
1080f1d2b4d3SLarry Finger {
1081f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1082f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1083f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1084f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1085f1d2b4d3SLarry Finger 
1086f1d2b4d3SLarry Finger 	rtlpci->up_first_time = true;
1087f1d2b4d3SLarry Finger 	rtlpci->being_init_adapter = false;
1088f1d2b4d3SLarry Finger 
1089f1d2b4d3SLarry Finger 	rtlhal->hw = hw;
1090f1d2b4d3SLarry Finger 	rtlpci->pdev = pdev;
1091f1d2b4d3SLarry Finger 
1092f1d2b4d3SLarry Finger 	/*Tx/Rx related var */
1093f1d2b4d3SLarry Finger 	_rtl_pci_init_trx_var(hw);
1094f1d2b4d3SLarry Finger 
109537c52934SLarry Finger 	/*IBSS*/
109637c52934SLarry Finger 	mac->beacon_interval = 100;
1097f1d2b4d3SLarry Finger 
1098f1d2b4d3SLarry Finger 	/*AMPDU*/
1099f1d2b4d3SLarry Finger 	mac->min_space_cfg = 0;
1100f1d2b4d3SLarry Finger 	mac->max_mss_density = 0;
1101f1d2b4d3SLarry Finger 	/*set sane AMPDU defaults */
1102f1d2b4d3SLarry Finger 	mac->current_ampdu_density = 7;
1103f1d2b4d3SLarry Finger 	mac->current_ampdu_factor = 3;
1104f1d2b4d3SLarry Finger 
11058d0d43e3SPing-Ke Shih 	/*Retry Limit*/
11068d0d43e3SPing-Ke Shih 	mac->retry_short = 7;
11078d0d43e3SPing-Ke Shih 	mac->retry_long = 7;
11088d0d43e3SPing-Ke Shih 
1109f1d2b4d3SLarry Finger 	/*QOS*/
1110f1d2b4d3SLarry Finger 	rtlpci->acm_method = EACMWAY2_SW;
1111f1d2b4d3SLarry Finger 
1112f1d2b4d3SLarry Finger 	/*task */
1113d3ccc14dSAllen Pais 	tasklet_setup(&rtlpriv->works.irq_tasklet, _rtl_pci_irq_tasklet);
1114d3ccc14dSAllen Pais 	tasklet_setup(&rtlpriv->works.irq_prepare_bcn_tasklet,
1115d3ccc14dSAllen Pais 		     _rtl_pci_prepare_bcn_tasklet);
1116f1d2b4d3SLarry Finger 	INIT_WORK(&rtlpriv->works.lps_change_work,
1117f1d2b4d3SLarry Finger 		  rtl_lps_change_work_callback);
1118f1d2b4d3SLarry Finger }
1119f1d2b4d3SLarry Finger 
_rtl_pci_init_tx_ring(struct ieee80211_hw * hw,unsigned int prio,unsigned int entries)1120f1d2b4d3SLarry Finger static int _rtl_pci_init_tx_ring(struct ieee80211_hw *hw,
1121f1d2b4d3SLarry Finger 				 unsigned int prio, unsigned int entries)
1122f1d2b4d3SLarry Finger {
1123f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1124f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1125f1d2b4d3SLarry Finger 	struct rtl_tx_buffer_desc *buffer_desc;
1126f1d2b4d3SLarry Finger 	struct rtl_tx_desc *desc;
1127f1d2b4d3SLarry Finger 	dma_addr_t buffer_desc_dma, desc_dma;
1128f1d2b4d3SLarry Finger 	u32 nextdescaddress;
1129f1d2b4d3SLarry Finger 	int i;
1130f1d2b4d3SLarry Finger 
1131f1d2b4d3SLarry Finger 	/* alloc tx buffer desc for new trx flow*/
1132f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow) {
1133f1d2b4d3SLarry Finger 		buffer_desc =
11340dc0b5c2SChristophe JAILLET 		   dma_alloc_coherent(&rtlpci->pdev->dev,
1135f1d2b4d3SLarry Finger 				      sizeof(*buffer_desc) * entries,
11360dc0b5c2SChristophe JAILLET 				      &buffer_desc_dma, GFP_KERNEL);
1137f1d2b4d3SLarry Finger 
1138f1d2b4d3SLarry Finger 		if (!buffer_desc || (unsigned long)buffer_desc & 0xFF) {
1139b03d968bSLarry Finger 			pr_err("Cannot allocate TX ring (prio = %d)\n",
1140f1d2b4d3SLarry Finger 			       prio);
1141f1d2b4d3SLarry Finger 			return -ENOMEM;
1142f1d2b4d3SLarry Finger 		}
1143f1d2b4d3SLarry Finger 
1144f1d2b4d3SLarry Finger 		rtlpci->tx_ring[prio].buffer_desc = buffer_desc;
1145f1d2b4d3SLarry Finger 		rtlpci->tx_ring[prio].buffer_desc_dma = buffer_desc_dma;
1146f1d2b4d3SLarry Finger 
1147f1d2b4d3SLarry Finger 		rtlpci->tx_ring[prio].cur_tx_rp = 0;
1148f1d2b4d3SLarry Finger 		rtlpci->tx_ring[prio].cur_tx_wp = 0;
1149f1d2b4d3SLarry Finger 	}
1150f1d2b4d3SLarry Finger 
1151f1d2b4d3SLarry Finger 	/* alloc dma for this ring */
11520dc0b5c2SChristophe JAILLET 	desc = dma_alloc_coherent(&rtlpci->pdev->dev, sizeof(*desc) * entries,
11530dc0b5c2SChristophe JAILLET 				  &desc_dma, GFP_KERNEL);
1154f1d2b4d3SLarry Finger 
1155f1d2b4d3SLarry Finger 	if (!desc || (unsigned long)desc & 0xFF) {
1156b03d968bSLarry Finger 		pr_err("Cannot allocate TX ring (prio = %d)\n", prio);
1157f1d2b4d3SLarry Finger 		return -ENOMEM;
1158f1d2b4d3SLarry Finger 	}
1159f1d2b4d3SLarry Finger 
1160f1d2b4d3SLarry Finger 	rtlpci->tx_ring[prio].desc = desc;
1161f1d2b4d3SLarry Finger 	rtlpci->tx_ring[prio].dma = desc_dma;
1162f1d2b4d3SLarry Finger 
1163f1d2b4d3SLarry Finger 	rtlpci->tx_ring[prio].idx = 0;
1164f1d2b4d3SLarry Finger 	rtlpci->tx_ring[prio].entries = entries;
1165f1d2b4d3SLarry Finger 	skb_queue_head_init(&rtlpci->tx_ring[prio].queue);
1166f1d2b4d3SLarry Finger 
1167f108a420SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "queue:%d, ring_addr:%p\n",
1168f1d2b4d3SLarry Finger 		prio, desc);
1169f1d2b4d3SLarry Finger 
1170f1d2b4d3SLarry Finger 	/* init every desc in this ring */
1171f1d2b4d3SLarry Finger 	if (!rtlpriv->use_new_trx_flow) {
1172f1d2b4d3SLarry Finger 		for (i = 0; i < entries; i++) {
1173f1d2b4d3SLarry Finger 			nextdescaddress = (u32)desc_dma +
1174f1d2b4d3SLarry Finger 					  ((i +	1) % entries) *
1175f1d2b4d3SLarry Finger 					  sizeof(*desc);
1176f1d2b4d3SLarry Finger 
1177f1d2b4d3SLarry Finger 			rtlpriv->cfg->ops->set_desc(hw, (u8 *)&desc[i],
1178f1d2b4d3SLarry Finger 						    true,
1179f1d2b4d3SLarry Finger 						    HW_DESC_TX_NEXTDESC_ADDR,
1180f1d2b4d3SLarry Finger 						    (u8 *)&nextdescaddress);
1181f1d2b4d3SLarry Finger 		}
1182f1d2b4d3SLarry Finger 	}
1183f1d2b4d3SLarry Finger 	return 0;
1184f1d2b4d3SLarry Finger }
1185f1d2b4d3SLarry Finger 
_rtl_pci_init_rx_ring(struct ieee80211_hw * hw,int rxring_idx)1186f1d2b4d3SLarry Finger static int _rtl_pci_init_rx_ring(struct ieee80211_hw *hw, int rxring_idx)
1187f1d2b4d3SLarry Finger {
1188f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1189f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1190f1d2b4d3SLarry Finger 	int i;
1191f1d2b4d3SLarry Finger 
1192f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow) {
1193f1d2b4d3SLarry Finger 		struct rtl_rx_buffer_desc *entry = NULL;
1194f1d2b4d3SLarry Finger 		/* alloc dma for this ring */
1195f1d2b4d3SLarry Finger 		rtlpci->rx_ring[rxring_idx].buffer_desc =
11960dc0b5c2SChristophe JAILLET 		    dma_alloc_coherent(&rtlpci->pdev->dev,
11970dc0b5c2SChristophe JAILLET 				       sizeof(*rtlpci->rx_ring[rxring_idx].buffer_desc) *
1198f1d2b4d3SLarry Finger 				       rtlpci->rxringcount,
11990dc0b5c2SChristophe JAILLET 				       &rtlpci->rx_ring[rxring_idx].dma, GFP_KERNEL);
1200f1d2b4d3SLarry Finger 		if (!rtlpci->rx_ring[rxring_idx].buffer_desc ||
1201f1d2b4d3SLarry Finger 		    (ulong)rtlpci->rx_ring[rxring_idx].buffer_desc & 0xFF) {
1202b03d968bSLarry Finger 			pr_err("Cannot allocate RX ring\n");
1203f1d2b4d3SLarry Finger 			return -ENOMEM;
1204f1d2b4d3SLarry Finger 		}
1205f1d2b4d3SLarry Finger 
1206f1d2b4d3SLarry Finger 		/* init every desc in this ring */
1207f1d2b4d3SLarry Finger 		rtlpci->rx_ring[rxring_idx].idx = 0;
1208f1d2b4d3SLarry Finger 		for (i = 0; i < rtlpci->rxringcount; i++) {
1209f1d2b4d3SLarry Finger 			entry = &rtlpci->rx_ring[rxring_idx].buffer_desc[i];
1210f1d2b4d3SLarry Finger 			if (!_rtl_pci_init_one_rxdesc(hw, NULL, (u8 *)entry,
1211f1d2b4d3SLarry Finger 						      rxring_idx, i))
1212f1d2b4d3SLarry Finger 				return -ENOMEM;
1213f1d2b4d3SLarry Finger 		}
1214f1d2b4d3SLarry Finger 	} else {
1215f1d2b4d3SLarry Finger 		struct rtl_rx_desc *entry = NULL;
1216f1d2b4d3SLarry Finger 		u8 tmp_one = 1;
1217f1d2b4d3SLarry Finger 		/* alloc dma for this ring */
1218f1d2b4d3SLarry Finger 		rtlpci->rx_ring[rxring_idx].desc =
12190dc0b5c2SChristophe JAILLET 		    dma_alloc_coherent(&rtlpci->pdev->dev,
12200dc0b5c2SChristophe JAILLET 				       sizeof(*rtlpci->rx_ring[rxring_idx].desc) *
12210dc0b5c2SChristophe JAILLET 				       rtlpci->rxringcount,
12220dc0b5c2SChristophe JAILLET 				       &rtlpci->rx_ring[rxring_idx].dma, GFP_KERNEL);
1223f1d2b4d3SLarry Finger 		if (!rtlpci->rx_ring[rxring_idx].desc ||
1224f1d2b4d3SLarry Finger 		    (unsigned long)rtlpci->rx_ring[rxring_idx].desc & 0xFF) {
1225b03d968bSLarry Finger 			pr_err("Cannot allocate RX ring\n");
1226f1d2b4d3SLarry Finger 			return -ENOMEM;
1227f1d2b4d3SLarry Finger 		}
1228f1d2b4d3SLarry Finger 
1229f1d2b4d3SLarry Finger 		/* init every desc in this ring */
1230f1d2b4d3SLarry Finger 		rtlpci->rx_ring[rxring_idx].idx = 0;
1231f1d2b4d3SLarry Finger 
1232f1d2b4d3SLarry Finger 		for (i = 0; i < rtlpci->rxringcount; i++) {
1233f1d2b4d3SLarry Finger 			entry = &rtlpci->rx_ring[rxring_idx].desc[i];
1234f1d2b4d3SLarry Finger 			if (!_rtl_pci_init_one_rxdesc(hw, NULL, (u8 *)entry,
1235f1d2b4d3SLarry Finger 						      rxring_idx, i))
1236f1d2b4d3SLarry Finger 				return -ENOMEM;
1237f1d2b4d3SLarry Finger 		}
1238f1d2b4d3SLarry Finger 
1239f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
1240f1d2b4d3SLarry Finger 					    HW_DESC_RXERO, &tmp_one);
1241f1d2b4d3SLarry Finger 	}
1242f1d2b4d3SLarry Finger 	return 0;
1243f1d2b4d3SLarry Finger }
1244f1d2b4d3SLarry Finger 
_rtl_pci_free_tx_ring(struct ieee80211_hw * hw,unsigned int prio)1245f1d2b4d3SLarry Finger static void _rtl_pci_free_tx_ring(struct ieee80211_hw *hw,
1246f1d2b4d3SLarry Finger 				  unsigned int prio)
1247f1d2b4d3SLarry Finger {
1248f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1249f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1250f1d2b4d3SLarry Finger 	struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[prio];
1251f1d2b4d3SLarry Finger 
1252f1d2b4d3SLarry Finger 	/* free every desc in this ring */
1253f1d2b4d3SLarry Finger 	while (skb_queue_len(&ring->queue)) {
1254f1d2b4d3SLarry Finger 		u8 *entry;
1255f1d2b4d3SLarry Finger 		struct sk_buff *skb = __skb_dequeue(&ring->queue);
1256f1d2b4d3SLarry Finger 
1257f1d2b4d3SLarry Finger 		if (rtlpriv->use_new_trx_flow)
1258f1d2b4d3SLarry Finger 			entry = (u8 *)(&ring->buffer_desc[ring->idx]);
1259f1d2b4d3SLarry Finger 		else
1260f1d2b4d3SLarry Finger 			entry = (u8 *)(&ring->desc[ring->idx]);
1261f1d2b4d3SLarry Finger 
12620dc0b5c2SChristophe JAILLET 		dma_unmap_single(&rtlpci->pdev->dev,
1263ae0122b6SLarry Finger 				 rtlpriv->cfg->ops->get_desc(hw, (u8 *)entry,
12640dc0b5c2SChristophe JAILLET 						true, HW_DESC_TXBUFF_ADDR),
12650dc0b5c2SChristophe JAILLET 				 skb->len, DMA_TO_DEVICE);
1266f1d2b4d3SLarry Finger 		kfree_skb(skb);
1267f1d2b4d3SLarry Finger 		ring->idx = (ring->idx + 1) % ring->entries;
1268f1d2b4d3SLarry Finger 	}
1269f1d2b4d3SLarry Finger 
1270f1d2b4d3SLarry Finger 	/* free dma of this ring */
12710dc0b5c2SChristophe JAILLET 	dma_free_coherent(&rtlpci->pdev->dev,
12720dc0b5c2SChristophe JAILLET 			  sizeof(*ring->desc) * ring->entries, ring->desc,
12730dc0b5c2SChristophe JAILLET 			  ring->dma);
1274f1d2b4d3SLarry Finger 	ring->desc = NULL;
1275f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow) {
12760dc0b5c2SChristophe JAILLET 		dma_free_coherent(&rtlpci->pdev->dev,
1277f1d2b4d3SLarry Finger 				  sizeof(*ring->buffer_desc) * ring->entries,
1278f1d2b4d3SLarry Finger 				  ring->buffer_desc, ring->buffer_desc_dma);
1279f1d2b4d3SLarry Finger 		ring->buffer_desc = NULL;
1280f1d2b4d3SLarry Finger 	}
1281f1d2b4d3SLarry Finger }
1282f1d2b4d3SLarry Finger 
_rtl_pci_free_rx_ring(struct ieee80211_hw * hw,int rxring_idx)1283f1d2b4d3SLarry Finger static void _rtl_pci_free_rx_ring(struct ieee80211_hw *hw, int rxring_idx)
1284f1d2b4d3SLarry Finger {
1285f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1286f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1287f1d2b4d3SLarry Finger 	int i;
1288f1d2b4d3SLarry Finger 
1289f1d2b4d3SLarry Finger 	/* free every desc in this ring */
1290f1d2b4d3SLarry Finger 	for (i = 0; i < rtlpci->rxringcount; i++) {
1291f1d2b4d3SLarry Finger 		struct sk_buff *skb = rtlpci->rx_ring[rxring_idx].rx_buf[i];
1292f1d2b4d3SLarry Finger 
1293f1d2b4d3SLarry Finger 		if (!skb)
1294f1d2b4d3SLarry Finger 			continue;
12950dc0b5c2SChristophe JAILLET 		dma_unmap_single(&rtlpci->pdev->dev, *((dma_addr_t *)skb->cb),
12960dc0b5c2SChristophe JAILLET 				 rtlpci->rxbuffersize, DMA_FROM_DEVICE);
1297f1d2b4d3SLarry Finger 		kfree_skb(skb);
1298f1d2b4d3SLarry Finger 	}
1299f1d2b4d3SLarry Finger 
1300f1d2b4d3SLarry Finger 	/* free dma of this ring */
1301f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow) {
13020dc0b5c2SChristophe JAILLET 		dma_free_coherent(&rtlpci->pdev->dev,
13030dc0b5c2SChristophe JAILLET 				  sizeof(*rtlpci->rx_ring[rxring_idx].buffer_desc) *
13040dc0b5c2SChristophe JAILLET 				  rtlpci->rxringcount,
1305f1d2b4d3SLarry Finger 				  rtlpci->rx_ring[rxring_idx].buffer_desc,
1306f1d2b4d3SLarry Finger 				  rtlpci->rx_ring[rxring_idx].dma);
1307f1d2b4d3SLarry Finger 		rtlpci->rx_ring[rxring_idx].buffer_desc = NULL;
1308f1d2b4d3SLarry Finger 	} else {
13090dc0b5c2SChristophe JAILLET 		dma_free_coherent(&rtlpci->pdev->dev,
1310f1d2b4d3SLarry Finger 				  sizeof(*rtlpci->rx_ring[rxring_idx].desc) *
1311f1d2b4d3SLarry Finger 				  rtlpci->rxringcount,
1312f1d2b4d3SLarry Finger 				  rtlpci->rx_ring[rxring_idx].desc,
1313f1d2b4d3SLarry Finger 				  rtlpci->rx_ring[rxring_idx].dma);
1314f1d2b4d3SLarry Finger 		rtlpci->rx_ring[rxring_idx].desc = NULL;
1315f1d2b4d3SLarry Finger 	}
1316f1d2b4d3SLarry Finger }
1317f1d2b4d3SLarry Finger 
_rtl_pci_init_trx_ring(struct ieee80211_hw * hw)1318f1d2b4d3SLarry Finger static int _rtl_pci_init_trx_ring(struct ieee80211_hw *hw)
1319f1d2b4d3SLarry Finger {
1320f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1321f1d2b4d3SLarry Finger 	int ret;
1322f1d2b4d3SLarry Finger 	int i, rxring_idx;
1323f1d2b4d3SLarry Finger 
1324f1d2b4d3SLarry Finger 	/* rxring_idx 0:RX_MPDU_QUEUE
1325f1d2b4d3SLarry Finger 	 * rxring_idx 1:RX_CMD_QUEUE
1326f1d2b4d3SLarry Finger 	 */
1327f1d2b4d3SLarry Finger 	for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++) {
1328f1d2b4d3SLarry Finger 		ret = _rtl_pci_init_rx_ring(hw, rxring_idx);
1329f1d2b4d3SLarry Finger 		if (ret)
1330f1d2b4d3SLarry Finger 			return ret;
1331f1d2b4d3SLarry Finger 	}
1332f1d2b4d3SLarry Finger 
1333f1d2b4d3SLarry Finger 	for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
1334ae0122b6SLarry Finger 		ret = _rtl_pci_init_tx_ring(hw, i, rtlpci->txringcount[i]);
1335f1d2b4d3SLarry Finger 		if (ret)
1336f1d2b4d3SLarry Finger 			goto err_free_rings;
1337f1d2b4d3SLarry Finger 	}
1338f1d2b4d3SLarry Finger 
1339f1d2b4d3SLarry Finger 	return 0;
1340f1d2b4d3SLarry Finger 
1341f1d2b4d3SLarry Finger err_free_rings:
1342f1d2b4d3SLarry Finger 	for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++)
1343f1d2b4d3SLarry Finger 		_rtl_pci_free_rx_ring(hw, rxring_idx);
1344f1d2b4d3SLarry Finger 
1345f1d2b4d3SLarry Finger 	for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
1346f1d2b4d3SLarry Finger 		if (rtlpci->tx_ring[i].desc ||
1347f1d2b4d3SLarry Finger 		    rtlpci->tx_ring[i].buffer_desc)
1348f1d2b4d3SLarry Finger 			_rtl_pci_free_tx_ring(hw, i);
1349f1d2b4d3SLarry Finger 
1350f1d2b4d3SLarry Finger 	return 1;
1351f1d2b4d3SLarry Finger }
1352f1d2b4d3SLarry Finger 
_rtl_pci_deinit_trx_ring(struct ieee80211_hw * hw)1353f1d2b4d3SLarry Finger static int _rtl_pci_deinit_trx_ring(struct ieee80211_hw *hw)
1354f1d2b4d3SLarry Finger {
1355f1d2b4d3SLarry Finger 	u32 i, rxring_idx;
1356f1d2b4d3SLarry Finger 
1357f1d2b4d3SLarry Finger 	/*free rx rings */
1358f1d2b4d3SLarry Finger 	for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++)
1359f1d2b4d3SLarry Finger 		_rtl_pci_free_rx_ring(hw, rxring_idx);
1360f1d2b4d3SLarry Finger 
1361f1d2b4d3SLarry Finger 	/*free tx rings */
1362f1d2b4d3SLarry Finger 	for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
1363f1d2b4d3SLarry Finger 		_rtl_pci_free_tx_ring(hw, i);
1364f1d2b4d3SLarry Finger 
1365f1d2b4d3SLarry Finger 	return 0;
1366f1d2b4d3SLarry Finger }
1367f1d2b4d3SLarry Finger 
rtl_pci_reset_trx_ring(struct ieee80211_hw * hw)1368f1d2b4d3SLarry Finger int rtl_pci_reset_trx_ring(struct ieee80211_hw *hw)
1369f1d2b4d3SLarry Finger {
1370f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1371f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1372f1d2b4d3SLarry Finger 	int i, rxring_idx;
1373f1d2b4d3SLarry Finger 	unsigned long flags;
1374f1d2b4d3SLarry Finger 	u8 tmp_one = 1;
1375f1d2b4d3SLarry Finger 	u32 bufferaddress;
1376f1d2b4d3SLarry Finger 	/* rxring_idx 0:RX_MPDU_QUEUE */
1377f1d2b4d3SLarry Finger 	/* rxring_idx 1:RX_CMD_QUEUE */
1378f1d2b4d3SLarry Finger 	for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++) {
1379f1d2b4d3SLarry Finger 		/* force the rx_ring[RX_MPDU_QUEUE/
1380f1d2b4d3SLarry Finger 		 * RX_CMD_QUEUE].idx to the first one
1381f1d2b4d3SLarry Finger 		 *new trx flow, do nothing
1382f1d2b4d3SLarry Finger 		 */
1383f1d2b4d3SLarry Finger 		if (!rtlpriv->use_new_trx_flow &&
1384f1d2b4d3SLarry Finger 		    rtlpci->rx_ring[rxring_idx].desc) {
1385f1d2b4d3SLarry Finger 			struct rtl_rx_desc *entry = NULL;
1386f1d2b4d3SLarry Finger 
1387f1d2b4d3SLarry Finger 			rtlpci->rx_ring[rxring_idx].idx = 0;
1388f1d2b4d3SLarry Finger 			for (i = 0; i < rtlpci->rxringcount; i++) {
1389f1d2b4d3SLarry Finger 				entry = &rtlpci->rx_ring[rxring_idx].desc[i];
1390f1d2b4d3SLarry Finger 				bufferaddress =
13910c07bd74SPing-Ke Shih 				  rtlpriv->cfg->ops->get_desc(hw, (u8 *)entry,
1392f1d2b4d3SLarry Finger 				  false, HW_DESC_RXBUFF_ADDR);
1393f1d2b4d3SLarry Finger 				memset((u8 *)entry, 0,
1394f1d2b4d3SLarry Finger 				       sizeof(*rtlpci->rx_ring
1395f1d2b4d3SLarry Finger 				       [rxring_idx].desc));/*clear one entry*/
1396f1d2b4d3SLarry Finger 				if (rtlpriv->use_new_trx_flow) {
1397f1d2b4d3SLarry Finger 					rtlpriv->cfg->ops->set_desc(hw,
1398f1d2b4d3SLarry Finger 					    (u8 *)entry, false,
1399f1d2b4d3SLarry Finger 					    HW_DESC_RX_PREPARE,
1400f1d2b4d3SLarry Finger 					    (u8 *)&bufferaddress);
1401f1d2b4d3SLarry Finger 				} else {
1402f1d2b4d3SLarry Finger 					rtlpriv->cfg->ops->set_desc(hw,
1403f1d2b4d3SLarry Finger 					    (u8 *)entry, false,
1404f1d2b4d3SLarry Finger 					    HW_DESC_RXBUFF_ADDR,
1405f1d2b4d3SLarry Finger 					    (u8 *)&bufferaddress);
1406f1d2b4d3SLarry Finger 					rtlpriv->cfg->ops->set_desc(hw,
1407f1d2b4d3SLarry Finger 					    (u8 *)entry, false,
1408f1d2b4d3SLarry Finger 					    HW_DESC_RXPKT_LEN,
1409f1d2b4d3SLarry Finger 					    (u8 *)&rtlpci->rxbuffersize);
1410f1d2b4d3SLarry Finger 					rtlpriv->cfg->ops->set_desc(hw,
1411f1d2b4d3SLarry Finger 					    (u8 *)entry, false,
1412f1d2b4d3SLarry Finger 					    HW_DESC_RXOWN,
1413f1d2b4d3SLarry Finger 					    (u8 *)&tmp_one);
1414f1d2b4d3SLarry Finger 				}
1415f1d2b4d3SLarry Finger 			}
1416f1d2b4d3SLarry Finger 			rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
1417f1d2b4d3SLarry Finger 					    HW_DESC_RXERO, (u8 *)&tmp_one);
1418f1d2b4d3SLarry Finger 		}
1419f1d2b4d3SLarry Finger 		rtlpci->rx_ring[rxring_idx].idx = 0;
1420f1d2b4d3SLarry Finger 	}
1421f1d2b4d3SLarry Finger 
1422ae0122b6SLarry Finger 	/*after reset, release previous pending packet,
1423f1d2b4d3SLarry Finger 	 *and force the  tx idx to the first one
1424f1d2b4d3SLarry Finger 	 */
1425f1d2b4d3SLarry Finger 	spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
1426f1d2b4d3SLarry Finger 	for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
1427f1d2b4d3SLarry Finger 		if (rtlpci->tx_ring[i].desc ||
1428f1d2b4d3SLarry Finger 		    rtlpci->tx_ring[i].buffer_desc) {
1429f1d2b4d3SLarry Finger 			struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[i];
1430f1d2b4d3SLarry Finger 
1431f1d2b4d3SLarry Finger 			while (skb_queue_len(&ring->queue)) {
1432f1d2b4d3SLarry Finger 				u8 *entry;
1433f1d2b4d3SLarry Finger 				struct sk_buff *skb =
1434f1d2b4d3SLarry Finger 					__skb_dequeue(&ring->queue);
1435f1d2b4d3SLarry Finger 				if (rtlpriv->use_new_trx_flow)
1436f1d2b4d3SLarry Finger 					entry = (u8 *)(&ring->buffer_desc
1437f1d2b4d3SLarry Finger 								[ring->idx]);
1438f1d2b4d3SLarry Finger 				else
1439f1d2b4d3SLarry Finger 					entry = (u8 *)(&ring->desc[ring->idx]);
1440f1d2b4d3SLarry Finger 
14410dc0b5c2SChristophe JAILLET 				dma_unmap_single(&rtlpci->pdev->dev,
14420dc0b5c2SChristophe JAILLET 						 rtlpriv->cfg->ops->get_desc(hw, (u8 *)entry,
14430dc0b5c2SChristophe JAILLET 								true, HW_DESC_TXBUFF_ADDR),
14440dc0b5c2SChristophe JAILLET 						 skb->len, DMA_TO_DEVICE);
1445cf968937Swang yanqing 				dev_kfree_skb_irq(skb);
1446f1d2b4d3SLarry Finger 				ring->idx = (ring->idx + 1) % ring->entries;
1447f1d2b4d3SLarry Finger 			}
1448b7573a0aSTsang-Shian Lin 
1449b7573a0aSTsang-Shian Lin 			if (rtlpriv->use_new_trx_flow) {
1450b7573a0aSTsang-Shian Lin 				rtlpci->tx_ring[i].cur_tx_rp = 0;
1451b7573a0aSTsang-Shian Lin 				rtlpci->tx_ring[i].cur_tx_wp = 0;
1452b7573a0aSTsang-Shian Lin 			}
1453b7573a0aSTsang-Shian Lin 
1454f1d2b4d3SLarry Finger 			ring->idx = 0;
1455b7573a0aSTsang-Shian Lin 			ring->entries = rtlpci->txringcount[i];
1456f1d2b4d3SLarry Finger 		}
1457f1d2b4d3SLarry Finger 	}
1458f1d2b4d3SLarry Finger 	spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1459f1d2b4d3SLarry Finger 
1460f1d2b4d3SLarry Finger 	return 0;
1461f1d2b4d3SLarry Finger }
1462f1d2b4d3SLarry Finger 
rtl_pci_tx_chk_waitq_insert(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct sk_buff * skb)1463f1d2b4d3SLarry Finger static bool rtl_pci_tx_chk_waitq_insert(struct ieee80211_hw *hw,
1464f1d2b4d3SLarry Finger 					struct ieee80211_sta *sta,
1465f1d2b4d3SLarry Finger 					struct sk_buff *skb)
1466f1d2b4d3SLarry Finger {
1467f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1468f1d2b4d3SLarry Finger 	struct rtl_sta_info *sta_entry = NULL;
1469f1d2b4d3SLarry Finger 	u8 tid = rtl_get_tid(skb);
1470f1d2b4d3SLarry Finger 	__le16 fc = rtl_get_fc(skb);
1471f1d2b4d3SLarry Finger 
1472f1d2b4d3SLarry Finger 	if (!sta)
1473f1d2b4d3SLarry Finger 		return false;
1474f1d2b4d3SLarry Finger 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1475f1d2b4d3SLarry Finger 
1476f1d2b4d3SLarry Finger 	if (!rtlpriv->rtlhal.earlymode_enable)
1477f1d2b4d3SLarry Finger 		return false;
1478f1d2b4d3SLarry Finger 	if (ieee80211_is_nullfunc(fc))
1479f1d2b4d3SLarry Finger 		return false;
1480f1d2b4d3SLarry Finger 	if (ieee80211_is_qos_nullfunc(fc))
1481f1d2b4d3SLarry Finger 		return false;
1482f1d2b4d3SLarry Finger 	if (ieee80211_is_pspoll(fc))
1483f1d2b4d3SLarry Finger 		return false;
1484f1d2b4d3SLarry Finger 	if (sta_entry->tids[tid].agg.agg_state != RTL_AGG_OPERATIONAL)
1485f1d2b4d3SLarry Finger 		return false;
1486f1d2b4d3SLarry Finger 	if (_rtl_mac_to_hwqueue(hw, skb) > VO_QUEUE)
1487f1d2b4d3SLarry Finger 		return false;
1488f1d2b4d3SLarry Finger 	if (tid > 7)
1489f1d2b4d3SLarry Finger 		return false;
1490f1d2b4d3SLarry Finger 
1491f1d2b4d3SLarry Finger 	/* maybe every tid should be checked */
1492f1d2b4d3SLarry Finger 	if (!rtlpriv->link_info.higher_busytxtraffic[tid])
1493f1d2b4d3SLarry Finger 		return false;
1494f1d2b4d3SLarry Finger 
1495f1d2b4d3SLarry Finger 	spin_lock_bh(&rtlpriv->locks.waitq_lock);
1496f1d2b4d3SLarry Finger 	skb_queue_tail(&rtlpriv->mac80211.skb_waitq[tid], skb);
1497f1d2b4d3SLarry Finger 	spin_unlock_bh(&rtlpriv->locks.waitq_lock);
1498f1d2b4d3SLarry Finger 
1499f1d2b4d3SLarry Finger 	return true;
1500f1d2b4d3SLarry Finger }
1501f1d2b4d3SLarry Finger 
rtl_pci_tx(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct sk_buff * skb,struct rtl_tcb_desc * ptcb_desc)1502f1d2b4d3SLarry Finger static int rtl_pci_tx(struct ieee80211_hw *hw,
1503f1d2b4d3SLarry Finger 		      struct ieee80211_sta *sta,
1504f1d2b4d3SLarry Finger 		      struct sk_buff *skb,
1505f1d2b4d3SLarry Finger 		      struct rtl_tcb_desc *ptcb_desc)
1506f1d2b4d3SLarry Finger {
1507f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1508f1d2b4d3SLarry Finger 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1509f1d2b4d3SLarry Finger 	struct rtl8192_tx_ring *ring;
1510f1d2b4d3SLarry Finger 	struct rtl_tx_desc *pdesc;
1511f1d2b4d3SLarry Finger 	struct rtl_tx_buffer_desc *ptx_bd_desc = NULL;
1512f1d2b4d3SLarry Finger 	u16 idx;
1513f1d2b4d3SLarry Finger 	u8 hw_queue = _rtl_mac_to_hwqueue(hw, skb);
1514f1d2b4d3SLarry Finger 	unsigned long flags;
1515f1d2b4d3SLarry Finger 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1516f1d2b4d3SLarry Finger 	__le16 fc = rtl_get_fc(skb);
1517f1d2b4d3SLarry Finger 	u8 *pda_addr = hdr->addr1;
1518f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1519f1d2b4d3SLarry Finger 	u8 own;
1520f1d2b4d3SLarry Finger 	u8 temp_one = 1;
1521f1d2b4d3SLarry Finger 
1522f1d2b4d3SLarry Finger 	if (ieee80211_is_mgmt(fc))
1523f1d2b4d3SLarry Finger 		rtl_tx_mgmt_proc(hw, skb);
1524f1d2b4d3SLarry Finger 
1525f1d2b4d3SLarry Finger 	if (rtlpriv->psc.sw_ps_enabled) {
1526f1d2b4d3SLarry Finger 		if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
1527f1d2b4d3SLarry Finger 		    !ieee80211_has_pm(fc))
1528f1d2b4d3SLarry Finger 			hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1529f1d2b4d3SLarry Finger 	}
1530f1d2b4d3SLarry Finger 
1531f1d2b4d3SLarry Finger 	rtl_action_proc(hw, skb, true);
1532f1d2b4d3SLarry Finger 
1533f1d2b4d3SLarry Finger 	if (is_multicast_ether_addr(pda_addr))
1534f1d2b4d3SLarry Finger 		rtlpriv->stats.txbytesmulticast += skb->len;
1535f1d2b4d3SLarry Finger 	else if (is_broadcast_ether_addr(pda_addr))
1536f1d2b4d3SLarry Finger 		rtlpriv->stats.txbytesbroadcast += skb->len;
1537f1d2b4d3SLarry Finger 	else
1538f1d2b4d3SLarry Finger 		rtlpriv->stats.txbytesunicast += skb->len;
1539f1d2b4d3SLarry Finger 
1540f1d2b4d3SLarry Finger 	spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
1541f1d2b4d3SLarry Finger 	ring = &rtlpci->tx_ring[hw_queue];
1542f1d2b4d3SLarry Finger 	if (hw_queue != BEACON_QUEUE) {
1543f1d2b4d3SLarry Finger 		if (rtlpriv->use_new_trx_flow)
1544f1d2b4d3SLarry Finger 			idx = ring->cur_tx_wp;
1545f1d2b4d3SLarry Finger 		else
1546f1d2b4d3SLarry Finger 			idx = (ring->idx + skb_queue_len(&ring->queue)) %
1547f1d2b4d3SLarry Finger 			      ring->entries;
1548f1d2b4d3SLarry Finger 	} else {
1549f1d2b4d3SLarry Finger 		idx = 0;
1550f1d2b4d3SLarry Finger 	}
1551f1d2b4d3SLarry Finger 
1552f1d2b4d3SLarry Finger 	pdesc = &ring->desc[idx];
1553f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow) {
1554f1d2b4d3SLarry Finger 		ptx_bd_desc = &ring->buffer_desc[idx];
1555f1d2b4d3SLarry Finger 	} else {
15560c07bd74SPing-Ke Shih 		own = (u8)rtlpriv->cfg->ops->get_desc(hw, (u8 *)pdesc,
1557f1d2b4d3SLarry Finger 				true, HW_DESC_OWN);
1558f1d2b4d3SLarry Finger 
1559ae0122b6SLarry Finger 		if (own == 1 && hw_queue != BEACON_QUEUE) {
1560f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
1561f1d2b4d3SLarry Finger 				"No more TX desc@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%x\n",
1562f1d2b4d3SLarry Finger 				hw_queue, ring->idx, idx,
1563f1d2b4d3SLarry Finger 				skb_queue_len(&ring->queue));
1564f1d2b4d3SLarry Finger 
1565f1d2b4d3SLarry Finger 			spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock,
1566f1d2b4d3SLarry Finger 					       flags);
1567f1d2b4d3SLarry Finger 			return skb->len;
1568f1d2b4d3SLarry Finger 		}
1569f1d2b4d3SLarry Finger 	}
1570f1d2b4d3SLarry Finger 
1571f1d2b4d3SLarry Finger 	if (rtlpriv->cfg->ops->get_available_desc &&
1572f1d2b4d3SLarry Finger 	    rtlpriv->cfg->ops->get_available_desc(hw, hw_queue) == 0) {
1573f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
1574f1d2b4d3SLarry Finger 			"get_available_desc fail\n");
1575ae0122b6SLarry Finger 		spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1576f1d2b4d3SLarry Finger 		return skb->len;
1577f1d2b4d3SLarry Finger 	}
1578f1d2b4d3SLarry Finger 
1579f1d2b4d3SLarry Finger 	if (ieee80211_is_data(fc))
1580f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
1581f1d2b4d3SLarry Finger 
1582f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc,
1583f1d2b4d3SLarry Finger 			(u8 *)ptx_bd_desc, info, sta, skb, hw_queue, ptcb_desc);
1584f1d2b4d3SLarry Finger 
1585f1d2b4d3SLarry Finger 	__skb_queue_tail(&ring->queue, skb);
1586f1d2b4d3SLarry Finger 
1587f1d2b4d3SLarry Finger 	if (rtlpriv->use_new_trx_flow) {
1588f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc, true,
1589f1d2b4d3SLarry Finger 					    HW_DESC_OWN, &hw_queue);
1590f1d2b4d3SLarry Finger 	} else {
1591f1d2b4d3SLarry Finger 		rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc, true,
1592f1d2b4d3SLarry Finger 					    HW_DESC_OWN, &temp_one);
1593f1d2b4d3SLarry Finger 	}
1594f1d2b4d3SLarry Finger 
1595f1d2b4d3SLarry Finger 	if ((ring->entries - skb_queue_len(&ring->queue)) < 2 &&
1596f1d2b4d3SLarry Finger 	    hw_queue != BEACON_QUEUE) {
1597f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
1598f1d2b4d3SLarry Finger 			"less desc left, stop skb_queue@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%x\n",
1599f1d2b4d3SLarry Finger 			 hw_queue, ring->idx, idx,
1600f1d2b4d3SLarry Finger 			 skb_queue_len(&ring->queue));
1601f1d2b4d3SLarry Finger 
1602f1d2b4d3SLarry Finger 		ieee80211_stop_queue(hw, skb_get_queue_mapping(skb));
1603f1d2b4d3SLarry Finger 	}
1604f1d2b4d3SLarry Finger 
1605f1d2b4d3SLarry Finger 	spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1606f1d2b4d3SLarry Finger 
1607f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->tx_polling(hw, hw_queue);
1608f1d2b4d3SLarry Finger 
1609f1d2b4d3SLarry Finger 	return 0;
1610f1d2b4d3SLarry Finger }
1611f1d2b4d3SLarry Finger 
rtl_pci_flush(struct ieee80211_hw * hw,u32 queues,bool drop)1612f1d2b4d3SLarry Finger static void rtl_pci_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
1613f1d2b4d3SLarry Finger {
1614f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1615f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1616f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1617f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1618f1d2b4d3SLarry Finger 	u16 i = 0;
1619f1d2b4d3SLarry Finger 	int queue_id;
1620f1d2b4d3SLarry Finger 	struct rtl8192_tx_ring *ring;
1621f1d2b4d3SLarry Finger 
1622f1d2b4d3SLarry Finger 	if (mac->skip_scan)
1623f1d2b4d3SLarry Finger 		return;
1624f1d2b4d3SLarry Finger 
1625f1d2b4d3SLarry Finger 	for (queue_id = RTL_PCI_MAX_TX_QUEUE_COUNT - 1; queue_id >= 0;) {
1626f1d2b4d3SLarry Finger 		u32 queue_len;
1627f1d2b4d3SLarry Finger 
1628f1d2b4d3SLarry Finger 		if (((queues >> queue_id) & 0x1) == 0) {
1629f1d2b4d3SLarry Finger 			queue_id--;
1630f1d2b4d3SLarry Finger 			continue;
1631f1d2b4d3SLarry Finger 		}
1632f1d2b4d3SLarry Finger 		ring = &pcipriv->dev.tx_ring[queue_id];
1633f1d2b4d3SLarry Finger 		queue_len = skb_queue_len(&ring->queue);
1634f1d2b4d3SLarry Finger 		if (queue_len == 0 || queue_id == BEACON_QUEUE ||
1635f1d2b4d3SLarry Finger 		    queue_id == TXCMD_QUEUE) {
1636f1d2b4d3SLarry Finger 			queue_id--;
1637f1d2b4d3SLarry Finger 			continue;
1638f1d2b4d3SLarry Finger 		} else {
1639f1d2b4d3SLarry Finger 			msleep(20);
1640f1d2b4d3SLarry Finger 			i++;
1641f1d2b4d3SLarry Finger 		}
1642f1d2b4d3SLarry Finger 
1643f1d2b4d3SLarry Finger 		/* we just wait 1s for all queues */
1644f1d2b4d3SLarry Finger 		if (rtlpriv->psc.rfpwr_state == ERFOFF ||
1645f1d2b4d3SLarry Finger 		    is_hal_stop(rtlhal) || i >= 200)
1646f1d2b4d3SLarry Finger 			return;
1647f1d2b4d3SLarry Finger 	}
1648f1d2b4d3SLarry Finger }
1649f1d2b4d3SLarry Finger 
rtl_pci_deinit(struct ieee80211_hw * hw)1650f1d2b4d3SLarry Finger static void rtl_pci_deinit(struct ieee80211_hw *hw)
1651f1d2b4d3SLarry Finger {
1652f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1653f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1654f1d2b4d3SLarry Finger 
1655f1d2b4d3SLarry Finger 	_rtl_pci_deinit_trx_ring(hw);
1656f1d2b4d3SLarry Finger 
1657f1d2b4d3SLarry Finger 	synchronize_irq(rtlpci->pdev->irq);
1658f1d2b4d3SLarry Finger 	tasklet_kill(&rtlpriv->works.irq_tasklet);
1659f1d2b4d3SLarry Finger 	cancel_work_sync(&rtlpriv->works.lps_change_work);
1660f1d2b4d3SLarry Finger }
1661f1d2b4d3SLarry Finger 
rtl_pci_init(struct ieee80211_hw * hw,struct pci_dev * pdev)1662f1d2b4d3SLarry Finger static int rtl_pci_init(struct ieee80211_hw *hw, struct pci_dev *pdev)
1663f1d2b4d3SLarry Finger {
1664f1d2b4d3SLarry Finger 	int err;
1665f1d2b4d3SLarry Finger 
1666f1d2b4d3SLarry Finger 	_rtl_pci_init_struct(hw, pdev);
1667f1d2b4d3SLarry Finger 
1668f1d2b4d3SLarry Finger 	err = _rtl_pci_init_trx_ring(hw);
1669f1d2b4d3SLarry Finger 	if (err) {
1670b03d968bSLarry Finger 		pr_err("tx ring initialization failed\n");
1671f1d2b4d3SLarry Finger 		return err;
1672f1d2b4d3SLarry Finger 	}
1673f1d2b4d3SLarry Finger 
1674f1d2b4d3SLarry Finger 	return 0;
1675f1d2b4d3SLarry Finger }
1676f1d2b4d3SLarry Finger 
rtl_pci_start(struct ieee80211_hw * hw)1677f1d2b4d3SLarry Finger static int rtl_pci_start(struct ieee80211_hw *hw)
1678f1d2b4d3SLarry Finger {
1679f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1680f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1681f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1682f1d2b4d3SLarry Finger 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
16838d0d43e3SPing-Ke Shih 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
16849177c336SPing-Ke Shih 	struct rtl_btc_ops *btc_ops = rtlpriv->btcoexist.btc_ops;
1685f1d2b4d3SLarry Finger 
1686f1d2b4d3SLarry Finger 	int err;
1687f1d2b4d3SLarry Finger 
1688f1d2b4d3SLarry Finger 	rtl_pci_reset_trx_ring(hw);
1689f1d2b4d3SLarry Finger 
1690f1d2b4d3SLarry Finger 	rtlpci->driver_is_goingto_unload = false;
1691f1d2b4d3SLarry Finger 	if (rtlpriv->cfg->ops->get_btc_status &&
1692f1d2b4d3SLarry Finger 	    rtlpriv->cfg->ops->get_btc_status()) {
1693f1cb27edSPing-Ke Shih 		rtlpriv->btcoexist.btc_info.ap_num = 36;
16949177c336SPing-Ke Shih 		btc_ops->btc_init_variables(rtlpriv);
16959177c336SPing-Ke Shih 		btc_ops->btc_init_hal_vars(rtlpriv);
16969177c336SPing-Ke Shih 	} else if (btc_ops) {
16979177c336SPing-Ke Shih 		btc_ops->btc_init_variables_wifi_only(rtlpriv);
1698f1d2b4d3SLarry Finger 	}
16999177c336SPing-Ke Shih 
1700f1d2b4d3SLarry Finger 	err = rtlpriv->cfg->ops->hw_init(hw);
1701f1d2b4d3SLarry Finger 	if (err) {
1702f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
1703f1d2b4d3SLarry Finger 			"Failed to config hardware!\n");
17048cc782cdSLarry Finger 		kfree(rtlpriv->btcoexist.btc_context);
17058cc782cdSLarry Finger 		kfree(rtlpriv->btcoexist.wifi_only_context);
1706f1d2b4d3SLarry Finger 		return err;
1707f1d2b4d3SLarry Finger 	}
17088d0d43e3SPing-Ke Shih 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
17098d0d43e3SPing-Ke Shih 			&rtlmac->retry_long);
1710f1d2b4d3SLarry Finger 
1711f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->enable_interrupt(hw);
1712f108a420SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "enable_interrupt OK\n");
1713f1d2b4d3SLarry Finger 
1714f1d2b4d3SLarry Finger 	rtl_init_rx_config(hw);
1715f1d2b4d3SLarry Finger 
1716f1d2b4d3SLarry Finger 	/*should be after adapter start and interrupt enable. */
1717f1d2b4d3SLarry Finger 	set_hal_start(rtlhal);
1718f1d2b4d3SLarry Finger 
1719f1d2b4d3SLarry Finger 	RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
1720f1d2b4d3SLarry Finger 
1721f1d2b4d3SLarry Finger 	rtlpci->up_first_time = false;
1722f1d2b4d3SLarry Finger 
1723f108a420SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG, "%s OK\n", __func__);
1724f1d2b4d3SLarry Finger 	return 0;
1725f1d2b4d3SLarry Finger }
1726f1d2b4d3SLarry Finger 
rtl_pci_stop(struct ieee80211_hw * hw)1727f1d2b4d3SLarry Finger static void rtl_pci_stop(struct ieee80211_hw *hw)
1728f1d2b4d3SLarry Finger {
1729f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1730f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1731f1d2b4d3SLarry Finger 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1732f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1733f1d2b4d3SLarry Finger 	unsigned long flags;
1734ae0122b6SLarry Finger 	u8 rf_timeout = 0;
1735f1d2b4d3SLarry Finger 
1736f1d2b4d3SLarry Finger 	if (rtlpriv->cfg->ops->get_btc_status())
173740d9dd4fSPing-Ke Shih 		rtlpriv->btcoexist.btc_ops->btc_halt_notify(rtlpriv);
173840d9dd4fSPing-Ke Shih 
173940d9dd4fSPing-Ke Shih 	if (rtlpriv->btcoexist.btc_ops)
174040d9dd4fSPing-Ke Shih 		rtlpriv->btcoexist.btc_ops->btc_deinit_variables(rtlpriv);
1741f1d2b4d3SLarry Finger 
1742ae0122b6SLarry Finger 	/*should be before disable interrupt&adapter
1743f1d2b4d3SLarry Finger 	 *and will do it immediately.
1744f1d2b4d3SLarry Finger 	 */
1745f1d2b4d3SLarry Finger 	set_hal_stop(rtlhal);
1746f1d2b4d3SLarry Finger 
1747f1d2b4d3SLarry Finger 	rtlpci->driver_is_goingto_unload = true;
1748f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->disable_interrupt(hw);
1749f1d2b4d3SLarry Finger 	cancel_work_sync(&rtlpriv->works.lps_change_work);
1750f1d2b4d3SLarry Finger 
1751f1d2b4d3SLarry Finger 	spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1752f1d2b4d3SLarry Finger 	while (ppsc->rfchange_inprogress) {
1753f1d2b4d3SLarry Finger 		spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1754ae0122b6SLarry Finger 		if (rf_timeout > 100) {
1755f1d2b4d3SLarry Finger 			spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1756f1d2b4d3SLarry Finger 			break;
1757f1d2b4d3SLarry Finger 		}
1758f1d2b4d3SLarry Finger 		mdelay(1);
1759ae0122b6SLarry Finger 		rf_timeout++;
1760f1d2b4d3SLarry Finger 		spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1761f1d2b4d3SLarry Finger 	}
1762f1d2b4d3SLarry Finger 	ppsc->rfchange_inprogress = true;
1763f1d2b4d3SLarry Finger 	spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1764f1d2b4d3SLarry Finger 
1765f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->hw_disable(hw);
1766f1d2b4d3SLarry Finger 	/* some things are not needed if firmware not available */
1767f1d2b4d3SLarry Finger 	if (!rtlpriv->max_fw_size)
1768f1d2b4d3SLarry Finger 		return;
1769f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->led_control(hw, LED_CTL_POWER_OFF);
1770f1d2b4d3SLarry Finger 
1771f1d2b4d3SLarry Finger 	spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
1772f1d2b4d3SLarry Finger 	ppsc->rfchange_inprogress = false;
1773f1d2b4d3SLarry Finger 	spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
1774f1d2b4d3SLarry Finger 
1775f1d2b4d3SLarry Finger 	rtl_pci_enable_aspm(hw);
1776f1d2b4d3SLarry Finger }
1777f1d2b4d3SLarry Finger 
_rtl_pci_find_adapter(struct pci_dev * pdev,struct ieee80211_hw * hw)1778f1d2b4d3SLarry Finger static bool _rtl_pci_find_adapter(struct pci_dev *pdev,
1779f1d2b4d3SLarry Finger 				  struct ieee80211_hw *hw)
1780f1d2b4d3SLarry Finger {
1781f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1782f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1783f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1784f1d2b4d3SLarry Finger 	struct pci_dev *bridge_pdev = pdev->bus->self;
1785f1d2b4d3SLarry Finger 	u16 venderid;
1786f1d2b4d3SLarry Finger 	u16 deviceid;
1787f1d2b4d3SLarry Finger 	u8 revisionid;
1788f1d2b4d3SLarry Finger 	u16 irqline;
1789f1d2b4d3SLarry Finger 	u8 tmp;
1790f1d2b4d3SLarry Finger 
1791f1d2b4d3SLarry Finger 	pcipriv->ndis_adapter.pcibridge_vendor = PCI_BRIDGE_VENDOR_UNKNOWN;
1792f1d2b4d3SLarry Finger 	venderid = pdev->vendor;
1793f1d2b4d3SLarry Finger 	deviceid = pdev->device;
1794f1d2b4d3SLarry Finger 	pci_read_config_byte(pdev, 0x8, &revisionid);
1795f1d2b4d3SLarry Finger 	pci_read_config_word(pdev, 0x3C, &irqline);
1796f1d2b4d3SLarry Finger 
1797f1d2b4d3SLarry Finger 	/* PCI ID 0x10ec:0x8192 occurs for both RTL8192E, which uses
1798f1d2b4d3SLarry Finger 	 * r8192e_pci, and RTL8192SE, which uses this driver. If the
1799f1d2b4d3SLarry Finger 	 * revision ID is RTL_PCI_REVISION_ID_8192PCIE (0x01), then
1800f1d2b4d3SLarry Finger 	 * the correct driver is r8192e_pci, thus this routine should
1801f1d2b4d3SLarry Finger 	 * return false.
1802f1d2b4d3SLarry Finger 	 */
1803f1d2b4d3SLarry Finger 	if (deviceid == RTL_PCI_8192SE_DID &&
1804f1d2b4d3SLarry Finger 	    revisionid == RTL_PCI_REVISION_ID_8192PCIE)
1805f1d2b4d3SLarry Finger 		return false;
1806f1d2b4d3SLarry Finger 
1807f1d2b4d3SLarry Finger 	if (deviceid == RTL_PCI_8192_DID ||
1808f1d2b4d3SLarry Finger 	    deviceid == RTL_PCI_0044_DID ||
1809f1d2b4d3SLarry Finger 	    deviceid == RTL_PCI_0047_DID ||
1810f1d2b4d3SLarry Finger 	    deviceid == RTL_PCI_8192SE_DID ||
1811f1d2b4d3SLarry Finger 	    deviceid == RTL_PCI_8174_DID ||
1812f1d2b4d3SLarry Finger 	    deviceid == RTL_PCI_8173_DID ||
1813f1d2b4d3SLarry Finger 	    deviceid == RTL_PCI_8172_DID ||
1814f1d2b4d3SLarry Finger 	    deviceid == RTL_PCI_8171_DID) {
1815f1d2b4d3SLarry Finger 		switch (revisionid) {
1816f1d2b4d3SLarry Finger 		case RTL_PCI_REVISION_ID_8192PCIE:
1817f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
1818f1d2b4d3SLarry Finger 				"8192 PCI-E is found - vid/did=%x/%x\n",
1819f1d2b4d3SLarry Finger 				venderid, deviceid);
1820f1d2b4d3SLarry Finger 			rtlhal->hw_type = HARDWARE_TYPE_RTL8192E;
1821f1d2b4d3SLarry Finger 			return false;
1822f1d2b4d3SLarry Finger 		case RTL_PCI_REVISION_ID_8192SE:
1823f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
1824f1d2b4d3SLarry Finger 				"8192SE is found - vid/did=%x/%x\n",
1825f1d2b4d3SLarry Finger 				venderid, deviceid);
1826f1d2b4d3SLarry Finger 			rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
1827f1d2b4d3SLarry Finger 			break;
1828f1d2b4d3SLarry Finger 		default:
1829f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
1830f1d2b4d3SLarry Finger 				"Err: Unknown device - vid/did=%x/%x\n",
1831f1d2b4d3SLarry Finger 				venderid, deviceid);
1832f1d2b4d3SLarry Finger 			rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
1833f1d2b4d3SLarry Finger 			break;
1834f1d2b4d3SLarry Finger 		}
1835f1d2b4d3SLarry Finger 	} else if (deviceid == RTL_PCI_8723AE_DID) {
1836f1d2b4d3SLarry Finger 		rtlhal->hw_type = HARDWARE_TYPE_RTL8723AE;
1837f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
1838ae0122b6SLarry Finger 			"8723AE PCI-E is found - vid/did=%x/%x\n",
1839ae0122b6SLarry Finger 			venderid, deviceid);
1840f1d2b4d3SLarry Finger 	} else if (deviceid == RTL_PCI_8192CET_DID ||
1841f1d2b4d3SLarry Finger 		   deviceid == RTL_PCI_8192CE_DID ||
1842f1d2b4d3SLarry Finger 		   deviceid == RTL_PCI_8191CE_DID ||
1843f1d2b4d3SLarry Finger 		   deviceid == RTL_PCI_8188CE_DID) {
1844f1d2b4d3SLarry Finger 		rtlhal->hw_type = HARDWARE_TYPE_RTL8192CE;
1845f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
1846f1d2b4d3SLarry Finger 			"8192C PCI-E is found - vid/did=%x/%x\n",
1847f1d2b4d3SLarry Finger 			venderid, deviceid);
1848f1d2b4d3SLarry Finger 	} else if (deviceid == RTL_PCI_8192DE_DID ||
1849f1d2b4d3SLarry Finger 		   deviceid == RTL_PCI_8192DE_DID2) {
1850f1d2b4d3SLarry Finger 		rtlhal->hw_type = HARDWARE_TYPE_RTL8192DE;
1851f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
1852f1d2b4d3SLarry Finger 			"8192D PCI-E is found - vid/did=%x/%x\n",
1853f1d2b4d3SLarry Finger 			venderid, deviceid);
1854f1d2b4d3SLarry Finger 	} else if (deviceid == RTL_PCI_8188EE_DID) {
1855f1d2b4d3SLarry Finger 		rtlhal->hw_type = HARDWARE_TYPE_RTL8188EE;
1856f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1857f1d2b4d3SLarry Finger 			"Find adapter, Hardware type is 8188EE\n");
1858f1d2b4d3SLarry Finger 	} else if (deviceid == RTL_PCI_8723BE_DID) {
1859f1d2b4d3SLarry Finger 		rtlhal->hw_type = HARDWARE_TYPE_RTL8723BE;
1860f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1861f1d2b4d3SLarry Finger 			"Find adapter, Hardware type is 8723BE\n");
1862f1d2b4d3SLarry Finger 	} else if (deviceid == RTL_PCI_8192EE_DID) {
1863f1d2b4d3SLarry Finger 		rtlhal->hw_type = HARDWARE_TYPE_RTL8192EE;
1864f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1865f1d2b4d3SLarry Finger 			"Find adapter, Hardware type is 8192EE\n");
1866f1d2b4d3SLarry Finger 	} else if (deviceid == RTL_PCI_8821AE_DID) {
1867f1d2b4d3SLarry Finger 		rtlhal->hw_type = HARDWARE_TYPE_RTL8821AE;
1868f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1869f1d2b4d3SLarry Finger 			"Find adapter, Hardware type is 8821AE\n");
1870f1d2b4d3SLarry Finger 	} else if (deviceid == RTL_PCI_8812AE_DID) {
1871f1d2b4d3SLarry Finger 		rtlhal->hw_type = HARDWARE_TYPE_RTL8812AE;
1872f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1873f1d2b4d3SLarry Finger 			"Find adapter, Hardware type is 8812AE\n");
187468929a83SPing-Ke Shih 	} else if (deviceid == RTL_PCI_8822BE_DID) {
187568929a83SPing-Ke Shih 		rtlhal->hw_type = HARDWARE_TYPE_RTL8822BE;
187668929a83SPing-Ke Shih 		rtlhal->bandset = BAND_ON_BOTH;
1877f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
187868929a83SPing-Ke Shih 			"Find adapter, Hardware type is 8822BE\n");
1879f1d2b4d3SLarry Finger 	} else {
1880f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
1881f1d2b4d3SLarry Finger 			"Err: Unknown device - vid/did=%x/%x\n",
1882f1d2b4d3SLarry Finger 			 venderid, deviceid);
1883f1d2b4d3SLarry Finger 
1884f1d2b4d3SLarry Finger 		rtlhal->hw_type = RTL_DEFAULT_HARDWARE_TYPE;
1885f1d2b4d3SLarry Finger 	}
1886f1d2b4d3SLarry Finger 
1887f1d2b4d3SLarry Finger 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192DE) {
1888f1d2b4d3SLarry Finger 		if (revisionid == 0 || revisionid == 1) {
1889f1d2b4d3SLarry Finger 			if (revisionid == 0) {
1890f108a420SLarry Finger 				rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1891f1d2b4d3SLarry Finger 					"Find 92DE MAC0\n");
1892f1d2b4d3SLarry Finger 				rtlhal->interfaceindex = 0;
1893f1d2b4d3SLarry Finger 			} else if (revisionid == 1) {
1894f108a420SLarry Finger 				rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1895f1d2b4d3SLarry Finger 					"Find 92DE MAC1\n");
1896f1d2b4d3SLarry Finger 				rtlhal->interfaceindex = 1;
1897f1d2b4d3SLarry Finger 			}
1898f1d2b4d3SLarry Finger 		} else {
1899f108a420SLarry Finger 			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1900f1d2b4d3SLarry Finger 				"Unknown device - VendorID/DeviceID=%x/%x, Revision=%x\n",
1901f1d2b4d3SLarry Finger 				 venderid, deviceid, revisionid);
1902f1d2b4d3SLarry Finger 			rtlhal->interfaceindex = 0;
1903f1d2b4d3SLarry Finger 		}
1904f1d2b4d3SLarry Finger 	}
1905f1d2b4d3SLarry Finger 
190657869e4bSPing-Ke Shih 	switch (rtlhal->hw_type) {
190757869e4bSPing-Ke Shih 	case HARDWARE_TYPE_RTL8192EE:
190857869e4bSPing-Ke Shih 	case HARDWARE_TYPE_RTL8822BE:
190957869e4bSPing-Ke Shih 		/* use new trx flow */
1910f1d2b4d3SLarry Finger 		rtlpriv->use_new_trx_flow = true;
191157869e4bSPing-Ke Shih 		break;
191257869e4bSPing-Ke Shih 
191357869e4bSPing-Ke Shih 	default:
1914f1d2b4d3SLarry Finger 		rtlpriv->use_new_trx_flow = false;
191557869e4bSPing-Ke Shih 		break;
191657869e4bSPing-Ke Shih 	}
1917f1d2b4d3SLarry Finger 
1918f1d2b4d3SLarry Finger 	/*find bus info */
1919f1d2b4d3SLarry Finger 	pcipriv->ndis_adapter.busnumber = pdev->bus->number;
1920f1d2b4d3SLarry Finger 	pcipriv->ndis_adapter.devnumber = PCI_SLOT(pdev->devfn);
1921f1d2b4d3SLarry Finger 	pcipriv->ndis_adapter.funcnumber = PCI_FUNC(pdev->devfn);
1922f1d2b4d3SLarry Finger 
1923f1d2b4d3SLarry Finger 	/*find bridge info */
1924f1d2b4d3SLarry Finger 	pcipriv->ndis_adapter.pcibridge_vendor = PCI_BRIDGE_VENDOR_UNKNOWN;
1925f1d2b4d3SLarry Finger 	/* some ARM have no bridge_pdev and will crash here
1926f1d2b4d3SLarry Finger 	 * so we should check if bridge_pdev is NULL
1927f1d2b4d3SLarry Finger 	 */
1928f1d2b4d3SLarry Finger 	if (bridge_pdev) {
1929f1d2b4d3SLarry Finger 		/*find bridge info if available */
1930f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.pcibridge_vendorid = bridge_pdev->vendor;
1931f1d2b4d3SLarry Finger 		for (tmp = 0; tmp < PCI_BRIDGE_VENDOR_MAX; tmp++) {
1932f1d2b4d3SLarry Finger 			if (bridge_pdev->vendor == pcibridge_vendors[tmp]) {
1933f1d2b4d3SLarry Finger 				pcipriv->ndis_adapter.pcibridge_vendor = tmp;
1934f108a420SLarry Finger 				rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
1935f1d2b4d3SLarry Finger 					"Pci Bridge Vendor is found index: %d\n",
1936f1d2b4d3SLarry Finger 					tmp);
1937f1d2b4d3SLarry Finger 				break;
1938f1d2b4d3SLarry Finger 			}
1939f1d2b4d3SLarry Finger 		}
1940f1d2b4d3SLarry Finger 	}
1941f1d2b4d3SLarry Finger 
1942f1d2b4d3SLarry Finger 	if (pcipriv->ndis_adapter.pcibridge_vendor !=
1943f1d2b4d3SLarry Finger 		PCI_BRIDGE_VENDOR_UNKNOWN) {
1944f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.pcibridge_busnum =
1945f1d2b4d3SLarry Finger 		    bridge_pdev->bus->number;
1946f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.pcibridge_devnum =
1947f1d2b4d3SLarry Finger 		    PCI_SLOT(bridge_pdev->devfn);
1948f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.pcibridge_funcnum =
1949f1d2b4d3SLarry Finger 		    PCI_FUNC(bridge_pdev->devfn);
1950f1d2b4d3SLarry Finger 
1951f1d2b4d3SLarry Finger 		if (pcipriv->ndis_adapter.pcibridge_vendor ==
1952f1d2b4d3SLarry Finger 		    PCI_BRIDGE_VENDOR_AMD) {
1953f1d2b4d3SLarry Finger 			pcipriv->ndis_adapter.amd_l1_patch =
1954f1d2b4d3SLarry Finger 			    rtl_pci_get_amd_l1_patch(hw);
1955f1d2b4d3SLarry Finger 		}
1956f1d2b4d3SLarry Finger 	}
1957f1d2b4d3SLarry Finger 
1958f108a420SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
1959f1d2b4d3SLarry Finger 		"pcidev busnumber:devnumber:funcnumber:vendor:link_ctl %d:%d:%d:%x:%x\n",
1960f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.busnumber,
1961f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.devnumber,
1962f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.funcnumber,
1963f1d2b4d3SLarry Finger 		pdev->vendor, pcipriv->ndis_adapter.linkctrl_reg);
1964f1d2b4d3SLarry Finger 
1965f108a420SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
19661f018dfaSIlpo Järvinen 		"pci_bridge busnumber:devnumber:funcnumber:vendor:amd %d:%d:%d:%x:%x\n",
1967f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.pcibridge_busnum,
1968f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.pcibridge_devnum,
1969f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.pcibridge_funcnum,
1970f1d2b4d3SLarry Finger 		pcibridge_vendors[pcipriv->ndis_adapter.pcibridge_vendor],
1971f1d2b4d3SLarry Finger 		pcipriv->ndis_adapter.amd_l1_patch);
1972f1d2b4d3SLarry Finger 
1973f1d2b4d3SLarry Finger 	rtl_pci_parse_configuration(pdev, hw);
1974f1d2b4d3SLarry Finger 
1975f1d2b4d3SLarry Finger 	return true;
1976f1d2b4d3SLarry Finger }
1977f1d2b4d3SLarry Finger 
rtl_pci_intr_mode_msi(struct ieee80211_hw * hw)1978f1d2b4d3SLarry Finger static int rtl_pci_intr_mode_msi(struct ieee80211_hw *hw)
1979f1d2b4d3SLarry Finger {
1980f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1981f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
1982f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
1983f1d2b4d3SLarry Finger 	int ret;
1984f1d2b4d3SLarry Finger 
1985f1d2b4d3SLarry Finger 	ret = pci_enable_msi(rtlpci->pdev);
1986f1d2b4d3SLarry Finger 	if (ret < 0)
1987f1d2b4d3SLarry Finger 		return ret;
1988f1d2b4d3SLarry Finger 
1989f1d2b4d3SLarry Finger 	ret = request_irq(rtlpci->pdev->irq, &_rtl_pci_interrupt,
1990f1d2b4d3SLarry Finger 			  IRQF_SHARED, KBUILD_MODNAME, hw);
1991f1d2b4d3SLarry Finger 	if (ret < 0) {
1992f1d2b4d3SLarry Finger 		pci_disable_msi(rtlpci->pdev);
1993f1d2b4d3SLarry Finger 		return ret;
1994f1d2b4d3SLarry Finger 	}
1995f1d2b4d3SLarry Finger 
1996f1d2b4d3SLarry Finger 	rtlpci->using_msi = true;
1997f1d2b4d3SLarry Finger 
1998f108a420SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT | COMP_INTR, DBG_DMESG,
1999f1d2b4d3SLarry Finger 		"MSI Interrupt Mode!\n");
2000f1d2b4d3SLarry Finger 	return 0;
2001f1d2b4d3SLarry Finger }
2002f1d2b4d3SLarry Finger 
rtl_pci_intr_mode_legacy(struct ieee80211_hw * hw)2003f1d2b4d3SLarry Finger static int rtl_pci_intr_mode_legacy(struct ieee80211_hw *hw)
2004f1d2b4d3SLarry Finger {
2005f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2006f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
2007f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
2008f1d2b4d3SLarry Finger 	int ret;
2009f1d2b4d3SLarry Finger 
2010f1d2b4d3SLarry Finger 	ret = request_irq(rtlpci->pdev->irq, &_rtl_pci_interrupt,
2011f1d2b4d3SLarry Finger 			  IRQF_SHARED, KBUILD_MODNAME, hw);
2012f1d2b4d3SLarry Finger 	if (ret < 0)
2013f1d2b4d3SLarry Finger 		return ret;
2014f1d2b4d3SLarry Finger 
2015f1d2b4d3SLarry Finger 	rtlpci->using_msi = false;
2016f108a420SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT | COMP_INTR, DBG_DMESG,
2017f1d2b4d3SLarry Finger 		"Pin-based Interrupt Mode!\n");
2018f1d2b4d3SLarry Finger 	return 0;
2019f1d2b4d3SLarry Finger }
2020f1d2b4d3SLarry Finger 
rtl_pci_intr_mode_decide(struct ieee80211_hw * hw)2021f1d2b4d3SLarry Finger static int rtl_pci_intr_mode_decide(struct ieee80211_hw *hw)
2022f1d2b4d3SLarry Finger {
2023f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
2024f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
2025f1d2b4d3SLarry Finger 	int ret;
2026f1d2b4d3SLarry Finger 
2027f1d2b4d3SLarry Finger 	if (rtlpci->msi_support) {
2028f1d2b4d3SLarry Finger 		ret = rtl_pci_intr_mode_msi(hw);
2029f1d2b4d3SLarry Finger 		if (ret < 0)
2030f1d2b4d3SLarry Finger 			ret = rtl_pci_intr_mode_legacy(hw);
2031f1d2b4d3SLarry Finger 	} else {
2032f1d2b4d3SLarry Finger 		ret = rtl_pci_intr_mode_legacy(hw);
2033f1d2b4d3SLarry Finger 	}
2034f1d2b4d3SLarry Finger 	return ret;
2035f1d2b4d3SLarry Finger }
2036f1d2b4d3SLarry Finger 
platform_enable_dma64(struct pci_dev * pdev,bool dma64)20370c07bd74SPing-Ke Shih static void platform_enable_dma64(struct pci_dev *pdev, bool dma64)
20380c07bd74SPing-Ke Shih {
20390c07bd74SPing-Ke Shih 	u8	value;
20400c07bd74SPing-Ke Shih 
20410c07bd74SPing-Ke Shih 	pci_read_config_byte(pdev, 0x719, &value);
20420c07bd74SPing-Ke Shih 
20430c07bd74SPing-Ke Shih 	/* 0x719 Bit5 is DMA64 bit fetch. */
20440c07bd74SPing-Ke Shih 	if (dma64)
20450c07bd74SPing-Ke Shih 		value |= BIT(5);
20460c07bd74SPing-Ke Shih 	else
20470c07bd74SPing-Ke Shih 		value &= ~BIT(5);
20480c07bd74SPing-Ke Shih 
20490c07bd74SPing-Ke Shih 	pci_write_config_byte(pdev, 0x719, value);
20500c07bd74SPing-Ke Shih }
20510c07bd74SPing-Ke Shih 
rtl_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)2052f1d2b4d3SLarry Finger int rtl_pci_probe(struct pci_dev *pdev,
2053f1d2b4d3SLarry Finger 		  const struct pci_device_id *id)
2054f1d2b4d3SLarry Finger {
2055f1d2b4d3SLarry Finger 	struct ieee80211_hw *hw = NULL;
2056f1d2b4d3SLarry Finger 
2057f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = NULL;
2058f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = NULL;
2059f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci;
2060f1d2b4d3SLarry Finger 	unsigned long pmem_start, pmem_len, pmem_flags;
2061f1d2b4d3SLarry Finger 	int err;
2062f1d2b4d3SLarry Finger 
2063f1d2b4d3SLarry Finger 	err = pci_enable_device(pdev);
2064f1d2b4d3SLarry Finger 	if (err) {
2065531940f9SLarry Finger 		WARN_ONCE(true, "%s : Cannot enable new PCI device\n",
2066f1d2b4d3SLarry Finger 			  pci_name(pdev));
2067f1d2b4d3SLarry Finger 		return err;
2068f1d2b4d3SLarry Finger 	}
2069f1d2b4d3SLarry Finger 
20700c07bd74SPing-Ke Shih 	if (((struct rtl_hal_cfg *)id->driver_data)->mod_params->dma64 &&
20710dc0b5c2SChristophe JAILLET 	    !dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
20720dc0b5c2SChristophe JAILLET 		if (dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
20730c07bd74SPing-Ke Shih 			WARN_ONCE(true,
20740c07bd74SPing-Ke Shih 				  "Unable to obtain 64bit DMA for consistent allocations\n");
20750c07bd74SPing-Ke Shih 			err = -ENOMEM;
20760c07bd74SPing-Ke Shih 			goto fail1;
20770c07bd74SPing-Ke Shih 		}
20780c07bd74SPing-Ke Shih 
20790c07bd74SPing-Ke Shih 		platform_enable_dma64(pdev, true);
20800dc0b5c2SChristophe JAILLET 	} else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
20810dc0b5c2SChristophe JAILLET 		if (dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32))) {
2082531940f9SLarry Finger 			WARN_ONCE(true,
2083531940f9SLarry Finger 				  "rtlwifi: Unable to obtain 32bit DMA for consistent allocations\n");
2084f1d2b4d3SLarry Finger 			err = -ENOMEM;
2085f1d2b4d3SLarry Finger 			goto fail1;
2086f1d2b4d3SLarry Finger 		}
20870c07bd74SPing-Ke Shih 
20880c07bd74SPing-Ke Shih 		platform_enable_dma64(pdev, false);
2089f1d2b4d3SLarry Finger 	}
2090f1d2b4d3SLarry Finger 
2091f1d2b4d3SLarry Finger 	pci_set_master(pdev);
2092f1d2b4d3SLarry Finger 
2093f1d2b4d3SLarry Finger 	hw = ieee80211_alloc_hw(sizeof(struct rtl_pci_priv) +
2094f1d2b4d3SLarry Finger 				sizeof(struct rtl_priv), &rtl_ops);
2095f1d2b4d3SLarry Finger 	if (!hw) {
2096531940f9SLarry Finger 		WARN_ONCE(true,
2097f1d2b4d3SLarry Finger 			  "%s : ieee80211 alloc failed\n", pci_name(pdev));
2098f1d2b4d3SLarry Finger 		err = -ENOMEM;
2099f1d2b4d3SLarry Finger 		goto fail1;
2100f1d2b4d3SLarry Finger 	}
2101f1d2b4d3SLarry Finger 
2102f1d2b4d3SLarry Finger 	SET_IEEE80211_DEV(hw, &pdev->dev);
2103f1d2b4d3SLarry Finger 	pci_set_drvdata(pdev, hw);
2104f1d2b4d3SLarry Finger 
2105f1d2b4d3SLarry Finger 	rtlpriv = hw->priv;
2106f1d2b4d3SLarry Finger 	rtlpriv->hw = hw;
2107f1d2b4d3SLarry Finger 	pcipriv = (void *)rtlpriv->priv;
2108f1d2b4d3SLarry Finger 	pcipriv->dev.pdev = pdev;
2109f1d2b4d3SLarry Finger 	init_completion(&rtlpriv->firmware_loading_complete);
2110f1d2b4d3SLarry Finger 	/*proximity init here*/
2111f1d2b4d3SLarry Finger 	rtlpriv->proximity.proxim_on = false;
2112f1d2b4d3SLarry Finger 
2113f1d2b4d3SLarry Finger 	pcipriv = (void *)rtlpriv->priv;
2114f1d2b4d3SLarry Finger 	pcipriv->dev.pdev = pdev;
2115f1d2b4d3SLarry Finger 
2116f1d2b4d3SLarry Finger 	/* init cfg & intf_ops */
2117f1d2b4d3SLarry Finger 	rtlpriv->rtlhal.interface = INTF_PCI;
2118f1d2b4d3SLarry Finger 	rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_data);
2119f1d2b4d3SLarry Finger 	rtlpriv->intf_ops = &rtl_pci_ops;
21202cdd634eSPing-Ke Shih 	rtl_efuse_ops_init(hw);
2121f1d2b4d3SLarry Finger 
2122f1d2b4d3SLarry Finger 	/* MEM map */
2123f1d2b4d3SLarry Finger 	err = pci_request_regions(pdev, KBUILD_MODNAME);
2124f1d2b4d3SLarry Finger 	if (err) {
2125531940f9SLarry Finger 		WARN_ONCE(true, "rtlwifi: Can't obtain PCI resources\n");
2126f1d2b4d3SLarry Finger 		goto fail1;
2127f1d2b4d3SLarry Finger 	}
2128f1d2b4d3SLarry Finger 
2129f1d2b4d3SLarry Finger 	pmem_start = pci_resource_start(pdev, rtlpriv->cfg->bar_id);
2130f1d2b4d3SLarry Finger 	pmem_len = pci_resource_len(pdev, rtlpriv->cfg->bar_id);
2131f1d2b4d3SLarry Finger 	pmem_flags = pci_resource_flags(pdev, rtlpriv->cfg->bar_id);
2132f1d2b4d3SLarry Finger 
2133f1d2b4d3SLarry Finger 	/*shared mem start */
2134f1d2b4d3SLarry Finger 	rtlpriv->io.pci_mem_start =
2135f1d2b4d3SLarry Finger 			(unsigned long)pci_iomap(pdev,
2136f1d2b4d3SLarry Finger 			rtlpriv->cfg->bar_id, pmem_len);
2137f1d2b4d3SLarry Finger 	if (rtlpriv->io.pci_mem_start == 0) {
2138531940f9SLarry Finger 		WARN_ONCE(true, "rtlwifi: Can't map PCI mem\n");
2139f1d2b4d3SLarry Finger 		err = -ENOMEM;
2140f1d2b4d3SLarry Finger 		goto fail2;
2141f1d2b4d3SLarry Finger 	}
2142f1d2b4d3SLarry Finger 
2143f108a420SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
2144f1d2b4d3SLarry Finger 		"mem mapped space: start: 0x%08lx len:%08lx flags:%08lx, after map:0x%08lx\n",
2145f1d2b4d3SLarry Finger 		pmem_start, pmem_len, pmem_flags,
2146f1d2b4d3SLarry Finger 		rtlpriv->io.pci_mem_start);
2147f1d2b4d3SLarry Finger 
2148f1d2b4d3SLarry Finger 	/* Disable Clk Request */
2149f1d2b4d3SLarry Finger 	pci_write_config_byte(pdev, 0x81, 0);
2150f1d2b4d3SLarry Finger 	/* leave D3 mode */
2151f1d2b4d3SLarry Finger 	pci_write_config_byte(pdev, 0x44, 0);
2152f1d2b4d3SLarry Finger 	pci_write_config_byte(pdev, 0x04, 0x06);
2153f1d2b4d3SLarry Finger 	pci_write_config_byte(pdev, 0x04, 0x07);
2154f1d2b4d3SLarry Finger 
2155f1d2b4d3SLarry Finger 	/* find adapter */
2156f1d2b4d3SLarry Finger 	if (!_rtl_pci_find_adapter(pdev, hw)) {
2157f1d2b4d3SLarry Finger 		err = -ENODEV;
2158fc81bab5SMalcolm Priestley 		goto fail2;
2159f1d2b4d3SLarry Finger 	}
2160f1d2b4d3SLarry Finger 
2161f1d2b4d3SLarry Finger 	/* Init IO handler */
2162f1d2b4d3SLarry Finger 	_rtl_pci_io_handler_init(&pdev->dev, hw);
2163f1d2b4d3SLarry Finger 
2164f1d2b4d3SLarry Finger 	/*like read eeprom and so on */
2165f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->read_eeprom_info(hw);
2166f1d2b4d3SLarry Finger 
2167f1d2b4d3SLarry Finger 	if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
2168b03d968bSLarry Finger 		pr_err("Can't init_sw_vars\n");
2169f1d2b4d3SLarry Finger 		err = -ENODEV;
2170624cea89SThadeu Lima de Souza Cascardo 		goto fail2;
2171f1d2b4d3SLarry Finger 	}
2172084f1f55SDmitry Antipov 	rtl_init_sw_leds(hw);
2173f1d2b4d3SLarry Finger 
2174f1d2b4d3SLarry Finger 	/*aspm */
2175f1d2b4d3SLarry Finger 	rtl_pci_init_aspm(hw);
2176f1d2b4d3SLarry Finger 
2177f1d2b4d3SLarry Finger 	/* Init mac80211 sw */
2178f1d2b4d3SLarry Finger 	err = rtl_init_core(hw);
2179f1d2b4d3SLarry Finger 	if (err) {
2180b03d968bSLarry Finger 		pr_err("Can't allocate sw for mac80211\n");
2181f1d2b4d3SLarry Finger 		goto fail3;
2182f1d2b4d3SLarry Finger 	}
2183f1d2b4d3SLarry Finger 
2184f1d2b4d3SLarry Finger 	/* Init PCI sw */
2185f1d2b4d3SLarry Finger 	err = rtl_pci_init(hw, pdev);
2186f1d2b4d3SLarry Finger 	if (err) {
2187b03d968bSLarry Finger 		pr_err("Failed to init PCI\n");
2188624cea89SThadeu Lima de Souza Cascardo 		goto fail4;
2189f1d2b4d3SLarry Finger 	}
2190f1d2b4d3SLarry Finger 
2191f1d2b4d3SLarry Finger 	err = ieee80211_register_hw(hw);
2192f1d2b4d3SLarry Finger 	if (err) {
2193b03d968bSLarry Finger 		pr_err("Can't register mac80211 hw.\n");
2194f1d2b4d3SLarry Finger 		err = -ENODEV;
2195624cea89SThadeu Lima de Souza Cascardo 		goto fail5;
2196f1d2b4d3SLarry Finger 	}
2197f1d2b4d3SLarry Finger 	rtlpriv->mac80211.mac80211_registered = 1;
2198f1d2b4d3SLarry Finger 
2199610247f4SPing-Ke Shih 	/* add for debug */
2200610247f4SPing-Ke Shih 	rtl_debug_add_one(hw);
2201610247f4SPing-Ke Shih 
2202f1d2b4d3SLarry Finger 	/*init rfkill */
2203f1d2b4d3SLarry Finger 	rtl_init_rfkill(hw);	/* Init PCI sw */
2204f1d2b4d3SLarry Finger 
2205f1d2b4d3SLarry Finger 	rtlpci = rtl_pcidev(pcipriv);
2206f1d2b4d3SLarry Finger 	err = rtl_pci_intr_mode_decide(hw);
2207f1d2b4d3SLarry Finger 	if (err) {
2208f108a420SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
2209f1d2b4d3SLarry Finger 			"%s: failed to register IRQ handler\n",
2210f1d2b4d3SLarry Finger 			wiphy_name(hw->wiphy));
2211f1d2b4d3SLarry Finger 		goto fail3;
2212f1d2b4d3SLarry Finger 	}
2213f1d2b4d3SLarry Finger 	rtlpci->irq_alloc = 1;
2214f1d2b4d3SLarry Finger 
2215f1d2b4d3SLarry Finger 	set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
2216f1d2b4d3SLarry Finger 	return 0;
2217f1d2b4d3SLarry Finger 
2218624cea89SThadeu Lima de Souza Cascardo fail5:
2219624cea89SThadeu Lima de Souza Cascardo 	rtl_pci_deinit(hw);
2220624cea89SThadeu Lima de Souza Cascardo fail4:
2221f1d2b4d3SLarry Finger 	rtl_deinit_core(hw);
2222624cea89SThadeu Lima de Souza Cascardo fail3:
2223*c2fe1678SThadeu Lima de Souza Cascardo 	wait_for_completion(&rtlpriv->firmware_loading_complete);
2224624cea89SThadeu Lima de Souza Cascardo 	rtlpriv->cfg->ops->deinit_sw_vars(hw);
2225f1d2b4d3SLarry Finger 
2226fc81bab5SMalcolm Priestley fail2:
2227f1d2b4d3SLarry Finger 	if (rtlpriv->io.pci_mem_start != 0)
2228f1d2b4d3SLarry Finger 		pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
2229f1d2b4d3SLarry Finger 
2230f1d2b4d3SLarry Finger 	pci_release_regions(pdev);
2231f1d2b4d3SLarry Finger 
2232f1d2b4d3SLarry Finger fail1:
2233f1d2b4d3SLarry Finger 	if (hw)
2234f1d2b4d3SLarry Finger 		ieee80211_free_hw(hw);
2235f1d2b4d3SLarry Finger 	pci_disable_device(pdev);
2236f1d2b4d3SLarry Finger 
2237f1d2b4d3SLarry Finger 	return err;
2238f1d2b4d3SLarry Finger }
2239f1d2b4d3SLarry Finger EXPORT_SYMBOL(rtl_pci_probe);
2240f1d2b4d3SLarry Finger 
rtl_pci_disconnect(struct pci_dev * pdev)2241f1d2b4d3SLarry Finger void rtl_pci_disconnect(struct pci_dev *pdev)
2242f1d2b4d3SLarry Finger {
2243f1d2b4d3SLarry Finger 	struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2244f1d2b4d3SLarry Finger 	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
2245f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2246f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
2247f1d2b4d3SLarry Finger 	struct rtl_mac *rtlmac = rtl_mac(rtlpriv);
2248f1d2b4d3SLarry Finger 
2249f1d2b4d3SLarry Finger 	/* just in case driver is removed before firmware callback */
2250f1d2b4d3SLarry Finger 	wait_for_completion(&rtlpriv->firmware_loading_complete);
2251f1d2b4d3SLarry Finger 	clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
2252f1d2b4d3SLarry Finger 
2253610247f4SPing-Ke Shih 	/* remove form debug */
2254610247f4SPing-Ke Shih 	rtl_debug_remove_one(hw);
2255610247f4SPing-Ke Shih 
2256f1d2b4d3SLarry Finger 	/*ieee80211_unregister_hw will call ops_stop */
2257f1d2b4d3SLarry Finger 	if (rtlmac->mac80211_registered == 1) {
2258f1d2b4d3SLarry Finger 		ieee80211_unregister_hw(hw);
2259f1d2b4d3SLarry Finger 		rtlmac->mac80211_registered = 0;
2260f1d2b4d3SLarry Finger 	} else {
226112dfa2f6SPing-Ke Shih 		rtl_deinit_deferred_work(hw, false);
2262f1d2b4d3SLarry Finger 		rtlpriv->intf_ops->adapter_stop(hw);
2263f1d2b4d3SLarry Finger 	}
2264f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->disable_interrupt(hw);
2265f1d2b4d3SLarry Finger 
2266f1d2b4d3SLarry Finger 	/*deinit rfkill */
2267f1d2b4d3SLarry Finger 	rtl_deinit_rfkill(hw);
2268f1d2b4d3SLarry Finger 
2269f1d2b4d3SLarry Finger 	rtl_pci_deinit(hw);
2270f1d2b4d3SLarry Finger 	rtl_deinit_core(hw);
2271f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->deinit_sw_vars(hw);
2272f1d2b4d3SLarry Finger 
2273f1d2b4d3SLarry Finger 	if (rtlpci->irq_alloc) {
2274f1d2b4d3SLarry Finger 		free_irq(rtlpci->pdev->irq, hw);
2275f1d2b4d3SLarry Finger 		rtlpci->irq_alloc = 0;
2276f1d2b4d3SLarry Finger 	}
2277f1d2b4d3SLarry Finger 
2278f1d2b4d3SLarry Finger 	if (rtlpci->using_msi)
2279f1d2b4d3SLarry Finger 		pci_disable_msi(rtlpci->pdev);
2280f1d2b4d3SLarry Finger 
2281f1d2b4d3SLarry Finger 	if (rtlpriv->io.pci_mem_start != 0) {
2282f1d2b4d3SLarry Finger 		pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
2283f1d2b4d3SLarry Finger 		pci_release_regions(pdev);
2284f1d2b4d3SLarry Finger 	}
2285f1d2b4d3SLarry Finger 
2286f1d2b4d3SLarry Finger 	pci_disable_device(pdev);
2287f1d2b4d3SLarry Finger 
2288f1d2b4d3SLarry Finger 	rtl_pci_disable_aspm(hw);
2289f1d2b4d3SLarry Finger 
2290f1d2b4d3SLarry Finger 	pci_set_drvdata(pdev, NULL);
2291f1d2b4d3SLarry Finger 
2292f1d2b4d3SLarry Finger 	ieee80211_free_hw(hw);
2293f1d2b4d3SLarry Finger }
2294f1d2b4d3SLarry Finger EXPORT_SYMBOL(rtl_pci_disconnect);
2295f1d2b4d3SLarry Finger 
2296f1d2b4d3SLarry Finger #ifdef CONFIG_PM_SLEEP
2297f1d2b4d3SLarry Finger /***************************************
2298ae0122b6SLarry Finger  * kernel pci power state define:
2299ae0122b6SLarry Finger  * PCI_D0         ((pci_power_t __force) 0)
2300ae0122b6SLarry Finger  * PCI_D1         ((pci_power_t __force) 1)
2301ae0122b6SLarry Finger  * PCI_D2         ((pci_power_t __force) 2)
2302ae0122b6SLarry Finger  * PCI_D3hot      ((pci_power_t __force) 3)
2303ae0122b6SLarry Finger  * PCI_D3cold     ((pci_power_t __force) 4)
2304ae0122b6SLarry Finger  * PCI_UNKNOWN    ((pci_power_t __force) 5)
2305f1d2b4d3SLarry Finger 
2306ae0122b6SLarry Finger  * This function is called when system
2307ae0122b6SLarry Finger  * goes into suspend state mac80211 will
2308ae0122b6SLarry Finger  * call rtl_mac_stop() from the mac80211
2309ae0122b6SLarry Finger  * suspend function first, So there is
2310ae0122b6SLarry Finger  * no need to call hw_disable here.
2311f1d2b4d3SLarry Finger  ****************************************/
rtl_pci_suspend(struct device * dev)2312f1d2b4d3SLarry Finger int rtl_pci_suspend(struct device *dev)
2313f1d2b4d3SLarry Finger {
2314e7338e03SChuhong Yuan 	struct ieee80211_hw *hw = dev_get_drvdata(dev);
2315f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2316f1d2b4d3SLarry Finger 
2317f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->hw_suspend(hw);
2318f1d2b4d3SLarry Finger 	rtl_deinit_rfkill(hw);
2319f1d2b4d3SLarry Finger 
2320f1d2b4d3SLarry Finger 	return 0;
2321f1d2b4d3SLarry Finger }
2322f1d2b4d3SLarry Finger EXPORT_SYMBOL(rtl_pci_suspend);
2323f1d2b4d3SLarry Finger 
rtl_pci_resume(struct device * dev)2324f1d2b4d3SLarry Finger int rtl_pci_resume(struct device *dev)
2325f1d2b4d3SLarry Finger {
2326e7338e03SChuhong Yuan 	struct ieee80211_hw *hw = dev_get_drvdata(dev);
2327f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2328f1d2b4d3SLarry Finger 
2329f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->hw_resume(hw);
2330f1d2b4d3SLarry Finger 	rtl_init_rfkill(hw);
2331f1d2b4d3SLarry Finger 	return 0;
2332f1d2b4d3SLarry Finger }
2333f1d2b4d3SLarry Finger EXPORT_SYMBOL(rtl_pci_resume);
2334f1d2b4d3SLarry Finger #endif /* CONFIG_PM_SLEEP */
2335f1d2b4d3SLarry Finger 
23361bfcfdccSJulia Lawall const struct rtl_intf_ops rtl_pci_ops = {
2337f1d2b4d3SLarry Finger 	.read_efuse_byte = read_efuse_byte,
2338f1d2b4d3SLarry Finger 	.adapter_start = rtl_pci_start,
2339f1d2b4d3SLarry Finger 	.adapter_stop = rtl_pci_stop,
2340f1d2b4d3SLarry Finger 	.adapter_tx = rtl_pci_tx,
2341f1d2b4d3SLarry Finger 	.flush = rtl_pci_flush,
2342f1d2b4d3SLarry Finger 	.reset_trx_ring = rtl_pci_reset_trx_ring,
2343f1d2b4d3SLarry Finger 	.waitq_insert = rtl_pci_tx_chk_waitq_insert,
2344f1d2b4d3SLarry Finger 
2345f1d2b4d3SLarry Finger 	.disable_aspm = rtl_pci_disable_aspm,
2346f1d2b4d3SLarry Finger 	.enable_aspm = rtl_pci_enable_aspm,
2347f1d2b4d3SLarry Finger };
2348