pci.c 53 KB

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