pci.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  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->get_available_desc &&
  478. rtlpriv->cfg->ops->get_available_desc(hw, prio) <= 1) {
  479. RT_TRACE(rtlpriv, (COMP_INTR | COMP_SEND), DBG_DMESG,
  480. "no available desc!\n");
  481. return;
  482. }
  483. if (!rtlpriv->cfg->ops->is_tx_desc_closed(hw, prio, ring->idx))
  484. return;
  485. ring->idx = (ring->idx + 1) % ring->entries;
  486. skb = __skb_dequeue(&ring->queue);
  487. pci_unmap_single(rtlpci->pdev,
  488. rtlpriv->cfg->ops->
  489. get_desc((u8 *)entry, true,
  490. HW_DESC_TXBUFF_ADDR),
  491. skb->len, PCI_DMA_TODEVICE);
  492. /* remove early mode header */
  493. if (rtlpriv->rtlhal.earlymode_enable)
  494. skb_pull(skb, EM_HDR_LEN);
  495. RT_TRACE(rtlpriv, (COMP_INTR | COMP_SEND), DBG_TRACE,
  496. "new ring->idx:%d, free: skb_queue_len:%d, free: seq:%x\n",
  497. ring->idx,
  498. skb_queue_len(&ring->queue),
  499. *(u16 *)(skb->data + 22));
  500. if (prio == TXCMD_QUEUE) {
  501. dev_kfree_skb(skb);
  502. goto tx_status_ok;
  503. }
  504. /* for sw LPS, just after NULL skb send out, we can
  505. * sure AP knows we are sleeping, we should not let
  506. * rf sleep
  507. */
  508. fc = rtl_get_fc(skb);
  509. if (ieee80211_is_nullfunc(fc)) {
  510. if (ieee80211_has_pm(fc)) {
  511. rtlpriv->mac80211.offchan_delay = true;
  512. rtlpriv->psc.state_inap = true;
  513. } else {
  514. rtlpriv->psc.state_inap = false;
  515. }
  516. }
  517. if (ieee80211_is_action(fc)) {
  518. struct ieee80211_mgmt *action_frame =
  519. (struct ieee80211_mgmt *)skb->data;
  520. if (action_frame->u.action.u.ht_smps.action ==
  521. WLAN_HT_ACTION_SMPS) {
  522. dev_kfree_skb(skb);
  523. goto tx_status_ok;
  524. }
  525. }
  526. /* update tid tx pkt num */
  527. tid = rtl_get_tid(skb);
  528. if (tid <= 7)
  529. rtlpriv->link_info.tidtx_inperiod[tid]++;
  530. info = IEEE80211_SKB_CB(skb);
  531. ieee80211_tx_info_clear_status(info);
  532. info->flags |= IEEE80211_TX_STAT_ACK;
  533. /*info->status.rates[0].count = 1; */
  534. ieee80211_tx_status_irqsafe(hw, skb);
  535. if ((ring->entries - skb_queue_len(&ring->queue)) <= 4) {
  536. RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
  537. "more desc left, wake skb_queue@%d, ring->idx = %d, skb_queue_len = 0x%x\n",
  538. prio, ring->idx,
  539. skb_queue_len(&ring->queue));
  540. ieee80211_wake_queue(hw,
  541. skb_get_queue_mapping
  542. (skb));
  543. }
  544. tx_status_ok:
  545. skb = NULL;
  546. }
  547. if (((rtlpriv->link_info.num_rx_inperiod +
  548. rtlpriv->link_info.num_tx_inperiod) > 8) ||
  549. (rtlpriv->link_info.num_rx_inperiod > 2)) {
  550. rtlpriv->enter_ps = false;
  551. schedule_work(&rtlpriv->works.lps_change_work);
  552. }
  553. }
  554. static int _rtl_pci_init_one_rxdesc(struct ieee80211_hw *hw,
  555. struct sk_buff *new_skb, u8 *entry,
  556. int rxring_idx, int desc_idx)
  557. {
  558. struct rtl_priv *rtlpriv = rtl_priv(hw);
  559. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  560. u32 bufferaddress;
  561. u8 tmp_one = 1;
  562. struct sk_buff *skb;
  563. if (likely(new_skb)) {
  564. skb = new_skb;
  565. goto remap;
  566. }
  567. skb = dev_alloc_skb(rtlpci->rxbuffersize);
  568. if (!skb)
  569. return 0;
  570. remap:
  571. /* just set skb->cb to mapping addr for pci_unmap_single use */
  572. *((dma_addr_t *)skb->cb) =
  573. pci_map_single(rtlpci->pdev, skb_tail_pointer(skb),
  574. rtlpci->rxbuffersize, PCI_DMA_FROMDEVICE);
  575. bufferaddress = *((dma_addr_t *)skb->cb);
  576. if (pci_dma_mapping_error(rtlpci->pdev, bufferaddress))
  577. return 0;
  578. rtlpci->rx_ring[rxring_idx].rx_buf[desc_idx] = skb;
  579. if (rtlpriv->use_new_trx_flow) {
  580. rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
  581. HW_DESC_RX_PREPARE,
  582. (u8 *)&bufferaddress);
  583. } else {
  584. rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
  585. HW_DESC_RXBUFF_ADDR,
  586. (u8 *)&bufferaddress);
  587. rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
  588. HW_DESC_RXPKT_LEN,
  589. (u8 *)&rtlpci->rxbuffersize);
  590. rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
  591. HW_DESC_RXOWN,
  592. (u8 *)&tmp_one);
  593. }
  594. return 1;
  595. }
  596. /* inorder to receive 8K AMSDU we have set skb to
  597. * 9100bytes in init rx ring, but if this packet is
  598. * not a AMSDU, this large packet will be sent to
  599. * TCP/IP directly, this cause big packet ping fail
  600. * like: "ping -s 65507", so here we will realloc skb
  601. * based on the true size of packet, Mac80211
  602. * Probably will do it better, but does not yet.
  603. *
  604. * Some platform will fail when alloc skb sometimes.
  605. * in this condition, we will send the old skb to
  606. * mac80211 directly, this will not cause any other
  607. * issues, but only this packet will be lost by TCP/IP
  608. */
  609. static void _rtl_pci_rx_to_mac80211(struct ieee80211_hw *hw,
  610. struct sk_buff *skb,
  611. struct ieee80211_rx_status rx_status)
  612. {
  613. if (unlikely(!rtl_action_proc(hw, skb, false))) {
  614. dev_kfree_skb_any(skb);
  615. } else {
  616. struct sk_buff *uskb = NULL;
  617. u8 *pdata;
  618. uskb = dev_alloc_skb(skb->len + 128);
  619. if (likely(uskb)) {
  620. memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
  621. sizeof(rx_status));
  622. pdata = (u8 *)skb_put(uskb, skb->len);
  623. memcpy(pdata, skb->data, skb->len);
  624. dev_kfree_skb_any(skb);
  625. ieee80211_rx_irqsafe(hw, uskb);
  626. } else {
  627. ieee80211_rx_irqsafe(hw, skb);
  628. }
  629. }
  630. }
  631. /*hsisr interrupt handler*/
  632. static void _rtl_pci_hs_interrupt(struct ieee80211_hw *hw)
  633. {
  634. struct rtl_priv *rtlpriv = rtl_priv(hw);
  635. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  636. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[MAC_HSISR],
  637. rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[MAC_HSISR]) |
  638. rtlpci->sys_irq_mask);
  639. }
  640. static void _rtl_pci_rx_interrupt(struct ieee80211_hw *hw)
  641. {
  642. struct rtl_priv *rtlpriv = rtl_priv(hw);
  643. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  644. int rxring_idx = RTL_PCI_RX_MPDU_QUEUE;
  645. struct ieee80211_rx_status rx_status = { 0 };
  646. unsigned int count = rtlpci->rxringcount;
  647. u8 own;
  648. u8 tmp_one;
  649. bool unicast = false;
  650. u8 hw_queue = 0;
  651. unsigned int rx_remained_cnt;
  652. struct rtl_stats stats = {
  653. .signal = 0,
  654. .rate = 0,
  655. };
  656. /*RX NORMAL PKT */
  657. while (count--) {
  658. struct ieee80211_hdr *hdr;
  659. __le16 fc;
  660. u16 len;
  661. /*rx buffer descriptor */
  662. struct rtl_rx_buffer_desc *buffer_desc = NULL;
  663. /*if use new trx flow, it means wifi info */
  664. struct rtl_rx_desc *pdesc = NULL;
  665. /*rx pkt */
  666. struct sk_buff *skb = rtlpci->rx_ring[rxring_idx].rx_buf[
  667. rtlpci->rx_ring[rxring_idx].idx];
  668. struct sk_buff *new_skb;
  669. if (rtlpriv->use_new_trx_flow) {
  670. rx_remained_cnt =
  671. rtlpriv->cfg->ops->rx_desc_buff_remained_cnt(hw,
  672. hw_queue);
  673. if (rx_remained_cnt == 0)
  674. return;
  675. } else { /* rx descriptor */
  676. pdesc = &rtlpci->rx_ring[rxring_idx].desc[
  677. rtlpci->rx_ring[rxring_idx].idx];
  678. own = (u8)rtlpriv->cfg->ops->get_desc((u8 *)pdesc,
  679. false,
  680. HW_DESC_OWN);
  681. if (own) /* wait data to be filled by hardware */
  682. return;
  683. }
  684. /* Reaching this point means: data is filled already
  685. * AAAAAAttention !!!
  686. * We can NOT access 'skb' before 'pci_unmap_single'
  687. */
  688. pci_unmap_single(rtlpci->pdev, *((dma_addr_t *)skb->cb),
  689. rtlpci->rxbuffersize, PCI_DMA_FROMDEVICE);
  690. /* get a new skb - if fail, old one will be reused */
  691. new_skb = dev_alloc_skb(rtlpci->rxbuffersize);
  692. if (unlikely(!new_skb))
  693. goto no_new;
  694. if (rtlpriv->use_new_trx_flow) {
  695. buffer_desc =
  696. &rtlpci->rx_ring[rxring_idx].buffer_desc
  697. [rtlpci->rx_ring[rxring_idx].idx];
  698. /*means rx wifi info*/
  699. pdesc = (struct rtl_rx_desc *)skb->data;
  700. }
  701. memset(&rx_status , 0 , sizeof(rx_status));
  702. rtlpriv->cfg->ops->query_rx_desc(hw, &stats,
  703. &rx_status, (u8 *)pdesc, skb);
  704. if (rtlpriv->use_new_trx_flow)
  705. rtlpriv->cfg->ops->rx_check_dma_ok(hw,
  706. (u8 *)buffer_desc,
  707. hw_queue);
  708. len = rtlpriv->cfg->ops->get_desc((u8 *)pdesc, false,
  709. HW_DESC_RXPKT_LEN);
  710. if (skb->end - skb->tail > len) {
  711. skb_put(skb, len);
  712. if (rtlpriv->use_new_trx_flow)
  713. skb_reserve(skb, stats.rx_drvinfo_size +
  714. stats.rx_bufshift + 24);
  715. else
  716. skb_reserve(skb, stats.rx_drvinfo_size +
  717. stats.rx_bufshift);
  718. } else {
  719. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  720. "skb->end - skb->tail = %d, len is %d\n",
  721. skb->end - skb->tail, len);
  722. dev_kfree_skb_any(skb);
  723. goto new_trx_end;
  724. }
  725. /* handle command packet here */
  726. if (rtlpriv->cfg->ops->rx_command_packet &&
  727. rtlpriv->cfg->ops->rx_command_packet(hw, stats, skb)) {
  728. dev_kfree_skb_any(skb);
  729. goto new_trx_end;
  730. }
  731. /*
  732. * NOTICE This can not be use for mac80211,
  733. * this is done in mac80211 code,
  734. * if done here sec DHCP will fail
  735. * skb_trim(skb, skb->len - 4);
  736. */
  737. hdr = rtl_get_hdr(skb);
  738. fc = rtl_get_fc(skb);
  739. if (!stats.crc && !stats.hwerror) {
  740. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status,
  741. sizeof(rx_status));
  742. if (is_broadcast_ether_addr(hdr->addr1)) {
  743. ;/*TODO*/
  744. } else if (is_multicast_ether_addr(hdr->addr1)) {
  745. ;/*TODO*/
  746. } else {
  747. unicast = true;
  748. rtlpriv->stats.rxbytesunicast += skb->len;
  749. }
  750. rtl_is_special_data(hw, skb, false, true);
  751. if (ieee80211_is_data(fc)) {
  752. rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
  753. if (unicast)
  754. rtlpriv->link_info.num_rx_inperiod++;
  755. }
  756. /* static bcn for roaming */
  757. rtl_beacon_statistic(hw, skb);
  758. rtl_p2p_info(hw, (void *)skb->data, skb->len);
  759. /* for sw lps */
  760. rtl_swlps_beacon(hw, (void *)skb->data, skb->len);
  761. rtl_recognize_peer(hw, (void *)skb->data, skb->len);
  762. if ((rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP) &&
  763. (rtlpriv->rtlhal.current_bandtype ==
  764. BAND_ON_2_4G) &&
  765. (ieee80211_is_beacon(fc) ||
  766. ieee80211_is_probe_resp(fc))) {
  767. dev_kfree_skb_any(skb);
  768. } else {
  769. _rtl_pci_rx_to_mac80211(hw, skb, rx_status);
  770. }
  771. } else {
  772. dev_kfree_skb_any(skb);
  773. }
  774. new_trx_end:
  775. if (rtlpriv->use_new_trx_flow) {
  776. rtlpci->rx_ring[hw_queue].next_rx_rp += 1;
  777. rtlpci->rx_ring[hw_queue].next_rx_rp %=
  778. RTL_PCI_MAX_RX_COUNT;
  779. rx_remained_cnt--;
  780. rtl_write_word(rtlpriv, 0x3B4,
  781. rtlpci->rx_ring[hw_queue].next_rx_rp);
  782. }
  783. if (((rtlpriv->link_info.num_rx_inperiod +
  784. rtlpriv->link_info.num_tx_inperiod) > 8) ||
  785. (rtlpriv->link_info.num_rx_inperiod > 2)) {
  786. rtlpriv->enter_ps = false;
  787. schedule_work(&rtlpriv->works.lps_change_work);
  788. }
  789. skb = new_skb;
  790. no_new:
  791. if (rtlpriv->use_new_trx_flow) {
  792. _rtl_pci_init_one_rxdesc(hw, skb, (u8 *)buffer_desc,
  793. rxring_idx,
  794. rtlpci->rx_ring[rxring_idx].idx);
  795. } else {
  796. _rtl_pci_init_one_rxdesc(hw, skb, (u8 *)pdesc,
  797. rxring_idx,
  798. rtlpci->rx_ring[rxring_idx].idx);
  799. if (rtlpci->rx_ring[rxring_idx].idx ==
  800. rtlpci->rxringcount - 1)
  801. rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc,
  802. false,
  803. HW_DESC_RXERO,
  804. (u8 *)&tmp_one);
  805. }
  806. rtlpci->rx_ring[rxring_idx].idx =
  807. (rtlpci->rx_ring[rxring_idx].idx + 1) %
  808. rtlpci->rxringcount;
  809. }
  810. }
  811. static irqreturn_t _rtl_pci_interrupt(int irq, void *dev_id)
  812. {
  813. struct ieee80211_hw *hw = dev_id;
  814. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  815. struct rtl_priv *rtlpriv = rtl_priv(hw);
  816. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  817. unsigned long flags;
  818. u32 inta = 0;
  819. u32 intb = 0;
  820. irqreturn_t ret = IRQ_HANDLED;
  821. if (rtlpci->irq_enabled == 0)
  822. return ret;
  823. spin_lock_irqsave(&rtlpriv->locks.irq_th_lock , flags);
  824. rtlpriv->cfg->ops->disable_interrupt(hw);
  825. /*read ISR: 4/8bytes */
  826. rtlpriv->cfg->ops->interrupt_recognized(hw, &inta, &intb);
  827. /*Shared IRQ or HW disappared */
  828. if (!inta || inta == 0xffff)
  829. goto done;
  830. /*<1> beacon related */
  831. if (inta & rtlpriv->cfg->maps[RTL_IMR_TBDOK]) {
  832. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  833. "beacon ok interrupt!\n");
  834. }
  835. if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_TBDER])) {
  836. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  837. "beacon err interrupt!\n");
  838. }
  839. if (inta & rtlpriv->cfg->maps[RTL_IMR_BDOK]) {
  840. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE, "beacon interrupt!\n");
  841. }
  842. if (inta & rtlpriv->cfg->maps[RTL_IMR_BCNINT]) {
  843. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  844. "prepare beacon for interrupt!\n");
  845. tasklet_schedule(&rtlpriv->works.irq_prepare_bcn_tasklet);
  846. }
  847. /*<2> Tx related */
  848. if (unlikely(intb & rtlpriv->cfg->maps[RTL_IMR_TXFOVW]))
  849. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, "IMR_TXFOVW!\n");
  850. if (inta & rtlpriv->cfg->maps[RTL_IMR_MGNTDOK]) {
  851. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  852. "Manage ok interrupt!\n");
  853. _rtl_pci_tx_isr(hw, MGNT_QUEUE);
  854. }
  855. if (inta & rtlpriv->cfg->maps[RTL_IMR_HIGHDOK]) {
  856. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  857. "HIGH_QUEUE ok interrupt!\n");
  858. _rtl_pci_tx_isr(hw, HIGH_QUEUE);
  859. }
  860. if (inta & rtlpriv->cfg->maps[RTL_IMR_BKDOK]) {
  861. rtlpriv->link_info.num_tx_inperiod++;
  862. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  863. "BK Tx OK interrupt!\n");
  864. _rtl_pci_tx_isr(hw, BK_QUEUE);
  865. }
  866. if (inta & rtlpriv->cfg->maps[RTL_IMR_BEDOK]) {
  867. rtlpriv->link_info.num_tx_inperiod++;
  868. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  869. "BE TX OK interrupt!\n");
  870. _rtl_pci_tx_isr(hw, BE_QUEUE);
  871. }
  872. if (inta & rtlpriv->cfg->maps[RTL_IMR_VIDOK]) {
  873. rtlpriv->link_info.num_tx_inperiod++;
  874. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  875. "VI TX OK interrupt!\n");
  876. _rtl_pci_tx_isr(hw, VI_QUEUE);
  877. }
  878. if (inta & rtlpriv->cfg->maps[RTL_IMR_VODOK]) {
  879. rtlpriv->link_info.num_tx_inperiod++;
  880. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  881. "Vo TX OK interrupt!\n");
  882. _rtl_pci_tx_isr(hw, VO_QUEUE);
  883. }
  884. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
  885. if (inta & rtlpriv->cfg->maps[RTL_IMR_COMDOK]) {
  886. rtlpriv->link_info.num_tx_inperiod++;
  887. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  888. "CMD TX OK interrupt!\n");
  889. _rtl_pci_tx_isr(hw, TXCMD_QUEUE);
  890. }
  891. }
  892. /*<3> Rx related */
  893. if (inta & rtlpriv->cfg->maps[RTL_IMR_ROK]) {
  894. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE, "Rx ok interrupt!\n");
  895. _rtl_pci_rx_interrupt(hw);
  896. }
  897. if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_RDU])) {
  898. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  899. "rx descriptor unavailable!\n");
  900. _rtl_pci_rx_interrupt(hw);
  901. }
  902. if (unlikely(intb & rtlpriv->cfg->maps[RTL_IMR_RXFOVW])) {
  903. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, "rx overflow !\n");
  904. _rtl_pci_rx_interrupt(hw);
  905. }
  906. /*<4> fw related*/
  907. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723AE) {
  908. if (inta & rtlpriv->cfg->maps[RTL_IMR_C2HCMD]) {
  909. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  910. "firmware interrupt!\n");
  911. queue_delayed_work(rtlpriv->works.rtl_wq,
  912. &rtlpriv->works.fwevt_wq, 0);
  913. }
  914. }
  915. /*<5> hsisr related*/
  916. /* Only 8188EE & 8723BE Supported.
  917. * If Other ICs Come in, System will corrupt,
  918. * because maps[RTL_IMR_HSISR_IND] & maps[MAC_HSISR]
  919. * are not initialized
  920. */
  921. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8188EE ||
  922. rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
  923. if (unlikely(inta & rtlpriv->cfg->maps[RTL_IMR_HSISR_IND])) {
  924. RT_TRACE(rtlpriv, COMP_INTR, DBG_TRACE,
  925. "hsisr interrupt!\n");
  926. _rtl_pci_hs_interrupt(hw);
  927. }
  928. }
  929. if (rtlpriv->rtlhal.earlymode_enable)
  930. tasklet_schedule(&rtlpriv->works.irq_tasklet);
  931. done:
  932. rtlpriv->cfg->ops->enable_interrupt(hw);
  933. spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
  934. return ret;
  935. }
  936. static void _rtl_pci_irq_tasklet(struct ieee80211_hw *hw)
  937. {
  938. _rtl_pci_tx_chk_waitq(hw);
  939. }
  940. static void _rtl_pci_prepare_bcn_tasklet(struct ieee80211_hw *hw)
  941. {
  942. struct rtl_priv *rtlpriv = rtl_priv(hw);
  943. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  944. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  945. struct rtl8192_tx_ring *ring = NULL;
  946. struct ieee80211_hdr *hdr = NULL;
  947. struct ieee80211_tx_info *info = NULL;
  948. struct sk_buff *pskb = NULL;
  949. struct rtl_tx_desc *pdesc = NULL;
  950. struct rtl_tcb_desc tcb_desc;
  951. /*This is for new trx flow*/
  952. struct rtl_tx_buffer_desc *pbuffer_desc = NULL;
  953. u8 temp_one = 1;
  954. u8 *entry;
  955. memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  956. ring = &rtlpci->tx_ring[BEACON_QUEUE];
  957. pskb = __skb_dequeue(&ring->queue);
  958. if (rtlpriv->use_new_trx_flow)
  959. entry = (u8 *)(&ring->buffer_desc[ring->idx]);
  960. else
  961. entry = (u8 *)(&ring->desc[ring->idx]);
  962. if (pskb) {
  963. pci_unmap_single(rtlpci->pdev,
  964. rtlpriv->cfg->ops->get_desc(
  965. (u8 *)entry, true, HW_DESC_TXBUFF_ADDR),
  966. pskb->len, PCI_DMA_TODEVICE);
  967. kfree_skb(pskb);
  968. }
  969. /*NB: the beacon data buffer must be 32-bit aligned. */
  970. pskb = ieee80211_beacon_get(hw, mac->vif);
  971. if (pskb == NULL)
  972. return;
  973. hdr = rtl_get_hdr(pskb);
  974. info = IEEE80211_SKB_CB(pskb);
  975. pdesc = &ring->desc[0];
  976. if (rtlpriv->use_new_trx_flow)
  977. pbuffer_desc = &ring->buffer_desc[0];
  978. rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc,
  979. (u8 *)pbuffer_desc, info, NULL, pskb,
  980. BEACON_QUEUE, &tcb_desc);
  981. __skb_queue_tail(&ring->queue, pskb);
  982. if (rtlpriv->use_new_trx_flow) {
  983. temp_one = 4;
  984. rtlpriv->cfg->ops->set_desc(hw, (u8 *)pbuffer_desc, true,
  985. HW_DESC_OWN, (u8 *)&temp_one);
  986. } else {
  987. rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc, true, HW_DESC_OWN,
  988. &temp_one);
  989. }
  990. return;
  991. }
  992. static void _rtl_pci_init_trx_var(struct ieee80211_hw *hw)
  993. {
  994. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  995. struct rtl_priv *rtlpriv = rtl_priv(hw);
  996. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  997. u8 i;
  998. u16 desc_num;
  999. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE)
  1000. desc_num = TX_DESC_NUM_92E;
  1001. else
  1002. desc_num = RT_TXDESC_NUM;
  1003. for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
  1004. rtlpci->txringcount[i] = desc_num;
  1005. /*
  1006. *we just alloc 2 desc for beacon queue,
  1007. *because we just need first desc in hw beacon.
  1008. */
  1009. rtlpci->txringcount[BEACON_QUEUE] = 2;
  1010. /*BE queue need more descriptor for performance
  1011. *consideration or, No more tx desc will happen,
  1012. *and may cause mac80211 mem leakage.
  1013. */
  1014. if (!rtl_priv(hw)->use_new_trx_flow)
  1015. rtlpci->txringcount[BE_QUEUE] = RT_TXDESC_NUM_BE_QUEUE;
  1016. rtlpci->rxbuffersize = 9100; /*2048/1024; */
  1017. rtlpci->rxringcount = RTL_PCI_MAX_RX_COUNT; /*64; */
  1018. }
  1019. static void _rtl_pci_init_struct(struct ieee80211_hw *hw,
  1020. struct pci_dev *pdev)
  1021. {
  1022. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1023. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1024. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1025. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1026. rtlpci->up_first_time = true;
  1027. rtlpci->being_init_adapter = false;
  1028. rtlhal->hw = hw;
  1029. rtlpci->pdev = pdev;
  1030. /*Tx/Rx related var */
  1031. _rtl_pci_init_trx_var(hw);
  1032. /*IBSS*/ mac->beacon_interval = 100;
  1033. /*AMPDU*/
  1034. mac->min_space_cfg = 0;
  1035. mac->max_mss_density = 0;
  1036. /*set sane AMPDU defaults */
  1037. mac->current_ampdu_density = 7;
  1038. mac->current_ampdu_factor = 3;
  1039. /*QOS*/
  1040. rtlpci->acm_method = EACMWAY2_SW;
  1041. /*task */
  1042. tasklet_init(&rtlpriv->works.irq_tasklet,
  1043. (void (*)(unsigned long))_rtl_pci_irq_tasklet,
  1044. (unsigned long)hw);
  1045. tasklet_init(&rtlpriv->works.irq_prepare_bcn_tasklet,
  1046. (void (*)(unsigned long))_rtl_pci_prepare_bcn_tasklet,
  1047. (unsigned long)hw);
  1048. INIT_WORK(&rtlpriv->works.lps_change_work,
  1049. rtl_lps_change_work_callback);
  1050. }
  1051. static int _rtl_pci_init_tx_ring(struct ieee80211_hw *hw,
  1052. unsigned int prio, unsigned int entries)
  1053. {
  1054. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1055. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1056. struct rtl_tx_buffer_desc *buffer_desc;
  1057. struct rtl_tx_desc *desc;
  1058. dma_addr_t buffer_desc_dma, desc_dma;
  1059. u32 nextdescaddress;
  1060. int i;
  1061. /* alloc tx buffer desc for new trx flow*/
  1062. if (rtlpriv->use_new_trx_flow) {
  1063. buffer_desc =
  1064. pci_zalloc_consistent(rtlpci->pdev,
  1065. sizeof(*buffer_desc) * entries,
  1066. &buffer_desc_dma);
  1067. if (!buffer_desc || (unsigned long)buffer_desc & 0xFF) {
  1068. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1069. "Cannot allocate TX ring (prio = %d)\n",
  1070. prio);
  1071. return -ENOMEM;
  1072. }
  1073. rtlpci->tx_ring[prio].buffer_desc = buffer_desc;
  1074. rtlpci->tx_ring[prio].buffer_desc_dma = buffer_desc_dma;
  1075. rtlpci->tx_ring[prio].cur_tx_rp = 0;
  1076. rtlpci->tx_ring[prio].cur_tx_wp = 0;
  1077. rtlpci->tx_ring[prio].avl_desc = entries;
  1078. }
  1079. /* alloc dma for this ring */
  1080. desc = pci_zalloc_consistent(rtlpci->pdev,
  1081. sizeof(*desc) * entries, &desc_dma);
  1082. if (!desc || (unsigned long)desc & 0xFF) {
  1083. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1084. "Cannot allocate TX ring (prio = %d)\n", prio);
  1085. return -ENOMEM;
  1086. }
  1087. rtlpci->tx_ring[prio].desc = desc;
  1088. rtlpci->tx_ring[prio].dma = desc_dma;
  1089. rtlpci->tx_ring[prio].idx = 0;
  1090. rtlpci->tx_ring[prio].entries = entries;
  1091. skb_queue_head_init(&rtlpci->tx_ring[prio].queue);
  1092. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "queue:%d, ring_addr:%p\n",
  1093. prio, desc);
  1094. /* init every desc in this ring */
  1095. if (!rtlpriv->use_new_trx_flow) {
  1096. for (i = 0; i < entries; i++) {
  1097. nextdescaddress = (u32)desc_dma +
  1098. ((i + 1) % entries) *
  1099. sizeof(*desc);
  1100. rtlpriv->cfg->ops->set_desc(hw, (u8 *)&desc[i],
  1101. true,
  1102. HW_DESC_TX_NEXTDESC_ADDR,
  1103. (u8 *)&nextdescaddress);
  1104. }
  1105. }
  1106. return 0;
  1107. }
  1108. static int _rtl_pci_init_rx_ring(struct ieee80211_hw *hw, int rxring_idx)
  1109. {
  1110. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1111. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1112. int i;
  1113. if (rtlpriv->use_new_trx_flow) {
  1114. struct rtl_rx_buffer_desc *entry = NULL;
  1115. /* alloc dma for this ring */
  1116. rtlpci->rx_ring[rxring_idx].buffer_desc =
  1117. pci_zalloc_consistent(rtlpci->pdev,
  1118. sizeof(*rtlpci->rx_ring[rxring_idx].
  1119. buffer_desc) *
  1120. rtlpci->rxringcount,
  1121. &rtlpci->rx_ring[rxring_idx].dma);
  1122. if (!rtlpci->rx_ring[rxring_idx].buffer_desc ||
  1123. (ulong)rtlpci->rx_ring[rxring_idx].buffer_desc & 0xFF) {
  1124. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1125. "Cannot allocate RX ring\n");
  1126. return -ENOMEM;
  1127. }
  1128. /* init every desc in this ring */
  1129. rtlpci->rx_ring[rxring_idx].idx = 0;
  1130. for (i = 0; i < rtlpci->rxringcount; i++) {
  1131. entry = &rtlpci->rx_ring[rxring_idx].buffer_desc[i];
  1132. if (!_rtl_pci_init_one_rxdesc(hw, NULL, (u8 *)entry,
  1133. rxring_idx, i))
  1134. return -ENOMEM;
  1135. }
  1136. } else {
  1137. struct rtl_rx_desc *entry = NULL;
  1138. u8 tmp_one = 1;
  1139. /* alloc dma for this ring */
  1140. rtlpci->rx_ring[rxring_idx].desc =
  1141. pci_zalloc_consistent(rtlpci->pdev,
  1142. sizeof(*rtlpci->rx_ring[rxring_idx].
  1143. desc) * rtlpci->rxringcount,
  1144. &rtlpci->rx_ring[rxring_idx].dma);
  1145. if (!rtlpci->rx_ring[rxring_idx].desc ||
  1146. (unsigned long)rtlpci->rx_ring[rxring_idx].desc & 0xFF) {
  1147. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1148. "Cannot allocate RX ring\n");
  1149. return -ENOMEM;
  1150. }
  1151. /* init every desc in this ring */
  1152. rtlpci->rx_ring[rxring_idx].idx = 0;
  1153. for (i = 0; i < rtlpci->rxringcount; i++) {
  1154. entry = &rtlpci->rx_ring[rxring_idx].desc[i];
  1155. if (!_rtl_pci_init_one_rxdesc(hw, NULL, (u8 *)entry,
  1156. rxring_idx, i))
  1157. return -ENOMEM;
  1158. }
  1159. rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
  1160. HW_DESC_RXERO, &tmp_one);
  1161. }
  1162. return 0;
  1163. }
  1164. static void _rtl_pci_free_tx_ring(struct ieee80211_hw *hw,
  1165. unsigned int prio)
  1166. {
  1167. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1168. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1169. struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[prio];
  1170. /* free every desc in this ring */
  1171. while (skb_queue_len(&ring->queue)) {
  1172. u8 *entry;
  1173. struct sk_buff *skb = __skb_dequeue(&ring->queue);
  1174. if (rtlpriv->use_new_trx_flow)
  1175. entry = (u8 *)(&ring->buffer_desc[ring->idx]);
  1176. else
  1177. entry = (u8 *)(&ring->desc[ring->idx]);
  1178. pci_unmap_single(rtlpci->pdev,
  1179. rtlpriv->cfg->
  1180. ops->get_desc((u8 *)entry, true,
  1181. HW_DESC_TXBUFF_ADDR),
  1182. skb->len, PCI_DMA_TODEVICE);
  1183. kfree_skb(skb);
  1184. ring->idx = (ring->idx + 1) % ring->entries;
  1185. }
  1186. /* free dma of this ring */
  1187. pci_free_consistent(rtlpci->pdev,
  1188. sizeof(*ring->desc) * ring->entries,
  1189. ring->desc, ring->dma);
  1190. ring->desc = NULL;
  1191. if (rtlpriv->use_new_trx_flow) {
  1192. pci_free_consistent(rtlpci->pdev,
  1193. sizeof(*ring->buffer_desc) * ring->entries,
  1194. ring->buffer_desc, ring->buffer_desc_dma);
  1195. ring->buffer_desc = NULL;
  1196. }
  1197. }
  1198. static void _rtl_pci_free_rx_ring(struct ieee80211_hw *hw, int rxring_idx)
  1199. {
  1200. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1201. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1202. int i;
  1203. /* free every desc in this ring */
  1204. for (i = 0; i < rtlpci->rxringcount; i++) {
  1205. struct sk_buff *skb = rtlpci->rx_ring[rxring_idx].rx_buf[i];
  1206. if (!skb)
  1207. continue;
  1208. pci_unmap_single(rtlpci->pdev, *((dma_addr_t *)skb->cb),
  1209. rtlpci->rxbuffersize, PCI_DMA_FROMDEVICE);
  1210. kfree_skb(skb);
  1211. }
  1212. /* free dma of this ring */
  1213. if (rtlpriv->use_new_trx_flow) {
  1214. pci_free_consistent(rtlpci->pdev,
  1215. sizeof(*rtlpci->rx_ring[rxring_idx].
  1216. buffer_desc) * rtlpci->rxringcount,
  1217. rtlpci->rx_ring[rxring_idx].buffer_desc,
  1218. rtlpci->rx_ring[rxring_idx].dma);
  1219. rtlpci->rx_ring[rxring_idx].buffer_desc = NULL;
  1220. } else {
  1221. pci_free_consistent(rtlpci->pdev,
  1222. sizeof(*rtlpci->rx_ring[rxring_idx].desc) *
  1223. rtlpci->rxringcount,
  1224. rtlpci->rx_ring[rxring_idx].desc,
  1225. rtlpci->rx_ring[rxring_idx].dma);
  1226. rtlpci->rx_ring[rxring_idx].desc = NULL;
  1227. }
  1228. }
  1229. static int _rtl_pci_init_trx_ring(struct ieee80211_hw *hw)
  1230. {
  1231. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1232. int ret;
  1233. int i, rxring_idx;
  1234. /* rxring_idx 0:RX_MPDU_QUEUE
  1235. * rxring_idx 1:RX_CMD_QUEUE
  1236. */
  1237. for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++) {
  1238. ret = _rtl_pci_init_rx_ring(hw, rxring_idx);
  1239. if (ret)
  1240. return ret;
  1241. }
  1242. for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
  1243. ret = _rtl_pci_init_tx_ring(hw, i,
  1244. rtlpci->txringcount[i]);
  1245. if (ret)
  1246. goto err_free_rings;
  1247. }
  1248. return 0;
  1249. err_free_rings:
  1250. for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++)
  1251. _rtl_pci_free_rx_ring(hw, rxring_idx);
  1252. for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
  1253. if (rtlpci->tx_ring[i].desc ||
  1254. rtlpci->tx_ring[i].buffer_desc)
  1255. _rtl_pci_free_tx_ring(hw, i);
  1256. return 1;
  1257. }
  1258. static int _rtl_pci_deinit_trx_ring(struct ieee80211_hw *hw)
  1259. {
  1260. u32 i, rxring_idx;
  1261. /*free rx rings */
  1262. for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++)
  1263. _rtl_pci_free_rx_ring(hw, rxring_idx);
  1264. /*free tx rings */
  1265. for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++)
  1266. _rtl_pci_free_tx_ring(hw, i);
  1267. return 0;
  1268. }
  1269. int rtl_pci_reset_trx_ring(struct ieee80211_hw *hw)
  1270. {
  1271. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1272. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1273. int i, rxring_idx;
  1274. unsigned long flags;
  1275. u8 tmp_one = 1;
  1276. u32 bufferaddress;
  1277. /* rxring_idx 0:RX_MPDU_QUEUE */
  1278. /* rxring_idx 1:RX_CMD_QUEUE */
  1279. for (rxring_idx = 0; rxring_idx < RTL_PCI_MAX_RX_QUEUE; rxring_idx++) {
  1280. /* force the rx_ring[RX_MPDU_QUEUE/
  1281. * RX_CMD_QUEUE].idx to the first one
  1282. *new trx flow, do nothing
  1283. */
  1284. if (!rtlpriv->use_new_trx_flow &&
  1285. rtlpci->rx_ring[rxring_idx].desc) {
  1286. struct rtl_rx_desc *entry = NULL;
  1287. rtlpci->rx_ring[rxring_idx].idx = 0;
  1288. for (i = 0; i < rtlpci->rxringcount; i++) {
  1289. entry = &rtlpci->rx_ring[rxring_idx].desc[i];
  1290. bufferaddress =
  1291. rtlpriv->cfg->ops->get_desc((u8 *)entry,
  1292. false , HW_DESC_RXBUFF_ADDR);
  1293. memset((u8 *)entry , 0 ,
  1294. sizeof(*rtlpci->rx_ring
  1295. [rxring_idx].desc));/*clear one entry*/
  1296. if (rtlpriv->use_new_trx_flow) {
  1297. rtlpriv->cfg->ops->set_desc(hw,
  1298. (u8 *)entry, false,
  1299. HW_DESC_RX_PREPARE,
  1300. (u8 *)&bufferaddress);
  1301. } else {
  1302. rtlpriv->cfg->ops->set_desc(hw,
  1303. (u8 *)entry, false,
  1304. HW_DESC_RXBUFF_ADDR,
  1305. (u8 *)&bufferaddress);
  1306. rtlpriv->cfg->ops->set_desc(hw,
  1307. (u8 *)entry, false,
  1308. HW_DESC_RXPKT_LEN,
  1309. (u8 *)&rtlpci->rxbuffersize);
  1310. rtlpriv->cfg->ops->set_desc(hw,
  1311. (u8 *)entry, false,
  1312. HW_DESC_RXOWN,
  1313. (u8 *)&tmp_one);
  1314. }
  1315. }
  1316. rtlpriv->cfg->ops->set_desc(hw, (u8 *)entry, false,
  1317. HW_DESC_RXERO, (u8 *)&tmp_one);
  1318. }
  1319. rtlpci->rx_ring[rxring_idx].idx = 0;
  1320. }
  1321. /*
  1322. *after reset, release previous pending packet,
  1323. *and force the tx idx to the first one
  1324. */
  1325. spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
  1326. for (i = 0; i < RTL_PCI_MAX_TX_QUEUE_COUNT; i++) {
  1327. if (rtlpci->tx_ring[i].desc ||
  1328. rtlpci->tx_ring[i].buffer_desc) {
  1329. struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[i];
  1330. while (skb_queue_len(&ring->queue)) {
  1331. u8 *entry;
  1332. struct sk_buff *skb =
  1333. __skb_dequeue(&ring->queue);
  1334. if (rtlpriv->use_new_trx_flow)
  1335. entry = (u8 *)(&ring->buffer_desc
  1336. [ring->idx]);
  1337. else
  1338. entry = (u8 *)(&ring->desc[ring->idx]);
  1339. pci_unmap_single(rtlpci->pdev,
  1340. rtlpriv->cfg->ops->
  1341. get_desc((u8 *)
  1342. entry,
  1343. true,
  1344. HW_DESC_TXBUFF_ADDR),
  1345. skb->len, PCI_DMA_TODEVICE);
  1346. kfree_skb(skb);
  1347. ring->idx = (ring->idx + 1) % ring->entries;
  1348. }
  1349. ring->idx = 0;
  1350. }
  1351. }
  1352. spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
  1353. return 0;
  1354. }
  1355. static bool rtl_pci_tx_chk_waitq_insert(struct ieee80211_hw *hw,
  1356. struct ieee80211_sta *sta,
  1357. struct sk_buff *skb)
  1358. {
  1359. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1360. struct rtl_sta_info *sta_entry = NULL;
  1361. u8 tid = rtl_get_tid(skb);
  1362. __le16 fc = rtl_get_fc(skb);
  1363. if (!sta)
  1364. return false;
  1365. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1366. if (!rtlpriv->rtlhal.earlymode_enable)
  1367. return false;
  1368. if (ieee80211_is_nullfunc(fc))
  1369. return false;
  1370. if (ieee80211_is_qos_nullfunc(fc))
  1371. return false;
  1372. if (ieee80211_is_pspoll(fc))
  1373. return false;
  1374. if (sta_entry->tids[tid].agg.agg_state != RTL_AGG_OPERATIONAL)
  1375. return false;
  1376. if (_rtl_mac_to_hwqueue(hw, skb) > VO_QUEUE)
  1377. return false;
  1378. if (tid > 7)
  1379. return false;
  1380. /* maybe every tid should be checked */
  1381. if (!rtlpriv->link_info.higher_busytxtraffic[tid])
  1382. return false;
  1383. spin_lock_bh(&rtlpriv->locks.waitq_lock);
  1384. skb_queue_tail(&rtlpriv->mac80211.skb_waitq[tid], skb);
  1385. spin_unlock_bh(&rtlpriv->locks.waitq_lock);
  1386. return true;
  1387. }
  1388. static int rtl_pci_tx(struct ieee80211_hw *hw,
  1389. struct ieee80211_sta *sta,
  1390. struct sk_buff *skb,
  1391. struct rtl_tcb_desc *ptcb_desc)
  1392. {
  1393. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1394. struct rtl_sta_info *sta_entry = NULL;
  1395. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1396. struct rtl8192_tx_ring *ring;
  1397. struct rtl_tx_desc *pdesc;
  1398. struct rtl_tx_buffer_desc *ptx_bd_desc = NULL;
  1399. u16 idx;
  1400. u8 hw_queue = _rtl_mac_to_hwqueue(hw, skb);
  1401. unsigned long flags;
  1402. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  1403. __le16 fc = rtl_get_fc(skb);
  1404. u8 *pda_addr = hdr->addr1;
  1405. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1406. /*ssn */
  1407. u8 tid = 0;
  1408. u16 seq_number = 0;
  1409. u8 own;
  1410. u8 temp_one = 1;
  1411. if (ieee80211_is_mgmt(fc))
  1412. rtl_tx_mgmt_proc(hw, skb);
  1413. if (rtlpriv->psc.sw_ps_enabled) {
  1414. if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
  1415. !ieee80211_has_pm(fc))
  1416. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  1417. }
  1418. rtl_action_proc(hw, skb, true);
  1419. if (is_multicast_ether_addr(pda_addr))
  1420. rtlpriv->stats.txbytesmulticast += skb->len;
  1421. else if (is_broadcast_ether_addr(pda_addr))
  1422. rtlpriv->stats.txbytesbroadcast += skb->len;
  1423. else
  1424. rtlpriv->stats.txbytesunicast += skb->len;
  1425. spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
  1426. ring = &rtlpci->tx_ring[hw_queue];
  1427. if (hw_queue != BEACON_QUEUE) {
  1428. if (rtlpriv->use_new_trx_flow)
  1429. idx = ring->cur_tx_wp;
  1430. else
  1431. idx = (ring->idx + skb_queue_len(&ring->queue)) %
  1432. ring->entries;
  1433. } else {
  1434. idx = 0;
  1435. }
  1436. pdesc = &ring->desc[idx];
  1437. if (rtlpriv->use_new_trx_flow) {
  1438. ptx_bd_desc = &ring->buffer_desc[idx];
  1439. } else {
  1440. own = (u8) rtlpriv->cfg->ops->get_desc((u8 *)pdesc,
  1441. true, HW_DESC_OWN);
  1442. if ((own == 1) && (hw_queue != BEACON_QUEUE)) {
  1443. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1444. "No more TX desc@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%x\n",
  1445. hw_queue, ring->idx, idx,
  1446. skb_queue_len(&ring->queue));
  1447. spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock,
  1448. flags);
  1449. return skb->len;
  1450. }
  1451. }
  1452. if (rtlpriv->cfg->ops->get_available_desc &&
  1453. rtlpriv->cfg->ops->get_available_desc(hw, hw_queue) == 0) {
  1454. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1455. "get_available_desc fail\n");
  1456. spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock,
  1457. flags);
  1458. return skb->len;
  1459. }
  1460. if (ieee80211_is_data_qos(fc)) {
  1461. tid = rtl_get_tid(skb);
  1462. if (sta) {
  1463. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1464. seq_number = (le16_to_cpu(hdr->seq_ctrl) &
  1465. IEEE80211_SCTL_SEQ) >> 4;
  1466. seq_number += 1;
  1467. if (!ieee80211_has_morefrags(hdr->frame_control))
  1468. sta_entry->tids[tid].seq_number = seq_number;
  1469. }
  1470. }
  1471. if (ieee80211_is_data(fc))
  1472. rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
  1473. rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc,
  1474. (u8 *)ptx_bd_desc, info, sta, skb, hw_queue, ptcb_desc);
  1475. __skb_queue_tail(&ring->queue, skb);
  1476. if (rtlpriv->use_new_trx_flow) {
  1477. rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc, true,
  1478. HW_DESC_OWN, &hw_queue);
  1479. } else {
  1480. rtlpriv->cfg->ops->set_desc(hw, (u8 *)pdesc, true,
  1481. HW_DESC_OWN, &temp_one);
  1482. }
  1483. if ((ring->entries - skb_queue_len(&ring->queue)) < 2 &&
  1484. hw_queue != BEACON_QUEUE) {
  1485. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  1486. "less desc left, stop skb_queue@%d, ring->idx = %d, idx = %d, skb_queue_len = 0x%x\n",
  1487. hw_queue, ring->idx, idx,
  1488. skb_queue_len(&ring->queue));
  1489. ieee80211_stop_queue(hw, skb_get_queue_mapping(skb));
  1490. }
  1491. spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
  1492. rtlpriv->cfg->ops->tx_polling(hw, hw_queue);
  1493. return 0;
  1494. }
  1495. static void rtl_pci_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
  1496. {
  1497. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1498. struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
  1499. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1500. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1501. u16 i = 0;
  1502. int queue_id;
  1503. struct rtl8192_tx_ring *ring;
  1504. if (mac->skip_scan)
  1505. return;
  1506. for (queue_id = RTL_PCI_MAX_TX_QUEUE_COUNT - 1; queue_id >= 0;) {
  1507. u32 queue_len;
  1508. if (((queues >> queue_id) & 0x1) == 0) {
  1509. queue_id--;
  1510. continue;
  1511. }
  1512. ring = &pcipriv->dev.tx_ring[queue_id];
  1513. queue_len = skb_queue_len(&ring->queue);
  1514. if (queue_len == 0 || queue_id == BEACON_QUEUE ||
  1515. queue_id == TXCMD_QUEUE) {
  1516. queue_id--;
  1517. continue;
  1518. } else {
  1519. msleep(20);
  1520. i++;
  1521. }
  1522. /* we just wait 1s for all queues */
  1523. if (rtlpriv->psc.rfpwr_state == ERFOFF ||
  1524. is_hal_stop(rtlhal) || i >= 200)
  1525. return;
  1526. }
  1527. }
  1528. static void rtl_pci_deinit(struct ieee80211_hw *hw)
  1529. {
  1530. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1531. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1532. _rtl_pci_deinit_trx_ring(hw);
  1533. synchronize_irq(rtlpci->pdev->irq);
  1534. tasklet_kill(&rtlpriv->works.irq_tasklet);
  1535. cancel_work_sync(&rtlpriv->works.lps_change_work);
  1536. flush_workqueue(rtlpriv->works.rtl_wq);
  1537. destroy_workqueue(rtlpriv->works.rtl_wq);
  1538. }
  1539. static int rtl_pci_init(struct ieee80211_hw *hw, struct pci_dev *pdev)
  1540. {
  1541. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1542. int err;
  1543. _rtl_pci_init_struct(hw, pdev);
  1544. err = _rtl_pci_init_trx_ring(hw);
  1545. if (err) {
  1546. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1547. "tx ring initialization failed\n");
  1548. return err;
  1549. }
  1550. return 0;
  1551. }
  1552. static int rtl_pci_start(struct ieee80211_hw *hw)
  1553. {
  1554. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1555. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1556. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1557. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1558. int err;
  1559. rtl_pci_reset_trx_ring(hw);
  1560. rtlpci->driver_is_goingto_unload = false;
  1561. if (rtlpriv->cfg->ops->get_btc_status &&
  1562. rtlpriv->cfg->ops->get_btc_status()) {
  1563. rtlpriv->btcoexist.btc_ops->btc_init_variables(rtlpriv);
  1564. rtlpriv->btcoexist.btc_ops->btc_init_hal_vars(rtlpriv);
  1565. }
  1566. err = rtlpriv->cfg->ops->hw_init(hw);
  1567. if (err) {
  1568. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1569. "Failed to config hardware!\n");
  1570. return err;
  1571. }
  1572. rtlpriv->cfg->ops->enable_interrupt(hw);
  1573. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "enable_interrupt OK\n");
  1574. rtl_init_rx_config(hw);
  1575. /*should be after adapter start and interrupt enable. */
  1576. set_hal_start(rtlhal);
  1577. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  1578. rtlpci->up_first_time = false;
  1579. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "rtl_pci_start OK\n");
  1580. return 0;
  1581. }
  1582. static void rtl_pci_stop(struct ieee80211_hw *hw)
  1583. {
  1584. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1585. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1586. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1587. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1588. unsigned long flags;
  1589. u8 RFInProgressTimeOut = 0;
  1590. if (rtlpriv->cfg->ops->get_btc_status())
  1591. rtlpriv->btcoexist.btc_ops->btc_halt_notify();
  1592. /*
  1593. *should be before disable interrupt&adapter
  1594. *and will do it immediately.
  1595. */
  1596. set_hal_stop(rtlhal);
  1597. rtlpci->driver_is_goingto_unload = true;
  1598. rtlpriv->cfg->ops->disable_interrupt(hw);
  1599. cancel_work_sync(&rtlpriv->works.lps_change_work);
  1600. spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
  1601. while (ppsc->rfchange_inprogress) {
  1602. spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
  1603. if (RFInProgressTimeOut > 100) {
  1604. spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
  1605. break;
  1606. }
  1607. mdelay(1);
  1608. RFInProgressTimeOut++;
  1609. spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
  1610. }
  1611. ppsc->rfchange_inprogress = true;
  1612. spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
  1613. rtlpriv->cfg->ops->hw_disable(hw);
  1614. /* some things are not needed if firmware not available */
  1615. if (!rtlpriv->max_fw_size)
  1616. return;
  1617. rtlpriv->cfg->ops->led_control(hw, LED_CTL_POWER_OFF);
  1618. spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flags);
  1619. ppsc->rfchange_inprogress = false;
  1620. spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flags);
  1621. rtl_pci_enable_aspm(hw);
  1622. }
  1623. static bool _rtl_pci_find_adapter(struct pci_dev *pdev,
  1624. struct ieee80211_hw *hw)
  1625. {
  1626. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1627. struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
  1628. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1629. struct pci_dev *bridge_pdev = pdev->bus->self;
  1630. u16 venderid;
  1631. u16 deviceid;
  1632. u8 revisionid;
  1633. u16 irqline;
  1634. u8 tmp;
  1635. pcipriv->ndis_adapter.pcibridge_vendor = PCI_BRIDGE_VENDOR_UNKNOWN;
  1636. venderid = pdev->vendor;
  1637. deviceid = pdev->device;
  1638. pci_read_config_byte(pdev, 0x8, &revisionid);
  1639. pci_read_config_word(pdev, 0x3C, &irqline);
  1640. /* PCI ID 0x10ec:0x8192 occurs for both RTL8192E, which uses
  1641. * r8192e_pci, and RTL8192SE, which uses this driver. If the
  1642. * revision ID is RTL_PCI_REVISION_ID_8192PCIE (0x01), then
  1643. * the correct driver is r8192e_pci, thus this routine should
  1644. * return false.
  1645. */
  1646. if (deviceid == RTL_PCI_8192SE_DID &&
  1647. revisionid == RTL_PCI_REVISION_ID_8192PCIE)
  1648. return false;
  1649. if (deviceid == RTL_PCI_8192_DID ||
  1650. deviceid == RTL_PCI_0044_DID ||
  1651. deviceid == RTL_PCI_0047_DID ||
  1652. deviceid == RTL_PCI_8192SE_DID ||
  1653. deviceid == RTL_PCI_8174_DID ||
  1654. deviceid == RTL_PCI_8173_DID ||
  1655. deviceid == RTL_PCI_8172_DID ||
  1656. deviceid == RTL_PCI_8171_DID) {
  1657. switch (revisionid) {
  1658. case RTL_PCI_REVISION_ID_8192PCIE:
  1659. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1660. "8192 PCI-E is found - vid/did=%x/%x\n",
  1661. venderid, deviceid);
  1662. rtlhal->hw_type = HARDWARE_TYPE_RTL8192E;
  1663. return false;
  1664. case RTL_PCI_REVISION_ID_8192SE:
  1665. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1666. "8192SE is found - vid/did=%x/%x\n",
  1667. venderid, deviceid);
  1668. rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
  1669. break;
  1670. default:
  1671. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1672. "Err: Unknown device - vid/did=%x/%x\n",
  1673. venderid, deviceid);
  1674. rtlhal->hw_type = HARDWARE_TYPE_RTL8192SE;
  1675. break;
  1676. }
  1677. } else if (deviceid == RTL_PCI_8723AE_DID) {
  1678. rtlhal->hw_type = HARDWARE_TYPE_RTL8723AE;
  1679. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1680. "8723AE PCI-E is found - "
  1681. "vid/did=%x/%x\n", venderid, deviceid);
  1682. } else if (deviceid == RTL_PCI_8192CET_DID ||
  1683. deviceid == RTL_PCI_8192CE_DID ||
  1684. deviceid == RTL_PCI_8191CE_DID ||
  1685. deviceid == RTL_PCI_8188CE_DID) {
  1686. rtlhal->hw_type = HARDWARE_TYPE_RTL8192CE;
  1687. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1688. "8192C PCI-E is found - vid/did=%x/%x\n",
  1689. venderid, deviceid);
  1690. } else if (deviceid == RTL_PCI_8192DE_DID ||
  1691. deviceid == RTL_PCI_8192DE_DID2) {
  1692. rtlhal->hw_type = HARDWARE_TYPE_RTL8192DE;
  1693. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1694. "8192D PCI-E is found - vid/did=%x/%x\n",
  1695. venderid, deviceid);
  1696. } else if (deviceid == RTL_PCI_8188EE_DID) {
  1697. rtlhal->hw_type = HARDWARE_TYPE_RTL8188EE;
  1698. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1699. "Find adapter, Hardware type is 8188EE\n");
  1700. } else if (deviceid == RTL_PCI_8723BE_DID) {
  1701. rtlhal->hw_type = HARDWARE_TYPE_RTL8723BE;
  1702. RT_TRACE(rtlpriv, COMP_INIT , DBG_LOUD,
  1703. "Find adapter, Hardware type is 8723BE\n");
  1704. } else if (deviceid == RTL_PCI_8192EE_DID) {
  1705. rtlhal->hw_type = HARDWARE_TYPE_RTL8192EE;
  1706. RT_TRACE(rtlpriv, COMP_INIT , DBG_LOUD,
  1707. "Find adapter, Hardware type is 8192EE\n");
  1708. } else if (deviceid == RTL_PCI_8821AE_DID) {
  1709. rtlhal->hw_type = HARDWARE_TYPE_RTL8821AE;
  1710. RT_TRACE(rtlpriv, COMP_INIT , DBG_LOUD,
  1711. "Find adapter, Hardware type is 8821AE\n");
  1712. } else if (deviceid == RTL_PCI_8812AE_DID) {
  1713. rtlhal->hw_type = HARDWARE_TYPE_RTL8812AE;
  1714. RT_TRACE(rtlpriv, COMP_INIT , DBG_LOUD,
  1715. "Find adapter, Hardware type is 8812AE\n");
  1716. } else {
  1717. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1718. "Err: Unknown device - vid/did=%x/%x\n",
  1719. venderid, deviceid);
  1720. rtlhal->hw_type = RTL_DEFAULT_HARDWARE_TYPE;
  1721. }
  1722. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192DE) {
  1723. if (revisionid == 0 || revisionid == 1) {
  1724. if (revisionid == 0) {
  1725. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1726. "Find 92DE MAC0\n");
  1727. rtlhal->interfaceindex = 0;
  1728. } else if (revisionid == 1) {
  1729. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1730. "Find 92DE MAC1\n");
  1731. rtlhal->interfaceindex = 1;
  1732. }
  1733. } else {
  1734. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  1735. "Unknown device - VendorID/DeviceID=%x/%x, Revision=%x\n",
  1736. venderid, deviceid, revisionid);
  1737. rtlhal->interfaceindex = 0;
  1738. }
  1739. }
  1740. /* 92ee use new trx flow */
  1741. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE)
  1742. rtlpriv->use_new_trx_flow = true;
  1743. else
  1744. rtlpriv->use_new_trx_flow = false;
  1745. /*find bus info */
  1746. pcipriv->ndis_adapter.busnumber = pdev->bus->number;
  1747. pcipriv->ndis_adapter.devnumber = PCI_SLOT(pdev->devfn);
  1748. pcipriv->ndis_adapter.funcnumber = PCI_FUNC(pdev->devfn);
  1749. /*find bridge info */
  1750. pcipriv->ndis_adapter.pcibridge_vendor = PCI_BRIDGE_VENDOR_UNKNOWN;
  1751. /* some ARM have no bridge_pdev and will crash here
  1752. * so we should check if bridge_pdev is NULL
  1753. */
  1754. if (bridge_pdev) {
  1755. /*find bridge info if available */
  1756. pcipriv->ndis_adapter.pcibridge_vendorid = bridge_pdev->vendor;
  1757. for (tmp = 0; tmp < PCI_BRIDGE_VENDOR_MAX; tmp++) {
  1758. if (bridge_pdev->vendor == pcibridge_vendors[tmp]) {
  1759. pcipriv->ndis_adapter.pcibridge_vendor = tmp;
  1760. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1761. "Pci Bridge Vendor is found index: %d\n",
  1762. tmp);
  1763. break;
  1764. }
  1765. }
  1766. }
  1767. if (pcipriv->ndis_adapter.pcibridge_vendor !=
  1768. PCI_BRIDGE_VENDOR_UNKNOWN) {
  1769. pcipriv->ndis_adapter.pcibridge_busnum =
  1770. bridge_pdev->bus->number;
  1771. pcipriv->ndis_adapter.pcibridge_devnum =
  1772. PCI_SLOT(bridge_pdev->devfn);
  1773. pcipriv->ndis_adapter.pcibridge_funcnum =
  1774. PCI_FUNC(bridge_pdev->devfn);
  1775. pcipriv->ndis_adapter.pcibridge_pciehdr_offset =
  1776. pci_pcie_cap(bridge_pdev);
  1777. pcipriv->ndis_adapter.num4bytes =
  1778. (pcipriv->ndis_adapter.pcibridge_pciehdr_offset + 0x10) / 4;
  1779. rtl_pci_get_linkcontrol_field(hw);
  1780. if (pcipriv->ndis_adapter.pcibridge_vendor ==
  1781. PCI_BRIDGE_VENDOR_AMD) {
  1782. pcipriv->ndis_adapter.amd_l1_patch =
  1783. rtl_pci_get_amd_l1_patch(hw);
  1784. }
  1785. }
  1786. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1787. "pcidev busnumber:devnumber:funcnumber:vendor:link_ctl %d:%d:%d:%x:%x\n",
  1788. pcipriv->ndis_adapter.busnumber,
  1789. pcipriv->ndis_adapter.devnumber,
  1790. pcipriv->ndis_adapter.funcnumber,
  1791. pdev->vendor, pcipriv->ndis_adapter.linkctrl_reg);
  1792. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1793. "pci_bridge busnumber:devnumber:funcnumber:vendor:pcie_cap:link_ctl_reg:amd %d:%d:%d:%x:%x:%x:%x\n",
  1794. pcipriv->ndis_adapter.pcibridge_busnum,
  1795. pcipriv->ndis_adapter.pcibridge_devnum,
  1796. pcipriv->ndis_adapter.pcibridge_funcnum,
  1797. pcibridge_vendors[pcipriv->ndis_adapter.pcibridge_vendor],
  1798. pcipriv->ndis_adapter.pcibridge_pciehdr_offset,
  1799. pcipriv->ndis_adapter.pcibridge_linkctrlreg,
  1800. pcipriv->ndis_adapter.amd_l1_patch);
  1801. rtl_pci_parse_configuration(pdev, hw);
  1802. list_add_tail(&rtlpriv->list, &rtlpriv->glb_var->glb_priv_list);
  1803. return true;
  1804. }
  1805. static int rtl_pci_intr_mode_msi(struct ieee80211_hw *hw)
  1806. {
  1807. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1808. struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
  1809. struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
  1810. int ret;
  1811. ret = pci_enable_msi(rtlpci->pdev);
  1812. if (ret < 0)
  1813. return ret;
  1814. ret = request_irq(rtlpci->pdev->irq, &_rtl_pci_interrupt,
  1815. IRQF_SHARED, KBUILD_MODNAME, hw);
  1816. if (ret < 0) {
  1817. pci_disable_msi(rtlpci->pdev);
  1818. return ret;
  1819. }
  1820. rtlpci->using_msi = true;
  1821. RT_TRACE(rtlpriv, COMP_INIT|COMP_INTR, DBG_DMESG,
  1822. "MSI Interrupt Mode!\n");
  1823. return 0;
  1824. }
  1825. static int rtl_pci_intr_mode_legacy(struct ieee80211_hw *hw)
  1826. {
  1827. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1828. struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
  1829. struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
  1830. int ret;
  1831. ret = request_irq(rtlpci->pdev->irq, &_rtl_pci_interrupt,
  1832. IRQF_SHARED, KBUILD_MODNAME, hw);
  1833. if (ret < 0)
  1834. return ret;
  1835. rtlpci->using_msi = false;
  1836. RT_TRACE(rtlpriv, COMP_INIT|COMP_INTR, DBG_DMESG,
  1837. "Pin-based Interrupt Mode!\n");
  1838. return 0;
  1839. }
  1840. static int rtl_pci_intr_mode_decide(struct ieee80211_hw *hw)
  1841. {
  1842. struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
  1843. struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
  1844. int ret;
  1845. if (rtlpci->msi_support) {
  1846. ret = rtl_pci_intr_mode_msi(hw);
  1847. if (ret < 0)
  1848. ret = rtl_pci_intr_mode_legacy(hw);
  1849. } else {
  1850. ret = rtl_pci_intr_mode_legacy(hw);
  1851. }
  1852. return ret;
  1853. }
  1854. int rtl_pci_probe(struct pci_dev *pdev,
  1855. const struct pci_device_id *id)
  1856. {
  1857. struct ieee80211_hw *hw = NULL;
  1858. struct rtl_priv *rtlpriv = NULL;
  1859. struct rtl_pci_priv *pcipriv = NULL;
  1860. struct rtl_pci *rtlpci;
  1861. unsigned long pmem_start, pmem_len, pmem_flags;
  1862. int err;
  1863. err = pci_enable_device(pdev);
  1864. if (err) {
  1865. RT_ASSERT(false, "%s : Cannot enable new PCI device\n",
  1866. pci_name(pdev));
  1867. return err;
  1868. }
  1869. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1870. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1871. RT_ASSERT(false,
  1872. "Unable to obtain 32bit DMA for consistent allocations\n");
  1873. err = -ENOMEM;
  1874. goto fail1;
  1875. }
  1876. }
  1877. pci_set_master(pdev);
  1878. hw = ieee80211_alloc_hw(sizeof(struct rtl_pci_priv) +
  1879. sizeof(struct rtl_priv), &rtl_ops);
  1880. if (!hw) {
  1881. RT_ASSERT(false,
  1882. "%s : ieee80211 alloc failed\n", pci_name(pdev));
  1883. err = -ENOMEM;
  1884. goto fail1;
  1885. }
  1886. SET_IEEE80211_DEV(hw, &pdev->dev);
  1887. pci_set_drvdata(pdev, hw);
  1888. rtlpriv = hw->priv;
  1889. rtlpriv->hw = hw;
  1890. pcipriv = (void *)rtlpriv->priv;
  1891. pcipriv->dev.pdev = pdev;
  1892. init_completion(&rtlpriv->firmware_loading_complete);
  1893. /*proximity init here*/
  1894. rtlpriv->proximity.proxim_on = false;
  1895. pcipriv = (void *)rtlpriv->priv;
  1896. pcipriv->dev.pdev = pdev;
  1897. /* init cfg & intf_ops */
  1898. rtlpriv->rtlhal.interface = INTF_PCI;
  1899. rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_data);
  1900. rtlpriv->intf_ops = &rtl_pci_ops;
  1901. rtlpriv->glb_var = &rtl_global_var;
  1902. /*
  1903. *init dbgp flags before all
  1904. *other functions, because we will
  1905. *use it in other funtions like
  1906. *RT_TRACE/RT_PRINT/RTL_PRINT_DATA
  1907. *you can not use these macro
  1908. *before this
  1909. */
  1910. rtl_dbgp_flag_init(hw);
  1911. /* MEM map */
  1912. err = pci_request_regions(pdev, KBUILD_MODNAME);
  1913. if (err) {
  1914. RT_ASSERT(false, "Can't obtain PCI resources\n");
  1915. goto fail1;
  1916. }
  1917. pmem_start = pci_resource_start(pdev, rtlpriv->cfg->bar_id);
  1918. pmem_len = pci_resource_len(pdev, rtlpriv->cfg->bar_id);
  1919. pmem_flags = pci_resource_flags(pdev, rtlpriv->cfg->bar_id);
  1920. /*shared mem start */
  1921. rtlpriv->io.pci_mem_start =
  1922. (unsigned long)pci_iomap(pdev,
  1923. rtlpriv->cfg->bar_id, pmem_len);
  1924. if (rtlpriv->io.pci_mem_start == 0) {
  1925. RT_ASSERT(false, "Can't map PCI mem\n");
  1926. err = -ENOMEM;
  1927. goto fail2;
  1928. }
  1929. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1930. "mem mapped space: start: 0x%08lx len:%08lx flags:%08lx, after map:0x%08lx\n",
  1931. pmem_start, pmem_len, pmem_flags,
  1932. rtlpriv->io.pci_mem_start);
  1933. /* Disable Clk Request */
  1934. pci_write_config_byte(pdev, 0x81, 0);
  1935. /* leave D3 mode */
  1936. pci_write_config_byte(pdev, 0x44, 0);
  1937. pci_write_config_byte(pdev, 0x04, 0x06);
  1938. pci_write_config_byte(pdev, 0x04, 0x07);
  1939. /* find adapter */
  1940. if (!_rtl_pci_find_adapter(pdev, hw)) {
  1941. err = -ENODEV;
  1942. goto fail3;
  1943. }
  1944. /* Init IO handler */
  1945. _rtl_pci_io_handler_init(&pdev->dev, hw);
  1946. /*like read eeprom and so on */
  1947. rtlpriv->cfg->ops->read_eeprom_info(hw);
  1948. if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
  1949. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
  1950. err = -ENODEV;
  1951. goto fail3;
  1952. }
  1953. rtlpriv->cfg->ops->init_sw_leds(hw);
  1954. /*aspm */
  1955. rtl_pci_init_aspm(hw);
  1956. /* Init mac80211 sw */
  1957. err = rtl_init_core(hw);
  1958. if (err) {
  1959. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1960. "Can't allocate sw for mac80211\n");
  1961. goto fail3;
  1962. }
  1963. /* Init PCI sw */
  1964. err = rtl_pci_init(hw, pdev);
  1965. if (err) {
  1966. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Failed to init PCI\n");
  1967. goto fail3;
  1968. }
  1969. err = ieee80211_register_hw(hw);
  1970. if (err) {
  1971. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1972. "Can't register mac80211 hw.\n");
  1973. err = -ENODEV;
  1974. goto fail3;
  1975. }
  1976. rtlpriv->mac80211.mac80211_registered = 1;
  1977. err = sysfs_create_group(&pdev->dev.kobj, &rtl_attribute_group);
  1978. if (err) {
  1979. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1980. "failed to create sysfs device attributes\n");
  1981. goto fail3;
  1982. }
  1983. /*init rfkill */
  1984. rtl_init_rfkill(hw); /* Init PCI sw */
  1985. rtlpci = rtl_pcidev(pcipriv);
  1986. err = rtl_pci_intr_mode_decide(hw);
  1987. if (err) {
  1988. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  1989. "%s: failed to register IRQ handler\n",
  1990. wiphy_name(hw->wiphy));
  1991. goto fail3;
  1992. }
  1993. rtlpci->irq_alloc = 1;
  1994. set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
  1995. return 0;
  1996. fail3:
  1997. pci_set_drvdata(pdev, NULL);
  1998. rtl_deinit_core(hw);
  1999. if (rtlpriv->io.pci_mem_start != 0)
  2000. pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
  2001. fail2:
  2002. pci_release_regions(pdev);
  2003. complete(&rtlpriv->firmware_loading_complete);
  2004. fail1:
  2005. if (hw)
  2006. ieee80211_free_hw(hw);
  2007. pci_disable_device(pdev);
  2008. return err;
  2009. }
  2010. EXPORT_SYMBOL(rtl_pci_probe);
  2011. void rtl_pci_disconnect(struct pci_dev *pdev)
  2012. {
  2013. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  2014. struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
  2015. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2016. struct rtl_pci *rtlpci = rtl_pcidev(pcipriv);
  2017. struct rtl_mac *rtlmac = rtl_mac(rtlpriv);
  2018. /* just in case driver is removed before firmware callback */
  2019. wait_for_completion(&rtlpriv->firmware_loading_complete);
  2020. clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
  2021. sysfs_remove_group(&pdev->dev.kobj, &rtl_attribute_group);
  2022. /*ieee80211_unregister_hw will call ops_stop */
  2023. if (rtlmac->mac80211_registered == 1) {
  2024. ieee80211_unregister_hw(hw);
  2025. rtlmac->mac80211_registered = 0;
  2026. } else {
  2027. rtl_deinit_deferred_work(hw);
  2028. rtlpriv->intf_ops->adapter_stop(hw);
  2029. }
  2030. rtlpriv->cfg->ops->disable_interrupt(hw);
  2031. /*deinit rfkill */
  2032. rtl_deinit_rfkill(hw);
  2033. rtl_pci_deinit(hw);
  2034. rtl_deinit_core(hw);
  2035. rtlpriv->cfg->ops->deinit_sw_vars(hw);
  2036. if (rtlpci->irq_alloc) {
  2037. synchronize_irq(rtlpci->pdev->irq);
  2038. free_irq(rtlpci->pdev->irq, hw);
  2039. rtlpci->irq_alloc = 0;
  2040. }
  2041. if (rtlpci->using_msi)
  2042. pci_disable_msi(rtlpci->pdev);
  2043. list_del(&rtlpriv->list);
  2044. if (rtlpriv->io.pci_mem_start != 0) {
  2045. pci_iounmap(pdev, (void __iomem *)rtlpriv->io.pci_mem_start);
  2046. pci_release_regions(pdev);
  2047. }
  2048. pci_disable_device(pdev);
  2049. rtl_pci_disable_aspm(hw);
  2050. pci_set_drvdata(pdev, NULL);
  2051. ieee80211_free_hw(hw);
  2052. }
  2053. EXPORT_SYMBOL(rtl_pci_disconnect);
  2054. #ifdef CONFIG_PM_SLEEP
  2055. /***************************************
  2056. kernel pci power state define:
  2057. PCI_D0 ((pci_power_t __force) 0)
  2058. PCI_D1 ((pci_power_t __force) 1)
  2059. PCI_D2 ((pci_power_t __force) 2)
  2060. PCI_D3hot ((pci_power_t __force) 3)
  2061. PCI_D3cold ((pci_power_t __force) 4)
  2062. PCI_UNKNOWN ((pci_power_t __force) 5)
  2063. This function is called when system
  2064. goes into suspend state mac80211 will
  2065. call rtl_mac_stop() from the mac80211
  2066. suspend function first, So there is
  2067. no need to call hw_disable here.
  2068. ****************************************/
  2069. int rtl_pci_suspend(struct device *dev)
  2070. {
  2071. struct pci_dev *pdev = to_pci_dev(dev);
  2072. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  2073. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2074. rtlpriv->cfg->ops->hw_suspend(hw);
  2075. rtl_deinit_rfkill(hw);
  2076. return 0;
  2077. }
  2078. EXPORT_SYMBOL(rtl_pci_suspend);
  2079. int rtl_pci_resume(struct device *dev)
  2080. {
  2081. struct pci_dev *pdev = to_pci_dev(dev);
  2082. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  2083. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2084. rtlpriv->cfg->ops->hw_resume(hw);
  2085. rtl_init_rfkill(hw);
  2086. return 0;
  2087. }
  2088. EXPORT_SYMBOL(rtl_pci_resume);
  2089. #endif /* CONFIG_PM_SLEEP */
  2090. struct rtl_intf_ops rtl_pci_ops = {
  2091. .read_efuse_byte = read_efuse_byte,
  2092. .adapter_start = rtl_pci_start,
  2093. .adapter_stop = rtl_pci_stop,
  2094. .check_buddy_priv = rtl_pci_check_buddy_priv,
  2095. .adapter_tx = rtl_pci_tx,
  2096. .flush = rtl_pci_flush,
  2097. .reset_trx_ring = rtl_pci_reset_trx_ring,
  2098. .waitq_insert = rtl_pci_tx_chk_waitq_insert,
  2099. .disable_aspm = rtl_pci_disable_aspm,
  2100. .enable_aspm = rtl_pci_enable_aspm,
  2101. };