pci.c 66 KB

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