pci.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /* Copyright(c) 2020 Realtek Corporation
  3. */
  4. #include <linux/pci.h>
  5. #include "mac.h"
  6. #include "pci.h"
  7. #include "reg.h"
  8. #include "ser.h"
  9. static bool rtw89_pci_disable_clkreq;
  10. static bool rtw89_pci_disable_aspm_l1;
  11. static bool rtw89_pci_disable_l1ss;
  12. module_param_named(disable_clkreq, rtw89_pci_disable_clkreq, bool, 0644);
  13. module_param_named(disable_aspm_l1, rtw89_pci_disable_aspm_l1, bool, 0644);
  14. module_param_named(disable_aspm_l1ss, rtw89_pci_disable_l1ss, bool, 0644);
  15. MODULE_PARM_DESC(disable_clkreq, "Set Y to disable PCI clkreq support");
  16. MODULE_PARM_DESC(disable_aspm_l1, "Set Y to disable PCI ASPM L1 support");
  17. MODULE_PARM_DESC(disable_aspm_l1ss, "Set Y to disable PCI L1SS support");
  18. static int rtw89_pci_rst_bdram_pcie(struct rtw89_dev *rtwdev)
  19. {
  20. u32 val;
  21. int ret;
  22. rtw89_write32(rtwdev, R_AX_PCIE_INIT_CFG1,
  23. rtw89_read32(rtwdev, R_AX_PCIE_INIT_CFG1) | B_AX_RST_BDRAM);
  24. ret = read_poll_timeout_atomic(rtw89_read32, val, !(val & B_AX_RST_BDRAM),
  25. 1, RTW89_PCI_POLL_BDRAM_RST_CNT, false,
  26. rtwdev, R_AX_PCIE_INIT_CFG1);
  27. if (ret)
  28. return -EBUSY;
  29. return 0;
  30. }
  31. static u32 rtw89_pci_dma_recalc(struct rtw89_dev *rtwdev,
  32. struct rtw89_pci_dma_ring *bd_ring,
  33. u32 cur_idx, bool tx)
  34. {
  35. u32 cnt, cur_rp, wp, rp, len;
  36. rp = bd_ring->rp;
  37. wp = bd_ring->wp;
  38. len = bd_ring->len;
  39. cur_rp = FIELD_GET(TXBD_HW_IDX_MASK, cur_idx);
  40. if (tx)
  41. cnt = cur_rp >= rp ? cur_rp - rp : len - (rp - cur_rp);
  42. else
  43. cnt = cur_rp >= wp ? cur_rp - wp : len - (wp - cur_rp);
  44. bd_ring->rp = cur_rp;
  45. return cnt;
  46. }
  47. static u32 rtw89_pci_txbd_recalc(struct rtw89_dev *rtwdev,
  48. struct rtw89_pci_tx_ring *tx_ring)
  49. {
  50. struct rtw89_pci_dma_ring *bd_ring = &tx_ring->bd_ring;
  51. u32 addr_idx = bd_ring->addr.idx;
  52. u32 cnt, idx;
  53. idx = rtw89_read32(rtwdev, addr_idx);
  54. cnt = rtw89_pci_dma_recalc(rtwdev, bd_ring, idx, true);
  55. return cnt;
  56. }
  57. static void rtw89_pci_release_fwcmd(struct rtw89_dev *rtwdev,
  58. struct rtw89_pci *rtwpci,
  59. u32 cnt, bool release_all)
  60. {
  61. struct rtw89_pci_tx_data *tx_data;
  62. struct sk_buff *skb;
  63. u32 qlen;
  64. while (cnt--) {
  65. skb = skb_dequeue(&rtwpci->h2c_queue);
  66. if (!skb) {
  67. rtw89_err(rtwdev, "failed to pre-release fwcmd\n");
  68. return;
  69. }
  70. skb_queue_tail(&rtwpci->h2c_release_queue, skb);
  71. }
  72. qlen = skb_queue_len(&rtwpci->h2c_release_queue);
  73. if (!release_all)
  74. qlen = qlen > RTW89_PCI_MULTITAG ? qlen - RTW89_PCI_MULTITAG : 0;
  75. while (qlen--) {
  76. skb = skb_dequeue(&rtwpci->h2c_release_queue);
  77. if (!skb) {
  78. rtw89_err(rtwdev, "failed to release fwcmd\n");
  79. return;
  80. }
  81. tx_data = RTW89_PCI_TX_SKB_CB(skb);
  82. dma_unmap_single(&rtwpci->pdev->dev, tx_data->dma, skb->len,
  83. DMA_TO_DEVICE);
  84. dev_kfree_skb_any(skb);
  85. }
  86. }
  87. static void rtw89_pci_reclaim_tx_fwcmd(struct rtw89_dev *rtwdev,
  88. struct rtw89_pci *rtwpci)
  89. {
  90. struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[RTW89_TXCH_CH12];
  91. u32 cnt;
  92. cnt = rtw89_pci_txbd_recalc(rtwdev, tx_ring);
  93. if (!cnt)
  94. return;
  95. rtw89_pci_release_fwcmd(rtwdev, rtwpci, cnt, false);
  96. }
  97. static u32 rtw89_pci_rxbd_recalc(struct rtw89_dev *rtwdev,
  98. struct rtw89_pci_rx_ring *rx_ring)
  99. {
  100. struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
  101. u32 addr_idx = bd_ring->addr.idx;
  102. u32 cnt, idx;
  103. idx = rtw89_read32(rtwdev, addr_idx);
  104. cnt = rtw89_pci_dma_recalc(rtwdev, bd_ring, idx, false);
  105. return cnt;
  106. }
  107. static void rtw89_pci_sync_skb_for_cpu(struct rtw89_dev *rtwdev,
  108. struct sk_buff *skb)
  109. {
  110. struct rtw89_pci_rx_info *rx_info;
  111. dma_addr_t dma;
  112. rx_info = RTW89_PCI_RX_SKB_CB(skb);
  113. dma = rx_info->dma;
  114. dma_sync_single_for_cpu(rtwdev->dev, dma, RTW89_PCI_RX_BUF_SIZE,
  115. DMA_FROM_DEVICE);
  116. }
  117. static void rtw89_pci_sync_skb_for_device(struct rtw89_dev *rtwdev,
  118. struct sk_buff *skb)
  119. {
  120. struct rtw89_pci_rx_info *rx_info;
  121. dma_addr_t dma;
  122. rx_info = RTW89_PCI_RX_SKB_CB(skb);
  123. dma = rx_info->dma;
  124. dma_sync_single_for_device(rtwdev->dev, dma, RTW89_PCI_RX_BUF_SIZE,
  125. DMA_FROM_DEVICE);
  126. }
  127. static int rtw89_pci_rxbd_info_update(struct rtw89_dev *rtwdev,
  128. struct sk_buff *skb)
  129. {
  130. struct rtw89_pci_rxbd_info *rxbd_info;
  131. struct rtw89_pci_rx_info *rx_info = RTW89_PCI_RX_SKB_CB(skb);
  132. rxbd_info = (struct rtw89_pci_rxbd_info *)skb->data;
  133. rx_info->fs = le32_get_bits(rxbd_info->dword, RTW89_PCI_RXBD_FS);
  134. rx_info->ls = le32_get_bits(rxbd_info->dword, RTW89_PCI_RXBD_LS);
  135. rx_info->len = le32_get_bits(rxbd_info->dword, RTW89_PCI_RXBD_WRITE_SIZE);
  136. rx_info->tag = le32_get_bits(rxbd_info->dword, RTW89_PCI_RXBD_TAG);
  137. return 0;
  138. }
  139. static void rtw89_pci_ctrl_txdma_ch_pcie(struct rtw89_dev *rtwdev, bool enable)
  140. {
  141. const struct rtw89_pci_info *info = rtwdev->pci_info;
  142. const struct rtw89_reg_def *dma_stop1 = &info->dma_stop1;
  143. const struct rtw89_reg_def *dma_stop2 = &info->dma_stop2;
  144. if (enable) {
  145. rtw89_write32_clr(rtwdev, dma_stop1->addr, dma_stop1->mask);
  146. if (dma_stop2->addr)
  147. rtw89_write32_clr(rtwdev, dma_stop2->addr, dma_stop2->mask);
  148. } else {
  149. rtw89_write32_set(rtwdev, dma_stop1->addr, dma_stop1->mask);
  150. if (dma_stop2->addr)
  151. rtw89_write32_set(rtwdev, dma_stop2->addr, dma_stop2->mask);
  152. }
  153. }
  154. static void rtw89_pci_ctrl_txdma_fw_ch_pcie(struct rtw89_dev *rtwdev, bool enable)
  155. {
  156. const struct rtw89_pci_info *info = rtwdev->pci_info;
  157. const struct rtw89_reg_def *dma_stop1 = &info->dma_stop1;
  158. if (enable)
  159. rtw89_write32_clr(rtwdev, dma_stop1->addr, B_AX_STOP_CH12);
  160. else
  161. rtw89_write32_set(rtwdev, dma_stop1->addr, B_AX_STOP_CH12);
  162. }
  163. static bool
  164. rtw89_skb_put_rx_data(struct rtw89_dev *rtwdev, bool fs, bool ls,
  165. struct sk_buff *new,
  166. const struct sk_buff *skb, u32 offset,
  167. const struct rtw89_pci_rx_info *rx_info,
  168. const struct rtw89_rx_desc_info *desc_info)
  169. {
  170. u32 copy_len = rx_info->len - offset;
  171. if (unlikely(skb_tailroom(new) < copy_len)) {
  172. rtw89_debug(rtwdev, RTW89_DBG_TXRX,
  173. "invalid rx data length bd_len=%d desc_len=%d offset=%d (fs=%d ls=%d)\n",
  174. rx_info->len, desc_info->pkt_size, offset, fs, ls);
  175. rtw89_hex_dump(rtwdev, RTW89_DBG_TXRX, "rx_data: ",
  176. skb->data, rx_info->len);
  177. /* length of a single segment skb is desc_info->pkt_size */
  178. if (fs && ls) {
  179. copy_len = desc_info->pkt_size;
  180. } else {
  181. rtw89_info(rtwdev, "drop rx data due to invalid length\n");
  182. return false;
  183. }
  184. }
  185. skb_put_data(new, skb->data + offset, copy_len);
  186. return true;
  187. }
  188. static u32 rtw89_pci_rxbd_deliver_skbs(struct rtw89_dev *rtwdev,
  189. struct rtw89_pci_rx_ring *rx_ring)
  190. {
  191. struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
  192. struct rtw89_pci_rx_info *rx_info;
  193. struct rtw89_rx_desc_info *desc_info = &rx_ring->diliver_desc;
  194. struct sk_buff *new = rx_ring->diliver_skb;
  195. struct sk_buff *skb;
  196. u32 rxinfo_size = sizeof(struct rtw89_pci_rxbd_info);
  197. u32 offset;
  198. u32 cnt = 1;
  199. bool fs, ls;
  200. int ret;
  201. skb = rx_ring->buf[bd_ring->wp];
  202. rtw89_pci_sync_skb_for_cpu(rtwdev, skb);
  203. ret = rtw89_pci_rxbd_info_update(rtwdev, skb);
  204. if (ret) {
  205. rtw89_err(rtwdev, "failed to update %d RXBD info: %d\n",
  206. bd_ring->wp, ret);
  207. goto err_sync_device;
  208. }
  209. rx_info = RTW89_PCI_RX_SKB_CB(skb);
  210. fs = rx_info->fs;
  211. ls = rx_info->ls;
  212. if (fs) {
  213. if (new) {
  214. rtw89_debug(rtwdev, RTW89_DBG_UNEXP,
  215. "skb should not be ready before first segment start\n");
  216. goto err_sync_device;
  217. }
  218. if (desc_info->ready) {
  219. rtw89_warn(rtwdev, "desc info should not be ready before first segment start\n");
  220. goto err_sync_device;
  221. }
  222. rtw89_chip_query_rxdesc(rtwdev, desc_info, skb->data, rxinfo_size);
  223. new = rtw89_alloc_skb_for_rx(rtwdev, desc_info->pkt_size);
  224. if (!new)
  225. goto err_sync_device;
  226. rx_ring->diliver_skb = new;
  227. /* first segment has RX desc */
  228. offset = desc_info->offset + desc_info->rxd_len;
  229. } else {
  230. offset = sizeof(struct rtw89_pci_rxbd_info);
  231. if (!new) {
  232. rtw89_debug(rtwdev, RTW89_DBG_UNEXP, "no last skb\n");
  233. goto err_sync_device;
  234. }
  235. }
  236. if (!rtw89_skb_put_rx_data(rtwdev, fs, ls, new, skb, offset, rx_info, desc_info))
  237. goto err_sync_device;
  238. rtw89_pci_sync_skb_for_device(rtwdev, skb);
  239. rtw89_pci_rxbd_increase(rx_ring, 1);
  240. if (!desc_info->ready) {
  241. rtw89_warn(rtwdev, "no rx desc information\n");
  242. goto err_free_resource;
  243. }
  244. if (ls) {
  245. rtw89_core_rx(rtwdev, desc_info, new);
  246. rx_ring->diliver_skb = NULL;
  247. desc_info->ready = false;
  248. }
  249. return cnt;
  250. err_sync_device:
  251. rtw89_pci_sync_skb_for_device(rtwdev, skb);
  252. rtw89_pci_rxbd_increase(rx_ring, 1);
  253. err_free_resource:
  254. if (new)
  255. dev_kfree_skb_any(new);
  256. rx_ring->diliver_skb = NULL;
  257. desc_info->ready = false;
  258. return cnt;
  259. }
  260. static void rtw89_pci_rxbd_deliver(struct rtw89_dev *rtwdev,
  261. struct rtw89_pci_rx_ring *rx_ring,
  262. u32 cnt)
  263. {
  264. struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
  265. u32 rx_cnt;
  266. while (cnt && rtwdev->napi_budget_countdown > 0) {
  267. rx_cnt = rtw89_pci_rxbd_deliver_skbs(rtwdev, rx_ring);
  268. if (!rx_cnt) {
  269. rtw89_err(rtwdev, "failed to deliver RXBD skb\n");
  270. /* skip the rest RXBD bufs */
  271. rtw89_pci_rxbd_increase(rx_ring, cnt);
  272. break;
  273. }
  274. cnt -= rx_cnt;
  275. }
  276. rtw89_write16(rtwdev, bd_ring->addr.idx, bd_ring->wp);
  277. }
  278. static int rtw89_pci_poll_rxq_dma(struct rtw89_dev *rtwdev,
  279. struct rtw89_pci *rtwpci, int budget)
  280. {
  281. struct rtw89_pci_rx_ring *rx_ring;
  282. int countdown = rtwdev->napi_budget_countdown;
  283. u32 cnt;
  284. rx_ring = &rtwpci->rx_rings[RTW89_RXCH_RXQ];
  285. cnt = rtw89_pci_rxbd_recalc(rtwdev, rx_ring);
  286. if (!cnt)
  287. return 0;
  288. cnt = min_t(u32, budget, cnt);
  289. rtw89_pci_rxbd_deliver(rtwdev, rx_ring, cnt);
  290. /* In case of flushing pending SKBs, the countdown may exceed. */
  291. if (rtwdev->napi_budget_countdown <= 0)
  292. return budget;
  293. return budget - countdown;
  294. }
  295. static void rtw89_pci_tx_status(struct rtw89_dev *rtwdev,
  296. struct rtw89_pci_tx_ring *tx_ring,
  297. struct sk_buff *skb, u8 tx_status)
  298. {
  299. struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
  300. struct ieee80211_tx_info *info;
  301. rtw89_core_tx_wait_complete(rtwdev, skb_data, tx_status == RTW89_TX_DONE);
  302. info = IEEE80211_SKB_CB(skb);
  303. ieee80211_tx_info_clear_status(info);
  304. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  305. info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
  306. if (tx_status == RTW89_TX_DONE) {
  307. info->flags |= IEEE80211_TX_STAT_ACK;
  308. tx_ring->tx_acked++;
  309. } else {
  310. if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  311. rtw89_debug(rtwdev, RTW89_DBG_FW,
  312. "failed to TX of status %x\n", tx_status);
  313. switch (tx_status) {
  314. case RTW89_TX_RETRY_LIMIT:
  315. tx_ring->tx_retry_lmt++;
  316. break;
  317. case RTW89_TX_LIFE_TIME:
  318. tx_ring->tx_life_time++;
  319. break;
  320. case RTW89_TX_MACID_DROP:
  321. tx_ring->tx_mac_id_drop++;
  322. break;
  323. default:
  324. rtw89_warn(rtwdev, "invalid TX status %x\n", tx_status);
  325. break;
  326. }
  327. }
  328. ieee80211_tx_status_ni(rtwdev->hw, skb);
  329. }
  330. static void rtw89_pci_reclaim_txbd(struct rtw89_dev *rtwdev, struct rtw89_pci_tx_ring *tx_ring)
  331. {
  332. struct rtw89_pci_tx_wd *txwd;
  333. u32 cnt;
  334. cnt = rtw89_pci_txbd_recalc(rtwdev, tx_ring);
  335. while (cnt--) {
  336. txwd = list_first_entry_or_null(&tx_ring->busy_pages, struct rtw89_pci_tx_wd, list);
  337. if (!txwd) {
  338. rtw89_warn(rtwdev, "No busy txwd pages available\n");
  339. break;
  340. }
  341. list_del_init(&txwd->list);
  342. /* this skb has been freed by RPP */
  343. if (skb_queue_len(&txwd->queue) == 0)
  344. rtw89_pci_enqueue_txwd(tx_ring, txwd);
  345. }
  346. }
  347. static void rtw89_pci_release_busy_txwd(struct rtw89_dev *rtwdev,
  348. struct rtw89_pci_tx_ring *tx_ring)
  349. {
  350. struct rtw89_pci_tx_wd_ring *wd_ring = &tx_ring->wd_ring;
  351. struct rtw89_pci_tx_wd *txwd;
  352. int i;
  353. for (i = 0; i < wd_ring->page_num; i++) {
  354. txwd = list_first_entry_or_null(&tx_ring->busy_pages, struct rtw89_pci_tx_wd, list);
  355. if (!txwd)
  356. break;
  357. list_del_init(&txwd->list);
  358. }
  359. }
  360. static void rtw89_pci_release_txwd_skb(struct rtw89_dev *rtwdev,
  361. struct rtw89_pci_tx_ring *tx_ring,
  362. struct rtw89_pci_tx_wd *txwd, u16 seq,
  363. u8 tx_status)
  364. {
  365. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  366. struct rtw89_pci_tx_data *tx_data;
  367. struct sk_buff *skb, *tmp;
  368. u8 txch = tx_ring->txch;
  369. if (!list_empty(&txwd->list)) {
  370. rtw89_pci_reclaim_txbd(rtwdev, tx_ring);
  371. /* In low power mode, RPP can receive before updating of TX BD.
  372. * In normal mode, it should not happen so give it a warning.
  373. */
  374. if (!rtwpci->low_power && !list_empty(&txwd->list))
  375. rtw89_warn(rtwdev, "queue %d txwd %d is not idle\n",
  376. txch, seq);
  377. }
  378. skb_queue_walk_safe(&txwd->queue, skb, tmp) {
  379. skb_unlink(skb, &txwd->queue);
  380. tx_data = RTW89_PCI_TX_SKB_CB(skb);
  381. dma_unmap_single(&rtwpci->pdev->dev, tx_data->dma, skb->len,
  382. DMA_TO_DEVICE);
  383. rtw89_pci_tx_status(rtwdev, tx_ring, skb, tx_status);
  384. }
  385. if (list_empty(&txwd->list))
  386. rtw89_pci_enqueue_txwd(tx_ring, txwd);
  387. }
  388. static void rtw89_pci_release_rpp(struct rtw89_dev *rtwdev,
  389. struct rtw89_pci_rpp_fmt *rpp)
  390. {
  391. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  392. struct rtw89_pci_tx_ring *tx_ring;
  393. struct rtw89_pci_tx_wd_ring *wd_ring;
  394. struct rtw89_pci_tx_wd *txwd;
  395. u16 seq;
  396. u8 qsel, tx_status, txch;
  397. seq = le32_get_bits(rpp->dword, RTW89_PCI_RPP_SEQ);
  398. qsel = le32_get_bits(rpp->dword, RTW89_PCI_RPP_QSEL);
  399. tx_status = le32_get_bits(rpp->dword, RTW89_PCI_RPP_TX_STATUS);
  400. txch = rtw89_core_get_ch_dma(rtwdev, qsel);
  401. if (txch == RTW89_TXCH_CH12) {
  402. rtw89_warn(rtwdev, "should no fwcmd release report\n");
  403. return;
  404. }
  405. tx_ring = &rtwpci->tx_rings[txch];
  406. wd_ring = &tx_ring->wd_ring;
  407. txwd = &wd_ring->pages[seq];
  408. rtw89_pci_release_txwd_skb(rtwdev, tx_ring, txwd, seq, tx_status);
  409. }
  410. static void rtw89_pci_release_pending_txwd_skb(struct rtw89_dev *rtwdev,
  411. struct rtw89_pci_tx_ring *tx_ring)
  412. {
  413. struct rtw89_pci_tx_wd_ring *wd_ring = &tx_ring->wd_ring;
  414. struct rtw89_pci_tx_wd *txwd;
  415. int i;
  416. for (i = 0; i < wd_ring->page_num; i++) {
  417. txwd = &wd_ring->pages[i];
  418. if (!list_empty(&txwd->list))
  419. continue;
  420. rtw89_pci_release_txwd_skb(rtwdev, tx_ring, txwd, i, RTW89_TX_MACID_DROP);
  421. }
  422. }
  423. static u32 rtw89_pci_release_tx_skbs(struct rtw89_dev *rtwdev,
  424. struct rtw89_pci_rx_ring *rx_ring,
  425. u32 max_cnt)
  426. {
  427. struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
  428. struct rtw89_pci_rx_info *rx_info;
  429. struct rtw89_pci_rpp_fmt *rpp;
  430. struct rtw89_rx_desc_info desc_info = {};
  431. struct sk_buff *skb;
  432. u32 cnt = 0;
  433. u32 rpp_size = sizeof(struct rtw89_pci_rpp_fmt);
  434. u32 rxinfo_size = sizeof(struct rtw89_pci_rxbd_info);
  435. u32 offset;
  436. int ret;
  437. skb = rx_ring->buf[bd_ring->wp];
  438. rtw89_pci_sync_skb_for_cpu(rtwdev, skb);
  439. ret = rtw89_pci_rxbd_info_update(rtwdev, skb);
  440. if (ret) {
  441. rtw89_err(rtwdev, "failed to update %d RXBD info: %d\n",
  442. bd_ring->wp, ret);
  443. goto err_sync_device;
  444. }
  445. rx_info = RTW89_PCI_RX_SKB_CB(skb);
  446. if (!rx_info->fs || !rx_info->ls) {
  447. rtw89_err(rtwdev, "cannot process RP frame not set FS/LS\n");
  448. return cnt;
  449. }
  450. rtw89_chip_query_rxdesc(rtwdev, &desc_info, skb->data, rxinfo_size);
  451. /* first segment has RX desc */
  452. offset = desc_info.offset + desc_info.rxd_len;
  453. for (; offset + rpp_size <= rx_info->len; offset += rpp_size) {
  454. rpp = (struct rtw89_pci_rpp_fmt *)(skb->data + offset);
  455. rtw89_pci_release_rpp(rtwdev, rpp);
  456. }
  457. rtw89_pci_sync_skb_for_device(rtwdev, skb);
  458. rtw89_pci_rxbd_increase(rx_ring, 1);
  459. cnt++;
  460. return cnt;
  461. err_sync_device:
  462. rtw89_pci_sync_skb_for_device(rtwdev, skb);
  463. return 0;
  464. }
  465. static void rtw89_pci_release_tx(struct rtw89_dev *rtwdev,
  466. struct rtw89_pci_rx_ring *rx_ring,
  467. u32 cnt)
  468. {
  469. struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
  470. u32 release_cnt;
  471. while (cnt) {
  472. release_cnt = rtw89_pci_release_tx_skbs(rtwdev, rx_ring, cnt);
  473. if (!release_cnt) {
  474. rtw89_err(rtwdev, "failed to release TX skbs\n");
  475. /* skip the rest RXBD bufs */
  476. rtw89_pci_rxbd_increase(rx_ring, cnt);
  477. break;
  478. }
  479. cnt -= release_cnt;
  480. }
  481. rtw89_write16(rtwdev, bd_ring->addr.idx, bd_ring->wp);
  482. }
  483. static int rtw89_pci_poll_rpq_dma(struct rtw89_dev *rtwdev,
  484. struct rtw89_pci *rtwpci, int budget)
  485. {
  486. struct rtw89_pci_rx_ring *rx_ring;
  487. u32 cnt;
  488. int work_done;
  489. rx_ring = &rtwpci->rx_rings[RTW89_RXCH_RPQ];
  490. spin_lock_bh(&rtwpci->trx_lock);
  491. cnt = rtw89_pci_rxbd_recalc(rtwdev, rx_ring);
  492. if (cnt == 0)
  493. goto out_unlock;
  494. rtw89_pci_release_tx(rtwdev, rx_ring, cnt);
  495. out_unlock:
  496. spin_unlock_bh(&rtwpci->trx_lock);
  497. /* always release all RPQ */
  498. work_done = min_t(int, cnt, budget);
  499. rtwdev->napi_budget_countdown -= work_done;
  500. return work_done;
  501. }
  502. static void rtw89_pci_isr_rxd_unavail(struct rtw89_dev *rtwdev,
  503. struct rtw89_pci *rtwpci)
  504. {
  505. struct rtw89_pci_rx_ring *rx_ring;
  506. struct rtw89_pci_dma_ring *bd_ring;
  507. u32 reg_idx;
  508. u16 hw_idx, hw_idx_next, host_idx;
  509. int i;
  510. for (i = 0; i < RTW89_RXCH_NUM; i++) {
  511. rx_ring = &rtwpci->rx_rings[i];
  512. bd_ring = &rx_ring->bd_ring;
  513. reg_idx = rtw89_read32(rtwdev, bd_ring->addr.idx);
  514. hw_idx = FIELD_GET(TXBD_HW_IDX_MASK, reg_idx);
  515. host_idx = FIELD_GET(TXBD_HOST_IDX_MASK, reg_idx);
  516. hw_idx_next = (hw_idx + 1) % bd_ring->len;
  517. if (hw_idx_next == host_idx)
  518. rtw89_debug(rtwdev, RTW89_DBG_UNEXP, "%d RXD unavailable\n", i);
  519. rtw89_debug(rtwdev, RTW89_DBG_TXRX,
  520. "%d RXD unavailable, idx=0x%08x, len=%d\n",
  521. i, reg_idx, bd_ring->len);
  522. }
  523. }
  524. void rtw89_pci_recognize_intrs(struct rtw89_dev *rtwdev,
  525. struct rtw89_pci *rtwpci,
  526. struct rtw89_pci_isrs *isrs)
  527. {
  528. isrs->halt_c2h_isrs = rtw89_read32(rtwdev, R_AX_HISR0) & rtwpci->halt_c2h_intrs;
  529. isrs->isrs[0] = rtw89_read32(rtwdev, R_AX_PCIE_HISR00) & rtwpci->intrs[0];
  530. isrs->isrs[1] = rtw89_read32(rtwdev, R_AX_PCIE_HISR10) & rtwpci->intrs[1];
  531. rtw89_write32(rtwdev, R_AX_HISR0, isrs->halt_c2h_isrs);
  532. rtw89_write32(rtwdev, R_AX_PCIE_HISR00, isrs->isrs[0]);
  533. rtw89_write32(rtwdev, R_AX_PCIE_HISR10, isrs->isrs[1]);
  534. }
  535. EXPORT_SYMBOL(rtw89_pci_recognize_intrs);
  536. void rtw89_pci_recognize_intrs_v1(struct rtw89_dev *rtwdev,
  537. struct rtw89_pci *rtwpci,
  538. struct rtw89_pci_isrs *isrs)
  539. {
  540. isrs->ind_isrs = rtw89_read32(rtwdev, R_AX_PCIE_HISR00_V1) & rtwpci->ind_intrs;
  541. isrs->halt_c2h_isrs = isrs->ind_isrs & B_AX_HS0ISR_IND_INT_EN ?
  542. rtw89_read32(rtwdev, R_AX_HISR0) & rtwpci->halt_c2h_intrs : 0;
  543. isrs->isrs[0] = isrs->ind_isrs & B_AX_HCI_AXIDMA_INT_EN ?
  544. rtw89_read32(rtwdev, R_AX_HAXI_HISR00) & rtwpci->intrs[0] : 0;
  545. isrs->isrs[1] = isrs->ind_isrs & B_AX_HS1ISR_IND_INT_EN ?
  546. rtw89_read32(rtwdev, R_AX_HISR1) & rtwpci->intrs[1] : 0;
  547. if (isrs->halt_c2h_isrs)
  548. rtw89_write32(rtwdev, R_AX_HISR0, isrs->halt_c2h_isrs);
  549. if (isrs->isrs[0])
  550. rtw89_write32(rtwdev, R_AX_HAXI_HISR00, isrs->isrs[0]);
  551. if (isrs->isrs[1])
  552. rtw89_write32(rtwdev, R_AX_HISR1, isrs->isrs[1]);
  553. }
  554. EXPORT_SYMBOL(rtw89_pci_recognize_intrs_v1);
  555. static void rtw89_pci_clear_isr0(struct rtw89_dev *rtwdev, u32 isr00)
  556. {
  557. /* write 1 clear */
  558. rtw89_write32(rtwdev, R_AX_PCIE_HISR00, isr00);
  559. }
  560. void rtw89_pci_enable_intr(struct rtw89_dev *rtwdev, struct rtw89_pci *rtwpci)
  561. {
  562. rtw89_write32(rtwdev, R_AX_HIMR0, rtwpci->halt_c2h_intrs);
  563. rtw89_write32(rtwdev, R_AX_PCIE_HIMR00, rtwpci->intrs[0]);
  564. rtw89_write32(rtwdev, R_AX_PCIE_HIMR10, rtwpci->intrs[1]);
  565. }
  566. EXPORT_SYMBOL(rtw89_pci_enable_intr);
  567. void rtw89_pci_disable_intr(struct rtw89_dev *rtwdev, struct rtw89_pci *rtwpci)
  568. {
  569. rtw89_write32(rtwdev, R_AX_HIMR0, 0);
  570. rtw89_write32(rtwdev, R_AX_PCIE_HIMR00, 0);
  571. rtw89_write32(rtwdev, R_AX_PCIE_HIMR10, 0);
  572. }
  573. EXPORT_SYMBOL(rtw89_pci_disable_intr);
  574. void rtw89_pci_enable_intr_v1(struct rtw89_dev *rtwdev, struct rtw89_pci *rtwpci)
  575. {
  576. rtw89_write32(rtwdev, R_AX_PCIE_HIMR00_V1, rtwpci->ind_intrs);
  577. rtw89_write32(rtwdev, R_AX_HIMR0, rtwpci->halt_c2h_intrs);
  578. rtw89_write32(rtwdev, R_AX_HAXI_HIMR00, rtwpci->intrs[0]);
  579. rtw89_write32(rtwdev, R_AX_HIMR1, rtwpci->intrs[1]);
  580. }
  581. EXPORT_SYMBOL(rtw89_pci_enable_intr_v1);
  582. void rtw89_pci_disable_intr_v1(struct rtw89_dev *rtwdev, struct rtw89_pci *rtwpci)
  583. {
  584. rtw89_write32(rtwdev, R_AX_PCIE_HIMR00_V1, 0);
  585. }
  586. EXPORT_SYMBOL(rtw89_pci_disable_intr_v1);
  587. static void rtw89_pci_ops_recovery_start(struct rtw89_dev *rtwdev)
  588. {
  589. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  590. unsigned long flags;
  591. spin_lock_irqsave(&rtwpci->irq_lock, flags);
  592. rtw89_chip_disable_intr(rtwdev, rtwpci);
  593. rtw89_chip_config_intr_mask(rtwdev, RTW89_PCI_INTR_MASK_RECOVERY_START);
  594. rtw89_chip_enable_intr(rtwdev, rtwpci);
  595. spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
  596. }
  597. static void rtw89_pci_ops_recovery_complete(struct rtw89_dev *rtwdev)
  598. {
  599. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  600. unsigned long flags;
  601. spin_lock_irqsave(&rtwpci->irq_lock, flags);
  602. rtw89_chip_disable_intr(rtwdev, rtwpci);
  603. rtw89_chip_config_intr_mask(rtwdev, RTW89_PCI_INTR_MASK_RECOVERY_COMPLETE);
  604. rtw89_chip_enable_intr(rtwdev, rtwpci);
  605. spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
  606. }
  607. static void rtw89_pci_low_power_interrupt_handler(struct rtw89_dev *rtwdev)
  608. {
  609. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  610. int budget = NAPI_POLL_WEIGHT;
  611. /* To prevent RXQ get stuck due to run out of budget. */
  612. rtwdev->napi_budget_countdown = budget;
  613. rtw89_pci_poll_rpq_dma(rtwdev, rtwpci, budget);
  614. rtw89_pci_poll_rxq_dma(rtwdev, rtwpci, budget);
  615. }
  616. static irqreturn_t rtw89_pci_interrupt_threadfn(int irq, void *dev)
  617. {
  618. struct rtw89_dev *rtwdev = dev;
  619. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  620. struct rtw89_pci_isrs isrs;
  621. unsigned long flags;
  622. spin_lock_irqsave(&rtwpci->irq_lock, flags);
  623. rtw89_chip_recognize_intrs(rtwdev, rtwpci, &isrs);
  624. spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
  625. if (unlikely(isrs.isrs[0] & B_AX_RDU_INT))
  626. rtw89_pci_isr_rxd_unavail(rtwdev, rtwpci);
  627. if (unlikely(isrs.halt_c2h_isrs & B_AX_HALT_C2H_INT_EN))
  628. rtw89_ser_notify(rtwdev, rtw89_mac_get_err_status(rtwdev));
  629. if (unlikely(isrs.halt_c2h_isrs & B_AX_WDT_TIMEOUT_INT_EN))
  630. rtw89_ser_notify(rtwdev, MAC_AX_ERR_L2_ERR_WDT_TIMEOUT_INT);
  631. if (unlikely(rtwpci->under_recovery))
  632. goto enable_intr;
  633. if (unlikely(rtwpci->low_power)) {
  634. rtw89_pci_low_power_interrupt_handler(rtwdev);
  635. goto enable_intr;
  636. }
  637. if (likely(rtwpci->running)) {
  638. local_bh_disable();
  639. napi_schedule(&rtwdev->napi);
  640. local_bh_enable();
  641. }
  642. return IRQ_HANDLED;
  643. enable_intr:
  644. spin_lock_irqsave(&rtwpci->irq_lock, flags);
  645. if (likely(rtwpci->running))
  646. rtw89_chip_enable_intr(rtwdev, rtwpci);
  647. spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
  648. return IRQ_HANDLED;
  649. }
  650. static irqreturn_t rtw89_pci_interrupt_handler(int irq, void *dev)
  651. {
  652. struct rtw89_dev *rtwdev = dev;
  653. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  654. unsigned long flags;
  655. irqreturn_t irqret = IRQ_WAKE_THREAD;
  656. spin_lock_irqsave(&rtwpci->irq_lock, flags);
  657. /* If interrupt event is on the road, it is still trigger interrupt
  658. * even we have done pci_stop() to turn off IMR.
  659. */
  660. if (unlikely(!rtwpci->running)) {
  661. irqret = IRQ_HANDLED;
  662. goto exit;
  663. }
  664. rtw89_chip_disable_intr(rtwdev, rtwpci);
  665. exit:
  666. spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
  667. return irqret;
  668. }
  669. #define DEF_TXCHADDRS_TYPE1(info, txch, v...) \
  670. [RTW89_TXCH_##txch] = { \
  671. .num = R_AX_##txch##_TXBD_NUM ##v, \
  672. .idx = R_AX_##txch##_TXBD_IDX ##v, \
  673. .bdram = R_AX_##txch##_BDRAM_CTRL ##v, \
  674. .desa_l = R_AX_##txch##_TXBD_DESA_L ##v, \
  675. .desa_h = R_AX_##txch##_TXBD_DESA_H ##v, \
  676. }
  677. #define DEF_TXCHADDRS(info, txch, v...) \
  678. [RTW89_TXCH_##txch] = { \
  679. .num = R_AX_##txch##_TXBD_NUM, \
  680. .idx = R_AX_##txch##_TXBD_IDX, \
  681. .bdram = R_AX_##txch##_BDRAM_CTRL ##v, \
  682. .desa_l = R_AX_##txch##_TXBD_DESA_L ##v, \
  683. .desa_h = R_AX_##txch##_TXBD_DESA_H ##v, \
  684. }
  685. #define DEF_RXCHADDRS(info, rxch, v...) \
  686. [RTW89_RXCH_##rxch] = { \
  687. .num = R_AX_##rxch##_RXBD_NUM ##v, \
  688. .idx = R_AX_##rxch##_RXBD_IDX ##v, \
  689. .desa_l = R_AX_##rxch##_RXBD_DESA_L ##v, \
  690. .desa_h = R_AX_##rxch##_RXBD_DESA_H ##v, \
  691. }
  692. const struct rtw89_pci_ch_dma_addr_set rtw89_pci_ch_dma_addr_set = {
  693. .tx = {
  694. DEF_TXCHADDRS(info, ACH0),
  695. DEF_TXCHADDRS(info, ACH1),
  696. DEF_TXCHADDRS(info, ACH2),
  697. DEF_TXCHADDRS(info, ACH3),
  698. DEF_TXCHADDRS(info, ACH4),
  699. DEF_TXCHADDRS(info, ACH5),
  700. DEF_TXCHADDRS(info, ACH6),
  701. DEF_TXCHADDRS(info, ACH7),
  702. DEF_TXCHADDRS(info, CH8),
  703. DEF_TXCHADDRS(info, CH9),
  704. DEF_TXCHADDRS_TYPE1(info, CH10),
  705. DEF_TXCHADDRS_TYPE1(info, CH11),
  706. DEF_TXCHADDRS(info, CH12),
  707. },
  708. .rx = {
  709. DEF_RXCHADDRS(info, RXQ),
  710. DEF_RXCHADDRS(info, RPQ),
  711. },
  712. };
  713. EXPORT_SYMBOL(rtw89_pci_ch_dma_addr_set);
  714. const struct rtw89_pci_ch_dma_addr_set rtw89_pci_ch_dma_addr_set_v1 = {
  715. .tx = {
  716. DEF_TXCHADDRS(info, ACH0, _V1),
  717. DEF_TXCHADDRS(info, ACH1, _V1),
  718. DEF_TXCHADDRS(info, ACH2, _V1),
  719. DEF_TXCHADDRS(info, ACH3, _V1),
  720. DEF_TXCHADDRS(info, ACH4, _V1),
  721. DEF_TXCHADDRS(info, ACH5, _V1),
  722. DEF_TXCHADDRS(info, ACH6, _V1),
  723. DEF_TXCHADDRS(info, ACH7, _V1),
  724. DEF_TXCHADDRS(info, CH8, _V1),
  725. DEF_TXCHADDRS(info, CH9, _V1),
  726. DEF_TXCHADDRS_TYPE1(info, CH10, _V1),
  727. DEF_TXCHADDRS_TYPE1(info, CH11, _V1),
  728. DEF_TXCHADDRS(info, CH12, _V1),
  729. },
  730. .rx = {
  731. DEF_RXCHADDRS(info, RXQ, _V1),
  732. DEF_RXCHADDRS(info, RPQ, _V1),
  733. },
  734. };
  735. EXPORT_SYMBOL(rtw89_pci_ch_dma_addr_set_v1);
  736. #undef DEF_TXCHADDRS_TYPE1
  737. #undef DEF_TXCHADDRS
  738. #undef DEF_RXCHADDRS
  739. static int rtw89_pci_get_txch_addrs(struct rtw89_dev *rtwdev,
  740. enum rtw89_tx_channel txch,
  741. const struct rtw89_pci_ch_dma_addr **addr)
  742. {
  743. const struct rtw89_pci_info *info = rtwdev->pci_info;
  744. if (txch >= RTW89_TXCH_NUM)
  745. return -EINVAL;
  746. *addr = &info->dma_addr_set->tx[txch];
  747. return 0;
  748. }
  749. static int rtw89_pci_get_rxch_addrs(struct rtw89_dev *rtwdev,
  750. enum rtw89_rx_channel rxch,
  751. const struct rtw89_pci_ch_dma_addr **addr)
  752. {
  753. const struct rtw89_pci_info *info = rtwdev->pci_info;
  754. if (rxch >= RTW89_RXCH_NUM)
  755. return -EINVAL;
  756. *addr = &info->dma_addr_set->rx[rxch];
  757. return 0;
  758. }
  759. static u32 rtw89_pci_get_avail_txbd_num(struct rtw89_pci_tx_ring *ring)
  760. {
  761. struct rtw89_pci_dma_ring *bd_ring = &ring->bd_ring;
  762. /* reserved 1 desc check ring is full or not */
  763. if (bd_ring->rp > bd_ring->wp)
  764. return bd_ring->rp - bd_ring->wp - 1;
  765. return bd_ring->len - (bd_ring->wp - bd_ring->rp) - 1;
  766. }
  767. static
  768. u32 __rtw89_pci_check_and_reclaim_tx_fwcmd_resource(struct rtw89_dev *rtwdev)
  769. {
  770. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  771. struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[RTW89_TXCH_CH12];
  772. u32 cnt;
  773. spin_lock_bh(&rtwpci->trx_lock);
  774. rtw89_pci_reclaim_tx_fwcmd(rtwdev, rtwpci);
  775. cnt = rtw89_pci_get_avail_txbd_num(tx_ring);
  776. spin_unlock_bh(&rtwpci->trx_lock);
  777. return cnt;
  778. }
  779. static
  780. u32 __rtw89_pci_check_and_reclaim_tx_resource_noio(struct rtw89_dev *rtwdev,
  781. u8 txch)
  782. {
  783. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  784. struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[txch];
  785. struct rtw89_pci_tx_wd_ring *wd_ring = &tx_ring->wd_ring;
  786. u32 cnt;
  787. spin_lock_bh(&rtwpci->trx_lock);
  788. cnt = rtw89_pci_get_avail_txbd_num(tx_ring);
  789. cnt = min(cnt, wd_ring->curr_num);
  790. spin_unlock_bh(&rtwpci->trx_lock);
  791. return cnt;
  792. }
  793. static u32 __rtw89_pci_check_and_reclaim_tx_resource(struct rtw89_dev *rtwdev,
  794. u8 txch)
  795. {
  796. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  797. struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[txch];
  798. struct rtw89_pci_tx_wd_ring *wd_ring = &tx_ring->wd_ring;
  799. const struct rtw89_chip_info *chip = rtwdev->chip;
  800. u32 bd_cnt, wd_cnt, min_cnt = 0;
  801. struct rtw89_pci_rx_ring *rx_ring;
  802. enum rtw89_debug_mask debug_mask;
  803. u32 cnt;
  804. rx_ring = &rtwpci->rx_rings[RTW89_RXCH_RPQ];
  805. spin_lock_bh(&rtwpci->trx_lock);
  806. bd_cnt = rtw89_pci_get_avail_txbd_num(tx_ring);
  807. wd_cnt = wd_ring->curr_num;
  808. if (wd_cnt == 0 || bd_cnt == 0) {
  809. cnt = rtw89_pci_rxbd_recalc(rtwdev, rx_ring);
  810. if (cnt)
  811. rtw89_pci_release_tx(rtwdev, rx_ring, cnt);
  812. else if (wd_cnt == 0)
  813. goto out_unlock;
  814. bd_cnt = rtw89_pci_get_avail_txbd_num(tx_ring);
  815. if (bd_cnt == 0)
  816. rtw89_pci_reclaim_txbd(rtwdev, tx_ring);
  817. }
  818. bd_cnt = rtw89_pci_get_avail_txbd_num(tx_ring);
  819. wd_cnt = wd_ring->curr_num;
  820. min_cnt = min(bd_cnt, wd_cnt);
  821. if (min_cnt == 0) {
  822. /* This message can be frequently shown in low power mode or
  823. * high traffic with small FIFO chips, and we have recognized it as normal
  824. * behavior, so print with mask RTW89_DBG_TXRX in these situations.
  825. */
  826. if (rtwpci->low_power || chip->small_fifo_size)
  827. debug_mask = RTW89_DBG_TXRX;
  828. else
  829. debug_mask = RTW89_DBG_UNEXP;
  830. rtw89_debug(rtwdev, debug_mask,
  831. "still no tx resource after reclaim: wd_cnt=%d bd_cnt=%d\n",
  832. wd_cnt, bd_cnt);
  833. }
  834. out_unlock:
  835. spin_unlock_bh(&rtwpci->trx_lock);
  836. return min_cnt;
  837. }
  838. static u32 rtw89_pci_check_and_reclaim_tx_resource(struct rtw89_dev *rtwdev,
  839. u8 txch)
  840. {
  841. if (rtwdev->hci.paused)
  842. return __rtw89_pci_check_and_reclaim_tx_resource_noio(rtwdev, txch);
  843. if (txch == RTW89_TXCH_CH12)
  844. return __rtw89_pci_check_and_reclaim_tx_fwcmd_resource(rtwdev);
  845. return __rtw89_pci_check_and_reclaim_tx_resource(rtwdev, txch);
  846. }
  847. static void __rtw89_pci_tx_kick_off(struct rtw89_dev *rtwdev, struct rtw89_pci_tx_ring *tx_ring)
  848. {
  849. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  850. struct rtw89_pci_dma_ring *bd_ring = &tx_ring->bd_ring;
  851. u32 host_idx, addr;
  852. spin_lock_bh(&rtwpci->trx_lock);
  853. addr = bd_ring->addr.idx;
  854. host_idx = bd_ring->wp;
  855. rtw89_write16(rtwdev, addr, host_idx);
  856. spin_unlock_bh(&rtwpci->trx_lock);
  857. }
  858. static void rtw89_pci_tx_bd_ring_update(struct rtw89_dev *rtwdev, struct rtw89_pci_tx_ring *tx_ring,
  859. int n_txbd)
  860. {
  861. struct rtw89_pci_dma_ring *bd_ring = &tx_ring->bd_ring;
  862. u32 host_idx, len;
  863. len = bd_ring->len;
  864. host_idx = bd_ring->wp + n_txbd;
  865. host_idx = host_idx < len ? host_idx : host_idx - len;
  866. bd_ring->wp = host_idx;
  867. }
  868. static void rtw89_pci_ops_tx_kick_off(struct rtw89_dev *rtwdev, u8 txch)
  869. {
  870. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  871. struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[txch];
  872. if (rtwdev->hci.paused) {
  873. set_bit(txch, rtwpci->kick_map);
  874. return;
  875. }
  876. __rtw89_pci_tx_kick_off(rtwdev, tx_ring);
  877. }
  878. static void rtw89_pci_tx_kick_off_pending(struct rtw89_dev *rtwdev)
  879. {
  880. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  881. struct rtw89_pci_tx_ring *tx_ring;
  882. int txch;
  883. for (txch = 0; txch < RTW89_TXCH_NUM; txch++) {
  884. if (!test_and_clear_bit(txch, rtwpci->kick_map))
  885. continue;
  886. tx_ring = &rtwpci->tx_rings[txch];
  887. __rtw89_pci_tx_kick_off(rtwdev, tx_ring);
  888. }
  889. }
  890. static void __pci_flush_txch(struct rtw89_dev *rtwdev, u8 txch, bool drop)
  891. {
  892. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  893. struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[txch];
  894. struct rtw89_pci_dma_ring *bd_ring = &tx_ring->bd_ring;
  895. u32 cur_idx, cur_rp;
  896. u8 i;
  897. /* Because the time taked by the I/O is a bit dynamic, it's hard to
  898. * define a reasonable fixed total timeout to use read_poll_timeout*
  899. * helper. Instead, we can ensure a reasonable polling times, so we
  900. * just use for loop with udelay here.
  901. */
  902. for (i = 0; i < 60; i++) {
  903. cur_idx = rtw89_read32(rtwdev, bd_ring->addr.idx);
  904. cur_rp = FIELD_GET(TXBD_HW_IDX_MASK, cur_idx);
  905. if (cur_rp == bd_ring->wp)
  906. return;
  907. udelay(1);
  908. }
  909. if (!drop)
  910. rtw89_info(rtwdev, "timed out to flush pci txch: %d\n", txch);
  911. }
  912. static void __rtw89_pci_ops_flush_txchs(struct rtw89_dev *rtwdev, u32 txchs,
  913. bool drop)
  914. {
  915. const struct rtw89_pci_info *info = rtwdev->pci_info;
  916. u8 i;
  917. for (i = 0; i < RTW89_TXCH_NUM; i++) {
  918. /* It may be unnecessary to flush FWCMD queue. */
  919. if (i == RTW89_TXCH_CH12)
  920. continue;
  921. if (info->tx_dma_ch_mask & BIT(i))
  922. continue;
  923. if (txchs & BIT(i))
  924. __pci_flush_txch(rtwdev, i, drop);
  925. }
  926. }
  927. static void rtw89_pci_ops_flush_queues(struct rtw89_dev *rtwdev, u32 queues,
  928. bool drop)
  929. {
  930. __rtw89_pci_ops_flush_txchs(rtwdev, BIT(RTW89_TXCH_NUM) - 1, drop);
  931. }
  932. u32 rtw89_pci_fill_txaddr_info(struct rtw89_dev *rtwdev,
  933. void *txaddr_info_addr, u32 total_len,
  934. dma_addr_t dma, u8 *add_info_nr)
  935. {
  936. struct rtw89_pci_tx_addr_info_32 *txaddr_info = txaddr_info_addr;
  937. txaddr_info->length = cpu_to_le16(total_len);
  938. txaddr_info->option = cpu_to_le16(RTW89_PCI_ADDR_MSDU_LS |
  939. RTW89_PCI_ADDR_NUM(1));
  940. txaddr_info->dma = cpu_to_le32(dma);
  941. *add_info_nr = 1;
  942. return sizeof(*txaddr_info);
  943. }
  944. EXPORT_SYMBOL(rtw89_pci_fill_txaddr_info);
  945. u32 rtw89_pci_fill_txaddr_info_v1(struct rtw89_dev *rtwdev,
  946. void *txaddr_info_addr, u32 total_len,
  947. dma_addr_t dma, u8 *add_info_nr)
  948. {
  949. struct rtw89_pci_tx_addr_info_32_v1 *txaddr_info = txaddr_info_addr;
  950. u32 remain = total_len;
  951. u32 len;
  952. u16 length_option;
  953. int n;
  954. for (n = 0; n < RTW89_TXADDR_INFO_NR_V1 && remain; n++) {
  955. len = remain >= TXADDR_INFO_LENTHG_V1_MAX ?
  956. TXADDR_INFO_LENTHG_V1_MAX : remain;
  957. remain -= len;
  958. length_option = FIELD_PREP(B_PCIADDR_LEN_V1_MASK, len) |
  959. FIELD_PREP(B_PCIADDR_HIGH_SEL_V1_MASK, 0) |
  960. FIELD_PREP(B_PCIADDR_LS_V1_MASK, remain == 0);
  961. txaddr_info->length_opt = cpu_to_le16(length_option);
  962. txaddr_info->dma_low_lsb = cpu_to_le16(FIELD_GET(GENMASK(15, 0), dma));
  963. txaddr_info->dma_low_msb = cpu_to_le16(FIELD_GET(GENMASK(31, 16), dma));
  964. dma += len;
  965. txaddr_info++;
  966. }
  967. WARN_ONCE(remain, "length overflow remain=%u total_len=%u",
  968. remain, total_len);
  969. *add_info_nr = n;
  970. return n * sizeof(*txaddr_info);
  971. }
  972. EXPORT_SYMBOL(rtw89_pci_fill_txaddr_info_v1);
  973. static int rtw89_pci_txwd_submit(struct rtw89_dev *rtwdev,
  974. struct rtw89_pci_tx_ring *tx_ring,
  975. struct rtw89_pci_tx_wd *txwd,
  976. struct rtw89_core_tx_request *tx_req)
  977. {
  978. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  979. const struct rtw89_chip_info *chip = rtwdev->chip;
  980. struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
  981. struct rtw89_pci_tx_wp_info *txwp_info;
  982. void *txaddr_info_addr;
  983. struct pci_dev *pdev = rtwpci->pdev;
  984. struct sk_buff *skb = tx_req->skb;
  985. struct rtw89_pci_tx_data *tx_data = RTW89_PCI_TX_SKB_CB(skb);
  986. struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
  987. bool en_wd_info = desc_info->en_wd_info;
  988. u32 txwd_len;
  989. u32 txwp_len;
  990. u32 txaddr_info_len;
  991. dma_addr_t dma;
  992. int ret;
  993. dma = dma_map_single(&pdev->dev, skb->data, skb->len, DMA_TO_DEVICE);
  994. if (dma_mapping_error(&pdev->dev, dma)) {
  995. rtw89_err(rtwdev, "failed to map skb dma data\n");
  996. ret = -EBUSY;
  997. goto err;
  998. }
  999. tx_data->dma = dma;
  1000. rcu_assign_pointer(skb_data->wait, NULL);
  1001. txwp_len = sizeof(*txwp_info);
  1002. txwd_len = chip->txwd_body_size;
  1003. txwd_len += en_wd_info ? chip->txwd_info_size : 0;
  1004. txwp_info = txwd->vaddr + txwd_len;
  1005. txwp_info->seq0 = cpu_to_le16(txwd->seq | RTW89_PCI_TXWP_VALID);
  1006. txwp_info->seq1 = 0;
  1007. txwp_info->seq2 = 0;
  1008. txwp_info->seq3 = 0;
  1009. tx_ring->tx_cnt++;
  1010. txaddr_info_addr = txwd->vaddr + txwd_len + txwp_len;
  1011. txaddr_info_len =
  1012. rtw89_chip_fill_txaddr_info(rtwdev, txaddr_info_addr, skb->len,
  1013. dma, &desc_info->addr_info_nr);
  1014. txwd->len = txwd_len + txwp_len + txaddr_info_len;
  1015. rtw89_chip_fill_txdesc(rtwdev, desc_info, txwd->vaddr);
  1016. skb_queue_tail(&txwd->queue, skb);
  1017. return 0;
  1018. err:
  1019. return ret;
  1020. }
  1021. static int rtw89_pci_fwcmd_submit(struct rtw89_dev *rtwdev,
  1022. struct rtw89_pci_tx_ring *tx_ring,
  1023. struct rtw89_pci_tx_bd_32 *txbd,
  1024. struct rtw89_core_tx_request *tx_req)
  1025. {
  1026. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1027. const struct rtw89_chip_info *chip = rtwdev->chip;
  1028. struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
  1029. void *txdesc;
  1030. int txdesc_size = chip->h2c_desc_size;
  1031. struct pci_dev *pdev = rtwpci->pdev;
  1032. struct sk_buff *skb = tx_req->skb;
  1033. struct rtw89_pci_tx_data *tx_data = RTW89_PCI_TX_SKB_CB(skb);
  1034. dma_addr_t dma;
  1035. txdesc = skb_push(skb, txdesc_size);
  1036. memset(txdesc, 0, txdesc_size);
  1037. rtw89_chip_fill_txdesc_fwcmd(rtwdev, desc_info, txdesc);
  1038. dma = dma_map_single(&pdev->dev, skb->data, skb->len, DMA_TO_DEVICE);
  1039. if (dma_mapping_error(&pdev->dev, dma)) {
  1040. rtw89_err(rtwdev, "failed to map fwcmd dma data\n");
  1041. return -EBUSY;
  1042. }
  1043. tx_data->dma = dma;
  1044. txbd->option = cpu_to_le16(RTW89_PCI_TXBD_OPTION_LS);
  1045. txbd->length = cpu_to_le16(skb->len);
  1046. txbd->dma = cpu_to_le32(tx_data->dma);
  1047. skb_queue_tail(&rtwpci->h2c_queue, skb);
  1048. rtw89_pci_tx_bd_ring_update(rtwdev, tx_ring, 1);
  1049. return 0;
  1050. }
  1051. static int rtw89_pci_txbd_submit(struct rtw89_dev *rtwdev,
  1052. struct rtw89_pci_tx_ring *tx_ring,
  1053. struct rtw89_pci_tx_bd_32 *txbd,
  1054. struct rtw89_core_tx_request *tx_req)
  1055. {
  1056. struct rtw89_pci_tx_wd *txwd;
  1057. int ret;
  1058. /* FWCMD queue doesn't have wd pages. Instead, it submits the CMD
  1059. * buffer with WD BODY only. So here we don't need to check the free
  1060. * pages of the wd ring.
  1061. */
  1062. if (tx_ring->txch == RTW89_TXCH_CH12)
  1063. return rtw89_pci_fwcmd_submit(rtwdev, tx_ring, txbd, tx_req);
  1064. txwd = rtw89_pci_dequeue_txwd(tx_ring);
  1065. if (!txwd) {
  1066. rtw89_err(rtwdev, "no available TXWD\n");
  1067. ret = -ENOSPC;
  1068. goto err;
  1069. }
  1070. ret = rtw89_pci_txwd_submit(rtwdev, tx_ring, txwd, tx_req);
  1071. if (ret) {
  1072. rtw89_err(rtwdev, "failed to submit TXWD %d\n", txwd->seq);
  1073. goto err_enqueue_wd;
  1074. }
  1075. list_add_tail(&txwd->list, &tx_ring->busy_pages);
  1076. txbd->option = cpu_to_le16(RTW89_PCI_TXBD_OPTION_LS);
  1077. txbd->length = cpu_to_le16(txwd->len);
  1078. txbd->dma = cpu_to_le32(txwd->paddr);
  1079. rtw89_pci_tx_bd_ring_update(rtwdev, tx_ring, 1);
  1080. return 0;
  1081. err_enqueue_wd:
  1082. rtw89_pci_enqueue_txwd(tx_ring, txwd);
  1083. err:
  1084. return ret;
  1085. }
  1086. static int rtw89_pci_tx_write(struct rtw89_dev *rtwdev, struct rtw89_core_tx_request *tx_req,
  1087. u8 txch)
  1088. {
  1089. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1090. struct rtw89_pci_tx_ring *tx_ring;
  1091. struct rtw89_pci_tx_bd_32 *txbd;
  1092. u32 n_avail_txbd;
  1093. int ret = 0;
  1094. /* check the tx type and dma channel for fw cmd queue */
  1095. if ((txch == RTW89_TXCH_CH12 ||
  1096. tx_req->tx_type == RTW89_CORE_TX_TYPE_FWCMD) &&
  1097. (txch != RTW89_TXCH_CH12 ||
  1098. tx_req->tx_type != RTW89_CORE_TX_TYPE_FWCMD)) {
  1099. rtw89_err(rtwdev, "only fw cmd uses dma channel 12\n");
  1100. return -EINVAL;
  1101. }
  1102. tx_ring = &rtwpci->tx_rings[txch];
  1103. spin_lock_bh(&rtwpci->trx_lock);
  1104. n_avail_txbd = rtw89_pci_get_avail_txbd_num(tx_ring);
  1105. if (n_avail_txbd == 0) {
  1106. rtw89_err(rtwdev, "no available TXBD\n");
  1107. ret = -ENOSPC;
  1108. goto err_unlock;
  1109. }
  1110. txbd = rtw89_pci_get_next_txbd(tx_ring);
  1111. ret = rtw89_pci_txbd_submit(rtwdev, tx_ring, txbd, tx_req);
  1112. if (ret) {
  1113. rtw89_err(rtwdev, "failed to submit TXBD\n");
  1114. goto err_unlock;
  1115. }
  1116. spin_unlock_bh(&rtwpci->trx_lock);
  1117. return 0;
  1118. err_unlock:
  1119. spin_unlock_bh(&rtwpci->trx_lock);
  1120. return ret;
  1121. }
  1122. static int rtw89_pci_ops_tx_write(struct rtw89_dev *rtwdev, struct rtw89_core_tx_request *tx_req)
  1123. {
  1124. struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
  1125. int ret;
  1126. ret = rtw89_pci_tx_write(rtwdev, tx_req, desc_info->ch_dma);
  1127. if (ret) {
  1128. rtw89_err(rtwdev, "failed to TX Queue %d\n", desc_info->ch_dma);
  1129. return ret;
  1130. }
  1131. return 0;
  1132. }
  1133. const struct rtw89_pci_bd_ram rtw89_bd_ram_table_dual[RTW89_TXCH_NUM] = {
  1134. [RTW89_TXCH_ACH0] = {.start_idx = 0, .max_num = 5, .min_num = 2},
  1135. [RTW89_TXCH_ACH1] = {.start_idx = 5, .max_num = 5, .min_num = 2},
  1136. [RTW89_TXCH_ACH2] = {.start_idx = 10, .max_num = 5, .min_num = 2},
  1137. [RTW89_TXCH_ACH3] = {.start_idx = 15, .max_num = 5, .min_num = 2},
  1138. [RTW89_TXCH_ACH4] = {.start_idx = 20, .max_num = 5, .min_num = 2},
  1139. [RTW89_TXCH_ACH5] = {.start_idx = 25, .max_num = 5, .min_num = 2},
  1140. [RTW89_TXCH_ACH6] = {.start_idx = 30, .max_num = 5, .min_num = 2},
  1141. [RTW89_TXCH_ACH7] = {.start_idx = 35, .max_num = 5, .min_num = 2},
  1142. [RTW89_TXCH_CH8] = {.start_idx = 40, .max_num = 5, .min_num = 1},
  1143. [RTW89_TXCH_CH9] = {.start_idx = 45, .max_num = 5, .min_num = 1},
  1144. [RTW89_TXCH_CH10] = {.start_idx = 50, .max_num = 5, .min_num = 1},
  1145. [RTW89_TXCH_CH11] = {.start_idx = 55, .max_num = 5, .min_num = 1},
  1146. [RTW89_TXCH_CH12] = {.start_idx = 60, .max_num = 4, .min_num = 1},
  1147. };
  1148. EXPORT_SYMBOL(rtw89_bd_ram_table_dual);
  1149. const struct rtw89_pci_bd_ram rtw89_bd_ram_table_single[RTW89_TXCH_NUM] = {
  1150. [RTW89_TXCH_ACH0] = {.start_idx = 0, .max_num = 5, .min_num = 2},
  1151. [RTW89_TXCH_ACH1] = {.start_idx = 5, .max_num = 5, .min_num = 2},
  1152. [RTW89_TXCH_ACH2] = {.start_idx = 10, .max_num = 5, .min_num = 2},
  1153. [RTW89_TXCH_ACH3] = {.start_idx = 15, .max_num = 5, .min_num = 2},
  1154. [RTW89_TXCH_CH8] = {.start_idx = 20, .max_num = 4, .min_num = 1},
  1155. [RTW89_TXCH_CH9] = {.start_idx = 24, .max_num = 4, .min_num = 1},
  1156. [RTW89_TXCH_CH12] = {.start_idx = 28, .max_num = 4, .min_num = 1},
  1157. };
  1158. EXPORT_SYMBOL(rtw89_bd_ram_table_single);
  1159. static void rtw89_pci_reset_trx_rings(struct rtw89_dev *rtwdev)
  1160. {
  1161. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1162. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1163. const struct rtw89_pci_bd_ram *bd_ram_table = *info->bd_ram_table;
  1164. struct rtw89_pci_tx_ring *tx_ring;
  1165. struct rtw89_pci_rx_ring *rx_ring;
  1166. struct rtw89_pci_dma_ring *bd_ring;
  1167. const struct rtw89_pci_bd_ram *bd_ram;
  1168. u32 addr_num;
  1169. u32 addr_bdram;
  1170. u32 addr_desa_l;
  1171. u32 val32;
  1172. int i;
  1173. for (i = 0; i < RTW89_TXCH_NUM; i++) {
  1174. if (info->tx_dma_ch_mask & BIT(i))
  1175. continue;
  1176. tx_ring = &rtwpci->tx_rings[i];
  1177. bd_ring = &tx_ring->bd_ring;
  1178. bd_ram = &bd_ram_table[i];
  1179. addr_num = bd_ring->addr.num;
  1180. addr_bdram = bd_ring->addr.bdram;
  1181. addr_desa_l = bd_ring->addr.desa_l;
  1182. bd_ring->wp = 0;
  1183. bd_ring->rp = 0;
  1184. val32 = FIELD_PREP(BDRAM_SIDX_MASK, bd_ram->start_idx) |
  1185. FIELD_PREP(BDRAM_MAX_MASK, bd_ram->max_num) |
  1186. FIELD_PREP(BDRAM_MIN_MASK, bd_ram->min_num);
  1187. rtw89_write16(rtwdev, addr_num, bd_ring->len);
  1188. rtw89_write32(rtwdev, addr_bdram, val32);
  1189. rtw89_write32(rtwdev, addr_desa_l, bd_ring->dma);
  1190. }
  1191. for (i = 0; i < RTW89_RXCH_NUM; i++) {
  1192. rx_ring = &rtwpci->rx_rings[i];
  1193. bd_ring = &rx_ring->bd_ring;
  1194. addr_num = bd_ring->addr.num;
  1195. addr_desa_l = bd_ring->addr.desa_l;
  1196. bd_ring->wp = 0;
  1197. bd_ring->rp = 0;
  1198. rx_ring->diliver_skb = NULL;
  1199. rx_ring->diliver_desc.ready = false;
  1200. rtw89_write16(rtwdev, addr_num, bd_ring->len);
  1201. rtw89_write32(rtwdev, addr_desa_l, bd_ring->dma);
  1202. }
  1203. }
  1204. static void rtw89_pci_release_tx_ring(struct rtw89_dev *rtwdev,
  1205. struct rtw89_pci_tx_ring *tx_ring)
  1206. {
  1207. rtw89_pci_release_busy_txwd(rtwdev, tx_ring);
  1208. rtw89_pci_release_pending_txwd_skb(rtwdev, tx_ring);
  1209. }
  1210. static void rtw89_pci_ops_reset(struct rtw89_dev *rtwdev)
  1211. {
  1212. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1213. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1214. int txch;
  1215. rtw89_pci_reset_trx_rings(rtwdev);
  1216. spin_lock_bh(&rtwpci->trx_lock);
  1217. for (txch = 0; txch < RTW89_TXCH_NUM; txch++) {
  1218. if (info->tx_dma_ch_mask & BIT(txch))
  1219. continue;
  1220. if (txch == RTW89_TXCH_CH12) {
  1221. rtw89_pci_release_fwcmd(rtwdev, rtwpci,
  1222. skb_queue_len(&rtwpci->h2c_queue), true);
  1223. continue;
  1224. }
  1225. rtw89_pci_release_tx_ring(rtwdev, &rtwpci->tx_rings[txch]);
  1226. }
  1227. spin_unlock_bh(&rtwpci->trx_lock);
  1228. }
  1229. static void rtw89_pci_enable_intr_lock(struct rtw89_dev *rtwdev)
  1230. {
  1231. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1232. unsigned long flags;
  1233. spin_lock_irqsave(&rtwpci->irq_lock, flags);
  1234. rtwpci->running = true;
  1235. rtw89_chip_enable_intr(rtwdev, rtwpci);
  1236. spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
  1237. }
  1238. static void rtw89_pci_disable_intr_lock(struct rtw89_dev *rtwdev)
  1239. {
  1240. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1241. unsigned long flags;
  1242. spin_lock_irqsave(&rtwpci->irq_lock, flags);
  1243. rtwpci->running = false;
  1244. rtw89_chip_disable_intr(rtwdev, rtwpci);
  1245. spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
  1246. }
  1247. static int rtw89_pci_ops_start(struct rtw89_dev *rtwdev)
  1248. {
  1249. rtw89_core_napi_start(rtwdev);
  1250. rtw89_pci_enable_intr_lock(rtwdev);
  1251. return 0;
  1252. }
  1253. static void rtw89_pci_ops_stop(struct rtw89_dev *rtwdev)
  1254. {
  1255. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1256. struct pci_dev *pdev = rtwpci->pdev;
  1257. rtw89_pci_disable_intr_lock(rtwdev);
  1258. synchronize_irq(pdev->irq);
  1259. rtw89_core_napi_stop(rtwdev);
  1260. }
  1261. static void rtw89_pci_ops_pause(struct rtw89_dev *rtwdev, bool pause)
  1262. {
  1263. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1264. struct pci_dev *pdev = rtwpci->pdev;
  1265. if (pause) {
  1266. rtw89_pci_disable_intr_lock(rtwdev);
  1267. synchronize_irq(pdev->irq);
  1268. if (test_bit(RTW89_FLAG_NAPI_RUNNING, rtwdev->flags))
  1269. napi_synchronize(&rtwdev->napi);
  1270. } else {
  1271. rtw89_pci_enable_intr_lock(rtwdev);
  1272. rtw89_pci_tx_kick_off_pending(rtwdev);
  1273. }
  1274. }
  1275. static
  1276. void rtw89_pci_switch_bd_idx_addr(struct rtw89_dev *rtwdev, bool low_power)
  1277. {
  1278. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1279. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1280. const struct rtw89_pci_bd_idx_addr *bd_idx_addr = info->bd_idx_addr_low_power;
  1281. const struct rtw89_pci_ch_dma_addr_set *dma_addr_set = info->dma_addr_set;
  1282. struct rtw89_pci_tx_ring *tx_ring;
  1283. struct rtw89_pci_rx_ring *rx_ring;
  1284. int i;
  1285. if (WARN(!bd_idx_addr, "only HCI with low power mode needs this\n"))
  1286. return;
  1287. for (i = 0; i < RTW89_TXCH_NUM; i++) {
  1288. tx_ring = &rtwpci->tx_rings[i];
  1289. tx_ring->bd_ring.addr.idx = low_power ?
  1290. bd_idx_addr->tx_bd_addrs[i] :
  1291. dma_addr_set->tx[i].idx;
  1292. }
  1293. for (i = 0; i < RTW89_RXCH_NUM; i++) {
  1294. rx_ring = &rtwpci->rx_rings[i];
  1295. rx_ring->bd_ring.addr.idx = low_power ?
  1296. bd_idx_addr->rx_bd_addrs[i] :
  1297. dma_addr_set->rx[i].idx;
  1298. }
  1299. }
  1300. static void rtw89_pci_ops_switch_mode(struct rtw89_dev *rtwdev, bool low_power)
  1301. {
  1302. enum rtw89_pci_intr_mask_cfg cfg;
  1303. WARN(!rtwdev->hci.paused, "HCI isn't paused\n");
  1304. cfg = low_power ? RTW89_PCI_INTR_MASK_LOW_POWER : RTW89_PCI_INTR_MASK_NORMAL;
  1305. rtw89_chip_config_intr_mask(rtwdev, cfg);
  1306. rtw89_pci_switch_bd_idx_addr(rtwdev, low_power);
  1307. }
  1308. static void rtw89_pci_ops_write32(struct rtw89_dev *rtwdev, u32 addr, u32 data);
  1309. static u32 rtw89_pci_ops_read32_cmac(struct rtw89_dev *rtwdev, u32 addr)
  1310. {
  1311. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1312. u32 val = readl(rtwpci->mmap + addr);
  1313. int count;
  1314. for (count = 0; ; count++) {
  1315. if (val != RTW89_R32_DEAD)
  1316. return val;
  1317. if (count >= MAC_REG_POOL_COUNT) {
  1318. rtw89_warn(rtwdev, "addr %#x = %#x\n", addr, val);
  1319. return RTW89_R32_DEAD;
  1320. }
  1321. rtw89_pci_ops_write32(rtwdev, R_AX_CK_EN, B_AX_CMAC_ALLCKEN);
  1322. val = readl(rtwpci->mmap + addr);
  1323. }
  1324. return val;
  1325. }
  1326. static u8 rtw89_pci_ops_read8(struct rtw89_dev *rtwdev, u32 addr)
  1327. {
  1328. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1329. u32 addr32, val32, shift;
  1330. if (!ACCESS_CMAC(addr))
  1331. return readb(rtwpci->mmap + addr);
  1332. addr32 = addr & ~0x3;
  1333. shift = (addr & 0x3) * 8;
  1334. val32 = rtw89_pci_ops_read32_cmac(rtwdev, addr32);
  1335. return val32 >> shift;
  1336. }
  1337. static u16 rtw89_pci_ops_read16(struct rtw89_dev *rtwdev, u32 addr)
  1338. {
  1339. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1340. u32 addr32, val32, shift;
  1341. if (!ACCESS_CMAC(addr))
  1342. return readw(rtwpci->mmap + addr);
  1343. addr32 = addr & ~0x3;
  1344. shift = (addr & 0x3) * 8;
  1345. val32 = rtw89_pci_ops_read32_cmac(rtwdev, addr32);
  1346. return val32 >> shift;
  1347. }
  1348. static u32 rtw89_pci_ops_read32(struct rtw89_dev *rtwdev, u32 addr)
  1349. {
  1350. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1351. if (!ACCESS_CMAC(addr))
  1352. return readl(rtwpci->mmap + addr);
  1353. return rtw89_pci_ops_read32_cmac(rtwdev, addr);
  1354. }
  1355. static void rtw89_pci_ops_write8(struct rtw89_dev *rtwdev, u32 addr, u8 data)
  1356. {
  1357. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1358. writeb(data, rtwpci->mmap + addr);
  1359. }
  1360. static void rtw89_pci_ops_write16(struct rtw89_dev *rtwdev, u32 addr, u16 data)
  1361. {
  1362. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1363. writew(data, rtwpci->mmap + addr);
  1364. }
  1365. static void rtw89_pci_ops_write32(struct rtw89_dev *rtwdev, u32 addr, u32 data)
  1366. {
  1367. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1368. writel(data, rtwpci->mmap + addr);
  1369. }
  1370. static void rtw89_pci_ctrl_dma_trx(struct rtw89_dev *rtwdev, bool enable)
  1371. {
  1372. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1373. if (enable)
  1374. rtw89_write32_set(rtwdev, info->init_cfg_reg,
  1375. info->rxhci_en_bit | info->txhci_en_bit);
  1376. else
  1377. rtw89_write32_clr(rtwdev, info->init_cfg_reg,
  1378. info->rxhci_en_bit | info->txhci_en_bit);
  1379. }
  1380. static void rtw89_pci_ctrl_dma_io(struct rtw89_dev *rtwdev, bool enable)
  1381. {
  1382. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1383. u32 reg, mask;
  1384. if (chip_id == RTL8852C) {
  1385. reg = R_AX_HAXI_INIT_CFG1;
  1386. mask = B_AX_STOP_AXI_MST;
  1387. } else {
  1388. reg = R_AX_PCIE_DMA_STOP1;
  1389. mask = B_AX_STOP_PCIEIO;
  1390. }
  1391. if (enable)
  1392. rtw89_write32_clr(rtwdev, reg, mask);
  1393. else
  1394. rtw89_write32_set(rtwdev, reg, mask);
  1395. }
  1396. static void rtw89_pci_ctrl_dma_all(struct rtw89_dev *rtwdev, bool enable)
  1397. {
  1398. rtw89_pci_ctrl_dma_io(rtwdev, enable);
  1399. rtw89_pci_ctrl_dma_trx(rtwdev, enable);
  1400. }
  1401. static int rtw89_pci_check_mdio(struct rtw89_dev *rtwdev, u8 addr, u8 speed, u16 rw_bit)
  1402. {
  1403. u16 val;
  1404. rtw89_write8(rtwdev, R_AX_MDIO_CFG, addr & 0x1F);
  1405. val = rtw89_read16(rtwdev, R_AX_MDIO_CFG);
  1406. switch (speed) {
  1407. case PCIE_PHY_GEN1:
  1408. if (addr < 0x20)
  1409. val = u16_replace_bits(val, MDIO_PG0_G1, B_AX_MDIO_PHY_ADDR_MASK);
  1410. else
  1411. val = u16_replace_bits(val, MDIO_PG1_G1, B_AX_MDIO_PHY_ADDR_MASK);
  1412. break;
  1413. case PCIE_PHY_GEN2:
  1414. if (addr < 0x20)
  1415. val = u16_replace_bits(val, MDIO_PG0_G2, B_AX_MDIO_PHY_ADDR_MASK);
  1416. else
  1417. val = u16_replace_bits(val, MDIO_PG1_G2, B_AX_MDIO_PHY_ADDR_MASK);
  1418. break;
  1419. default:
  1420. rtw89_err(rtwdev, "[ERR]Error Speed %d!\n", speed);
  1421. return -EINVAL;
  1422. }
  1423. rtw89_write16(rtwdev, R_AX_MDIO_CFG, val);
  1424. rtw89_write16_set(rtwdev, R_AX_MDIO_CFG, rw_bit);
  1425. return read_poll_timeout(rtw89_read16, val, !(val & rw_bit), 10, 2000,
  1426. false, rtwdev, R_AX_MDIO_CFG);
  1427. }
  1428. static int
  1429. rtw89_read16_mdio(struct rtw89_dev *rtwdev, u8 addr, u8 speed, u16 *val)
  1430. {
  1431. int ret;
  1432. ret = rtw89_pci_check_mdio(rtwdev, addr, speed, B_AX_MDIO_RFLAG);
  1433. if (ret) {
  1434. rtw89_err(rtwdev, "[ERR]MDIO R16 0x%X fail ret=%d!\n", addr, ret);
  1435. return ret;
  1436. }
  1437. *val = rtw89_read16(rtwdev, R_AX_MDIO_RDATA);
  1438. return 0;
  1439. }
  1440. static int
  1441. rtw89_write16_mdio(struct rtw89_dev *rtwdev, u8 addr, u16 data, u8 speed)
  1442. {
  1443. int ret;
  1444. rtw89_write16(rtwdev, R_AX_MDIO_WDATA, data);
  1445. ret = rtw89_pci_check_mdio(rtwdev, addr, speed, B_AX_MDIO_WFLAG);
  1446. if (ret) {
  1447. rtw89_err(rtwdev, "[ERR]MDIO W16 0x%X = %x fail ret=%d!\n", addr, data, ret);
  1448. return ret;
  1449. }
  1450. return 0;
  1451. }
  1452. static int
  1453. rtw89_write16_mdio_mask(struct rtw89_dev *rtwdev, u8 addr, u16 mask, u16 data, u8 speed)
  1454. {
  1455. u32 shift;
  1456. int ret;
  1457. u16 val;
  1458. ret = rtw89_read16_mdio(rtwdev, addr, speed, &val);
  1459. if (ret)
  1460. return ret;
  1461. shift = __ffs(mask);
  1462. val &= ~mask;
  1463. val |= ((data << shift) & mask);
  1464. ret = rtw89_write16_mdio(rtwdev, addr, val, speed);
  1465. if (ret)
  1466. return ret;
  1467. return 0;
  1468. }
  1469. static int rtw89_write16_mdio_set(struct rtw89_dev *rtwdev, u8 addr, u16 mask, u8 speed)
  1470. {
  1471. int ret;
  1472. u16 val;
  1473. ret = rtw89_read16_mdio(rtwdev, addr, speed, &val);
  1474. if (ret)
  1475. return ret;
  1476. ret = rtw89_write16_mdio(rtwdev, addr, val | mask, speed);
  1477. if (ret)
  1478. return ret;
  1479. return 0;
  1480. }
  1481. static int rtw89_write16_mdio_clr(struct rtw89_dev *rtwdev, u8 addr, u16 mask, u8 speed)
  1482. {
  1483. int ret;
  1484. u16 val;
  1485. ret = rtw89_read16_mdio(rtwdev, addr, speed, &val);
  1486. if (ret)
  1487. return ret;
  1488. ret = rtw89_write16_mdio(rtwdev, addr, val & ~mask, speed);
  1489. if (ret)
  1490. return ret;
  1491. return 0;
  1492. }
  1493. static int rtw89_pci_write_config_byte(struct rtw89_dev *rtwdev, u16 addr,
  1494. u8 data)
  1495. {
  1496. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1497. struct pci_dev *pdev = rtwpci->pdev;
  1498. return pci_write_config_byte(pdev, addr, data);
  1499. }
  1500. static int rtw89_pci_read_config_byte(struct rtw89_dev *rtwdev, u16 addr,
  1501. u8 *value)
  1502. {
  1503. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  1504. struct pci_dev *pdev = rtwpci->pdev;
  1505. return pci_read_config_byte(pdev, addr, value);
  1506. }
  1507. static int rtw89_pci_config_byte_set(struct rtw89_dev *rtwdev, u16 addr,
  1508. u8 bit)
  1509. {
  1510. u8 value;
  1511. int ret;
  1512. ret = rtw89_pci_read_config_byte(rtwdev, addr, &value);
  1513. if (ret)
  1514. return ret;
  1515. value |= bit;
  1516. ret = rtw89_pci_write_config_byte(rtwdev, addr, value);
  1517. return ret;
  1518. }
  1519. static int rtw89_pci_config_byte_clr(struct rtw89_dev *rtwdev, u16 addr,
  1520. u8 bit)
  1521. {
  1522. u8 value;
  1523. int ret;
  1524. ret = rtw89_pci_read_config_byte(rtwdev, addr, &value);
  1525. if (ret)
  1526. return ret;
  1527. value &= ~bit;
  1528. ret = rtw89_pci_write_config_byte(rtwdev, addr, value);
  1529. return ret;
  1530. }
  1531. static int
  1532. __get_target(struct rtw89_dev *rtwdev, u16 *target, enum rtw89_pcie_phy phy_rate)
  1533. {
  1534. u16 val, tar;
  1535. int ret;
  1536. /* Enable counter */
  1537. ret = rtw89_read16_mdio(rtwdev, RAC_CTRL_PPR_V1, phy_rate, &val);
  1538. if (ret)
  1539. return ret;
  1540. ret = rtw89_write16_mdio(rtwdev, RAC_CTRL_PPR_V1, val & ~B_AX_CLK_CALIB_EN,
  1541. phy_rate);
  1542. if (ret)
  1543. return ret;
  1544. ret = rtw89_write16_mdio(rtwdev, RAC_CTRL_PPR_V1, val | B_AX_CLK_CALIB_EN,
  1545. phy_rate);
  1546. if (ret)
  1547. return ret;
  1548. fsleep(300);
  1549. ret = rtw89_read16_mdio(rtwdev, RAC_CTRL_PPR_V1, phy_rate, &tar);
  1550. if (ret)
  1551. return ret;
  1552. ret = rtw89_write16_mdio(rtwdev, RAC_CTRL_PPR_V1, val & ~B_AX_CLK_CALIB_EN,
  1553. phy_rate);
  1554. if (ret)
  1555. return ret;
  1556. tar = tar & 0x0FFF;
  1557. if (tar == 0 || tar == 0x0FFF) {
  1558. rtw89_err(rtwdev, "[ERR]Get target failed.\n");
  1559. return -EINVAL;
  1560. }
  1561. *target = tar;
  1562. return 0;
  1563. }
  1564. static int rtw89_pci_autok_x(struct rtw89_dev *rtwdev)
  1565. {
  1566. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1567. int ret;
  1568. if (chip_id != RTL8852B && chip_id != RTL8851B)
  1569. return 0;
  1570. ret = rtw89_write16_mdio_mask(rtwdev, RAC_REG_FLD_0, BAC_AUTOK_N_MASK,
  1571. PCIE_AUTOK_4, PCIE_PHY_GEN1);
  1572. return ret;
  1573. }
  1574. static int rtw89_pci_auto_refclk_cal(struct rtw89_dev *rtwdev, bool autook_en)
  1575. {
  1576. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1577. enum rtw89_pcie_phy phy_rate;
  1578. u16 val16, mgn_set, div_set, tar;
  1579. u8 val8, bdr_ori;
  1580. bool l1_flag = false;
  1581. int ret = 0;
  1582. if (chip_id != RTL8852B && chip_id != RTL8851B)
  1583. return 0;
  1584. ret = rtw89_pci_read_config_byte(rtwdev, RTW89_PCIE_PHY_RATE, &val8);
  1585. if (ret) {
  1586. rtw89_err(rtwdev, "[ERR]pci config read %X\n",
  1587. RTW89_PCIE_PHY_RATE);
  1588. return ret;
  1589. }
  1590. if (FIELD_GET(RTW89_PCIE_PHY_RATE_MASK, val8) == 0x1) {
  1591. phy_rate = PCIE_PHY_GEN1;
  1592. } else if (FIELD_GET(RTW89_PCIE_PHY_RATE_MASK, val8) == 0x2) {
  1593. phy_rate = PCIE_PHY_GEN2;
  1594. } else {
  1595. rtw89_err(rtwdev, "[ERR]PCIe PHY rate %#x not support\n", val8);
  1596. return -EOPNOTSUPP;
  1597. }
  1598. /* Disable L1BD */
  1599. ret = rtw89_pci_read_config_byte(rtwdev, RTW89_PCIE_L1_CTRL, &bdr_ori);
  1600. if (ret) {
  1601. rtw89_err(rtwdev, "[ERR]pci config read %X\n", RTW89_PCIE_L1_CTRL);
  1602. return ret;
  1603. }
  1604. if (bdr_ori & RTW89_PCIE_BIT_L1) {
  1605. ret = rtw89_pci_write_config_byte(rtwdev, RTW89_PCIE_L1_CTRL,
  1606. bdr_ori & ~RTW89_PCIE_BIT_L1);
  1607. if (ret) {
  1608. rtw89_err(rtwdev, "[ERR]pci config write %X\n",
  1609. RTW89_PCIE_L1_CTRL);
  1610. return ret;
  1611. }
  1612. l1_flag = true;
  1613. }
  1614. ret = rtw89_read16_mdio(rtwdev, RAC_CTRL_PPR_V1, phy_rate, &val16);
  1615. if (ret) {
  1616. rtw89_err(rtwdev, "[ERR]mdio_r16_pcie %X\n", RAC_CTRL_PPR_V1);
  1617. goto end;
  1618. }
  1619. if (val16 & B_AX_CALIB_EN) {
  1620. ret = rtw89_write16_mdio(rtwdev, RAC_CTRL_PPR_V1,
  1621. val16 & ~B_AX_CALIB_EN, phy_rate);
  1622. if (ret) {
  1623. rtw89_err(rtwdev, "[ERR]mdio_w16_pcie %X\n", RAC_CTRL_PPR_V1);
  1624. goto end;
  1625. }
  1626. }
  1627. if (!autook_en)
  1628. goto end;
  1629. /* Set div */
  1630. ret = rtw89_write16_mdio_clr(rtwdev, RAC_CTRL_PPR_V1, B_AX_DIV, phy_rate);
  1631. if (ret) {
  1632. rtw89_err(rtwdev, "[ERR]mdio_w16_pcie %X\n", RAC_CTRL_PPR_V1);
  1633. goto end;
  1634. }
  1635. /* Obtain div and margin */
  1636. ret = __get_target(rtwdev, &tar, phy_rate);
  1637. if (ret) {
  1638. rtw89_err(rtwdev, "[ERR]1st get target fail %d\n", ret);
  1639. goto end;
  1640. }
  1641. mgn_set = tar * INTF_INTGRA_HOSTREF_V1 / INTF_INTGRA_MINREF_V1 - tar;
  1642. if (mgn_set >= 128) {
  1643. div_set = 0x0003;
  1644. mgn_set = 0x000F;
  1645. } else if (mgn_set >= 64) {
  1646. div_set = 0x0003;
  1647. mgn_set >>= 3;
  1648. } else if (mgn_set >= 32) {
  1649. div_set = 0x0002;
  1650. mgn_set >>= 2;
  1651. } else if (mgn_set >= 16) {
  1652. div_set = 0x0001;
  1653. mgn_set >>= 1;
  1654. } else if (mgn_set == 0) {
  1655. rtw89_err(rtwdev, "[ERR]cal mgn is 0,tar = %d\n", tar);
  1656. goto end;
  1657. } else {
  1658. div_set = 0x0000;
  1659. }
  1660. ret = rtw89_read16_mdio(rtwdev, RAC_CTRL_PPR_V1, phy_rate, &val16);
  1661. if (ret) {
  1662. rtw89_err(rtwdev, "[ERR]mdio_r16_pcie %X\n", RAC_CTRL_PPR_V1);
  1663. goto end;
  1664. }
  1665. val16 |= u16_encode_bits(div_set, B_AX_DIV);
  1666. ret = rtw89_write16_mdio(rtwdev, RAC_CTRL_PPR_V1, val16, phy_rate);
  1667. if (ret) {
  1668. rtw89_err(rtwdev, "[ERR]mdio_w16_pcie %X\n", RAC_CTRL_PPR_V1);
  1669. goto end;
  1670. }
  1671. ret = __get_target(rtwdev, &tar, phy_rate);
  1672. if (ret) {
  1673. rtw89_err(rtwdev, "[ERR]2nd get target fail %d\n", ret);
  1674. goto end;
  1675. }
  1676. rtw89_debug(rtwdev, RTW89_DBG_HCI, "[TRACE]target = 0x%X, div = 0x%X, margin = 0x%X\n",
  1677. tar, div_set, mgn_set);
  1678. ret = rtw89_write16_mdio(rtwdev, RAC_SET_PPR_V1,
  1679. (tar & 0x0FFF) | (mgn_set << 12), phy_rate);
  1680. if (ret) {
  1681. rtw89_err(rtwdev, "[ERR]mdio_w16_pcie %X\n", RAC_SET_PPR_V1);
  1682. goto end;
  1683. }
  1684. /* Enable function */
  1685. ret = rtw89_write16_mdio_set(rtwdev, RAC_CTRL_PPR_V1, B_AX_CALIB_EN, phy_rate);
  1686. if (ret) {
  1687. rtw89_err(rtwdev, "[ERR]mdio_w16_pcie %X\n", RAC_CTRL_PPR_V1);
  1688. goto end;
  1689. }
  1690. /* CLK delay = 0 */
  1691. ret = rtw89_pci_write_config_byte(rtwdev, RTW89_PCIE_CLK_CTRL,
  1692. PCIE_CLKDLY_HW_0);
  1693. end:
  1694. /* Set L1BD to ori */
  1695. if (l1_flag) {
  1696. ret = rtw89_pci_write_config_byte(rtwdev, RTW89_PCIE_L1_CTRL,
  1697. bdr_ori);
  1698. if (ret) {
  1699. rtw89_err(rtwdev, "[ERR]pci config write %X\n",
  1700. RTW89_PCIE_L1_CTRL);
  1701. return ret;
  1702. }
  1703. }
  1704. return ret;
  1705. }
  1706. static int rtw89_pci_deglitch_setting(struct rtw89_dev *rtwdev)
  1707. {
  1708. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1709. int ret;
  1710. if (chip_id == RTL8852A) {
  1711. ret = rtw89_write16_mdio_clr(rtwdev, RAC_ANA24, B_AX_DEGLITCH,
  1712. PCIE_PHY_GEN1);
  1713. if (ret)
  1714. return ret;
  1715. ret = rtw89_write16_mdio_clr(rtwdev, RAC_ANA24, B_AX_DEGLITCH,
  1716. PCIE_PHY_GEN2);
  1717. if (ret)
  1718. return ret;
  1719. } else if (chip_id == RTL8852C) {
  1720. rtw89_write16_clr(rtwdev, R_RAC_DIRECT_OFFSET_G1 + RAC_ANA24 * 2,
  1721. B_AX_DEGLITCH);
  1722. rtw89_write16_clr(rtwdev, R_RAC_DIRECT_OFFSET_G2 + RAC_ANA24 * 2,
  1723. B_AX_DEGLITCH);
  1724. }
  1725. return 0;
  1726. }
  1727. static void rtw89_pci_rxdma_prefth(struct rtw89_dev *rtwdev)
  1728. {
  1729. if (rtwdev->chip->chip_id != RTL8852A)
  1730. return;
  1731. rtw89_write32_set(rtwdev, R_AX_PCIE_INIT_CFG1, B_AX_DIS_RXDMA_PRE);
  1732. }
  1733. static void rtw89_pci_l1off_pwroff(struct rtw89_dev *rtwdev)
  1734. {
  1735. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1736. if (chip_id != RTL8852A && chip_id != RTL8852B && chip_id != RTL8851B)
  1737. return;
  1738. rtw89_write32_clr(rtwdev, R_AX_PCIE_PS_CTRL, B_AX_L1OFF_PWR_OFF_EN);
  1739. }
  1740. static u32 rtw89_pci_l2_rxen_lat(struct rtw89_dev *rtwdev)
  1741. {
  1742. int ret;
  1743. if (rtwdev->chip->chip_id != RTL8852A)
  1744. return 0;
  1745. ret = rtw89_write16_mdio_clr(rtwdev, RAC_ANA26, B_AX_RXEN,
  1746. PCIE_PHY_GEN1);
  1747. if (ret)
  1748. return ret;
  1749. ret = rtw89_write16_mdio_clr(rtwdev, RAC_ANA26, B_AX_RXEN,
  1750. PCIE_PHY_GEN2);
  1751. if (ret)
  1752. return ret;
  1753. return 0;
  1754. }
  1755. static void rtw89_pci_aphy_pwrcut(struct rtw89_dev *rtwdev)
  1756. {
  1757. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1758. if (chip_id != RTL8852A && chip_id != RTL8852B && chip_id != RTL8851B)
  1759. return;
  1760. rtw89_write32_clr(rtwdev, R_AX_SYS_PW_CTRL, B_AX_PSUS_OFF_CAPC_EN);
  1761. }
  1762. static void rtw89_pci_hci_ldo(struct rtw89_dev *rtwdev)
  1763. {
  1764. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1765. if (chip_id == RTL8852A || chip_id == RTL8852B || chip_id == RTL8851B) {
  1766. rtw89_write32_set(rtwdev, R_AX_SYS_SDIO_CTRL,
  1767. B_AX_PCIE_DIS_L2_CTRL_LDO_HCI);
  1768. rtw89_write32_clr(rtwdev, R_AX_SYS_SDIO_CTRL,
  1769. B_AX_PCIE_DIS_WLSUS_AFT_PDN);
  1770. } else if (rtwdev->chip->chip_id == RTL8852C) {
  1771. rtw89_write32_clr(rtwdev, R_AX_SYS_SDIO_CTRL,
  1772. B_AX_PCIE_DIS_L2_CTRL_LDO_HCI);
  1773. }
  1774. }
  1775. static int rtw89_pci_dphy_delay(struct rtw89_dev *rtwdev)
  1776. {
  1777. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1778. if (chip_id != RTL8852B && chip_id != RTL8851B)
  1779. return 0;
  1780. return rtw89_write16_mdio_mask(rtwdev, RAC_REG_REV2, BAC_CMU_EN_DLY_MASK,
  1781. PCIE_DPHY_DLY_25US, PCIE_PHY_GEN1);
  1782. }
  1783. static void rtw89_pci_power_wake(struct rtw89_dev *rtwdev, bool pwr_up)
  1784. {
  1785. if (pwr_up)
  1786. rtw89_write32_set(rtwdev, R_AX_HCI_OPT_CTRL, BIT_WAKE_CTRL);
  1787. else
  1788. rtw89_write32_clr(rtwdev, R_AX_HCI_OPT_CTRL, BIT_WAKE_CTRL);
  1789. }
  1790. static void rtw89_pci_autoload_hang(struct rtw89_dev *rtwdev)
  1791. {
  1792. if (rtwdev->chip->chip_id != RTL8852C)
  1793. return;
  1794. rtw89_write32_set(rtwdev, R_AX_PCIE_BG_CLR, B_AX_BG_CLR_ASYNC_M3);
  1795. rtw89_write32_clr(rtwdev, R_AX_PCIE_BG_CLR, B_AX_BG_CLR_ASYNC_M3);
  1796. }
  1797. static void rtw89_pci_l12_vmain(struct rtw89_dev *rtwdev)
  1798. {
  1799. if (!(rtwdev->chip->chip_id == RTL8852C && rtwdev->hal.cv == CHIP_CAV))
  1800. return;
  1801. rtw89_write32_set(rtwdev, R_AX_SYS_SDIO_CTRL, B_AX_PCIE_FORCE_PWR_NGAT);
  1802. }
  1803. static void rtw89_pci_gen2_force_ib(struct rtw89_dev *rtwdev)
  1804. {
  1805. if (!(rtwdev->chip->chip_id == RTL8852C && rtwdev->hal.cv == CHIP_CAV))
  1806. return;
  1807. rtw89_write32_set(rtwdev, R_AX_PMC_DBG_CTRL2,
  1808. B_AX_SYSON_DIS_PMCR_AX_WRMSK);
  1809. rtw89_write32_set(rtwdev, R_AX_HCI_BG_CTRL, B_AX_BG_CLR_ASYNC_M3);
  1810. rtw89_write32_clr(rtwdev, R_AX_PMC_DBG_CTRL2,
  1811. B_AX_SYSON_DIS_PMCR_AX_WRMSK);
  1812. }
  1813. static void rtw89_pci_l1_ent_lat(struct rtw89_dev *rtwdev)
  1814. {
  1815. if (rtwdev->chip->chip_id != RTL8852C)
  1816. return;
  1817. rtw89_write32_clr(rtwdev, R_AX_PCIE_PS_CTRL_V1, B_AX_SEL_REQ_ENTR_L1);
  1818. }
  1819. static void rtw89_pci_wd_exit_l1(struct rtw89_dev *rtwdev)
  1820. {
  1821. if (rtwdev->chip->chip_id != RTL8852C)
  1822. return;
  1823. rtw89_write32_set(rtwdev, R_AX_PCIE_PS_CTRL_V1, B_AX_DMAC0_EXIT_L1_EN);
  1824. }
  1825. static void rtw89_pci_set_sic(struct rtw89_dev *rtwdev)
  1826. {
  1827. if (rtwdev->chip->chip_id == RTL8852C)
  1828. return;
  1829. rtw89_write32_clr(rtwdev, R_AX_PCIE_EXP_CTRL,
  1830. B_AX_SIC_EN_FORCE_CLKREQ);
  1831. }
  1832. static void rtw89_pci_set_lbc(struct rtw89_dev *rtwdev)
  1833. {
  1834. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1835. u32 lbc;
  1836. if (rtwdev->chip->chip_id == RTL8852C)
  1837. return;
  1838. lbc = rtw89_read32(rtwdev, R_AX_LBC_WATCHDOG);
  1839. if (info->lbc_en == MAC_AX_PCIE_ENABLE) {
  1840. lbc = u32_replace_bits(lbc, info->lbc_tmr, B_AX_LBC_TIMER);
  1841. lbc |= B_AX_LBC_FLAG | B_AX_LBC_EN;
  1842. rtw89_write32(rtwdev, R_AX_LBC_WATCHDOG, lbc);
  1843. } else {
  1844. lbc &= ~B_AX_LBC_EN;
  1845. }
  1846. rtw89_write32_set(rtwdev, R_AX_LBC_WATCHDOG, lbc);
  1847. }
  1848. static void rtw89_pci_set_io_rcy(struct rtw89_dev *rtwdev)
  1849. {
  1850. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1851. u32 val32;
  1852. if (rtwdev->chip->chip_id != RTL8852C)
  1853. return;
  1854. if (info->io_rcy_en == MAC_AX_PCIE_ENABLE) {
  1855. val32 = FIELD_PREP(B_AX_PCIE_WDT_TIMER_M1_MASK,
  1856. info->io_rcy_tmr);
  1857. rtw89_write32(rtwdev, R_AX_PCIE_WDT_TIMER_M1, val32);
  1858. rtw89_write32(rtwdev, R_AX_PCIE_WDT_TIMER_M2, val32);
  1859. rtw89_write32(rtwdev, R_AX_PCIE_WDT_TIMER_E0, val32);
  1860. rtw89_write32_set(rtwdev, R_AX_PCIE_IO_RCY_M1, B_AX_PCIE_IO_RCY_WDT_MODE_M1);
  1861. rtw89_write32_set(rtwdev, R_AX_PCIE_IO_RCY_M2, B_AX_PCIE_IO_RCY_WDT_MODE_M2);
  1862. rtw89_write32_set(rtwdev, R_AX_PCIE_IO_RCY_E0, B_AX_PCIE_IO_RCY_WDT_MODE_E0);
  1863. } else {
  1864. rtw89_write32_clr(rtwdev, R_AX_PCIE_IO_RCY_M1, B_AX_PCIE_IO_RCY_WDT_MODE_M1);
  1865. rtw89_write32_clr(rtwdev, R_AX_PCIE_IO_RCY_M2, B_AX_PCIE_IO_RCY_WDT_MODE_M2);
  1866. rtw89_write32_clr(rtwdev, R_AX_PCIE_IO_RCY_E0, B_AX_PCIE_IO_RCY_WDT_MODE_E0);
  1867. }
  1868. rtw89_write32_clr(rtwdev, R_AX_PCIE_IO_RCY_S1, B_AX_PCIE_IO_RCY_WDT_MODE_S1);
  1869. }
  1870. static void rtw89_pci_set_dbg(struct rtw89_dev *rtwdev)
  1871. {
  1872. if (rtwdev->chip->chip_id == RTL8852C)
  1873. return;
  1874. rtw89_write32_set(rtwdev, R_AX_PCIE_DBG_CTRL,
  1875. B_AX_ASFF_FULL_NO_STK | B_AX_EN_STUCK_DBG);
  1876. if (rtwdev->chip->chip_id == RTL8852A)
  1877. rtw89_write32_set(rtwdev, R_AX_PCIE_EXP_CTRL,
  1878. B_AX_EN_CHKDSC_NO_RX_STUCK);
  1879. }
  1880. static void rtw89_pci_set_keep_reg(struct rtw89_dev *rtwdev)
  1881. {
  1882. if (rtwdev->chip->chip_id == RTL8852C)
  1883. return;
  1884. rtw89_write32_set(rtwdev, R_AX_PCIE_INIT_CFG1,
  1885. B_AX_PCIE_TXRST_KEEP_REG | B_AX_PCIE_RXRST_KEEP_REG);
  1886. }
  1887. static void rtw89_pci_clr_idx_all(struct rtw89_dev *rtwdev)
  1888. {
  1889. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1890. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1891. u32 val = B_AX_CLR_ACH0_IDX | B_AX_CLR_ACH1_IDX | B_AX_CLR_ACH2_IDX |
  1892. B_AX_CLR_ACH3_IDX | B_AX_CLR_CH8_IDX | B_AX_CLR_CH9_IDX |
  1893. B_AX_CLR_CH12_IDX;
  1894. u32 rxbd_rwptr_clr = info->rxbd_rwptr_clr_reg;
  1895. u32 txbd_rwptr_clr2 = info->txbd_rwptr_clr2_reg;
  1896. if (chip_id == RTL8852A || chip_id == RTL8852C)
  1897. val |= B_AX_CLR_ACH4_IDX | B_AX_CLR_ACH5_IDX |
  1898. B_AX_CLR_ACH6_IDX | B_AX_CLR_ACH7_IDX;
  1899. /* clear DMA indexes */
  1900. rtw89_write32_set(rtwdev, R_AX_TXBD_RWPTR_CLR1, val);
  1901. if (chip_id == RTL8852A || chip_id == RTL8852C)
  1902. rtw89_write32_set(rtwdev, txbd_rwptr_clr2,
  1903. B_AX_CLR_CH10_IDX | B_AX_CLR_CH11_IDX);
  1904. rtw89_write32_set(rtwdev, rxbd_rwptr_clr,
  1905. B_AX_CLR_RXQ_IDX | B_AX_CLR_RPQ_IDX);
  1906. }
  1907. static int rtw89_poll_txdma_ch_idle_pcie(struct rtw89_dev *rtwdev)
  1908. {
  1909. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1910. u32 ret, check, dma_busy;
  1911. u32 dma_busy1 = info->dma_busy1.addr;
  1912. u32 dma_busy2 = info->dma_busy2_reg;
  1913. check = info->dma_busy1.mask;
  1914. ret = read_poll_timeout(rtw89_read32, dma_busy, (dma_busy & check) == 0,
  1915. 10, 100, false, rtwdev, dma_busy1);
  1916. if (ret)
  1917. return ret;
  1918. if (!dma_busy2)
  1919. return 0;
  1920. check = B_AX_CH10_BUSY | B_AX_CH11_BUSY;
  1921. ret = read_poll_timeout(rtw89_read32, dma_busy, (dma_busy & check) == 0,
  1922. 10, 100, false, rtwdev, dma_busy2);
  1923. if (ret)
  1924. return ret;
  1925. return 0;
  1926. }
  1927. static int rtw89_poll_rxdma_ch_idle_pcie(struct rtw89_dev *rtwdev)
  1928. {
  1929. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1930. u32 ret, check, dma_busy;
  1931. u32 dma_busy3 = info->dma_busy3_reg;
  1932. check = B_AX_RXQ_BUSY | B_AX_RPQ_BUSY;
  1933. ret = read_poll_timeout(rtw89_read32, dma_busy, (dma_busy & check) == 0,
  1934. 10, 100, false, rtwdev, dma_busy3);
  1935. if (ret)
  1936. return ret;
  1937. return 0;
  1938. }
  1939. static int rtw89_pci_poll_dma_all_idle(struct rtw89_dev *rtwdev)
  1940. {
  1941. u32 ret;
  1942. ret = rtw89_poll_txdma_ch_idle_pcie(rtwdev);
  1943. if (ret) {
  1944. rtw89_err(rtwdev, "txdma ch busy\n");
  1945. return ret;
  1946. }
  1947. ret = rtw89_poll_rxdma_ch_idle_pcie(rtwdev);
  1948. if (ret) {
  1949. rtw89_err(rtwdev, "rxdma ch busy\n");
  1950. return ret;
  1951. }
  1952. return 0;
  1953. }
  1954. static int rtw89_pci_mode_op(struct rtw89_dev *rtwdev)
  1955. {
  1956. const struct rtw89_pci_info *info = rtwdev->pci_info;
  1957. enum mac_ax_bd_trunc_mode txbd_trunc_mode = info->txbd_trunc_mode;
  1958. enum mac_ax_bd_trunc_mode rxbd_trunc_mode = info->rxbd_trunc_mode;
  1959. enum mac_ax_rxbd_mode rxbd_mode = info->rxbd_mode;
  1960. enum mac_ax_tag_mode tag_mode = info->tag_mode;
  1961. enum mac_ax_wd_dma_intvl wd_dma_idle_intvl = info->wd_dma_idle_intvl;
  1962. enum mac_ax_wd_dma_intvl wd_dma_act_intvl = info->wd_dma_act_intvl;
  1963. enum mac_ax_tx_burst tx_burst = info->tx_burst;
  1964. enum mac_ax_rx_burst rx_burst = info->rx_burst;
  1965. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  1966. u8 cv = rtwdev->hal.cv;
  1967. u32 val32;
  1968. if (txbd_trunc_mode == MAC_AX_BD_TRUNC) {
  1969. if (chip_id == RTL8852A && cv == CHIP_CBV)
  1970. rtw89_write32_set(rtwdev, R_AX_PCIE_INIT_CFG1, B_AX_TX_TRUNC_MODE);
  1971. } else if (txbd_trunc_mode == MAC_AX_BD_NORM) {
  1972. if (chip_id == RTL8852A || chip_id == RTL8852B)
  1973. rtw89_write32_clr(rtwdev, R_AX_PCIE_INIT_CFG1, B_AX_TX_TRUNC_MODE);
  1974. }
  1975. if (rxbd_trunc_mode == MAC_AX_BD_TRUNC) {
  1976. if (chip_id == RTL8852A && cv == CHIP_CBV)
  1977. rtw89_write32_set(rtwdev, R_AX_PCIE_INIT_CFG1, B_AX_RX_TRUNC_MODE);
  1978. } else if (rxbd_trunc_mode == MAC_AX_BD_NORM) {
  1979. if (chip_id == RTL8852A || chip_id == RTL8852B)
  1980. rtw89_write32_clr(rtwdev, R_AX_PCIE_INIT_CFG1, B_AX_RX_TRUNC_MODE);
  1981. }
  1982. if (rxbd_mode == MAC_AX_RXBD_PKT) {
  1983. rtw89_write32_clr(rtwdev, info->init_cfg_reg, info->rxbd_mode_bit);
  1984. } else if (rxbd_mode == MAC_AX_RXBD_SEP) {
  1985. rtw89_write32_set(rtwdev, info->init_cfg_reg, info->rxbd_mode_bit);
  1986. if (chip_id == RTL8852A || chip_id == RTL8852B)
  1987. rtw89_write32_mask(rtwdev, R_AX_PCIE_INIT_CFG2,
  1988. B_AX_PCIE_RX_APPLEN_MASK, 0);
  1989. }
  1990. if (chip_id == RTL8852A || chip_id == RTL8852B) {
  1991. rtw89_write32_mask(rtwdev, R_AX_PCIE_INIT_CFG1, B_AX_PCIE_MAX_TXDMA_MASK, tx_burst);
  1992. rtw89_write32_mask(rtwdev, R_AX_PCIE_INIT_CFG1, B_AX_PCIE_MAX_RXDMA_MASK, rx_burst);
  1993. } else if (chip_id == RTL8852C) {
  1994. rtw89_write32_mask(rtwdev, R_AX_HAXI_INIT_CFG1, B_AX_HAXI_MAX_TXDMA_MASK, tx_burst);
  1995. rtw89_write32_mask(rtwdev, R_AX_HAXI_INIT_CFG1, B_AX_HAXI_MAX_RXDMA_MASK, rx_burst);
  1996. }
  1997. if (chip_id == RTL8852A || chip_id == RTL8852B) {
  1998. if (tag_mode == MAC_AX_TAG_SGL) {
  1999. val32 = rtw89_read32(rtwdev, R_AX_PCIE_INIT_CFG1) &
  2000. ~B_AX_LATENCY_CONTROL;
  2001. rtw89_write32(rtwdev, R_AX_PCIE_INIT_CFG1, val32);
  2002. } else if (tag_mode == MAC_AX_TAG_MULTI) {
  2003. val32 = rtw89_read32(rtwdev, R_AX_PCIE_INIT_CFG1) |
  2004. B_AX_LATENCY_CONTROL;
  2005. rtw89_write32(rtwdev, R_AX_PCIE_INIT_CFG1, val32);
  2006. }
  2007. }
  2008. rtw89_write32_mask(rtwdev, info->exp_ctrl_reg, info->max_tag_num_mask,
  2009. info->multi_tag_num);
  2010. if (chip_id == RTL8852A || chip_id == RTL8852B) {
  2011. rtw89_write32_mask(rtwdev, R_AX_PCIE_INIT_CFG2, B_AX_WD_ITVL_IDLE,
  2012. wd_dma_idle_intvl);
  2013. rtw89_write32_mask(rtwdev, R_AX_PCIE_INIT_CFG2, B_AX_WD_ITVL_ACT,
  2014. wd_dma_act_intvl);
  2015. } else if (chip_id == RTL8852C) {
  2016. rtw89_write32_mask(rtwdev, R_AX_HAXI_INIT_CFG1, B_AX_WD_ITVL_IDLE_V1_MASK,
  2017. wd_dma_idle_intvl);
  2018. rtw89_write32_mask(rtwdev, R_AX_HAXI_INIT_CFG1, B_AX_WD_ITVL_ACT_V1_MASK,
  2019. wd_dma_act_intvl);
  2020. }
  2021. if (txbd_trunc_mode == MAC_AX_BD_TRUNC) {
  2022. rtw89_write32_set(rtwdev, R_AX_TX_ADDRESS_INFO_MODE_SETTING,
  2023. B_AX_HOST_ADDR_INFO_8B_SEL);
  2024. rtw89_write32_clr(rtwdev, R_AX_PKTIN_SETTING, B_AX_WD_ADDR_INFO_LENGTH);
  2025. } else if (txbd_trunc_mode == MAC_AX_BD_NORM) {
  2026. rtw89_write32_clr(rtwdev, R_AX_TX_ADDRESS_INFO_MODE_SETTING,
  2027. B_AX_HOST_ADDR_INFO_8B_SEL);
  2028. rtw89_write32_set(rtwdev, R_AX_PKTIN_SETTING, B_AX_WD_ADDR_INFO_LENGTH);
  2029. }
  2030. return 0;
  2031. }
  2032. static int rtw89_pci_ops_deinit(struct rtw89_dev *rtwdev)
  2033. {
  2034. const struct rtw89_pci_info *info = rtwdev->pci_info;
  2035. if (rtwdev->chip->chip_id == RTL8852A) {
  2036. /* ltr sw trigger */
  2037. rtw89_write32_set(rtwdev, R_AX_LTR_CTRL_0, B_AX_APP_LTR_IDLE);
  2038. }
  2039. info->ltr_set(rtwdev, false);
  2040. rtw89_pci_ctrl_dma_all(rtwdev, false);
  2041. rtw89_pci_clr_idx_all(rtwdev);
  2042. return 0;
  2043. }
  2044. static int rtw89_pci_ops_mac_pre_init(struct rtw89_dev *rtwdev)
  2045. {
  2046. const struct rtw89_pci_info *info = rtwdev->pci_info;
  2047. int ret;
  2048. rtw89_pci_rxdma_prefth(rtwdev);
  2049. rtw89_pci_l1off_pwroff(rtwdev);
  2050. rtw89_pci_deglitch_setting(rtwdev);
  2051. ret = rtw89_pci_l2_rxen_lat(rtwdev);
  2052. if (ret) {
  2053. rtw89_err(rtwdev, "[ERR] pcie l2 rxen lat %d\n", ret);
  2054. return ret;
  2055. }
  2056. rtw89_pci_aphy_pwrcut(rtwdev);
  2057. rtw89_pci_hci_ldo(rtwdev);
  2058. rtw89_pci_dphy_delay(rtwdev);
  2059. ret = rtw89_pci_autok_x(rtwdev);
  2060. if (ret) {
  2061. rtw89_err(rtwdev, "[ERR] pcie autok_x fail %d\n", ret);
  2062. return ret;
  2063. }
  2064. ret = rtw89_pci_auto_refclk_cal(rtwdev, false);
  2065. if (ret) {
  2066. rtw89_err(rtwdev, "[ERR] pcie autok fail %d\n", ret);
  2067. return ret;
  2068. }
  2069. rtw89_pci_power_wake(rtwdev, true);
  2070. rtw89_pci_autoload_hang(rtwdev);
  2071. rtw89_pci_l12_vmain(rtwdev);
  2072. rtw89_pci_gen2_force_ib(rtwdev);
  2073. rtw89_pci_l1_ent_lat(rtwdev);
  2074. rtw89_pci_wd_exit_l1(rtwdev);
  2075. rtw89_pci_set_sic(rtwdev);
  2076. rtw89_pci_set_lbc(rtwdev);
  2077. rtw89_pci_set_io_rcy(rtwdev);
  2078. rtw89_pci_set_dbg(rtwdev);
  2079. rtw89_pci_set_keep_reg(rtwdev);
  2080. rtw89_write32_set(rtwdev, info->dma_stop1.addr, B_AX_STOP_WPDMA);
  2081. /* stop DMA activities */
  2082. rtw89_pci_ctrl_dma_all(rtwdev, false);
  2083. ret = rtw89_pci_poll_dma_all_idle(rtwdev);
  2084. if (ret) {
  2085. rtw89_err(rtwdev, "[ERR] poll pcie dma all idle\n");
  2086. return ret;
  2087. }
  2088. rtw89_pci_clr_idx_all(rtwdev);
  2089. rtw89_pci_mode_op(rtwdev);
  2090. /* fill TRX BD indexes */
  2091. rtw89_pci_ops_reset(rtwdev);
  2092. ret = rtw89_pci_rst_bdram_pcie(rtwdev);
  2093. if (ret) {
  2094. rtw89_warn(rtwdev, "reset bdram busy\n");
  2095. return ret;
  2096. }
  2097. /* disable all channels except to FW CMD channel to download firmware */
  2098. rtw89_pci_ctrl_txdma_ch_pcie(rtwdev, false);
  2099. rtw89_pci_ctrl_txdma_fw_ch_pcie(rtwdev, true);
  2100. /* start DMA activities */
  2101. rtw89_pci_ctrl_dma_all(rtwdev, true);
  2102. return 0;
  2103. }
  2104. int rtw89_pci_ltr_set(struct rtw89_dev *rtwdev, bool en)
  2105. {
  2106. u32 val;
  2107. if (!en)
  2108. return 0;
  2109. val = rtw89_read32(rtwdev, R_AX_LTR_CTRL_0);
  2110. if (rtw89_pci_ltr_is_err_reg_val(val))
  2111. return -EINVAL;
  2112. val = rtw89_read32(rtwdev, R_AX_LTR_CTRL_1);
  2113. if (rtw89_pci_ltr_is_err_reg_val(val))
  2114. return -EINVAL;
  2115. val = rtw89_read32(rtwdev, R_AX_LTR_IDLE_LATENCY);
  2116. if (rtw89_pci_ltr_is_err_reg_val(val))
  2117. return -EINVAL;
  2118. val = rtw89_read32(rtwdev, R_AX_LTR_ACTIVE_LATENCY);
  2119. if (rtw89_pci_ltr_is_err_reg_val(val))
  2120. return -EINVAL;
  2121. rtw89_write32_set(rtwdev, R_AX_LTR_CTRL_0, B_AX_LTR_HW_EN | B_AX_LTR_EN |
  2122. B_AX_LTR_WD_NOEMP_CHK);
  2123. rtw89_write32_mask(rtwdev, R_AX_LTR_CTRL_0, B_AX_LTR_SPACE_IDX_MASK,
  2124. PCI_LTR_SPC_500US);
  2125. rtw89_write32_mask(rtwdev, R_AX_LTR_CTRL_0, B_AX_LTR_IDLE_TIMER_IDX_MASK,
  2126. PCI_LTR_IDLE_TIMER_3_2MS);
  2127. rtw89_write32_mask(rtwdev, R_AX_LTR_CTRL_1, B_AX_LTR_RX0_TH_MASK, 0x28);
  2128. rtw89_write32_mask(rtwdev, R_AX_LTR_CTRL_1, B_AX_LTR_RX1_TH_MASK, 0x28);
  2129. rtw89_write32(rtwdev, R_AX_LTR_IDLE_LATENCY, 0x90039003);
  2130. rtw89_write32(rtwdev, R_AX_LTR_ACTIVE_LATENCY, 0x880b880b);
  2131. return 0;
  2132. }
  2133. EXPORT_SYMBOL(rtw89_pci_ltr_set);
  2134. int rtw89_pci_ltr_set_v1(struct rtw89_dev *rtwdev, bool en)
  2135. {
  2136. u32 dec_ctrl;
  2137. u32 val32;
  2138. val32 = rtw89_read32(rtwdev, R_AX_LTR_CTRL_0);
  2139. if (rtw89_pci_ltr_is_err_reg_val(val32))
  2140. return -EINVAL;
  2141. val32 = rtw89_read32(rtwdev, R_AX_LTR_CTRL_1);
  2142. if (rtw89_pci_ltr_is_err_reg_val(val32))
  2143. return -EINVAL;
  2144. dec_ctrl = rtw89_read32(rtwdev, R_AX_LTR_DEC_CTRL);
  2145. if (rtw89_pci_ltr_is_err_reg_val(dec_ctrl))
  2146. return -EINVAL;
  2147. val32 = rtw89_read32(rtwdev, R_AX_LTR_LATENCY_IDX3);
  2148. if (rtw89_pci_ltr_is_err_reg_val(val32))
  2149. return -EINVAL;
  2150. val32 = rtw89_read32(rtwdev, R_AX_LTR_LATENCY_IDX0);
  2151. if (rtw89_pci_ltr_is_err_reg_val(val32))
  2152. return -EINVAL;
  2153. if (!en) {
  2154. dec_ctrl &= ~(LTR_EN_BITS | B_AX_LTR_IDX_DRV_MASK | B_AX_LTR_HW_DEC_EN);
  2155. dec_ctrl |= FIELD_PREP(B_AX_LTR_IDX_DRV_MASK, PCIE_LTR_IDX_IDLE) |
  2156. B_AX_LTR_REQ_DRV;
  2157. } else {
  2158. dec_ctrl |= B_AX_LTR_HW_DEC_EN;
  2159. }
  2160. dec_ctrl &= ~B_AX_LTR_SPACE_IDX_V1_MASK;
  2161. dec_ctrl |= FIELD_PREP(B_AX_LTR_SPACE_IDX_V1_MASK, PCI_LTR_SPC_500US);
  2162. if (en)
  2163. rtw89_write32_set(rtwdev, R_AX_LTR_CTRL_0,
  2164. B_AX_LTR_WD_NOEMP_CHK_V1 | B_AX_LTR_HW_EN);
  2165. rtw89_write32_mask(rtwdev, R_AX_LTR_CTRL_0, B_AX_LTR_IDLE_TIMER_IDX_MASK,
  2166. PCI_LTR_IDLE_TIMER_3_2MS);
  2167. rtw89_write32_mask(rtwdev, R_AX_LTR_CTRL_1, B_AX_LTR_RX0_TH_MASK, 0x28);
  2168. rtw89_write32_mask(rtwdev, R_AX_LTR_CTRL_1, B_AX_LTR_RX1_TH_MASK, 0x28);
  2169. rtw89_write32(rtwdev, R_AX_LTR_DEC_CTRL, dec_ctrl);
  2170. rtw89_write32(rtwdev, R_AX_LTR_LATENCY_IDX3, 0x90039003);
  2171. rtw89_write32(rtwdev, R_AX_LTR_LATENCY_IDX0, 0x880b880b);
  2172. return 0;
  2173. }
  2174. EXPORT_SYMBOL(rtw89_pci_ltr_set_v1);
  2175. static int rtw89_pci_ops_mac_post_init(struct rtw89_dev *rtwdev)
  2176. {
  2177. const struct rtw89_pci_info *info = rtwdev->pci_info;
  2178. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  2179. int ret;
  2180. ret = info->ltr_set(rtwdev, true);
  2181. if (ret) {
  2182. rtw89_err(rtwdev, "pci ltr set fail\n");
  2183. return ret;
  2184. }
  2185. if (chip_id == RTL8852A) {
  2186. /* ltr sw trigger */
  2187. rtw89_write32_set(rtwdev, R_AX_LTR_CTRL_0, B_AX_APP_LTR_ACT);
  2188. }
  2189. if (chip_id == RTL8852A || chip_id == RTL8852B) {
  2190. /* ADDR info 8-byte mode */
  2191. rtw89_write32_set(rtwdev, R_AX_TX_ADDRESS_INFO_MODE_SETTING,
  2192. B_AX_HOST_ADDR_INFO_8B_SEL);
  2193. rtw89_write32_clr(rtwdev, R_AX_PKTIN_SETTING, B_AX_WD_ADDR_INFO_LENGTH);
  2194. }
  2195. /* enable DMA for all queues */
  2196. rtw89_pci_ctrl_txdma_ch_pcie(rtwdev, true);
  2197. /* Release PCI IO */
  2198. rtw89_write32_clr(rtwdev, info->dma_stop1.addr,
  2199. B_AX_STOP_WPDMA | B_AX_STOP_PCIEIO);
  2200. return 0;
  2201. }
  2202. static int rtw89_pci_claim_device(struct rtw89_dev *rtwdev,
  2203. struct pci_dev *pdev)
  2204. {
  2205. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2206. int ret;
  2207. ret = pci_enable_device(pdev);
  2208. if (ret) {
  2209. rtw89_err(rtwdev, "failed to enable pci device\n");
  2210. return ret;
  2211. }
  2212. pci_set_master(pdev);
  2213. pci_set_drvdata(pdev, rtwdev->hw);
  2214. rtwpci->pdev = pdev;
  2215. return 0;
  2216. }
  2217. static void rtw89_pci_declaim_device(struct rtw89_dev *rtwdev,
  2218. struct pci_dev *pdev)
  2219. {
  2220. pci_disable_device(pdev);
  2221. }
  2222. static int rtw89_pci_setup_mapping(struct rtw89_dev *rtwdev,
  2223. struct pci_dev *pdev)
  2224. {
  2225. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2226. unsigned long resource_len;
  2227. u8 bar_id = 2;
  2228. int ret;
  2229. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  2230. if (ret) {
  2231. rtw89_err(rtwdev, "failed to request pci regions\n");
  2232. goto err;
  2233. }
  2234. ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  2235. if (ret) {
  2236. rtw89_err(rtwdev, "failed to set dma mask to 32-bit\n");
  2237. goto err_release_regions;
  2238. }
  2239. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  2240. if (ret) {
  2241. rtw89_err(rtwdev, "failed to set consistent dma mask to 32-bit\n");
  2242. goto err_release_regions;
  2243. }
  2244. resource_len = pci_resource_len(pdev, bar_id);
  2245. rtwpci->mmap = pci_iomap(pdev, bar_id, resource_len);
  2246. if (!rtwpci->mmap) {
  2247. rtw89_err(rtwdev, "failed to map pci io\n");
  2248. ret = -EIO;
  2249. goto err_release_regions;
  2250. }
  2251. return 0;
  2252. err_release_regions:
  2253. pci_release_regions(pdev);
  2254. err:
  2255. return ret;
  2256. }
  2257. static void rtw89_pci_clear_mapping(struct rtw89_dev *rtwdev,
  2258. struct pci_dev *pdev)
  2259. {
  2260. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2261. if (rtwpci->mmap) {
  2262. pci_iounmap(pdev, rtwpci->mmap);
  2263. pci_release_regions(pdev);
  2264. }
  2265. }
  2266. static void rtw89_pci_free_tx_wd_ring(struct rtw89_dev *rtwdev,
  2267. struct pci_dev *pdev,
  2268. struct rtw89_pci_tx_ring *tx_ring)
  2269. {
  2270. struct rtw89_pci_tx_wd_ring *wd_ring = &tx_ring->wd_ring;
  2271. u8 *head = wd_ring->head;
  2272. dma_addr_t dma = wd_ring->dma;
  2273. u32 page_size = wd_ring->page_size;
  2274. u32 page_num = wd_ring->page_num;
  2275. u32 ring_sz = page_size * page_num;
  2276. dma_free_coherent(&pdev->dev, ring_sz, head, dma);
  2277. wd_ring->head = NULL;
  2278. }
  2279. static void rtw89_pci_free_tx_ring(struct rtw89_dev *rtwdev,
  2280. struct pci_dev *pdev,
  2281. struct rtw89_pci_tx_ring *tx_ring)
  2282. {
  2283. int ring_sz;
  2284. u8 *head;
  2285. dma_addr_t dma;
  2286. head = tx_ring->bd_ring.head;
  2287. dma = tx_ring->bd_ring.dma;
  2288. ring_sz = tx_ring->bd_ring.desc_size * tx_ring->bd_ring.len;
  2289. dma_free_coherent(&pdev->dev, ring_sz, head, dma);
  2290. tx_ring->bd_ring.head = NULL;
  2291. }
  2292. static void rtw89_pci_free_tx_rings(struct rtw89_dev *rtwdev,
  2293. struct pci_dev *pdev)
  2294. {
  2295. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2296. const struct rtw89_pci_info *info = rtwdev->pci_info;
  2297. struct rtw89_pci_tx_ring *tx_ring;
  2298. int i;
  2299. for (i = 0; i < RTW89_TXCH_NUM; i++) {
  2300. if (info->tx_dma_ch_mask & BIT(i))
  2301. continue;
  2302. tx_ring = &rtwpci->tx_rings[i];
  2303. rtw89_pci_free_tx_wd_ring(rtwdev, pdev, tx_ring);
  2304. rtw89_pci_free_tx_ring(rtwdev, pdev, tx_ring);
  2305. }
  2306. }
  2307. static void rtw89_pci_free_rx_ring(struct rtw89_dev *rtwdev,
  2308. struct pci_dev *pdev,
  2309. struct rtw89_pci_rx_ring *rx_ring)
  2310. {
  2311. struct rtw89_pci_rx_info *rx_info;
  2312. struct sk_buff *skb;
  2313. dma_addr_t dma;
  2314. u32 buf_sz;
  2315. u8 *head;
  2316. int ring_sz = rx_ring->bd_ring.desc_size * rx_ring->bd_ring.len;
  2317. int i;
  2318. buf_sz = rx_ring->buf_sz;
  2319. for (i = 0; i < rx_ring->bd_ring.len; i++) {
  2320. skb = rx_ring->buf[i];
  2321. if (!skb)
  2322. continue;
  2323. rx_info = RTW89_PCI_RX_SKB_CB(skb);
  2324. dma = rx_info->dma;
  2325. dma_unmap_single(&pdev->dev, dma, buf_sz, DMA_FROM_DEVICE);
  2326. dev_kfree_skb(skb);
  2327. rx_ring->buf[i] = NULL;
  2328. }
  2329. head = rx_ring->bd_ring.head;
  2330. dma = rx_ring->bd_ring.dma;
  2331. dma_free_coherent(&pdev->dev, ring_sz, head, dma);
  2332. rx_ring->bd_ring.head = NULL;
  2333. }
  2334. static void rtw89_pci_free_rx_rings(struct rtw89_dev *rtwdev,
  2335. struct pci_dev *pdev)
  2336. {
  2337. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2338. struct rtw89_pci_rx_ring *rx_ring;
  2339. int i;
  2340. for (i = 0; i < RTW89_RXCH_NUM; i++) {
  2341. rx_ring = &rtwpci->rx_rings[i];
  2342. rtw89_pci_free_rx_ring(rtwdev, pdev, rx_ring);
  2343. }
  2344. }
  2345. static void rtw89_pci_free_trx_rings(struct rtw89_dev *rtwdev,
  2346. struct pci_dev *pdev)
  2347. {
  2348. rtw89_pci_free_rx_rings(rtwdev, pdev);
  2349. rtw89_pci_free_tx_rings(rtwdev, pdev);
  2350. }
  2351. static int rtw89_pci_init_rx_bd(struct rtw89_dev *rtwdev, struct pci_dev *pdev,
  2352. struct rtw89_pci_rx_ring *rx_ring,
  2353. struct sk_buff *skb, int buf_sz, u32 idx)
  2354. {
  2355. struct rtw89_pci_rx_info *rx_info;
  2356. struct rtw89_pci_rx_bd_32 *rx_bd;
  2357. dma_addr_t dma;
  2358. if (!skb)
  2359. return -EINVAL;
  2360. dma = dma_map_single(&pdev->dev, skb->data, buf_sz, DMA_FROM_DEVICE);
  2361. if (dma_mapping_error(&pdev->dev, dma))
  2362. return -EBUSY;
  2363. rx_info = RTW89_PCI_RX_SKB_CB(skb);
  2364. rx_bd = RTW89_PCI_RX_BD(rx_ring, idx);
  2365. memset(rx_bd, 0, sizeof(*rx_bd));
  2366. rx_bd->buf_size = cpu_to_le16(buf_sz);
  2367. rx_bd->dma = cpu_to_le32(dma);
  2368. rx_info->dma = dma;
  2369. return 0;
  2370. }
  2371. static int rtw89_pci_alloc_tx_wd_ring(struct rtw89_dev *rtwdev,
  2372. struct pci_dev *pdev,
  2373. struct rtw89_pci_tx_ring *tx_ring,
  2374. enum rtw89_tx_channel txch)
  2375. {
  2376. struct rtw89_pci_tx_wd_ring *wd_ring = &tx_ring->wd_ring;
  2377. struct rtw89_pci_tx_wd *txwd;
  2378. dma_addr_t dma;
  2379. dma_addr_t cur_paddr;
  2380. u8 *head;
  2381. u8 *cur_vaddr;
  2382. u32 page_size = RTW89_PCI_TXWD_PAGE_SIZE;
  2383. u32 page_num = RTW89_PCI_TXWD_NUM_MAX;
  2384. u32 ring_sz = page_size * page_num;
  2385. u32 page_offset;
  2386. int i;
  2387. /* FWCMD queue doesn't use txwd as pages */
  2388. if (txch == RTW89_TXCH_CH12)
  2389. return 0;
  2390. head = dma_alloc_coherent(&pdev->dev, ring_sz, &dma, GFP_KERNEL);
  2391. if (!head)
  2392. return -ENOMEM;
  2393. INIT_LIST_HEAD(&wd_ring->free_pages);
  2394. wd_ring->head = head;
  2395. wd_ring->dma = dma;
  2396. wd_ring->page_size = page_size;
  2397. wd_ring->page_num = page_num;
  2398. page_offset = 0;
  2399. for (i = 0; i < page_num; i++) {
  2400. txwd = &wd_ring->pages[i];
  2401. cur_paddr = dma + page_offset;
  2402. cur_vaddr = head + page_offset;
  2403. skb_queue_head_init(&txwd->queue);
  2404. INIT_LIST_HEAD(&txwd->list);
  2405. txwd->paddr = cur_paddr;
  2406. txwd->vaddr = cur_vaddr;
  2407. txwd->len = page_size;
  2408. txwd->seq = i;
  2409. rtw89_pci_enqueue_txwd(tx_ring, txwd);
  2410. page_offset += page_size;
  2411. }
  2412. return 0;
  2413. }
  2414. static int rtw89_pci_alloc_tx_ring(struct rtw89_dev *rtwdev,
  2415. struct pci_dev *pdev,
  2416. struct rtw89_pci_tx_ring *tx_ring,
  2417. u32 desc_size, u32 len,
  2418. enum rtw89_tx_channel txch)
  2419. {
  2420. const struct rtw89_pci_ch_dma_addr *txch_addr;
  2421. int ring_sz = desc_size * len;
  2422. u8 *head;
  2423. dma_addr_t dma;
  2424. int ret;
  2425. ret = rtw89_pci_alloc_tx_wd_ring(rtwdev, pdev, tx_ring, txch);
  2426. if (ret) {
  2427. rtw89_err(rtwdev, "failed to alloc txwd ring of txch %d\n", txch);
  2428. goto err;
  2429. }
  2430. ret = rtw89_pci_get_txch_addrs(rtwdev, txch, &txch_addr);
  2431. if (ret) {
  2432. rtw89_err(rtwdev, "failed to get address of txch %d", txch);
  2433. goto err_free_wd_ring;
  2434. }
  2435. head = dma_alloc_coherent(&pdev->dev, ring_sz, &dma, GFP_KERNEL);
  2436. if (!head) {
  2437. ret = -ENOMEM;
  2438. goto err_free_wd_ring;
  2439. }
  2440. INIT_LIST_HEAD(&tx_ring->busy_pages);
  2441. tx_ring->bd_ring.head = head;
  2442. tx_ring->bd_ring.dma = dma;
  2443. tx_ring->bd_ring.len = len;
  2444. tx_ring->bd_ring.desc_size = desc_size;
  2445. tx_ring->bd_ring.addr = *txch_addr;
  2446. tx_ring->bd_ring.wp = 0;
  2447. tx_ring->bd_ring.rp = 0;
  2448. tx_ring->txch = txch;
  2449. return 0;
  2450. err_free_wd_ring:
  2451. rtw89_pci_free_tx_wd_ring(rtwdev, pdev, tx_ring);
  2452. err:
  2453. return ret;
  2454. }
  2455. static int rtw89_pci_alloc_tx_rings(struct rtw89_dev *rtwdev,
  2456. struct pci_dev *pdev)
  2457. {
  2458. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2459. const struct rtw89_pci_info *info = rtwdev->pci_info;
  2460. struct rtw89_pci_tx_ring *tx_ring;
  2461. u32 desc_size;
  2462. u32 len;
  2463. u32 i, tx_allocated;
  2464. int ret;
  2465. for (i = 0; i < RTW89_TXCH_NUM; i++) {
  2466. if (info->tx_dma_ch_mask & BIT(i))
  2467. continue;
  2468. tx_ring = &rtwpci->tx_rings[i];
  2469. desc_size = sizeof(struct rtw89_pci_tx_bd_32);
  2470. len = RTW89_PCI_TXBD_NUM_MAX;
  2471. ret = rtw89_pci_alloc_tx_ring(rtwdev, pdev, tx_ring,
  2472. desc_size, len, i);
  2473. if (ret) {
  2474. rtw89_err(rtwdev, "failed to alloc tx ring %d\n", i);
  2475. goto err_free;
  2476. }
  2477. }
  2478. return 0;
  2479. err_free:
  2480. tx_allocated = i;
  2481. for (i = 0; i < tx_allocated; i++) {
  2482. tx_ring = &rtwpci->tx_rings[i];
  2483. rtw89_pci_free_tx_ring(rtwdev, pdev, tx_ring);
  2484. }
  2485. return ret;
  2486. }
  2487. static int rtw89_pci_alloc_rx_ring(struct rtw89_dev *rtwdev,
  2488. struct pci_dev *pdev,
  2489. struct rtw89_pci_rx_ring *rx_ring,
  2490. u32 desc_size, u32 len, u32 rxch)
  2491. {
  2492. const struct rtw89_pci_ch_dma_addr *rxch_addr;
  2493. struct sk_buff *skb;
  2494. u8 *head;
  2495. dma_addr_t dma;
  2496. int ring_sz = desc_size * len;
  2497. int buf_sz = RTW89_PCI_RX_BUF_SIZE;
  2498. int i, allocated;
  2499. int ret;
  2500. ret = rtw89_pci_get_rxch_addrs(rtwdev, rxch, &rxch_addr);
  2501. if (ret) {
  2502. rtw89_err(rtwdev, "failed to get address of rxch %d", rxch);
  2503. return ret;
  2504. }
  2505. head = dma_alloc_coherent(&pdev->dev, ring_sz, &dma, GFP_KERNEL);
  2506. if (!head) {
  2507. ret = -ENOMEM;
  2508. goto err;
  2509. }
  2510. rx_ring->bd_ring.head = head;
  2511. rx_ring->bd_ring.dma = dma;
  2512. rx_ring->bd_ring.len = len;
  2513. rx_ring->bd_ring.desc_size = desc_size;
  2514. rx_ring->bd_ring.addr = *rxch_addr;
  2515. rx_ring->bd_ring.wp = 0;
  2516. rx_ring->bd_ring.rp = 0;
  2517. rx_ring->buf_sz = buf_sz;
  2518. rx_ring->diliver_skb = NULL;
  2519. rx_ring->diliver_desc.ready = false;
  2520. for (i = 0; i < len; i++) {
  2521. skb = dev_alloc_skb(buf_sz);
  2522. if (!skb) {
  2523. ret = -ENOMEM;
  2524. goto err_free;
  2525. }
  2526. memset(skb->data, 0, buf_sz);
  2527. rx_ring->buf[i] = skb;
  2528. ret = rtw89_pci_init_rx_bd(rtwdev, pdev, rx_ring, skb,
  2529. buf_sz, i);
  2530. if (ret) {
  2531. rtw89_err(rtwdev, "failed to init rx buf %d\n", i);
  2532. dev_kfree_skb_any(skb);
  2533. rx_ring->buf[i] = NULL;
  2534. goto err_free;
  2535. }
  2536. }
  2537. return 0;
  2538. err_free:
  2539. allocated = i;
  2540. for (i = 0; i < allocated; i++) {
  2541. skb = rx_ring->buf[i];
  2542. if (!skb)
  2543. continue;
  2544. dma = *((dma_addr_t *)skb->cb);
  2545. dma_unmap_single(&pdev->dev, dma, buf_sz, DMA_FROM_DEVICE);
  2546. dev_kfree_skb(skb);
  2547. rx_ring->buf[i] = NULL;
  2548. }
  2549. head = rx_ring->bd_ring.head;
  2550. dma = rx_ring->bd_ring.dma;
  2551. dma_free_coherent(&pdev->dev, ring_sz, head, dma);
  2552. rx_ring->bd_ring.head = NULL;
  2553. err:
  2554. return ret;
  2555. }
  2556. static int rtw89_pci_alloc_rx_rings(struct rtw89_dev *rtwdev,
  2557. struct pci_dev *pdev)
  2558. {
  2559. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2560. struct rtw89_pci_rx_ring *rx_ring;
  2561. u32 desc_size;
  2562. u32 len;
  2563. int i, rx_allocated;
  2564. int ret;
  2565. for (i = 0; i < RTW89_RXCH_NUM; i++) {
  2566. rx_ring = &rtwpci->rx_rings[i];
  2567. desc_size = sizeof(struct rtw89_pci_rx_bd_32);
  2568. len = RTW89_PCI_RXBD_NUM_MAX;
  2569. ret = rtw89_pci_alloc_rx_ring(rtwdev, pdev, rx_ring,
  2570. desc_size, len, i);
  2571. if (ret) {
  2572. rtw89_err(rtwdev, "failed to alloc rx ring %d\n", i);
  2573. goto err_free;
  2574. }
  2575. }
  2576. return 0;
  2577. err_free:
  2578. rx_allocated = i;
  2579. for (i = 0; i < rx_allocated; i++) {
  2580. rx_ring = &rtwpci->rx_rings[i];
  2581. rtw89_pci_free_rx_ring(rtwdev, pdev, rx_ring);
  2582. }
  2583. return ret;
  2584. }
  2585. static int rtw89_pci_alloc_trx_rings(struct rtw89_dev *rtwdev,
  2586. struct pci_dev *pdev)
  2587. {
  2588. int ret;
  2589. ret = rtw89_pci_alloc_tx_rings(rtwdev, pdev);
  2590. if (ret) {
  2591. rtw89_err(rtwdev, "failed to alloc dma tx rings\n");
  2592. goto err;
  2593. }
  2594. ret = rtw89_pci_alloc_rx_rings(rtwdev, pdev);
  2595. if (ret) {
  2596. rtw89_err(rtwdev, "failed to alloc dma rx rings\n");
  2597. goto err_free_tx_rings;
  2598. }
  2599. return 0;
  2600. err_free_tx_rings:
  2601. rtw89_pci_free_tx_rings(rtwdev, pdev);
  2602. err:
  2603. return ret;
  2604. }
  2605. static void rtw89_pci_h2c_init(struct rtw89_dev *rtwdev,
  2606. struct rtw89_pci *rtwpci)
  2607. {
  2608. skb_queue_head_init(&rtwpci->h2c_queue);
  2609. skb_queue_head_init(&rtwpci->h2c_release_queue);
  2610. }
  2611. static int rtw89_pci_setup_resource(struct rtw89_dev *rtwdev,
  2612. struct pci_dev *pdev)
  2613. {
  2614. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2615. int ret;
  2616. ret = rtw89_pci_setup_mapping(rtwdev, pdev);
  2617. if (ret) {
  2618. rtw89_err(rtwdev, "failed to setup pci mapping\n");
  2619. goto err;
  2620. }
  2621. ret = rtw89_pci_alloc_trx_rings(rtwdev, pdev);
  2622. if (ret) {
  2623. rtw89_err(rtwdev, "failed to alloc pci trx rings\n");
  2624. goto err_pci_unmap;
  2625. }
  2626. rtw89_pci_h2c_init(rtwdev, rtwpci);
  2627. spin_lock_init(&rtwpci->irq_lock);
  2628. spin_lock_init(&rtwpci->trx_lock);
  2629. return 0;
  2630. err_pci_unmap:
  2631. rtw89_pci_clear_mapping(rtwdev, pdev);
  2632. err:
  2633. return ret;
  2634. }
  2635. static void rtw89_pci_clear_resource(struct rtw89_dev *rtwdev,
  2636. struct pci_dev *pdev)
  2637. {
  2638. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2639. rtw89_pci_free_trx_rings(rtwdev, pdev);
  2640. rtw89_pci_clear_mapping(rtwdev, pdev);
  2641. rtw89_pci_release_fwcmd(rtwdev, rtwpci,
  2642. skb_queue_len(&rtwpci->h2c_queue), true);
  2643. }
  2644. void rtw89_pci_config_intr_mask(struct rtw89_dev *rtwdev)
  2645. {
  2646. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2647. const struct rtw89_chip_info *chip = rtwdev->chip;
  2648. u32 hs0isr_ind_int_en = B_AX_HS0ISR_IND_INT_EN;
  2649. if (chip->chip_id == RTL8851B)
  2650. hs0isr_ind_int_en = B_AX_HS0ISR_IND_INT_EN_WKARND;
  2651. rtwpci->halt_c2h_intrs = B_AX_HALT_C2H_INT_EN | 0;
  2652. if (rtwpci->under_recovery) {
  2653. rtwpci->intrs[0] = hs0isr_ind_int_en;
  2654. rtwpci->intrs[1] = 0;
  2655. } else {
  2656. rtwpci->intrs[0] = B_AX_TXDMA_STUCK_INT_EN |
  2657. B_AX_RXDMA_INT_EN |
  2658. B_AX_RXP1DMA_INT_EN |
  2659. B_AX_RPQDMA_INT_EN |
  2660. B_AX_RXDMA_STUCK_INT_EN |
  2661. B_AX_RDU_INT_EN |
  2662. B_AX_RPQBD_FULL_INT_EN |
  2663. hs0isr_ind_int_en;
  2664. rtwpci->intrs[1] = B_AX_HC10ISR_IND_INT_EN;
  2665. }
  2666. }
  2667. EXPORT_SYMBOL(rtw89_pci_config_intr_mask);
  2668. static void rtw89_pci_recovery_intr_mask_v1(struct rtw89_dev *rtwdev)
  2669. {
  2670. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2671. rtwpci->ind_intrs = B_AX_HS0ISR_IND_INT_EN;
  2672. rtwpci->halt_c2h_intrs = B_AX_HALT_C2H_INT_EN | B_AX_WDT_TIMEOUT_INT_EN;
  2673. rtwpci->intrs[0] = 0;
  2674. rtwpci->intrs[1] = 0;
  2675. }
  2676. static void rtw89_pci_default_intr_mask_v1(struct rtw89_dev *rtwdev)
  2677. {
  2678. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2679. rtwpci->ind_intrs = B_AX_HCI_AXIDMA_INT_EN |
  2680. B_AX_HS1ISR_IND_INT_EN |
  2681. B_AX_HS0ISR_IND_INT_EN;
  2682. rtwpci->halt_c2h_intrs = B_AX_HALT_C2H_INT_EN | B_AX_WDT_TIMEOUT_INT_EN;
  2683. rtwpci->intrs[0] = B_AX_TXDMA_STUCK_INT_EN |
  2684. B_AX_RXDMA_INT_EN |
  2685. B_AX_RXP1DMA_INT_EN |
  2686. B_AX_RPQDMA_INT_EN |
  2687. B_AX_RXDMA_STUCK_INT_EN |
  2688. B_AX_RDU_INT_EN |
  2689. B_AX_RPQBD_FULL_INT_EN;
  2690. rtwpci->intrs[1] = B_AX_GPIO18_INT_EN;
  2691. }
  2692. static void rtw89_pci_low_power_intr_mask_v1(struct rtw89_dev *rtwdev)
  2693. {
  2694. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2695. rtwpci->ind_intrs = B_AX_HS1ISR_IND_INT_EN |
  2696. B_AX_HS0ISR_IND_INT_EN;
  2697. rtwpci->halt_c2h_intrs = B_AX_HALT_C2H_INT_EN | B_AX_WDT_TIMEOUT_INT_EN;
  2698. rtwpci->intrs[0] = 0;
  2699. rtwpci->intrs[1] = B_AX_GPIO18_INT_EN;
  2700. }
  2701. void rtw89_pci_config_intr_mask_v1(struct rtw89_dev *rtwdev)
  2702. {
  2703. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2704. if (rtwpci->under_recovery)
  2705. rtw89_pci_recovery_intr_mask_v1(rtwdev);
  2706. else if (rtwpci->low_power)
  2707. rtw89_pci_low_power_intr_mask_v1(rtwdev);
  2708. else
  2709. rtw89_pci_default_intr_mask_v1(rtwdev);
  2710. }
  2711. EXPORT_SYMBOL(rtw89_pci_config_intr_mask_v1);
  2712. static int rtw89_pci_request_irq(struct rtw89_dev *rtwdev,
  2713. struct pci_dev *pdev)
  2714. {
  2715. unsigned long flags = 0;
  2716. int ret;
  2717. flags |= PCI_IRQ_LEGACY | PCI_IRQ_MSI;
  2718. ret = pci_alloc_irq_vectors(pdev, 1, 1, flags);
  2719. if (ret < 0) {
  2720. rtw89_err(rtwdev, "failed to alloc irq vectors, ret %d\n", ret);
  2721. goto err;
  2722. }
  2723. ret = devm_request_threaded_irq(rtwdev->dev, pdev->irq,
  2724. rtw89_pci_interrupt_handler,
  2725. rtw89_pci_interrupt_threadfn,
  2726. IRQF_SHARED, KBUILD_MODNAME, rtwdev);
  2727. if (ret) {
  2728. rtw89_err(rtwdev, "failed to request threaded irq\n");
  2729. goto err_free_vector;
  2730. }
  2731. rtw89_chip_config_intr_mask(rtwdev, RTW89_PCI_INTR_MASK_RESET);
  2732. return 0;
  2733. err_free_vector:
  2734. pci_free_irq_vectors(pdev);
  2735. err:
  2736. return ret;
  2737. }
  2738. static void rtw89_pci_free_irq(struct rtw89_dev *rtwdev,
  2739. struct pci_dev *pdev)
  2740. {
  2741. devm_free_irq(rtwdev->dev, pdev->irq, rtwdev);
  2742. pci_free_irq_vectors(pdev);
  2743. }
  2744. static u16 gray_code_to_bin(u16 gray_code, u32 bit_num)
  2745. {
  2746. u16 bin = 0, gray_bit;
  2747. u32 bit_idx;
  2748. for (bit_idx = 0; bit_idx < bit_num; bit_idx++) {
  2749. gray_bit = (gray_code >> bit_idx) & 0x1;
  2750. if (bit_num - bit_idx > 1)
  2751. gray_bit ^= (gray_code >> (bit_idx + 1)) & 0x1;
  2752. bin |= (gray_bit << bit_idx);
  2753. }
  2754. return bin;
  2755. }
  2756. static int rtw89_pci_filter_out(struct rtw89_dev *rtwdev)
  2757. {
  2758. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2759. struct pci_dev *pdev = rtwpci->pdev;
  2760. u16 val16, filter_out_val;
  2761. u32 val, phy_offset;
  2762. int ret;
  2763. if (rtwdev->chip->chip_id != RTL8852C)
  2764. return 0;
  2765. val = rtw89_read32_mask(rtwdev, R_AX_PCIE_MIX_CFG_V1, B_AX_ASPM_CTRL_MASK);
  2766. if (val == B_AX_ASPM_CTRL_L1)
  2767. return 0;
  2768. ret = pci_read_config_dword(pdev, RTW89_PCIE_L1_STS_V1, &val);
  2769. if (ret)
  2770. return ret;
  2771. val = FIELD_GET(RTW89_BCFG_LINK_SPEED_MASK, val);
  2772. if (val == RTW89_PCIE_GEN1_SPEED) {
  2773. phy_offset = R_RAC_DIRECT_OFFSET_G1;
  2774. } else if (val == RTW89_PCIE_GEN2_SPEED) {
  2775. phy_offset = R_RAC_DIRECT_OFFSET_G2;
  2776. val16 = rtw89_read16(rtwdev, phy_offset + RAC_ANA10 * RAC_MULT);
  2777. rtw89_write16_set(rtwdev, phy_offset + RAC_ANA10 * RAC_MULT,
  2778. val16 | B_PCIE_BIT_PINOUT_DIS);
  2779. rtw89_write16_set(rtwdev, phy_offset + RAC_ANA19 * RAC_MULT,
  2780. val16 & ~B_PCIE_BIT_RD_SEL);
  2781. val16 = rtw89_read16_mask(rtwdev,
  2782. phy_offset + RAC_ANA1F * RAC_MULT,
  2783. FILTER_OUT_EQ_MASK);
  2784. val16 = gray_code_to_bin(val16, hweight16(val16));
  2785. filter_out_val = rtw89_read16(rtwdev, phy_offset + RAC_ANA24 *
  2786. RAC_MULT);
  2787. filter_out_val &= ~REG_FILTER_OUT_MASK;
  2788. filter_out_val |= FIELD_PREP(REG_FILTER_OUT_MASK, val16);
  2789. rtw89_write16(rtwdev, phy_offset + RAC_ANA24 * RAC_MULT,
  2790. filter_out_val);
  2791. rtw89_write16_set(rtwdev, phy_offset + RAC_ANA0A * RAC_MULT,
  2792. B_BAC_EQ_SEL);
  2793. rtw89_write16_set(rtwdev,
  2794. R_RAC_DIRECT_OFFSET_G1 + RAC_ANA0C * RAC_MULT,
  2795. B_PCIE_BIT_PSAVE);
  2796. } else {
  2797. return -EOPNOTSUPP;
  2798. }
  2799. rtw89_write16_set(rtwdev, phy_offset + RAC_ANA0C * RAC_MULT,
  2800. B_PCIE_BIT_PSAVE);
  2801. return 0;
  2802. }
  2803. static void rtw89_pci_clkreq_set(struct rtw89_dev *rtwdev, bool enable)
  2804. {
  2805. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  2806. int ret;
  2807. if (rtw89_pci_disable_clkreq)
  2808. return;
  2809. ret = rtw89_pci_write_config_byte(rtwdev, RTW89_PCIE_CLK_CTRL,
  2810. PCIE_CLKDLY_HW_30US);
  2811. if (ret)
  2812. rtw89_err(rtwdev, "failed to set CLKREQ Delay\n");
  2813. if (chip_id == RTL8852A || chip_id == RTL8852B || chip_id == RTL8851B) {
  2814. if (enable)
  2815. ret = rtw89_pci_config_byte_set(rtwdev,
  2816. RTW89_PCIE_L1_CTRL,
  2817. RTW89_PCIE_BIT_CLK);
  2818. else
  2819. ret = rtw89_pci_config_byte_clr(rtwdev,
  2820. RTW89_PCIE_L1_CTRL,
  2821. RTW89_PCIE_BIT_CLK);
  2822. if (ret)
  2823. rtw89_err(rtwdev, "failed to %s CLKREQ_L1, ret=%d",
  2824. enable ? "set" : "unset", ret);
  2825. } else if (chip_id == RTL8852C) {
  2826. rtw89_write32_set(rtwdev, R_AX_PCIE_LAT_CTRL,
  2827. B_AX_CLK_REQ_SEL_OPT | B_AX_CLK_REQ_SEL);
  2828. if (enable)
  2829. rtw89_write32_set(rtwdev, R_AX_L1_CLK_CTRL,
  2830. B_AX_CLK_REQ_N);
  2831. else
  2832. rtw89_write32_clr(rtwdev, R_AX_L1_CLK_CTRL,
  2833. B_AX_CLK_REQ_N);
  2834. }
  2835. }
  2836. static void rtw89_pci_aspm_set(struct rtw89_dev *rtwdev, bool enable)
  2837. {
  2838. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  2839. u8 value = 0;
  2840. int ret;
  2841. if (rtw89_pci_disable_aspm_l1)
  2842. return;
  2843. ret = rtw89_pci_read_config_byte(rtwdev, RTW89_PCIE_ASPM_CTRL, &value);
  2844. if (ret)
  2845. rtw89_err(rtwdev, "failed to read ASPM Delay\n");
  2846. value &= ~(RTW89_L1DLY_MASK | RTW89_L0DLY_MASK);
  2847. value |= FIELD_PREP(RTW89_L1DLY_MASK, PCIE_L1DLY_16US) |
  2848. FIELD_PREP(RTW89_L0DLY_MASK, PCIE_L0SDLY_4US);
  2849. ret = rtw89_pci_write_config_byte(rtwdev, RTW89_PCIE_ASPM_CTRL, value);
  2850. if (ret)
  2851. rtw89_err(rtwdev, "failed to read ASPM Delay\n");
  2852. if (chip_id == RTL8852A || chip_id == RTL8852B || chip_id == RTL8851B) {
  2853. if (enable)
  2854. ret = rtw89_pci_config_byte_set(rtwdev,
  2855. RTW89_PCIE_L1_CTRL,
  2856. RTW89_PCIE_BIT_L1);
  2857. else
  2858. ret = rtw89_pci_config_byte_clr(rtwdev,
  2859. RTW89_PCIE_L1_CTRL,
  2860. RTW89_PCIE_BIT_L1);
  2861. } else if (chip_id == RTL8852C) {
  2862. if (enable)
  2863. rtw89_write32_set(rtwdev, R_AX_PCIE_MIX_CFG_V1,
  2864. B_AX_ASPM_CTRL_L1);
  2865. else
  2866. rtw89_write32_clr(rtwdev, R_AX_PCIE_MIX_CFG_V1,
  2867. B_AX_ASPM_CTRL_L1);
  2868. }
  2869. if (ret)
  2870. rtw89_err(rtwdev, "failed to %s ASPM L1, ret=%d",
  2871. enable ? "set" : "unset", ret);
  2872. }
  2873. static void rtw89_pci_recalc_int_mit(struct rtw89_dev *rtwdev)
  2874. {
  2875. struct rtw89_traffic_stats *stats = &rtwdev->stats;
  2876. enum rtw89_tfc_lv tx_tfc_lv = stats->tx_tfc_lv;
  2877. enum rtw89_tfc_lv rx_tfc_lv = stats->rx_tfc_lv;
  2878. u32 val = 0;
  2879. if (!rtwdev->scanning &&
  2880. (tx_tfc_lv >= RTW89_TFC_HIGH || rx_tfc_lv >= RTW89_TFC_HIGH))
  2881. val = B_AX_RXMIT_RXP2_SEL | B_AX_RXMIT_RXP1_SEL |
  2882. FIELD_PREP(B_AX_RXCOUNTER_MATCH_MASK, RTW89_PCI_RXBD_NUM_MAX / 2) |
  2883. FIELD_PREP(B_AX_RXTIMER_UNIT_MASK, AX_RXTIMER_UNIT_64US) |
  2884. FIELD_PREP(B_AX_RXTIMER_MATCH_MASK, 2048 / 64);
  2885. rtw89_write32(rtwdev, R_AX_INT_MIT_RX, val);
  2886. }
  2887. static void rtw89_pci_link_cfg(struct rtw89_dev *rtwdev)
  2888. {
  2889. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2890. struct pci_dev *pdev = rtwpci->pdev;
  2891. u16 link_ctrl;
  2892. int ret;
  2893. /* Though there is standard PCIE configuration space to set the
  2894. * link control register, but by Realtek's design, driver should
  2895. * check if host supports CLKREQ/ASPM to enable the HW module.
  2896. *
  2897. * These functions are implemented by two HW modules associated,
  2898. * one is responsible to access PCIE configuration space to
  2899. * follow the host settings, and another is in charge of doing
  2900. * CLKREQ/ASPM mechanisms, it is default disabled. Because sometimes
  2901. * the host does not support it, and due to some reasons or wrong
  2902. * settings (ex. CLKREQ# not Bi-Direction), it could lead to device
  2903. * loss if HW misbehaves on the link.
  2904. *
  2905. * Hence it's designed that driver should first check the PCIE
  2906. * configuration space is sync'ed and enabled, then driver can turn
  2907. * on the other module that is actually working on the mechanism.
  2908. */
  2909. ret = pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &link_ctrl);
  2910. if (ret) {
  2911. rtw89_err(rtwdev, "failed to read PCI cap, ret=%d\n", ret);
  2912. return;
  2913. }
  2914. if (link_ctrl & PCI_EXP_LNKCTL_CLKREQ_EN)
  2915. rtw89_pci_clkreq_set(rtwdev, true);
  2916. if (link_ctrl & PCI_EXP_LNKCTL_ASPM_L1)
  2917. rtw89_pci_aspm_set(rtwdev, true);
  2918. }
  2919. static void rtw89_pci_l1ss_set(struct rtw89_dev *rtwdev, bool enable)
  2920. {
  2921. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  2922. int ret;
  2923. if (chip_id == RTL8852A || chip_id == RTL8852B || chip_id == RTL8851B) {
  2924. if (enable)
  2925. ret = rtw89_pci_config_byte_set(rtwdev,
  2926. RTW89_PCIE_TIMER_CTRL,
  2927. RTW89_PCIE_BIT_L1SUB);
  2928. else
  2929. ret = rtw89_pci_config_byte_clr(rtwdev,
  2930. RTW89_PCIE_TIMER_CTRL,
  2931. RTW89_PCIE_BIT_L1SUB);
  2932. if (ret)
  2933. rtw89_err(rtwdev, "failed to %s L1SS, ret=%d",
  2934. enable ? "set" : "unset", ret);
  2935. } else if (chip_id == RTL8852C) {
  2936. ret = rtw89_pci_config_byte_clr(rtwdev, RTW89_PCIE_L1SS_STS_V1,
  2937. RTW89_PCIE_BIT_ASPM_L11 |
  2938. RTW89_PCIE_BIT_PCI_L11);
  2939. if (ret)
  2940. rtw89_warn(rtwdev, "failed to unset ASPM L1.1, ret=%d", ret);
  2941. if (enable)
  2942. rtw89_write32_clr(rtwdev, R_AX_PCIE_MIX_CFG_V1,
  2943. B_AX_L1SUB_DISABLE);
  2944. else
  2945. rtw89_write32_set(rtwdev, R_AX_PCIE_MIX_CFG_V1,
  2946. B_AX_L1SUB_DISABLE);
  2947. }
  2948. }
  2949. static void rtw89_pci_l1ss_cfg(struct rtw89_dev *rtwdev)
  2950. {
  2951. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  2952. struct pci_dev *pdev = rtwpci->pdev;
  2953. u32 l1ss_cap_ptr, l1ss_ctrl;
  2954. if (rtw89_pci_disable_l1ss)
  2955. return;
  2956. l1ss_cap_ptr = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS);
  2957. if (!l1ss_cap_ptr)
  2958. return;
  2959. pci_read_config_dword(pdev, l1ss_cap_ptr + PCI_L1SS_CTL1, &l1ss_ctrl);
  2960. if (l1ss_ctrl & PCI_L1SS_CTL1_L1SS_MASK)
  2961. rtw89_pci_l1ss_set(rtwdev, true);
  2962. }
  2963. static int rtw89_pci_poll_io_idle(struct rtw89_dev *rtwdev)
  2964. {
  2965. int ret = 0;
  2966. u32 sts;
  2967. u32 busy = B_AX_PCIEIO_BUSY | B_AX_PCIEIO_TX_BUSY | B_AX_PCIEIO_RX_BUSY;
  2968. ret = read_poll_timeout_atomic(rtw89_read32, sts, (sts & busy) == 0x0,
  2969. 10, 1000, false, rtwdev,
  2970. R_AX_PCIE_DMA_BUSY1);
  2971. if (ret) {
  2972. rtw89_err(rtwdev, "pci dmach busy1 0x%X\n",
  2973. rtw89_read32(rtwdev, R_AX_PCIE_DMA_BUSY1));
  2974. return -EINVAL;
  2975. }
  2976. return ret;
  2977. }
  2978. static int rtw89_pci_lv1rst_stop_dma(struct rtw89_dev *rtwdev)
  2979. {
  2980. u32 val;
  2981. int ret;
  2982. if (rtwdev->chip->chip_id == RTL8852C)
  2983. return 0;
  2984. rtw89_pci_ctrl_dma_all(rtwdev, false);
  2985. ret = rtw89_pci_poll_io_idle(rtwdev);
  2986. if (ret) {
  2987. val = rtw89_read32(rtwdev, R_AX_DBG_ERR_FLAG);
  2988. rtw89_debug(rtwdev, RTW89_DBG_HCI,
  2989. "[PCIe] poll_io_idle fail, before 0x%08x: 0x%08x\n",
  2990. R_AX_DBG_ERR_FLAG, val);
  2991. if (val & B_AX_TX_STUCK || val & B_AX_PCIE_TXBD_LEN0)
  2992. rtw89_mac_ctrl_hci_dma_tx(rtwdev, false);
  2993. if (val & B_AX_RX_STUCK)
  2994. rtw89_mac_ctrl_hci_dma_rx(rtwdev, false);
  2995. rtw89_mac_ctrl_hci_dma_trx(rtwdev, true);
  2996. ret = rtw89_pci_poll_io_idle(rtwdev);
  2997. val = rtw89_read32(rtwdev, R_AX_DBG_ERR_FLAG);
  2998. rtw89_debug(rtwdev, RTW89_DBG_HCI,
  2999. "[PCIe] poll_io_idle fail, after 0x%08x: 0x%08x\n",
  3000. R_AX_DBG_ERR_FLAG, val);
  3001. }
  3002. return ret;
  3003. }
  3004. static int rtw89_pci_rst_bdram(struct rtw89_dev *rtwdev)
  3005. {
  3006. int ret = 0;
  3007. u32 val32, sts;
  3008. val32 = B_AX_RST_BDRAM;
  3009. rtw89_write32_set(rtwdev, R_AX_PCIE_INIT_CFG1, val32);
  3010. ret = read_poll_timeout_atomic(rtw89_read32, sts,
  3011. (sts & B_AX_RST_BDRAM) == 0x0, 1, 100,
  3012. true, rtwdev, R_AX_PCIE_INIT_CFG1);
  3013. return ret;
  3014. }
  3015. static int rtw89_pci_lv1rst_start_dma(struct rtw89_dev *rtwdev)
  3016. {
  3017. u32 ret;
  3018. if (rtwdev->chip->chip_id == RTL8852C)
  3019. return 0;
  3020. rtw89_mac_ctrl_hci_dma_trx(rtwdev, false);
  3021. rtw89_mac_ctrl_hci_dma_trx(rtwdev, true);
  3022. rtw89_pci_clr_idx_all(rtwdev);
  3023. ret = rtw89_pci_rst_bdram(rtwdev);
  3024. if (ret)
  3025. return ret;
  3026. rtw89_pci_ctrl_dma_all(rtwdev, true);
  3027. return ret;
  3028. }
  3029. static int rtw89_pci_ops_mac_lv1_recovery(struct rtw89_dev *rtwdev,
  3030. enum rtw89_lv1_rcvy_step step)
  3031. {
  3032. int ret;
  3033. switch (step) {
  3034. case RTW89_LV1_RCVY_STEP_1:
  3035. ret = rtw89_pci_lv1rst_stop_dma(rtwdev);
  3036. if (ret)
  3037. rtw89_err(rtwdev, "lv1 rcvy pci stop dma fail\n");
  3038. break;
  3039. case RTW89_LV1_RCVY_STEP_2:
  3040. ret = rtw89_pci_lv1rst_start_dma(rtwdev);
  3041. if (ret)
  3042. rtw89_err(rtwdev, "lv1 rcvy pci start dma fail\n");
  3043. break;
  3044. default:
  3045. return -EINVAL;
  3046. }
  3047. return ret;
  3048. }
  3049. static void rtw89_pci_ops_dump_err_status(struct rtw89_dev *rtwdev)
  3050. {
  3051. rtw89_info(rtwdev, "R_AX_RPQ_RXBD_IDX =0x%08x\n",
  3052. rtw89_read32(rtwdev, R_AX_RPQ_RXBD_IDX));
  3053. rtw89_info(rtwdev, "R_AX_DBG_ERR_FLAG=0x%08x\n",
  3054. rtw89_read32(rtwdev, R_AX_DBG_ERR_FLAG));
  3055. rtw89_info(rtwdev, "R_AX_LBC_WATCHDOG=0x%08x\n",
  3056. rtw89_read32(rtwdev, R_AX_LBC_WATCHDOG));
  3057. }
  3058. static int rtw89_pci_napi_poll(struct napi_struct *napi, int budget)
  3059. {
  3060. struct rtw89_dev *rtwdev = container_of(napi, struct rtw89_dev, napi);
  3061. struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
  3062. unsigned long flags;
  3063. int work_done;
  3064. rtwdev->napi_budget_countdown = budget;
  3065. rtw89_pci_clear_isr0(rtwdev, B_AX_RPQDMA_INT | B_AX_RPQBD_FULL_INT);
  3066. work_done = rtw89_pci_poll_rpq_dma(rtwdev, rtwpci, rtwdev->napi_budget_countdown);
  3067. if (work_done == budget)
  3068. return budget;
  3069. rtw89_pci_clear_isr0(rtwdev, B_AX_RXP1DMA_INT | B_AX_RXDMA_INT | B_AX_RDU_INT);
  3070. work_done += rtw89_pci_poll_rxq_dma(rtwdev, rtwpci, rtwdev->napi_budget_countdown);
  3071. if (work_done < budget && napi_complete_done(napi, work_done)) {
  3072. spin_lock_irqsave(&rtwpci->irq_lock, flags);
  3073. if (likely(rtwpci->running))
  3074. rtw89_chip_enable_intr(rtwdev, rtwpci);
  3075. spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
  3076. }
  3077. return work_done;
  3078. }
  3079. static int __maybe_unused rtw89_pci_suspend(struct device *dev)
  3080. {
  3081. struct ieee80211_hw *hw = dev_get_drvdata(dev);
  3082. struct rtw89_dev *rtwdev = hw->priv;
  3083. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  3084. rtw89_write32_set(rtwdev, R_AX_RSV_CTRL, B_AX_WLOCK_1C_BIT6);
  3085. rtw89_write32_set(rtwdev, R_AX_RSV_CTRL, B_AX_R_DIS_PRST);
  3086. rtw89_write32_clr(rtwdev, R_AX_RSV_CTRL, B_AX_WLOCK_1C_BIT6);
  3087. if (chip_id == RTL8852A || chip_id == RTL8852B || chip_id == RTL8851B) {
  3088. rtw89_write32_clr(rtwdev, R_AX_SYS_SDIO_CTRL,
  3089. B_AX_PCIE_DIS_L2_CTRL_LDO_HCI);
  3090. rtw89_write32_set(rtwdev, R_AX_PCIE_INIT_CFG1,
  3091. B_AX_PCIE_PERST_KEEP_REG | B_AX_PCIE_TRAIN_KEEP_REG);
  3092. } else {
  3093. rtw89_write32_clr(rtwdev, R_AX_PCIE_PS_CTRL_V1,
  3094. B_AX_CMAC_EXIT_L1_EN | B_AX_DMAC0_EXIT_L1_EN);
  3095. }
  3096. return 0;
  3097. }
  3098. static void rtw89_pci_l2_hci_ldo(struct rtw89_dev *rtwdev)
  3099. {
  3100. if (rtwdev->chip->chip_id == RTL8852C)
  3101. return;
  3102. /* Hardware need write the reg twice to ensure the setting work */
  3103. rtw89_pci_write_config_byte(rtwdev, RTW89_PCIE_RST_MSTATE,
  3104. RTW89_PCIE_BIT_CFG_RST_MSTATE);
  3105. rtw89_pci_write_config_byte(rtwdev, RTW89_PCIE_RST_MSTATE,
  3106. RTW89_PCIE_BIT_CFG_RST_MSTATE);
  3107. }
  3108. static int __maybe_unused rtw89_pci_resume(struct device *dev)
  3109. {
  3110. struct ieee80211_hw *hw = dev_get_drvdata(dev);
  3111. struct rtw89_dev *rtwdev = hw->priv;
  3112. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  3113. rtw89_write32_set(rtwdev, R_AX_RSV_CTRL, B_AX_WLOCK_1C_BIT6);
  3114. rtw89_write32_clr(rtwdev, R_AX_RSV_CTRL, B_AX_R_DIS_PRST);
  3115. rtw89_write32_clr(rtwdev, R_AX_RSV_CTRL, B_AX_WLOCK_1C_BIT6);
  3116. if (chip_id == RTL8852A || chip_id == RTL8852B || chip_id == RTL8851B) {
  3117. rtw89_write32_set(rtwdev, R_AX_SYS_SDIO_CTRL,
  3118. B_AX_PCIE_DIS_L2_CTRL_LDO_HCI);
  3119. rtw89_write32_clr(rtwdev, R_AX_PCIE_INIT_CFG1,
  3120. B_AX_PCIE_PERST_KEEP_REG | B_AX_PCIE_TRAIN_KEEP_REG);
  3121. } else {
  3122. rtw89_write32_set(rtwdev, R_AX_PCIE_PS_CTRL_V1,
  3123. B_AX_CMAC_EXIT_L1_EN | B_AX_DMAC0_EXIT_L1_EN);
  3124. rtw89_write32_clr(rtwdev, R_AX_PCIE_PS_CTRL_V1,
  3125. B_AX_SEL_REQ_ENTR_L1);
  3126. }
  3127. rtw89_pci_l2_hci_ldo(rtwdev);
  3128. rtw89_pci_filter_out(rtwdev);
  3129. rtw89_pci_link_cfg(rtwdev);
  3130. rtw89_pci_l1ss_cfg(rtwdev);
  3131. return 0;
  3132. }
  3133. SIMPLE_DEV_PM_OPS(rtw89_pm_ops, rtw89_pci_suspend, rtw89_pci_resume);
  3134. EXPORT_SYMBOL(rtw89_pm_ops);
  3135. static const struct rtw89_hci_ops rtw89_pci_ops = {
  3136. .tx_write = rtw89_pci_ops_tx_write,
  3137. .tx_kick_off = rtw89_pci_ops_tx_kick_off,
  3138. .flush_queues = rtw89_pci_ops_flush_queues,
  3139. .reset = rtw89_pci_ops_reset,
  3140. .start = rtw89_pci_ops_start,
  3141. .stop = rtw89_pci_ops_stop,
  3142. .pause = rtw89_pci_ops_pause,
  3143. .switch_mode = rtw89_pci_ops_switch_mode,
  3144. .recalc_int_mit = rtw89_pci_recalc_int_mit,
  3145. .read8 = rtw89_pci_ops_read8,
  3146. .read16 = rtw89_pci_ops_read16,
  3147. .read32 = rtw89_pci_ops_read32,
  3148. .write8 = rtw89_pci_ops_write8,
  3149. .write16 = rtw89_pci_ops_write16,
  3150. .write32 = rtw89_pci_ops_write32,
  3151. .mac_pre_init = rtw89_pci_ops_mac_pre_init,
  3152. .mac_post_init = rtw89_pci_ops_mac_post_init,
  3153. .deinit = rtw89_pci_ops_deinit,
  3154. .check_and_reclaim_tx_resource = rtw89_pci_check_and_reclaim_tx_resource,
  3155. .mac_lv1_rcvy = rtw89_pci_ops_mac_lv1_recovery,
  3156. .dump_err_status = rtw89_pci_ops_dump_err_status,
  3157. .napi_poll = rtw89_pci_napi_poll,
  3158. .recovery_start = rtw89_pci_ops_recovery_start,
  3159. .recovery_complete = rtw89_pci_ops_recovery_complete,
  3160. .ctrl_txdma_ch = rtw89_pci_ctrl_txdma_ch_pcie,
  3161. .ctrl_txdma_fw_ch = rtw89_pci_ctrl_txdma_fw_ch_pcie,
  3162. .ctrl_trxhci = rtw89_pci_ctrl_dma_trx,
  3163. .poll_txdma_ch = rtw89_poll_txdma_ch_idle_pcie,
  3164. .clr_idx_all = rtw89_pci_clr_idx_all,
  3165. .clear = rtw89_pci_clear_resource,
  3166. .disable_intr = rtw89_pci_disable_intr_lock,
  3167. .enable_intr = rtw89_pci_enable_intr_lock,
  3168. .rst_bdram = rtw89_pci_rst_bdram_pcie,
  3169. };
  3170. int rtw89_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  3171. {
  3172. struct rtw89_dev *rtwdev;
  3173. const struct rtw89_driver_info *info;
  3174. const struct rtw89_pci_info *pci_info;
  3175. int ret;
  3176. info = (const struct rtw89_driver_info *)id->driver_data;
  3177. rtwdev = rtw89_alloc_ieee80211_hw(&pdev->dev,
  3178. sizeof(struct rtw89_pci),
  3179. info->chip);
  3180. if (!rtwdev) {
  3181. dev_err(&pdev->dev, "failed to allocate hw\n");
  3182. return -ENOMEM;
  3183. }
  3184. pci_info = info->bus.pci;
  3185. rtwdev->pci_info = info->bus.pci;
  3186. rtwdev->hci.ops = &rtw89_pci_ops;
  3187. rtwdev->hci.type = RTW89_HCI_TYPE_PCIE;
  3188. rtwdev->hci.rpwm_addr = pci_info->rpwm_addr;
  3189. rtwdev->hci.cpwm_addr = pci_info->cpwm_addr;
  3190. SET_IEEE80211_DEV(rtwdev->hw, &pdev->dev);
  3191. ret = rtw89_core_init(rtwdev);
  3192. if (ret) {
  3193. rtw89_err(rtwdev, "failed to initialise core\n");
  3194. goto err_release_hw;
  3195. }
  3196. ret = rtw89_pci_claim_device(rtwdev, pdev);
  3197. if (ret) {
  3198. rtw89_err(rtwdev, "failed to claim pci device\n");
  3199. goto err_core_deinit;
  3200. }
  3201. ret = rtw89_pci_setup_resource(rtwdev, pdev);
  3202. if (ret) {
  3203. rtw89_err(rtwdev, "failed to setup pci resource\n");
  3204. goto err_declaim_pci;
  3205. }
  3206. ret = rtw89_chip_info_setup(rtwdev);
  3207. if (ret) {
  3208. rtw89_err(rtwdev, "failed to setup chip information\n");
  3209. goto err_clear_resource;
  3210. }
  3211. rtw89_pci_filter_out(rtwdev);
  3212. rtw89_pci_link_cfg(rtwdev);
  3213. rtw89_pci_l1ss_cfg(rtwdev);
  3214. rtw89_core_napi_init(rtwdev);
  3215. ret = rtw89_pci_request_irq(rtwdev, pdev);
  3216. if (ret) {
  3217. rtw89_err(rtwdev, "failed to request pci irq\n");
  3218. goto err_deinit_napi;
  3219. }
  3220. ret = rtw89_core_register(rtwdev);
  3221. if (ret) {
  3222. rtw89_err(rtwdev, "failed to register core\n");
  3223. goto err_free_irq;
  3224. }
  3225. return 0;
  3226. err_free_irq:
  3227. rtw89_pci_free_irq(rtwdev, pdev);
  3228. err_deinit_napi:
  3229. rtw89_core_napi_deinit(rtwdev);
  3230. err_clear_resource:
  3231. rtw89_pci_clear_resource(rtwdev, pdev);
  3232. err_declaim_pci:
  3233. rtw89_pci_declaim_device(rtwdev, pdev);
  3234. err_core_deinit:
  3235. rtw89_core_deinit(rtwdev);
  3236. err_release_hw:
  3237. rtw89_free_ieee80211_hw(rtwdev);
  3238. return ret;
  3239. }
  3240. EXPORT_SYMBOL(rtw89_pci_probe);
  3241. void rtw89_pci_remove(struct pci_dev *pdev)
  3242. {
  3243. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  3244. struct rtw89_dev *rtwdev;
  3245. rtwdev = hw->priv;
  3246. rtw89_pci_free_irq(rtwdev, pdev);
  3247. rtw89_core_napi_deinit(rtwdev);
  3248. rtw89_core_unregister(rtwdev);
  3249. rtw89_pci_clear_resource(rtwdev, pdev);
  3250. rtw89_pci_declaim_device(rtwdev, pdev);
  3251. rtw89_core_deinit(rtwdev);
  3252. rtw89_free_ieee80211_hw(rtwdev);
  3253. }
  3254. EXPORT_SYMBOL(rtw89_pci_remove);
  3255. MODULE_AUTHOR("Realtek Corporation");
  3256. MODULE_DESCRIPTION("Realtek PCI 802.11ax wireless driver");
  3257. MODULE_LICENSE("Dual BSD/GPL");