pci.c 67 KB

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