pci_intf.c 54 KB

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