pci_intf.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2017 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. *****************************************************************************/
  15. #define _HCI_INTF_C_
  16. #include <drv_types.h>
  17. #include <hal_data.h>
  18. #include <linux/pci_regs.h>
  19. #ifndef CONFIG_PCI_HCI
  20. #error "CONFIG_PCI_HCI shall be on!\n"
  21. #endif
  22. #if defined(PLATFORM_LINUX) && defined(PLATFORM_WINDOWS)
  23. #error "Shall be Linux or Windows, but not both!\n"
  24. #endif
  25. #ifdef CONFIG_80211N_HT
  26. extern int rtw_ht_enable;
  27. extern int rtw_bw_mode;
  28. extern int rtw_ampdu_enable;/* for enable tx_ampdu */
  29. #endif
  30. #ifdef CONFIG_GLOBAL_UI_PID
  31. int ui_pid[3] = {0, 0, 0};
  32. #endif
  33. extern int pm_netdev_open(struct net_device *pnetdev, u8 bnormal);
  34. int rtw_resume_process(_adapter *padapter);
  35. #ifdef CONFIG_PM
  36. static int rtw_pci_suspend(struct pci_dev *pdev, pm_message_t state);
  37. static int rtw_pci_resume(struct pci_dev *pdev);
  38. #endif
  39. static int rtw_drv_init(struct pci_dev *pdev, const struct pci_device_id *pdid);
  40. static void rtw_dev_remove(struct pci_dev *pdev);
  41. static void rtw_dev_shutdown(struct pci_dev *pdev);
  42. static struct specific_device_id specific_device_id_tbl[] = {
  43. {.idVendor = 0x0b05, .idProduct = 0x1791, .flags = SPEC_DEV_ID_DISABLE_HT},
  44. {.idVendor = 0x13D3, .idProduct = 0x3311, .flags = SPEC_DEV_ID_DISABLE_HT},
  45. {}
  46. };
  47. struct pci_device_id rtw_pci_id_tbl[] = {
  48. #ifdef CONFIG_RTL8188E
  49. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0x8179), .driver_data = RTL8188E},
  50. #endif
  51. #ifdef CONFIG_RTL8812A
  52. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0x8812), .driver_data = RTL8812},
  53. #endif
  54. #ifdef CONFIG_RTL8821A
  55. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0x8821), .driver_data = RTL8821},
  56. #endif
  57. #ifdef CONFIG_RTL8192E
  58. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0x818B), .driver_data = RTL8192E},
  59. #endif
  60. #ifdef CONFIG_RTL8192F
  61. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0xf192), .driver_data = RTL8192F},
  62. #endif
  63. #ifdef CONFIG_RTL8723B
  64. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0xb723), .driver_data = RTL8723B},
  65. #endif
  66. #ifdef CONFIG_RTL8723D
  67. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0xd723), .driver_data = RTL8723D},
  68. #endif
  69. #ifdef CONFIG_RTL8814A
  70. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0x8813), .driver_data = RTL8814A},
  71. #endif
  72. #ifdef CONFIG_RTL8822B
  73. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0xB822), .driver_data = RTL8822B},
  74. #endif
  75. #ifdef CONFIG_RTL8821C
  76. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0xC821), .driver_data = RTL8821C},
  77. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0xC82A), .driver_data = RTL8821C},
  78. {PCI_DEVICE(PCI_VENDER_ID_REALTEK, 0xC82B), .driver_data = RTL8821C},
  79. #endif
  80. {},
  81. };
  82. struct pci_drv_priv {
  83. struct pci_driver rtw_pci_drv;
  84. int drv_registered;
  85. };
  86. static struct pci_drv_priv pci_drvpriv = {
  87. .rtw_pci_drv.name = (char *)DRV_NAME,
  88. .rtw_pci_drv.probe = rtw_drv_init,
  89. .rtw_pci_drv.remove = rtw_dev_remove,
  90. .rtw_pci_drv.shutdown = rtw_dev_shutdown,
  91. .rtw_pci_drv.id_table = rtw_pci_id_tbl,
  92. #ifdef CONFIG_PM
  93. .rtw_pci_drv.suspend = rtw_pci_suspend,
  94. .rtw_pci_drv.resume = rtw_pci_resume,
  95. #endif
  96. };
  97. MODULE_DEVICE_TABLE(pci, rtw_pci_id_tbl);
  98. static u16 pcibridge_vendors[PCI_BRIDGE_VENDOR_MAX] = {
  99. INTEL_VENDOR_ID,
  100. ATI_VENDOR_ID,
  101. AMD_VENDOR_ID,
  102. SIS_VENDOR_ID
  103. };
  104. #define PCI_PM_CAP_ID 0x01 /* The Capability ID for PME function */
  105. void PlatformClearPciPMEStatus(PADAPTER Adapter)
  106. {
  107. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(Adapter);
  108. struct pci_dev *pdev = pdvobjpriv->ppcidev;
  109. BOOLEAN PCIClkReq = _FALSE;
  110. u8 CapId = 0xff;
  111. u8 CapPointer = 0;
  112. /* u16 CapHdr; */
  113. RT_PCI_CAPABILITIES_HEADER CapHdr;
  114. u8 PMCSReg;
  115. int result;
  116. /* Get the Capability pointer first, */
  117. /* the Capability Pointer is located at offset 0x34 from the Function Header */
  118. result = pci_read_config_byte(pdev, 0x34, &CapPointer);
  119. if (result != 0)
  120. RTW_INFO("%s() pci_read_config_byte 0x34 Failed!\n", __func__);
  121. else {
  122. RTW_INFO("PlatformClearPciPMEStatus(): PCI configration 0x34 = 0x%2x\n", CapPointer);
  123. do {
  124. /* end of pci capability */
  125. if (CapPointer == 0x00) {
  126. CapId = 0xff;
  127. break;
  128. }
  129. /* result = pci_read_config_word(pdev, CapPointer, &CapHdr); */
  130. result = pci_read_config_byte(pdev, CapPointer, &CapHdr.CapabilityID);
  131. if (result != 0) {
  132. RTW_INFO("%s() pci_read_config_byte %x Failed!\n", __func__, CapPointer);
  133. CapId = 0xff;
  134. break;
  135. }
  136. result = pci_read_config_byte(pdev, CapPointer + 1, &CapHdr.Next);
  137. if (result != 0) {
  138. RTW_INFO("%s() pci_read_config_byte %x Failed!\n", __func__, CapPointer);
  139. CapId = 0xff;
  140. break;
  141. }
  142. /* CapId = CapHdr & 0xFF; */
  143. CapId = CapHdr.CapabilityID;
  144. RTW_INFO("PlatformClearPciPMEStatus(): in pci configration1, CapPointer%x = %x\n", CapPointer, CapId);
  145. if (CapId == PCI_PM_CAP_ID)
  146. break;
  147. else {
  148. /* point to next Capability */
  149. /* CapPointer = (CapHdr >> 8) & 0xFF; */
  150. CapPointer = CapHdr.Next;
  151. }
  152. } while (_TRUE);
  153. if (CapId == PCI_PM_CAP_ID) {
  154. /* Get the PM CSR (Control/Status Register), */
  155. /* The PME_Status is located at PM Capatibility offset 5, bit 7 */
  156. result = pci_read_config_byte(pdev, CapPointer + 5, &PMCSReg);
  157. if (PMCSReg & BIT7) {
  158. /* PME event occured, clear the PM_Status by write 1 */
  159. PMCSReg = PMCSReg | BIT7;
  160. pci_write_config_byte(pdev, CapPointer + 5, PMCSReg);
  161. PCIClkReq = _TRUE;
  162. /* Read it back to check */
  163. pci_read_config_byte(pdev, CapPointer + 5, &PMCSReg);
  164. RTW_INFO("PlatformClearPciPMEStatus(): Clear PME status 0x%2x to 0x%2x\n", CapPointer + 5, PMCSReg);
  165. } else
  166. RTW_INFO("PlatformClearPciPMEStatus(): PME status(0x%2x) = 0x%2x\n", CapPointer + 5, PMCSReg);
  167. } else
  168. RTW_INFO("PlatformClearPciPMEStatus(): Cannot find PME Capability\n");
  169. }
  170. RTW_INFO("PME, value_offset = %x, PME EN = %x\n", CapPointer + 5, PCIClkReq);
  171. }
  172. void rtw_pci_aspm_config_clkreql0sl1(_adapter *padapter)
  173. {
  174. HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter);
  175. u8 tmp8 = 0;
  176. u16 tmp16 = 0;
  177. /* 0x70f Bit7 for L0s */
  178. tmp8 = rtw_hal_pci_dbi_read(padapter, 0x70f);
  179. if (pHalData->pci_backdoor_ctrl & PCI_BC_ASPM_L0s)
  180. tmp8 |= BIT7;
  181. else
  182. tmp8 &= (~BIT7);
  183. /* Default set L1 entrance latency to 16us */
  184. /* L0s: b[0-2], L1: b[3-5]*/
  185. if (pHalData->pci_backdoor_ctrl & PCI_BC_ASPM_L1) {
  186. tmp8 &= (~0x38);
  187. tmp8 |= 0x20;
  188. #ifdef CONFIG_PCI_DYNAMIC_ASPM
  189. pHalData->bAspmL1LastIdle = 1;
  190. #endif
  191. }
  192. rtw_hal_pci_dbi_write(padapter, 0x70f, tmp8);
  193. /* 0x719 Bit 3 for L1 , Bit4 for clock req */
  194. tmp8 = rtw_hal_pci_dbi_read(padapter, 0x719);
  195. if (pHalData->pci_backdoor_ctrl & PCI_BC_ASPM_L1)
  196. tmp8 |= BIT3;
  197. else
  198. tmp8 &= (~BIT3);
  199. if (pHalData->pci_backdoor_ctrl & PCI_BC_CLK_REQ)
  200. tmp8 |= BIT4;
  201. else
  202. tmp8 &= (~BIT4);
  203. rtw_hal_pci_dbi_write(padapter, 0x719, tmp8);
  204. if (pHalData->pci_backdoor_ctrl & PCI_BC_CLK_REQ) {
  205. tmp16 = rtw_hal_pci_mdio_read(padapter, 0x10);
  206. rtw_hal_pci_mdio_write(padapter, 0x10, (tmp16 | BIT2));
  207. }
  208. }
  209. void rtw_pci_aspm_config_l1off(_adapter *padapter)
  210. {
  211. HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter);
  212. u8 enable_l1off = _FALSE;
  213. if (pHalData->pci_backdoor_ctrl & PCI_BC_ASPM_L1Off)
  214. enable_l1off = rtw_hal_pci_l1off_nic_support(padapter);
  215. padapter->hal_func.hal_set_l1ssbackdoor_handler(padapter, enable_l1off);
  216. }
  217. void rtw_pci_aspm_config_l1off_general(_adapter *padapter, u8 enablel1off)
  218. {
  219. u8 tmp8;
  220. u16 tmp16;
  221. if (enablel1off) {
  222. /* 0x718 Bit5 for L1SS */
  223. tmp8 = rtw_hal_pci_dbi_read(padapter, 0x718);
  224. rtw_hal_pci_dbi_write(padapter, 0x718, (tmp8 | BIT5));
  225. tmp16 = rtw_hal_pci_mdio_read(padapter, 0x1b);
  226. rtw_hal_pci_mdio_write(padapter, 0x1b, (tmp16 | BIT4));
  227. } else {
  228. tmp8 = rtw_hal_pci_dbi_read(padapter, 0x718);
  229. rtw_hal_pci_dbi_write(padapter, 0x718, (tmp8 & (~BIT5)));
  230. }
  231. }
  232. #ifdef CONFIG_PCI_DYNAMIC_ASPM
  233. void rtw_pci_aspm_config_dynamic_l1_ilde_time(_adapter *padapter)
  234. {
  235. BOOLEAN bCurrentIdle = 1; /* Default idle 4us (0x70F = 0x17)*/
  236. HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter);
  237. struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
  238. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  239. int current_tx_tp = pdvobjpriv->traffic_stat.cur_tx_tp;
  240. int current_rx_tp = pdvobjpriv->traffic_stat.cur_rx_tp;
  241. int current_tp = current_tx_tp + current_rx_tp;
  242. u8 tmp8 = 0;
  243. if (padapter->registrypriv.wifi_spec)
  244. return;
  245. if (!(pHalData->pci_backdoor_ctrl & PCI_BC_ASPM_L1))
  246. return;
  247. #if 0
  248. RTW_INFO("current_tx_tp = %d\n", current_tx_tp);
  249. RTW_INFO("current_rx_tp = %d\n", current_rx_tp);
  250. RTW_INFO("current_tp = %d\n", current_tp);
  251. #endif
  252. if ((rtw_linked_check(padapter) == _TRUE) &&
  253. ((current_tx_tp >= 50)||
  254. (current_rx_tp >= 50)))
  255. /*(current_rx_tp >= 10))*/
  256. /*(current_tp >= 10))*/
  257. {
  258. bCurrentIdle = 0;
  259. }
  260. else
  261. {
  262. bCurrentIdle = 1;
  263. }
  264. if(bCurrentIdle != pHalData->bAspmL1LastIdle)
  265. {
  266. pHalData->bAspmL1LastIdle = bCurrentIdle;
  267. tmp8 = rtw_hal_pci_dbi_read(padapter, 0x70F);
  268. tmp8 &= (~0x38);
  269. if(bCurrentIdle) {
  270. /*tmp8 |= 0x10; *//*L1 entrance latency: 4us*/
  271. /*tmp8 |= 0x18; *//*L1 entrance latency: 8us*/
  272. tmp8 |= 0x20; /*L1 entrance latency: 16us*/
  273. rtw_hal_pci_dbi_write(padapter, 0x70F, tmp8 );
  274. }
  275. else {
  276. tmp8 |= 0x28; /*L1 entrance latency: 32us*/
  277. rtw_hal_pci_dbi_write(padapter, 0x70F, tmp8 );
  278. }
  279. }
  280. }
  281. #endif
  282. void rtw_pci_dump_aspm_info(_adapter *padapter)
  283. {
  284. HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter);
  285. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  286. struct pci_priv *pcipriv = &(pdvobjpriv->pcipriv);
  287. u8 tmp8 = 0;
  288. u16 tmp16 = 0;
  289. u32 tmp32 = 0;
  290. u8 l1_idle = 0;
  291. RTW_INFO("***** ASPM Capability *****\n");
  292. pci_read_config_dword(pdvobjpriv->ppcidev, pcipriv->pciehdr_offset + PCI_EXP_LNKCAP, &tmp32);
  293. RTW_INFO("CLK REQ: %s\n", (tmp32&PCI_EXP_LNKCAP_CLKPM) ? "Enable" : "Disable");
  294. RTW_INFO("ASPM L0s: %s\n", (tmp32&BIT10) ? "Enable" : "Disable");
  295. RTW_INFO("ASPM L1: %s\n", (tmp32&BIT11) ? "Enable" : "Disable");
  296. tmp8 = rtw_hal_pci_l1off_capability(padapter);
  297. RTW_INFO("ASPM L1OFF:%s\n", tmp8 ? "Enable" : "Disable");
  298. RTW_INFO("***** ASPM CTRL Reg *****\n");
  299. pci_read_config_word(pdvobjpriv->ppcidev, pcipriv->pciehdr_offset + PCI_EXP_LNKCTL, &tmp16);
  300. RTW_INFO("CLK REQ: %s\n", (tmp16&PCI_EXP_LNKCTL_CLKREQ_EN) ? "Enable" : "Disable");
  301. RTW_INFO("ASPM L0s: %s\n", (tmp16&BIT0) ? "Enable" : "Disable");
  302. RTW_INFO("ASPM L1: %s\n", (tmp16&BIT1) ? "Enable" : "Disable");
  303. tmp8 = rtw_hal_pci_l1off_nic_support(padapter);
  304. RTW_INFO("ASPM L1OFF:%s\n", tmp8 ? "Enable" : "Disable");
  305. RTW_INFO("***** ASPM Backdoor *****\n");
  306. tmp8 = rtw_hal_pci_dbi_read(padapter, 0x719);
  307. RTW_INFO("CLK REQ: %s\n", (tmp8 & BIT4) ? "Enable" : "Disable");
  308. tmp8 = rtw_hal_pci_dbi_read(padapter, 0x70f);
  309. l1_idle = tmp8 & 0x38;
  310. RTW_INFO("ASPM L0s: %s\n", (tmp8&BIT7) ? "Enable" : "Disable");
  311. tmp8 = rtw_hal_pci_dbi_read(padapter, 0x719);
  312. RTW_INFO("ASPM L1: %s\n", (tmp8 & BIT3) ? "Enable" : "Disable");
  313. tmp8 = rtw_hal_pci_dbi_read(padapter, 0x718);
  314. RTW_INFO("ASPM L1OFF:%s\n", (tmp8 & BIT5) ? "Enable" : "Disable");
  315. RTW_INFO("********* MISC **********\n");
  316. RTW_INFO("ASPM L1 Idel Time: 0x%x\n", l1_idle>>3);
  317. RTW_INFO("*************************\n");
  318. }
  319. void rtw_pci_aspm_config(_adapter *padapter)
  320. {
  321. rtw_pci_aspm_config_clkreql0sl1(padapter);
  322. rtw_pci_aspm_config_l1off(padapter);
  323. rtw_pci_dump_aspm_info(padapter);
  324. }
  325. static u8 rtw_pci_platform_switch_device_pci_aspm(_adapter *padapter, u8 value)
  326. {
  327. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  328. struct pci_priv *pcipriv = &(pdvobjpriv->pcipriv);
  329. BOOLEAN bResult = _FALSE;
  330. int Result = 0;
  331. int error;
  332. Result = pci_write_config_byte(pdvobjpriv->ppcidev, pcipriv->pciehdr_offset + 0x10, value); /* enable I/O space */
  333. RTW_INFO("PlatformSwitchDevicePciASPM(0x%x) = 0x%x\n", pcipriv->pciehdr_offset + 0x10, value);
  334. if (Result != 0) {
  335. RTW_INFO("PlatformSwitchDevicePciASPM() Failed!\n");
  336. bResult = _FALSE;
  337. } else
  338. bResult = _TRUE;
  339. return bResult;
  340. }
  341. /*
  342. * When we set 0x01 to enable clk request. Set 0x0 to disable clk req.
  343. */
  344. static u8 rtw_pci_switch_clk_req(_adapter *padapter, u8 value)
  345. {
  346. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  347. u8 buffer, bResult = _FALSE;
  348. int error;
  349. buffer = value;
  350. if (!rtw_is_hw_init_completed(padapter))
  351. return bResult;
  352. /* the clock request is located at offset 0x81, suppose the PCIE Capability register is located at offset 0x70 */
  353. /* the correct code should be: search the PCIE capability register first and then the clock request is located offset 0x11 */
  354. error = pci_write_config_byte(pdvobjpriv->ppcidev, 0x81, buffer);
  355. if (error != 0)
  356. RTW_INFO("rtw_pci_switch_clk_req error (%d)\n", error);
  357. else
  358. bResult = _TRUE;
  359. return bResult;
  360. }
  361. /*Disable RTL8192SE ASPM & Disable Pci Bridge ASPM*/
  362. void rtw_pci_disable_aspm(_adapter *padapter)
  363. {
  364. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  365. struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(pdvobjpriv);
  366. struct pci_dev *pdev = pdvobjpriv->ppcidev;
  367. struct pci_dev *bridge_pdev = pdev->bus->self;
  368. struct pci_priv *pcipriv = &(pdvobjpriv->pcipriv);
  369. u8 linkctrl_reg;
  370. u8 pcibridge_linkctrlreg, aspmlevel = 0;
  371. /* We shall check RF Off Level for ASPM function instead of registry settings, revised by Roger, 2013.03.29. */
  372. if (!(pwrpriv->reg_rfps_level & (RT_RF_LPS_LEVEL_ASPM | RT_RF_PS_LEVEL_ALWAYS_ASPM)))
  373. return;
  374. if (!rtw_is_hw_init_completed(padapter))
  375. return;
  376. if (pcipriv->pcibridge_vendor == PCI_BRIDGE_VENDOR_UNKNOWN)
  377. return;
  378. linkctrl_reg = pcipriv->linkctrl_reg;
  379. pcibridge_linkctrlreg = pcipriv->pcibridge_linkctrlreg;
  380. /* Set corresponding value. */
  381. aspmlevel |= BIT(0) | BIT(1);
  382. linkctrl_reg &= ~aspmlevel;
  383. pcibridge_linkctrlreg &= ~aspmlevel;
  384. /* */
  385. /* 09/08/21 MH From Sd1 suggestion. we need to adjust ASPM enable sequence */
  386. /* CLK_REQ ==> delay 50us ==> Device ==> Host ==> delay 50us */
  387. /* */
  388. if (pwrpriv->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) {
  389. RT_CLEAR_PS_LEVEL(pwrpriv, RT_RF_OFF_LEVL_CLK_REQ);
  390. rtw_pci_switch_clk_req(padapter, 0x0);
  391. }
  392. {
  393. /*for promising device will in L0 state after an I/O.*/
  394. u8 tmp_u1b;
  395. pci_read_config_byte(pdev, (pcipriv->pciehdr_offset + 0x10), &tmp_u1b);
  396. }
  397. rtw_pci_platform_switch_device_pci_aspm(padapter, linkctrl_reg);
  398. rtw_udelay_os(50);
  399. /* When there exists anyone's BusNum, DevNum, and FuncNum that are set to 0xff, */
  400. /* we do not execute any action and return. Added by tynli. */
  401. if ((pcipriv->busnumber == 0xff && pcipriv->devnumber == 0xff && pcipriv->funcnumber == 0xff) ||
  402. (pcipriv->pcibridge_busnum == 0xff && pcipriv->pcibridge_devnum == 0xff && pcipriv->pcibridge_funcnum == 0xff)) {
  403. /* Do Nothing!! */
  404. } else {
  405. /* 4 */ /* Disable Pci Bridge ASPM */
  406. pci_write_config_byte(bridge_pdev, (pcipriv->pcibridge_pciehdr_offset + 0x10), pcibridge_linkctrlreg);
  407. RTW_INFO("PlatformDisableASPM():PciBridge Write reg[%x] = %x\n",
  408. (pcipriv->pcibridge_pciehdr_offset + 0x10), pcibridge_linkctrlreg);
  409. rtw_udelay_os(50);
  410. }
  411. }
  412. /*Enable RTL8192SE ASPM & Enable Pci Bridge ASPM for
  413. * power saving We should follow the sequence to enable
  414. * RTL8192SE first then enable Pci Bridge ASPM
  415. * or the system will show bluescreen.
  416. */
  417. void rtw_pci_enable_aspm(_adapter *padapter)
  418. {
  419. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  420. struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(pdvobjpriv);
  421. struct pci_dev *pdev = pdvobjpriv->ppcidev;
  422. struct pci_dev *bridge_pdev = pdev->bus->self;
  423. struct pci_priv *pcipriv = &(pdvobjpriv->pcipriv);
  424. u16 aspmlevel = 0;
  425. u8 u_pcibridge_aspmsetting = 0;
  426. u8 u_device_aspmsetting = 0;
  427. u32 u_device_aspmsupportsetting = 0;
  428. /* We shall check RF Off Level for ASPM function instead of registry settings, revised by Roger, 2013.03.29. */
  429. if (!(pwrpriv->reg_rfps_level & (RT_RF_LPS_LEVEL_ASPM | RT_RF_PS_LEVEL_ALWAYS_ASPM)))
  430. return;
  431. /* When there exists anyone's BusNum, DevNum, and FuncNum that are set to 0xff, */
  432. /* we do not execute any action and return. Added by tynli. */
  433. if ((pcipriv->busnumber == 0xff && pcipriv->devnumber == 0xff && pcipriv->funcnumber == 0xff) ||
  434. (pcipriv->pcibridge_busnum == 0xff && pcipriv->pcibridge_devnum == 0xff && pcipriv->pcibridge_funcnum == 0xff)) {
  435. RTW_INFO("rtw_pci_enable_aspm(): Fail to enable ASPM. Cannot find the Bus of PCI(Bridge).\n");
  436. return;
  437. }
  438. /* Get Bridge ASPM Support
  439. * not to enable bridge aspm if bridge does not support
  440. * Added by sherry 20100803
  441. */
  442. {
  443. /* Get the Link Capability, it ls located at offset 0x0c from the PCIE Capability */
  444. pci_read_config_dword(bridge_pdev, (pcipriv->pcibridge_pciehdr_offset + 0x0C), &u_device_aspmsupportsetting);
  445. RTW_INFO("rtw_pci_enable_aspm(): Bridge ASPM support %x\n", u_device_aspmsupportsetting);
  446. if (((u_device_aspmsupportsetting & BIT(11)) != BIT(11)) || ((u_device_aspmsupportsetting & BIT(10)) != BIT(10))) {
  447. if (pdvobjpriv->const_devicepci_aspm_setting == 3) {
  448. RTW_INFO("rtw_pci_enable_aspm(): Bridge not support L0S or L1\n");
  449. return;
  450. } else if (pdvobjpriv->const_devicepci_aspm_setting == 2) {
  451. if ((u_device_aspmsupportsetting & BIT(11)) != BIT(11)) {
  452. RTW_INFO("rtw_pci_enable_aspm(): Bridge not support L1\n");
  453. return;
  454. }
  455. } else if (pdvobjpriv->const_devicepci_aspm_setting == 1) {
  456. if ((u_device_aspmsupportsetting & BIT(10)) != BIT(10)) {
  457. RTW_INFO("rtw_pci_enable_aspm(): Bridge not support L0s\n");
  458. return;
  459. }
  460. }
  461. } else
  462. RTW_INFO("rtw_pci_enable_aspm(): Bridge support L0s and L1\n");
  463. }
  464. /*
  465. * Skip following settings if ASPM has already enabled, added by Roger, 2013.03.15.
  466. */
  467. if ((pcipriv->pcibridge_linkctrlreg & (BIT0 | BIT1)) &&
  468. (pcipriv->linkctrl_reg & (BIT0 | BIT1))) {
  469. /* BIT0: L0S, BIT1:L1 */
  470. RTW_INFO("PlatformEnableASPM(): ASPM is already enabled, skip incoming settings!!\n");
  471. return;
  472. }
  473. /* 4 Enable Pci Bridge ASPM */
  474. /* Write PCI bridge PCIE-capability Link Control Register */
  475. /* Justin: Can we change the ASPM Control register ? */
  476. /* The system BIOS should set this register with a correct value */
  477. /* If we change the force enable the ASPM L1/L0s, this may cause the system hang */
  478. u_pcibridge_aspmsetting = pcipriv->pcibridge_linkctrlreg;
  479. u_pcibridge_aspmsetting |= pdvobjpriv->const_hostpci_aspm_setting;
  480. if (pcipriv->pcibridge_vendor == PCI_BRIDGE_VENDOR_INTEL ||
  481. pcipriv->pcibridge_vendor == PCI_BRIDGE_VENDOR_SIS)
  482. u_pcibridge_aspmsetting &= ~BIT(0); /* for intel host 42 device 43 */
  483. pci_write_config_byte(bridge_pdev, (pcipriv->pcibridge_pciehdr_offset + 0x10), u_pcibridge_aspmsetting);
  484. RTW_INFO("PlatformEnableASPM():PciBridge Write reg[%x] = %x\n",
  485. (pcipriv->pcibridge_pciehdr_offset + 0x10),
  486. u_pcibridge_aspmsetting);
  487. rtw_udelay_os(50);
  488. /*Get ASPM level (with/without Clock Req)*/
  489. aspmlevel |= pdvobjpriv->const_devicepci_aspm_setting;
  490. u_device_aspmsetting = pcipriv->linkctrl_reg;
  491. u_device_aspmsetting |= aspmlevel; /* device 43 */
  492. rtw_pci_platform_switch_device_pci_aspm(padapter, u_device_aspmsetting);
  493. if (pwrpriv->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) {
  494. rtw_pci_switch_clk_req(padapter, (pwrpriv->reg_rfps_level & RT_RF_OFF_LEVL_CLK_REQ) ? 1 : 0);
  495. RT_SET_PS_LEVEL(pwrpriv, RT_RF_OFF_LEVL_CLK_REQ);
  496. }
  497. rtw_udelay_os(50);
  498. }
  499. static u8 rtw_pci_get_amd_l1_patch(struct dvobj_priv *pdvobjpriv, struct pci_dev *pdev)
  500. {
  501. u8 status = _FALSE;
  502. u8 offset_e0;
  503. u32 offset_e4;
  504. pci_write_config_byte(pdev, 0xE0, 0xA0);
  505. pci_read_config_byte(pdev, 0xE0, &offset_e0);
  506. if (offset_e0 == 0xA0) {
  507. pci_read_config_dword(pdev, 0xE4, &offset_e4);
  508. if (offset_e4 & BIT(23))
  509. status = _TRUE;
  510. }
  511. return status;
  512. }
  513. static s32 rtw_pci_get_linkcontrol_reg(struct pci_dev *pdev, u8 *LinkCtrlReg, u8 *HdrOffset)
  514. {
  515. u8 CapabilityPointer;
  516. RT_PCI_CAPABILITIES_HEADER CapabilityHdr;
  517. s32 status = _FAIL;
  518. /* get CapabilityOffset */
  519. pci_read_config_byte(pdev, 0x34, &CapabilityPointer); /* the capability pointer is located offset 0x34 */
  520. /* Loop through the capabilities in search of the power management capability. */
  521. /* The list is NULL-terminated, so the last offset will always be zero. */
  522. while (CapabilityPointer != 0) {
  523. /* Read the header of the capability at this offset. If the retrieved capability is not */
  524. /* the power management capability that we are looking for, follow the link to the */
  525. /* next capability and continue looping. */
  526. /* 4 get CapabilityHdr */
  527. /* pci_read_config_word(pdev, CapabilityPointer, (u16 *)&CapabilityHdr); */
  528. pci_read_config_byte(pdev, CapabilityPointer, (u8 *)&CapabilityHdr.CapabilityID);
  529. pci_read_config_byte(pdev, CapabilityPointer + 1, (u8 *)&CapabilityHdr.Next);
  530. /* Found the PCI express capability */
  531. if (CapabilityHdr.CapabilityID == PCI_CAPABILITY_ID_PCI_EXPRESS)
  532. break;
  533. else {
  534. /* This is some other capability. Keep looking for the PCI express capability. */
  535. CapabilityPointer = CapabilityHdr.Next;
  536. }
  537. }
  538. /* Get the Link Control Register, it located at offset 0x10 from the Capability Header */
  539. if (CapabilityHdr.CapabilityID == PCI_CAPABILITY_ID_PCI_EXPRESS) {
  540. *HdrOffset = CapabilityPointer;
  541. pci_read_config_byte(pdev, CapabilityPointer + 0x10, LinkCtrlReg);
  542. status = _SUCCESS;
  543. } else {
  544. /* We didn't find a PCIe capability. */
  545. RTW_INFO("GetPciLinkCtrlReg(): Cannot Find PCIe Capability\n");
  546. }
  547. return status;
  548. }
  549. static s32 rtw_set_pci_cache_line_size(struct pci_dev *pdev, u8 CacheLineSizeToSet)
  550. {
  551. u8 ucPciCacheLineSize;
  552. s32 Result;
  553. /* ucPciCacheLineSize = pPciConfig->CacheLineSize; */
  554. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &ucPciCacheLineSize);
  555. if (ucPciCacheLineSize < 8 || ucPciCacheLineSize > 16) {
  556. RTW_INFO("Driver Sets default Cache Line Size...\n");
  557. ucPciCacheLineSize = CacheLineSizeToSet;
  558. Result = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, ucPciCacheLineSize);
  559. if (Result != 0) {
  560. RTW_INFO("pci_write_config_byte (CacheLineSize) Result=%d\n", Result);
  561. goto _SET_CACHELINE_SIZE_FAIL;
  562. }
  563. Result = pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &ucPciCacheLineSize);
  564. if (Result != 0) {
  565. RTW_INFO("pci_read_config_byte (PciCacheLineSize) Result=%d\n", Result);
  566. goto _SET_CACHELINE_SIZE_FAIL;
  567. }
  568. if (ucPciCacheLineSize != CacheLineSizeToSet) {
  569. RTW_INFO("Failed to set Cache Line Size to 0x%x! ucPciCacheLineSize=%x\n", CacheLineSizeToSet, ucPciCacheLineSize);
  570. goto _SET_CACHELINE_SIZE_FAIL;
  571. }
  572. }
  573. return _SUCCESS;
  574. _SET_CACHELINE_SIZE_FAIL:
  575. return _FAIL;
  576. }
  577. #define PCI_CMD_ENABLE_BUS_MASTER BIT(2)
  578. #define PCI_CMD_DISABLE_INTERRUPT BIT(10)
  579. #define CMD_BUS_MASTER BIT(2)
  580. static s32 rtw_pci_parse_configuration(struct pci_dev *pdev, struct dvobj_priv *pdvobjpriv)
  581. {
  582. struct pci_priv *pcipriv = &(pdvobjpriv->pcipriv);
  583. /* PPCI_COMMON_CONFIG pPciConfig = (PPCI_COMMON_CONFIG) pucBuffer; */
  584. /* u16 usPciCommand = pPciConfig->Command; */
  585. u16 usPciCommand = 0;
  586. int Result, ret = _FAIL;
  587. u8 CapabilityOffset;
  588. RT_PCI_CAPABILITIES_HEADER CapabilityHdr;
  589. u8 PCIeCap;
  590. u8 LinkCtrlReg;
  591. u8 ClkReqReg;
  592. /* RTW_INFO("%s==>\n", __func__); */
  593. pci_read_config_word(pdev, PCI_COMMAND, &usPciCommand);
  594. do {
  595. /* 3 Enable bus matering if it isn't enabled by the BIOS */
  596. if (!(usPciCommand & PCI_CMD_ENABLE_BUS_MASTER)) {
  597. RTW_INFO("Bus master is not enabled by BIOS! usPciCommand=%x\n", usPciCommand);
  598. usPciCommand |= CMD_BUS_MASTER;
  599. Result = pci_write_config_word(pdev, PCI_COMMAND, usPciCommand);
  600. if (Result != 0) {
  601. RTW_INFO("pci_write_config_word (Command) Result=%d\n", Result);
  602. ret = _FAIL;
  603. break;
  604. }
  605. Result = pci_read_config_word(pdev, PCI_COMMAND, &usPciCommand);
  606. if (Result != 0) {
  607. RTW_INFO("pci_read_config_word (Command) Result=%d\n", Result);
  608. ret = _FAIL;
  609. break;
  610. }
  611. if (!(usPciCommand & PCI_CMD_ENABLE_BUS_MASTER)) {
  612. RTW_INFO("Failed to enable bus master! usPciCommand=%x\n", usPciCommand);
  613. ret = _FAIL;
  614. break;
  615. }
  616. }
  617. RTW_INFO("Bus master is enabled. usPciCommand=%x\n", usPciCommand);
  618. /* 3 Enable interrupt */
  619. if ((usPciCommand & PCI_CMD_DISABLE_INTERRUPT)) {
  620. RTW_INFO("INTDIS==1 usPciCommand=%x\n", usPciCommand);
  621. usPciCommand &= (~PCI_CMD_DISABLE_INTERRUPT);
  622. Result = pci_write_config_word(pdev, PCI_COMMAND, usPciCommand);
  623. if (Result != 0) {
  624. RTW_INFO("pci_write_config_word (Command) Result=%d\n", Result);
  625. ret = _FAIL;
  626. break;
  627. }
  628. Result = pci_read_config_word(pdev, PCI_COMMAND, &usPciCommand);
  629. if (Result != 0) {
  630. RTW_INFO("pci_read_config_word (Command) Result=%d\n", Result);
  631. ret = _FAIL;
  632. break;
  633. }
  634. if ((usPciCommand & PCI_CMD_DISABLE_INTERRUPT)) {
  635. RTW_INFO("Failed to set INTDIS to 0! usPciCommand=%x\n", usPciCommand);
  636. ret = _FAIL;
  637. break;
  638. }
  639. }
  640. /* */
  641. /* Description: Find PCI express capability offset. Porting from 818xB by tynli 2008.12.19 */
  642. /* */
  643. /* ------------------------------------------------------------- */
  644. /* 3 PCIeCap */
  645. /* The device supports capability lists. Find the capabilities. */
  646. /* CapabilityOffset = pPciConfig->u.type0.CapabilitiesPtr; */
  647. pci_read_config_byte(pdev, PCI_CAPABILITY_LIST, &CapabilityOffset);
  648. /* Loop through the capabilities in search of the power management capability. */
  649. /* The list is NULL-terminated, so the last offset will always be zero. */
  650. while (CapabilityOffset != 0) {
  651. /* Read the header of the capability at this offset. If the retrieved capability is not */
  652. /* the power management capability that we are looking for, follow the link to the */
  653. /* next capability and continue looping. */
  654. /* Result = pci_read_config_word(pdev, CapabilityOffset, (u16 *)&CapabilityHdr); */
  655. Result = pci_read_config_byte(pdev, CapabilityOffset, (u8 *)&CapabilityHdr.CapabilityID);
  656. if (Result != 0)
  657. break;
  658. Result = pci_read_config_byte(pdev, CapabilityOffset + 1, (u8 *)&CapabilityHdr.Next);
  659. if (Result != 0)
  660. break;
  661. /* Found the PCI express capability */
  662. if (CapabilityHdr.CapabilityID == PCI_CAPABILITY_ID_PCI_EXPRESS)
  663. break;
  664. else {
  665. /* This is some other capability. Keep looking for the PCI express capability. */
  666. CapabilityOffset = CapabilityHdr.Next;
  667. }
  668. }
  669. if (Result != 0) {
  670. RTW_INFO("pci_read_config_word (RT_PCI_CAPABILITIES_HEADER) Result=%d\n", Result);
  671. break;
  672. }
  673. if (CapabilityHdr.CapabilityID == PCI_CAPABILITY_ID_PCI_EXPRESS) {
  674. pcipriv->pciehdr_offset = CapabilityOffset;
  675. RTW_INFO("PCIe Header Offset =%x\n", CapabilityOffset);
  676. /* Skip past the capabilities header and read the PCI express capability */
  677. /* Justin: The PCI-e capability size should be 2 bytes, why we just get 1 byte */
  678. /* Beside, this PCIeCap seems no one reference it in the driver code */
  679. Result = pci_read_config_byte(pdev, CapabilityOffset + 2, &PCIeCap);
  680. if (Result != 0) {
  681. RTW_INFO("pci_read_config_byte (PCIE Capability) Result=%d\n", Result);
  682. break;
  683. }
  684. pcipriv->pcie_cap = PCIeCap;
  685. RTW_INFO("PCIe Capability =%x\n", PCIeCap);
  686. /* 3 Link Control Register */
  687. /* Read "Link Control Register" Field (80h ~81h) */
  688. Result = pci_read_config_byte(pdev, CapabilityOffset + 0x10, &LinkCtrlReg);
  689. if (Result != 0) {
  690. RTW_INFO("pci_read_config_byte (Link Control Register) Result=%d\n", Result);
  691. break;
  692. }
  693. pcipriv->linkctrl_reg = LinkCtrlReg;
  694. RTW_INFO("Link Control Register =%x\n", LinkCtrlReg);
  695. /* 3 Get Capability of PCI Clock Request */
  696. /* The clock request setting is located at 0x81[0] */
  697. Result = pci_read_config_byte(pdev, CapabilityOffset + 0x11, &ClkReqReg);
  698. if (Result != 0) {
  699. pcipriv->pci_clk_req = _FALSE;
  700. RTW_INFO("pci_read_config_byte (Clock Request Register) Result=%d\n", Result);
  701. break;
  702. }
  703. if (ClkReqReg & BIT(0))
  704. pcipriv->pci_clk_req = _TRUE;
  705. else
  706. pcipriv->pci_clk_req = _FALSE;
  707. RTW_INFO("Clock Request =%x\n", pcipriv->pci_clk_req);
  708. } else {
  709. /* We didn't find a PCIe capability. */
  710. RTW_INFO("Didn't Find PCIe Capability\n");
  711. break;
  712. }
  713. /* 3 Fill Cacheline */
  714. ret = rtw_set_pci_cache_line_size(pdev, 8);
  715. if (ret != _SUCCESS) {
  716. RTW_INFO("rtw_set_pci_cache_line_size fail\n");
  717. break;
  718. }
  719. /* Include 92C suggested by SD1. Added by tynli. 2009.11.25.
  720. * Enable the Backdoor
  721. */
  722. {
  723. u8 tmp;
  724. Result = pci_read_config_byte(pdev, 0x98, &tmp);
  725. tmp |= BIT4;
  726. Result = pci_write_config_byte(pdev, 0x98, tmp);
  727. }
  728. ret = _SUCCESS;
  729. } while (_FALSE);
  730. return ret;
  731. }
  732. /*
  733. * Update PCI dependent default settings.
  734. *
  735. */
  736. static void rtw_pci_update_default_setting(_adapter *padapter)
  737. {
  738. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  739. struct pci_priv *pcipriv = &(pdvobjpriv->pcipriv);
  740. struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(pdvobjpriv);
  741. HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter);
  742. /* reset pPSC->reg_rfps_level & priv->b_support_aspm */
  743. pwrpriv->reg_rfps_level = 0;
  744. /* Update PCI ASPM setting */
  745. /* pwrpriv->const_amdpci_aspm = pdvobjpriv->const_amdpci_aspm; */
  746. switch (pdvobjpriv->const_pci_aspm) {
  747. case 0: /* No ASPM */
  748. break;
  749. case 1: /* ASPM dynamically enabled/disable. */
  750. pwrpriv->reg_rfps_level |= RT_RF_LPS_LEVEL_ASPM;
  751. break;
  752. case 2: /* ASPM with Clock Req dynamically enabled/disable. */
  753. pwrpriv->reg_rfps_level |= (RT_RF_LPS_LEVEL_ASPM | RT_RF_OFF_LEVL_CLK_REQ);
  754. break;
  755. case 3: /* Always enable ASPM and Clock Req from initialization to halt. */
  756. pwrpriv->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM);
  757. pwrpriv->reg_rfps_level |= (RT_RF_PS_LEVEL_ALWAYS_ASPM | RT_RF_OFF_LEVL_CLK_REQ);
  758. break;
  759. case 4: /* Always enable ASPM without Clock Req from initialization to halt. */
  760. pwrpriv->reg_rfps_level &= ~(RT_RF_LPS_LEVEL_ASPM | RT_RF_OFF_LEVL_CLK_REQ);
  761. pwrpriv->reg_rfps_level |= RT_RF_PS_LEVEL_ALWAYS_ASPM;
  762. break;
  763. case 5: /* Linux do not support ASPM OSC, added by Roger, 2013.03.27. */
  764. break;
  765. }
  766. pwrpriv->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
  767. /* Update Radio OFF setting */
  768. switch (pdvobjpriv->const_hwsw_rfoff_d3) {
  769. case 1:
  770. if (pwrpriv->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
  771. pwrpriv->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
  772. break;
  773. case 2:
  774. if (pwrpriv->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM)
  775. pwrpriv->reg_rfps_level |= RT_RF_OFF_LEVL_ASPM;
  776. pwrpriv->reg_rfps_level |= RT_RF_OFF_LEVL_HALT_NIC;
  777. break;
  778. case 3:
  779. pwrpriv->reg_rfps_level |= RT_RF_OFF_LEVL_PCI_D3;
  780. break;
  781. }
  782. /* Update Rx 2R setting */
  783. /* pPSC->reg_rfps_level |= ((pDevice->RegLPS2RDisable) ? RT_RF_LPS_DISALBE_2R : 0); */
  784. /* */
  785. /* Set HW definition to determine if it supports ASPM. */
  786. /* */
  787. switch (pdvobjpriv->const_support_pciaspm) {
  788. case 1: { /* Support ASPM. */
  789. u8 b_support_backdoor = _TRUE;
  790. u8 b_support_l1_on_amd = _FALSE;
  791. rtw_hal_get_def_var(padapter, HAL_DEF_PCI_AMD_L1_SUPPORT, &b_support_l1_on_amd);
  792. if (pHalData->CustomerID == RT_CID_TOSHIBA &&
  793. pcipriv->pcibridge_vendor == PCI_BRIDGE_VENDOR_AMD &&
  794. !pcipriv->amd_l1_patch && !b_support_l1_on_amd) {
  795. RTW_INFO("%s(): Disable L1 Backdoor!!\n", __func__);
  796. b_support_backdoor = _FALSE;
  797. }
  798. rtw_hal_set_def_var(padapter, HAL_DEF_PCI_SUUPORT_L1_BACKDOOR, &b_support_backdoor);
  799. }
  800. break;
  801. default:
  802. /* Do nothing. Set when finding the chipset. */
  803. break;
  804. }
  805. }
  806. static void rtw_pci_initialize_adapter_common(_adapter *padapter)
  807. {
  808. struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
  809. rtw_pci_update_default_setting(padapter);
  810. if (pwrpriv->reg_rfps_level & RT_RF_PS_LEVEL_ALWAYS_ASPM) {
  811. /* Always enable ASPM & Clock Req. */
  812. rtw_pci_enable_aspm(padapter);
  813. RT_SET_PS_LEVEL(pwrpriv, RT_RF_PS_LEVEL_ALWAYS_ASPM);
  814. }
  815. }
  816. /*
  817. * 2009/10/28 MH Enable rtl8192ce DMA64 function. We need to enable 0x719 BIT5
  818. * */
  819. #ifdef CONFIG_64BIT_DMA
  820. u8 PlatformEnableDMA64(PADAPTER Adapter)
  821. {
  822. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(Adapter);
  823. struct pci_dev *pdev = pdvobjpriv->ppcidev;
  824. u8 bResult = _TRUE;
  825. u8 value;
  826. pci_read_config_byte(pdev, 0x719, &value);
  827. /* 0x719 Bit5 is DMA64 bit fetch. */
  828. value |= (BIT5);
  829. pci_write_config_byte(pdev, 0x719, value);
  830. return bResult;
  831. }
  832. #endif
  833. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)) || (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  834. #define rtw_pci_interrupt(x, y, z) rtw_pci_interrupt(x, y)
  835. #endif
  836. static irqreturn_t rtw_pci_interrupt(int irq, void *priv, struct pt_regs *regs)
  837. {
  838. struct dvobj_priv *dvobj = (struct dvobj_priv *)priv;
  839. _adapter *adapter = dvobj_get_primary_adapter(dvobj);
  840. if (dvobj->irq_enabled == 0)
  841. return IRQ_HANDLED;
  842. if (rtw_hal_interrupt_handler(adapter) == _FAIL)
  843. return IRQ_HANDLED;
  844. /* return IRQ_NONE; */
  845. return IRQ_HANDLED;
  846. }
  847. #ifdef RTK_DMP_PLATFORM
  848. #define pci_iounmap(x, y) iounmap(y)
  849. #endif
  850. int pci_alloc_irq(struct dvobj_priv *dvobj)
  851. {
  852. int err;
  853. struct pci_dev *pdev = dvobj->ppcidev;
  854. int ret;
  855. ret = pci_enable_msi(pdev);
  856. RTW_INFO("pci_enable_msi ret=%d\n", ret);
  857. #if defined(IRQF_SHARED)
  858. err = request_irq(pdev->irq, &rtw_pci_interrupt, IRQF_SHARED, DRV_NAME, dvobj);
  859. #else
  860. err = request_irq(pdev->irq, &rtw_pci_interrupt, SA_SHIRQ, DRV_NAME, dvobj);
  861. #endif
  862. if (err)
  863. RTW_INFO("Error allocating IRQ %d", pdev->irq);
  864. else {
  865. dvobj->irq_alloc = 1;
  866. dvobj->irq = pdev->irq;
  867. RTW_INFO("Request_irq OK, IRQ %d\n", pdev->irq);
  868. }
  869. return err ? _FAIL : _SUCCESS;
  870. }
  871. static void rtw_decide_chip_type_by_pci_driver_data(struct dvobj_priv *pdvobj, const struct pci_device_id *pdid)
  872. {
  873. pdvobj->chip_type = pdid->driver_data;
  874. #ifdef CONFIG_RTL8188E
  875. if (pdvobj->chip_type == RTL8188E) {
  876. pdvobj->HardwareType = HARDWARE_TYPE_RTL8188EE;
  877. RTW_INFO("CHIP TYPE: RTL8188E\n");
  878. }
  879. #endif
  880. #ifdef CONFIG_RTL8812A
  881. if (pdvobj->chip_type == RTL8812) {
  882. pdvobj->HardwareType = HARDWARE_TYPE_RTL8812E;
  883. RTW_INFO("CHIP TYPE: RTL8812AE\n");
  884. }
  885. #endif
  886. #ifdef CONFIG_RTL8821A
  887. if (pdvobj->chip_type == RTL8821) {
  888. pdvobj->HardwareType = HARDWARE_TYPE_RTL8821E;
  889. RTW_INFO("CHIP TYPE: RTL8821AE\n");
  890. }
  891. #endif
  892. #ifdef CONFIG_RTL8723B
  893. if (pdvobj->chip_type == RTL8723B) {
  894. pdvobj->HardwareType = HARDWARE_TYPE_RTL8723BE;
  895. RTW_INFO("CHIP TYPE: RTL8723BE\n");
  896. }
  897. #endif
  898. #ifdef CONFIG_RTL8723D
  899. if (pdvobj->chip_type == RTL8723D) {
  900. pdvobj->HardwareType = HARDWARE_TYPE_RTL8723DE;
  901. RTW_INFO("CHIP TYPE: RTL8723DE\n");
  902. }
  903. #endif
  904. #ifdef CONFIG_RTL8192E
  905. if (pdvobj->chip_type == RTL8192E) {
  906. pdvobj->HardwareType = HARDWARE_TYPE_RTL8192EE;
  907. RTW_INFO("CHIP TYPE: RTL8192EE\n");
  908. }
  909. #endif
  910. #ifdef CONFIG_RTL8192F
  911. if (pdvobj->chip_type == RTL8192F) {
  912. pdvobj->HardwareType = HARDWARE_TYPE_RTL8192FE;
  913. RTW_INFO("CHIP TYPE: RTL8192FE\n");
  914. }
  915. #endif
  916. #ifdef CONFIG_RTL8814A
  917. if (pdvobj->chip_type == RTL8814A) {
  918. pdvobj->HardwareType = HARDWARE_TYPE_RTL8814AE;
  919. RTW_INFO("CHIP TYPE: RTL8814AE\n");
  920. }
  921. #endif
  922. #if defined(CONFIG_RTL8822B)
  923. if (pdvobj->chip_type == RTL8822B) {
  924. pdvobj->HardwareType = HARDWARE_TYPE_RTL8822BE;
  925. RTW_INFO("CHIP TYPE: RTL8822BE\n");
  926. }
  927. #endif
  928. #if defined(CONFIG_RTL8821C)
  929. if (pdvobj->chip_type == RTL8821C) {
  930. pdvobj->HardwareType = HARDWARE_TYPE_RTL8821CE;
  931. RTW_INFO("CHIP TYPE: RTL8821CE\n");
  932. }
  933. #endif
  934. }
  935. static struct dvobj_priv *pci_dvobj_init(struct pci_dev *pdev, const struct pci_device_id *pdid)
  936. {
  937. int err;
  938. u32 status = _FAIL;
  939. struct dvobj_priv *dvobj = NULL;
  940. struct pci_priv *pcipriv = NULL;
  941. struct pci_dev *bridge_pdev = pdev->bus->self;
  942. /* u32 pci_cfg_space[16]; */
  943. unsigned long pmem_start, pmem_len, pmem_flags;
  944. u8 tmp;
  945. u8 PciBgVIdIdx;
  946. int i;
  947. dvobj = devobj_init();
  948. if (dvobj == NULL)
  949. goto exit;
  950. dvobj->ppcidev = pdev;
  951. pcipriv = &(dvobj->pcipriv);
  952. pci_set_drvdata(pdev, dvobj);
  953. err = pci_enable_device(pdev);
  954. if (err != 0) {
  955. RTW_ERR("%s : Cannot enable new PCI device\n", pci_name(pdev));
  956. goto free_dvobj;
  957. }
  958. #ifdef CONFIG_64BIT_DMA
  959. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  960. RTW_INFO("RTL819xCE: Using 64bit DMA\n");
  961. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  962. if (err != 0) {
  963. RTW_ERR("Unable to obtain 64bit DMA for consistent allocations\n");
  964. goto disable_picdev;
  965. }
  966. dvobj->bdma64 = _TRUE;
  967. } else
  968. #endif
  969. {
  970. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  971. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  972. if (err != 0) {
  973. RTW_ERR("Unable to obtain 32bit DMA for consistent allocations\n");
  974. goto disable_picdev;
  975. }
  976. }
  977. dvobj->bdma64 = _FALSE;
  978. }
  979. pci_set_master(pdev);
  980. err = pci_request_regions(pdev, DRV_NAME);
  981. if (err != 0) {
  982. RTW_ERR("Can't obtain PCI resources\n");
  983. goto disable_picdev;
  984. }
  985. #ifdef RTK_129X_PLATFORM
  986. if (pdev->bus->number == 0x00) {
  987. pmem_start = PCIE_SLOT1_MEM_START;
  988. pmem_len = PCIE_SLOT1_MEM_LEN;
  989. pmem_flags = 0;
  990. RTW_PRINT("RTD129X: PCIE SLOT1\n");
  991. } else if (pdev->bus->number == 0x01) {
  992. pmem_start = PCIE_SLOT2_MEM_START;
  993. pmem_len = PCIE_SLOT2_MEM_LEN;
  994. pmem_flags = 0;
  995. RTW_PRINT("RTD129X: PCIE SLOT2\n");
  996. } else {
  997. RTW_ERR(KERN_ERR "RTD129X: Wrong Slot Num\n");
  998. goto release_regions;
  999. }
  1000. #else
  1001. /* Search for memory map resource (index 0~5) */
  1002. for (i = 0 ; i < 6 ; i++) {
  1003. pmem_start = pci_resource_start(pdev, i);
  1004. pmem_len = pci_resource_len(pdev, i);
  1005. pmem_flags = pci_resource_flags(pdev, i);
  1006. if (pmem_flags & IORESOURCE_MEM)
  1007. break;
  1008. }
  1009. if (i == 6) {
  1010. RTW_ERR("%s: No MMIO resource found, abort!\n", __func__);
  1011. goto release_regions;
  1012. }
  1013. #endif /* RTK_DMP_PLATFORM */
  1014. #ifdef RTK_DMP_PLATFORM
  1015. dvobj->pci_mem_start = (unsigned long)ioremap_nocache(pmem_start, pmem_len);
  1016. #elif defined(RTK_129X_PLATFORM)
  1017. if (pdev->bus->number == 0x00)
  1018. dvobj->ctrl_start =
  1019. (unsigned long)ioremap(PCIE_SLOT1_CTRL_START, 0x200);
  1020. else if (pdev->bus->number == 0x01)
  1021. dvobj->ctrl_start =
  1022. (unsigned long)ioremap(PCIE_SLOT2_CTRL_START, 0x200);
  1023. if (dvobj->ctrl_start == 0) {
  1024. RTW_ERR("RTD129X: Can't map CTRL mem\n");
  1025. goto release_regions;
  1026. }
  1027. dvobj->mask_addr = dvobj->ctrl_start + PCIE_MASK_OFFSET;
  1028. dvobj->tran_addr = dvobj->ctrl_start + PCIE_TRANSLATE_OFFSET;
  1029. dvobj->pci_mem_start =
  1030. (unsigned long)ioremap_nocache(pmem_start, pmem_len);
  1031. #else
  1032. /* shared mem start */
  1033. dvobj->pci_mem_start = (unsigned long)pci_iomap(pdev, i, pmem_len);
  1034. #endif
  1035. if (dvobj->pci_mem_start == 0) {
  1036. RTW_ERR("Can't map PCI mem\n");
  1037. goto release_regions;
  1038. }
  1039. RTW_INFO("Memory mapped space start: 0x%08lx len:%08lx flags:%08lx, after map:0x%08lx\n",
  1040. pmem_start, pmem_len, pmem_flags, dvobj->pci_mem_start);
  1041. /*find bus info*/
  1042. pcipriv->busnumber = pdev->bus->number;
  1043. pcipriv->devnumber = PCI_SLOT(pdev->devfn);
  1044. pcipriv->funcnumber = PCI_FUNC(pdev->devfn);
  1045. /*find bridge info*/
  1046. if (bridge_pdev) {
  1047. pcipriv->pcibridge_busnum = bridge_pdev->bus->number;
  1048. pcipriv->pcibridge_devnum = PCI_SLOT(bridge_pdev->devfn);
  1049. pcipriv->pcibridge_funcnum = PCI_FUNC(bridge_pdev->devfn);
  1050. pcipriv->pcibridge_vendor = PCI_BRIDGE_VENDOR_UNKNOWN;
  1051. pcipriv->pcibridge_vendorid = bridge_pdev->vendor;
  1052. pcipriv->pcibridge_deviceid = bridge_pdev->device;
  1053. }
  1054. #if 0
  1055. /* Read PCI configuration Space Header */
  1056. for (i = 0; i < 16; i++)
  1057. pci_read_config_dword(pdev, (i << 2), &pci_cfg_space[i]);
  1058. #endif
  1059. /*step 1-1., decide the chip_type via device info*/
  1060. dvobj->interface_type = RTW_PCIE;
  1061. rtw_decide_chip_type_by_pci_driver_data(dvobj, pdid);
  1062. /* rtw_pci_parse_configuration(pdev, dvobj, (u8 *)&pci_cfg_space); */
  1063. rtw_pci_parse_configuration(pdev, dvobj);
  1064. for (PciBgVIdIdx = 0; PciBgVIdIdx < PCI_BRIDGE_VENDOR_MAX; PciBgVIdIdx++) {
  1065. if (pcipriv->pcibridge_vendorid == pcibridge_vendors[PciBgVIdIdx]) {
  1066. pcipriv->pcibridge_vendor = PciBgVIdIdx;
  1067. RTW_INFO("Pci Bridge Vendor is found: VID=0x%x, VendorIdx=%d\n", pcipriv->pcibridge_vendorid, PciBgVIdIdx);
  1068. break;
  1069. }
  1070. }
  1071. if (pcipriv->pcibridge_vendor != PCI_BRIDGE_VENDOR_UNKNOWN) {
  1072. rtw_pci_get_linkcontrol_reg(bridge_pdev, &pcipriv->pcibridge_linkctrlreg, &pcipriv->pcibridge_pciehdr_offset);
  1073. if (pcipriv->pcibridge_vendor == PCI_BRIDGE_VENDOR_AMD)
  1074. pcipriv->amd_l1_patch = rtw_pci_get_amd_l1_patch(dvobj, bridge_pdev);
  1075. }
  1076. status = _SUCCESS;
  1077. iounmap:
  1078. if (status != _SUCCESS && dvobj->pci_mem_start != 0) {
  1079. #if 1/* def RTK_DMP_PLATFORM */
  1080. pci_iounmap(pdev, (void *)dvobj->pci_mem_start);
  1081. #endif
  1082. dvobj->pci_mem_start = 0;
  1083. }
  1084. #ifdef RTK_129X_PLATFORM
  1085. if (status != _SUCCESS && dvobj->ctrl_start != 0) {
  1086. pci_iounmap(pdev, (void *)dvobj->ctrl_start);
  1087. dvobj->ctrl_start = 0;
  1088. }
  1089. #endif
  1090. release_regions:
  1091. if (status != _SUCCESS)
  1092. pci_release_regions(pdev);
  1093. disable_picdev:
  1094. if (status != _SUCCESS)
  1095. pci_disable_device(pdev);
  1096. free_dvobj:
  1097. if (status != _SUCCESS && dvobj) {
  1098. pci_set_drvdata(pdev, NULL);
  1099. devobj_deinit(dvobj);
  1100. dvobj = NULL;
  1101. }
  1102. exit:
  1103. return dvobj;
  1104. }
  1105. static void pci_dvobj_deinit(struct pci_dev *pdev)
  1106. {
  1107. struct dvobj_priv *dvobj = pci_get_drvdata(pdev);
  1108. pci_set_drvdata(pdev, NULL);
  1109. if (dvobj) {
  1110. if (dvobj->irq_alloc) {
  1111. free_irq(pdev->irq, dvobj);
  1112. pci_disable_msi(pdev);
  1113. dvobj->irq_alloc = 0;
  1114. }
  1115. if (dvobj->pci_mem_start != 0) {
  1116. #if 1/* def RTK_DMP_PLATFORM */
  1117. pci_iounmap(pdev, (void *)dvobj->pci_mem_start);
  1118. #endif
  1119. dvobj->pci_mem_start = 0;
  1120. }
  1121. #ifdef RTK_129X_PLATFORM
  1122. if (dvobj->ctrl_start != 0) {
  1123. pci_iounmap(pdev, (void *)dvobj->ctrl_start);
  1124. dvobj->ctrl_start = 0;
  1125. }
  1126. #endif
  1127. devobj_deinit(dvobj);
  1128. }
  1129. pci_release_regions(pdev);
  1130. pci_disable_device(pdev);
  1131. }
  1132. u8 rtw_set_hal_ops(_adapter *padapter)
  1133. {
  1134. /* alloc memory for HAL DATA */
  1135. if (rtw_hal_data_init(padapter) == _FAIL)
  1136. return _FAIL;
  1137. #ifdef CONFIG_RTL8188E
  1138. if (rtw_get_chip_type(padapter) == RTL8188E)
  1139. rtl8188ee_set_hal_ops(padapter);
  1140. #endif
  1141. #if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A)
  1142. if ((rtw_get_chip_type(padapter) == RTL8812) || (rtw_get_chip_type(padapter) == RTL8821))
  1143. rtl8812ae_set_hal_ops(padapter);
  1144. #endif
  1145. #ifdef CONFIG_RTL8723B
  1146. if (rtw_get_chip_type(padapter) == RTL8723B)
  1147. rtl8723be_set_hal_ops(padapter);
  1148. #endif
  1149. #ifdef CONFIG_RTL8723D
  1150. if (rtw_get_chip_type(padapter) == RTL8723D)
  1151. rtl8723de_set_hal_ops(padapter);
  1152. #endif
  1153. #ifdef CONFIG_RTL8192E
  1154. if (rtw_get_chip_type(padapter) == RTL8192E)
  1155. rtl8192ee_set_hal_ops(padapter);
  1156. #endif
  1157. #ifdef CONFIG_RTL8192F
  1158. if (rtw_get_chip_type(padapter) == RTL8192F)
  1159. rtl8192fe_set_hal_ops(padapter);
  1160. #endif
  1161. #ifdef CONFIG_RTL8814A
  1162. if (rtw_get_chip_type(padapter) == RTL8814A)
  1163. rtl8814ae_set_hal_ops(padapter);
  1164. #endif
  1165. #if defined(CONFIG_RTL8822B)
  1166. if (rtw_get_chip_type(padapter) == RTL8822B)
  1167. rtl8822be_set_hal_ops(padapter);
  1168. #endif
  1169. #if defined(CONFIG_RTL8821C)
  1170. if (rtw_get_chip_type(padapter) == RTL8821C)
  1171. rtl8821ce_set_hal_ops(padapter);
  1172. #endif
  1173. if (rtw_hal_ops_check(padapter) == _FAIL)
  1174. return _FAIL;
  1175. if (hal_spec_init(padapter) == _FAIL)
  1176. return _FAIL;
  1177. return _SUCCESS;
  1178. }
  1179. void pci_set_intf_ops(_adapter *padapter, struct _io_ops *pops)
  1180. {
  1181. #ifdef CONFIG_RTL8188E
  1182. if (rtw_get_chip_type(padapter) == RTL8188E)
  1183. rtl8188ee_set_intf_ops(pops);
  1184. #endif
  1185. #if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A)
  1186. if ((rtw_get_chip_type(padapter) == RTL8812) || (rtw_get_chip_type(padapter) == RTL8821))
  1187. rtl8812ae_set_intf_ops(pops);
  1188. #endif
  1189. #ifdef CONFIG_RTL8723B
  1190. if (rtw_get_chip_type(padapter) == RTL8723B)
  1191. rtl8723be_set_intf_ops(pops);
  1192. #endif
  1193. #ifdef CONFIG_RTL8723D
  1194. if (rtw_get_chip_type(padapter) == RTL8723D)
  1195. rtl8723de_set_intf_ops(pops);
  1196. #endif
  1197. #ifdef CONFIG_RTL8192E
  1198. if (rtw_get_chip_type(padapter) == RTL8192E)
  1199. rtl8192ee_set_intf_ops(pops);
  1200. #endif
  1201. #ifdef CONFIG_RTL8192F
  1202. if (rtw_get_chip_type(padapter) == RTL8192F)
  1203. rtl8192fe_set_intf_ops(pops);
  1204. #endif
  1205. #ifdef CONFIG_RTL8814A
  1206. if (rtw_get_chip_type(padapter) == RTL8814A)
  1207. rtl8814ae_set_intf_ops(pops);
  1208. #endif
  1209. #if defined(CONFIG_RTL8822B)
  1210. if (rtw_get_chip_type(padapter) == RTL8822B)
  1211. rtl8822be_set_intf_ops(pops);
  1212. #endif
  1213. #if defined(CONFIG_RTL8821C)
  1214. if (rtw_get_chip_type(padapter) == RTL8821C)
  1215. rtl8821ce_set_intf_ops(pops);
  1216. #endif
  1217. }
  1218. static void pci_intf_start(_adapter *padapter)
  1219. {
  1220. RTW_INFO("+pci_intf_start\n");
  1221. /* Enable hw interrupt */
  1222. rtw_hal_enable_interrupt(padapter);
  1223. RTW_INFO("-pci_intf_start\n");
  1224. }
  1225. static void rtw_mi_pci_tasklets_kill(_adapter *padapter)
  1226. {
  1227. int i;
  1228. _adapter *iface;
  1229. struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
  1230. for (i = 0; i < dvobj->iface_nums; i++) {
  1231. iface = dvobj->padapters[i];
  1232. if ((iface) && rtw_is_adapter_up(iface)) {
  1233. tasklet_kill(&(padapter->recvpriv.recv_tasklet));
  1234. tasklet_kill(&(padapter->recvpriv.irq_prepare_beacon_tasklet));
  1235. tasklet_kill(&(padapter->xmitpriv.xmit_tasklet));
  1236. }
  1237. }
  1238. }
  1239. static void pci_intf_stop(_adapter *padapter)
  1240. {
  1241. /* Disable hw interrupt */
  1242. if (!rtw_is_surprise_removed(padapter)) {
  1243. /* device still exists, so driver can do i/o operation */
  1244. rtw_hal_disable_interrupt(padapter);
  1245. rtw_mi_pci_tasklets_kill(padapter);
  1246. rtw_hal_set_hwreg(padapter, HW_VAR_PCIE_STOP_TX_DMA, 0);
  1247. rtw_hal_irp_reset(padapter);
  1248. } else {
  1249. /* Clear irq_enabled to prevent handle interrupt function. */
  1250. adapter_to_dvobj(padapter)->irq_enabled = 0;
  1251. }
  1252. }
  1253. static void disable_ht_for_spec_devid(const struct pci_device_id *pdid)
  1254. {
  1255. #ifdef CONFIG_80211N_HT
  1256. u16 vid, pid;
  1257. u32 flags;
  1258. int i;
  1259. int num = sizeof(specific_device_id_tbl) / sizeof(struct specific_device_id);
  1260. for (i = 0; i < num; i++) {
  1261. vid = specific_device_id_tbl[i].idVendor;
  1262. pid = specific_device_id_tbl[i].idProduct;
  1263. flags = specific_device_id_tbl[i].flags;
  1264. if ((pdid->vendor == vid) && (pdid->device == pid) && (flags & SPEC_DEV_ID_DISABLE_HT)) {
  1265. rtw_ht_enable = 0;
  1266. rtw_bw_mode = 0;
  1267. rtw_ampdu_enable = 0;
  1268. }
  1269. }
  1270. #endif
  1271. }
  1272. #ifdef CONFIG_PM
  1273. static int rtw_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1274. {
  1275. int ret = 0;
  1276. struct dvobj_priv *dvobj = pci_get_drvdata(pdev);
  1277. _adapter *padapter = dvobj_get_primary_adapter(dvobj);
  1278. ret = rtw_suspend_common(padapter);
  1279. ret = pci_save_state(pdev);
  1280. if (ret != 0) {
  1281. RTW_INFO("%s Failed on pci_save_state (%d)\n", __func__, ret);
  1282. goto exit;
  1283. }
  1284. #ifdef CONFIG_WOWLAN
  1285. device_set_wakeup_enable(&pdev->dev, true);
  1286. #endif
  1287. pci_disable_device(pdev);
  1288. #ifdef CONFIG_WOWLAN
  1289. ret = pci_enable_wake(pdev, pci_choose_state(pdev, state), true);
  1290. if (ret != 0)
  1291. RTW_INFO("%s Failed on pci_enable_wake (%d)\n", __func__, ret);
  1292. #endif
  1293. ret = pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1294. if (ret != 0)
  1295. RTW_INFO("%s Failed on pci_set_power_state (%d)\n", __func__, ret);
  1296. exit:
  1297. return ret;
  1298. }
  1299. int rtw_resume_process(_adapter *padapter)
  1300. {
  1301. return rtw_resume_common(padapter);
  1302. }
  1303. static int rtw_pci_resume(struct pci_dev *pdev)
  1304. {
  1305. struct dvobj_priv *dvobj = pci_get_drvdata(pdev);
  1306. _adapter *padapter = dvobj_get_primary_adapter(dvobj);
  1307. struct net_device *pnetdev = padapter->pnetdev;
  1308. struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
  1309. int err = 0;
  1310. err = pci_set_power_state(pdev, PCI_D0);
  1311. if (err != 0) {
  1312. RTW_INFO("%s Failed on pci_set_power_state (%d)\n", __func__, err);
  1313. goto exit;
  1314. }
  1315. err = pci_enable_device(pdev);
  1316. if (err != 0) {
  1317. RTW_INFO("%s Failed on pci_enable_device (%d)\n", __func__, err);
  1318. goto exit;
  1319. }
  1320. #ifdef CONFIG_WOWLAN
  1321. err = pci_enable_wake(pdev, PCI_D0, 0);
  1322. if (err != 0) {
  1323. RTW_INFO("%s Failed on pci_enable_wake (%d)\n", __func__, err);
  1324. goto exit;
  1325. }
  1326. #endif
  1327. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 37))
  1328. pci_restore_state(pdev);
  1329. #else
  1330. err = pci_restore_state(pdev);
  1331. if (err != 0) {
  1332. RTW_INFO("%s Failed on pci_restore_state (%d)\n", __func__, err);
  1333. goto exit;
  1334. }
  1335. #endif
  1336. #ifdef CONFIG_WOWLAN
  1337. device_set_wakeup_enable(&pdev->dev, false);
  1338. #endif
  1339. if (pwrpriv->wowlan_mode || pwrpriv->wowlan_ap_mode) {
  1340. rtw_resume_lock_suspend();
  1341. err = rtw_resume_process(padapter);
  1342. rtw_resume_unlock_suspend();
  1343. } else {
  1344. #ifdef CONFIG_RESUME_IN_WORKQUEUE
  1345. rtw_resume_in_workqueue(pwrpriv);
  1346. #else
  1347. if (rtw_is_earlysuspend_registered(pwrpriv)) {
  1348. /* jeff: bypass resume here, do in late_resume */
  1349. rtw_set_do_late_resume(pwrpriv, _TRUE);
  1350. } else {
  1351. rtw_resume_lock_suspend();
  1352. err = rtw_resume_process(padapter);
  1353. rtw_resume_unlock_suspend();
  1354. }
  1355. #endif
  1356. }
  1357. exit:
  1358. return err;
  1359. }
  1360. #endif/* CONFIG_PM */
  1361. _adapter *rtw_pci_primary_adapter_init(struct dvobj_priv *dvobj, struct pci_dev *pdev)
  1362. {
  1363. _adapter *padapter = NULL;
  1364. int status = _FAIL;
  1365. padapter = (_adapter *)rtw_zvmalloc(sizeof(*padapter));
  1366. if (padapter == NULL)
  1367. goto exit;
  1368. if (loadparam(padapter) != _SUCCESS)
  1369. goto free_adapter;
  1370. padapter->dvobj = dvobj;
  1371. rtw_set_drv_stopped(padapter);/*init*/
  1372. dvobj->padapters[dvobj->iface_nums++] = padapter;
  1373. padapter->iface_id = IFACE_ID0;
  1374. /* set adapter_type/iface type for primary padapter */
  1375. padapter->isprimary = _TRUE;
  1376. padapter->adapter_type = PRIMARY_ADAPTER;
  1377. #ifdef CONFIG_MI_WITH_MBSSID_CAM
  1378. padapter->hw_port = HW_PORT0;
  1379. #else
  1380. padapter->hw_port = HW_PORT0;
  1381. #endif
  1382. if (rtw_init_io_priv(padapter, pci_set_intf_ops) == _FAIL)
  1383. goto free_adapter;
  1384. /* step 2. hook HalFunc, allocate HalData */
  1385. /* hal_set_hal_ops(padapter); */
  1386. if (rtw_set_hal_ops(padapter) == _FAIL)
  1387. goto free_hal_data;
  1388. /* step 3. */
  1389. padapter->intf_start = &pci_intf_start;
  1390. padapter->intf_stop = &pci_intf_stop;
  1391. /* .3 */
  1392. rtw_hal_read_chip_version(padapter);
  1393. /* .4 */
  1394. rtw_hal_chip_configure(padapter);
  1395. #ifdef CONFIG_BT_COEXIST
  1396. rtw_btcoex_Initialize(padapter);
  1397. #endif
  1398. rtw_btcoex_wifionly_initialize(padapter);
  1399. /* step 4. read efuse/eeprom data and get mac_addr */
  1400. if (rtw_hal_read_chip_info(padapter) == _FAIL)
  1401. goto free_hal_data;
  1402. /* step 5. */
  1403. if (rtw_init_drv_sw(padapter) == _FAIL)
  1404. goto free_hal_data;
  1405. if (rtw_hal_inirp_init(padapter) == _FAIL)
  1406. goto free_hal_data;
  1407. rtw_macaddr_cfg(adapter_mac_addr(padapter), get_hal_mac_addr(padapter));
  1408. #ifdef CONFIG_MI_WITH_MBSSID_CAM
  1409. rtw_mbid_camid_alloc(padapter, adapter_mac_addr(padapter));
  1410. #endif
  1411. #ifdef CONFIG_P2P
  1412. rtw_init_wifidirect_addrs(padapter, adapter_mac_addr(padapter), adapter_mac_addr(padapter));
  1413. #endif /* CONFIG_P2P */
  1414. rtw_hal_disable_interrupt(padapter);
  1415. /* step 6. Init pci related configuration */
  1416. rtw_pci_initialize_adapter_common(padapter);
  1417. RTW_INFO("bDriverStopped:%s, bSurpriseRemoved:%s, bup:%d, hw_init_completed:%s\n"
  1418. , rtw_is_drv_stopped(padapter) ? "True" : "False"
  1419. , rtw_is_surprise_removed(padapter) ? "True" : "False"
  1420. , padapter->bup
  1421. , rtw_is_hw_init_completed(padapter) ? "True" : "False"
  1422. );
  1423. status = _SUCCESS;
  1424. free_hal_data:
  1425. if (status != _SUCCESS && padapter->HalData)
  1426. rtw_hal_free_data(padapter);
  1427. free_adapter:
  1428. if (status != _SUCCESS && padapter) {
  1429. #ifdef RTW_HALMAC
  1430. rtw_halmac_deinit_adapter(dvobj);
  1431. #endif
  1432. rtw_vmfree((u8 *)padapter, sizeof(*padapter));
  1433. padapter = NULL;
  1434. }
  1435. exit:
  1436. return padapter;
  1437. }
  1438. static void rtw_pci_primary_adapter_deinit(_adapter *padapter)
  1439. {
  1440. struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
  1441. /* padapter->intf_stop(padapter); */
  1442. if (check_fwstate(pmlmepriv, _FW_LINKED))
  1443. rtw_disassoc_cmd(padapter, 0, RTW_CMDF_DIRECTLY);
  1444. #ifdef CONFIG_AP_MODE
  1445. if (MLME_IS_AP(padapter) || MLME_IS_MESH(padapter)) {
  1446. free_mlme_ap_info(padapter);
  1447. #ifdef CONFIG_HOSTAPD_MLME
  1448. hostapd_mode_unload(padapter);
  1449. #endif
  1450. }
  1451. #endif
  1452. /*rtw_cancel_all_timer(padapte);*/
  1453. #ifdef CONFIG_WOWLAN
  1454. adapter_to_pwrctl(padapter)->wowlan_mode = _FALSE;
  1455. #endif /* CONFIG_WOWLAN */
  1456. rtw_dev_unload(padapter);
  1457. RTW_INFO("%s, hw_init_completed=%s\n", __func__, rtw_is_hw_init_completed(padapter) ? "_TRUE" : "_FALSE");
  1458. rtw_hal_inirp_deinit(padapter);
  1459. rtw_free_drv_sw(padapter);
  1460. /* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */
  1461. rtw_os_ndev_free(padapter);
  1462. #ifdef RTW_HALMAC
  1463. rtw_halmac_deinit_adapter(adapter_to_dvobj(padapter));
  1464. #endif /* RTW_HALMAC */
  1465. rtw_vmfree((u8 *)padapter, sizeof(_adapter));
  1466. #ifdef CONFIG_PLATFORM_RTD2880B
  1467. RTW_INFO("wlan link down\n");
  1468. rtd2885_wlan_netlink_sendMsg("linkdown", "8712");
  1469. #endif
  1470. }
  1471. /*
  1472. * drv_init() - a device potentially for us
  1473. *
  1474. * notes: drv_init() is called when the bus driver has located a card for us to support.
  1475. * We accept the new device by returning 0.
  1476. */
  1477. static int rtw_drv_init(struct pci_dev *pdev, const struct pci_device_id *pdid)
  1478. {
  1479. int i, err = -ENODEV;
  1480. int status = _FAIL;
  1481. _adapter *padapter = NULL;
  1482. struct dvobj_priv *dvobj;
  1483. struct net_device *pnetdev;
  1484. /* RTW_INFO("+rtw_drv_init\n"); */
  1485. /* step 0. */
  1486. disable_ht_for_spec_devid(pdid);
  1487. /* Initialize dvobj_priv */
  1488. dvobj = pci_dvobj_init(pdev, pdid);
  1489. if (dvobj == NULL)
  1490. goto exit;
  1491. /* Initialize primary adapter */
  1492. padapter = rtw_pci_primary_adapter_init(dvobj, pdev);
  1493. if (padapter == NULL) {
  1494. RTW_INFO("rtw_pci_primary_adapter_init Failed!\n");
  1495. goto free_dvobj;
  1496. }
  1497. /* Initialize virtual interface */
  1498. #ifdef CONFIG_CONCURRENT_MODE
  1499. if (padapter->registrypriv.virtual_iface_num > (CONFIG_IFACE_NUMBER - 1))
  1500. padapter->registrypriv.virtual_iface_num = (CONFIG_IFACE_NUMBER - 1);
  1501. for (i = 0; i < padapter->registrypriv.virtual_iface_num; i++) {
  1502. if (rtw_drv_add_vir_if(padapter, pci_set_intf_ops) == NULL) {
  1503. RTW_INFO("rtw_drv_add_iface failed! (%d)\n", i);
  1504. goto free_if_vir;
  1505. }
  1506. }
  1507. #endif
  1508. #ifdef CONFIG_GLOBAL_UI_PID
  1509. if (ui_pid[1] != 0) {
  1510. RTW_INFO("ui_pid[1]:%d\n", ui_pid[1]);
  1511. rtw_signal_process(ui_pid[1], SIGUSR2);
  1512. }
  1513. #endif
  1514. /* dev_alloc_name && register_netdev */
  1515. if (rtw_os_ndevs_init(dvobj) != _SUCCESS)
  1516. goto free_if_vir;
  1517. #ifdef CONFIG_HOSTAPD_MLME
  1518. hostapd_mode_init(padapter);
  1519. #endif
  1520. #ifdef CONFIG_PLATFORM_RTD2880B
  1521. RTW_INFO("wlan link up\n");
  1522. rtd2885_wlan_netlink_sendMsg("linkup", "8712");
  1523. #endif
  1524. /* alloc irq */
  1525. if (pci_alloc_irq(dvobj) != _SUCCESS)
  1526. goto os_ndevs_deinit;
  1527. /* RTW_INFO("-871x_drv - drv_init, success!\n"); */
  1528. status = _SUCCESS;
  1529. os_ndevs_deinit:
  1530. if (status != _SUCCESS)
  1531. rtw_os_ndevs_deinit(dvobj);
  1532. free_if_vir:
  1533. if (status != _SUCCESS) {
  1534. #ifdef CONFIG_CONCURRENT_MODE
  1535. rtw_drv_stop_vir_ifaces(dvobj);
  1536. rtw_drv_free_vir_ifaces(dvobj);
  1537. #endif
  1538. }
  1539. if (status != _SUCCESS && padapter)
  1540. rtw_pci_primary_adapter_deinit(padapter);
  1541. free_dvobj:
  1542. if (status != _SUCCESS)
  1543. pci_dvobj_deinit(pdev);
  1544. exit:
  1545. return status == _SUCCESS ? 0 : -ENODEV;
  1546. }
  1547. /*
  1548. * dev_remove() - our device is being removed
  1549. */
  1550. /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove() => how to recognize both */
  1551. static void rtw_dev_remove(struct pci_dev *pdev)
  1552. {
  1553. struct dvobj_priv *pdvobjpriv = pci_get_drvdata(pdev);
  1554. _adapter *padapter = dvobj_get_primary_adapter(pdvobjpriv);
  1555. struct net_device *pnetdev = padapter->pnetdev;
  1556. if (pdvobjpriv->processing_dev_remove == _TRUE) {
  1557. RTW_WARN("%s-line%d: Warning! device has been removed!\n", __func__, __LINE__);
  1558. return;
  1559. }
  1560. RTW_INFO("+rtw_dev_remove\n");
  1561. pdvobjpriv->processing_dev_remove = _TRUE;
  1562. if (unlikely(!padapter))
  1563. return;
  1564. /* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */
  1565. rtw_os_ndevs_unregister(pdvobjpriv);
  1566. #if 0
  1567. #ifdef RTK_DMP_PLATFORM
  1568. rtw_clr_surprise_removed(padapter); /* always trate as device exists*/
  1569. /* this will let the driver to disable it's interrupt */
  1570. #else
  1571. if (pci_drvpriv.drv_registered == _TRUE) {
  1572. /* RTW_INFO("r871xu_dev_remove():padapter->bSurpriseRemoved == _TRUE\n"); */
  1573. rtw_set_surprise_removed(padapter);
  1574. }
  1575. /*else
  1576. {
  1577. rtw_set_hw_init_completed(padapter, _FALSE);
  1578. }*/
  1579. #endif
  1580. #endif
  1581. #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
  1582. rtw_unregister_early_suspend(dvobj_to_pwrctl(pdvobjpriv));
  1583. #endif
  1584. if (GET_HAL_DATA(padapter)->bFWReady == _TRUE) {
  1585. rtw_pm_set_ips(padapter, IPS_NONE);
  1586. rtw_pm_set_lps(padapter, PS_MODE_ACTIVE);
  1587. LeaveAllPowerSaveMode(padapter);
  1588. }
  1589. rtw_set_drv_stopped(padapter); /*for stop thread*/
  1590. rtw_stop_cmd_thread(padapter);
  1591. #ifdef CONFIG_CONCURRENT_MODE
  1592. rtw_drv_stop_vir_ifaces(pdvobjpriv);
  1593. #endif
  1594. #ifdef CONFIG_BT_COEXIST
  1595. #ifdef CONFIG_BT_COEXIST_SOCKET_TRX
  1596. if (GET_HAL_DATA(padapter)->EEPROMBluetoothCoexist)
  1597. rtw_btcoex_close_socket(padapter);
  1598. #endif
  1599. rtw_btcoex_HaltNotify(padapter);
  1600. #endif
  1601. rtw_pci_primary_adapter_deinit(padapter);
  1602. #ifdef CONFIG_CONCURRENT_MODE
  1603. rtw_drv_free_vir_ifaces(pdvobjpriv);
  1604. #endif
  1605. pci_dvobj_deinit(pdev);
  1606. RTW_INFO("-r871xu_dev_remove, done\n");
  1607. return;
  1608. }
  1609. static void rtw_dev_shutdown(struct pci_dev *pdev)
  1610. {
  1611. struct dvobj_priv *pdvobjpriv = pci_get_drvdata(pdev);
  1612. _adapter *padapter = dvobj_get_primary_adapter(pdvobjpriv);
  1613. struct net_device *pnetdev = padapter->pnetdev;
  1614. #ifdef CONFIG_RTL8723D
  1615. if (IS_HARDWARE_TYPE_8723DE(padapter)) {
  1616. u1Byte u1Tmp;
  1617. u1Tmp = PlatformEFIORead1Byte(padapter, 0x75 /*REG_HCI_OPT_CTRL_8723D+1*/);
  1618. PlatformEFIOWrite1Byte(padapter, 0x75 /*REG_HCI_OPT_CTRL_8723D+1*/, (u1Tmp|BIT0));/*Disable USB Suspend Signal*/
  1619. }
  1620. #endif
  1621. rtw_dev_remove(pdev);
  1622. }
  1623. static int __init rtw_drv_entry(void)
  1624. {
  1625. int ret = 0;
  1626. RTW_PRINT("module init start\n");
  1627. dump_drv_version(RTW_DBGDUMP);
  1628. #ifdef BTCOEXVERSION
  1629. RTW_PRINT(DRV_NAME" BT-Coex version = %s\n", BTCOEXVERSION);
  1630. #endif /* BTCOEXVERSION */
  1631. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24))
  1632. /* console_suspend_enabled=0; */
  1633. #endif
  1634. pci_drvpriv.drv_registered = _TRUE;
  1635. rtw_suspend_lock_init();
  1636. rtw_drv_proc_init();
  1637. rtw_ndev_notifier_register();
  1638. rtw_inetaddr_notifier_register();
  1639. ret = pci_register_driver(&pci_drvpriv.rtw_pci_drv);
  1640. if (ret != 0) {
  1641. pci_drvpriv.drv_registered = _FALSE;
  1642. rtw_suspend_lock_uninit();
  1643. rtw_drv_proc_deinit();
  1644. rtw_ndev_notifier_unregister();
  1645. rtw_inetaddr_notifier_unregister();
  1646. goto exit;
  1647. }
  1648. exit:
  1649. RTW_PRINT("module init ret=%d\n", ret);
  1650. return ret;
  1651. }
  1652. static void __exit rtw_drv_halt(void)
  1653. {
  1654. RTW_PRINT("module exit start\n");
  1655. pci_drvpriv.drv_registered = _FALSE;
  1656. pci_unregister_driver(&pci_drvpriv.rtw_pci_drv);
  1657. rtw_suspend_lock_uninit();
  1658. rtw_drv_proc_deinit();
  1659. rtw_ndev_notifier_unregister();
  1660. rtw_inetaddr_notifier_unregister();
  1661. RTW_PRINT("module exit success\n");
  1662. rtw_mstat_dump(RTW_DBGDUMP);
  1663. }
  1664. module_init(rtw_drv_entry);
  1665. module_exit(rtw_drv_halt);