pci.c 67 KB

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