pci.c 165 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/cma.h>
  7. #include <linux/completion.h>
  8. #include <linux/io.h>
  9. #include <linux/irq.h>
  10. #include <linux/memblock.h>
  11. #include <linux/module.h>
  12. #include <linux/msi.h>
  13. #include <linux/of.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/of_reserved_mem.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/suspend.h>
  18. #include <linux/version.h>
  19. #include "main.h"
  20. #include "bus.h"
  21. #include "debug.h"
  22. #include "pci.h"
  23. #include "reg.h"
  24. #define PCI_LINK_UP 1
  25. #define PCI_LINK_DOWN 0
  26. #define SAVE_PCI_CONFIG_SPACE 1
  27. #define RESTORE_PCI_CONFIG_SPACE 0
  28. #define PM_OPTIONS_DEFAULT 0
  29. #define PCI_BAR_NUM 0
  30. #define PCI_INVALID_READ(val) ((val) == U32_MAX)
  31. #define PCI_DMA_MASK_32_BIT DMA_BIT_MASK(32)
  32. #define PCI_DMA_MASK_36_BIT DMA_BIT_MASK(36)
  33. #define PCI_DMA_MASK_64_BIT DMA_BIT_MASK(64)
  34. #define MHI_NODE_NAME "qcom,mhi"
  35. #define MHI_MSI_NAME "MHI"
  36. #define QCA6390_PATH_PREFIX "qca6390/"
  37. #define QCA6490_PATH_PREFIX "qca6490/"
  38. #define KIWI_PATH_PREFIX "kiwi/"
  39. #define DEFAULT_PHY_M3_FILE_NAME "m3.bin"
  40. #define DEFAULT_PHY_UCODE_FILE_NAME "phy_ucode.elf"
  41. #define PHY_UCODE_V2_FILE_NAME "phy_ucode20.elf"
  42. #define DEFAULT_FW_FILE_NAME "amss.bin"
  43. #define FW_V2_FILE_NAME "amss20.bin"
  44. #define DEVICE_MAJOR_VERSION_MASK 0xF
  45. #define WAKE_MSI_NAME "WAKE"
  46. #define DEV_RDDM_TIMEOUT 5000
  47. #define WAKE_EVENT_TIMEOUT 5000
  48. #ifdef CONFIG_CNSS_EMULATION
  49. #define EMULATION_HW 1
  50. #else
  51. #define EMULATION_HW 0
  52. #endif
  53. #define RAMDUMP_SIZE_DEFAULT 0x420000
  54. #define DEVICE_RDDM_COOKIE 0xCAFECACE
  55. static DEFINE_SPINLOCK(pci_link_down_lock);
  56. static DEFINE_SPINLOCK(pci_reg_window_lock);
  57. static DEFINE_SPINLOCK(time_sync_lock);
  58. #define MHI_TIMEOUT_OVERWRITE_MS (plat_priv->ctrl_params.mhi_timeout)
  59. #define MHI_M2_TIMEOUT_MS (plat_priv->ctrl_params.mhi_m2_timeout)
  60. #define WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US 1000
  61. #define WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US 2000
  62. #define FORCE_WAKE_DELAY_MIN_US 4000
  63. #define FORCE_WAKE_DELAY_MAX_US 6000
  64. #define FORCE_WAKE_DELAY_TIMEOUT_US 60000
  65. #define LINK_TRAINING_RETRY_MAX_TIMES 3
  66. #define LINK_TRAINING_RETRY_DELAY_MS 500
  67. #define MHI_SUSPEND_RETRY_MAX_TIMES 3
  68. #define MHI_SUSPEND_RETRY_DELAY_US 5000
  69. #define BOOT_DEBUG_TIMEOUT_MS 7000
  70. #define HANG_DATA_LENGTH 384
  71. #define HST_HANG_DATA_OFFSET ((3 * 1024 * 1024) - HANG_DATA_LENGTH)
  72. #define HSP_HANG_DATA_OFFSET ((2 * 1024 * 1024) - HANG_DATA_LENGTH)
  73. static const struct mhi_channel_config cnss_mhi_channels[] = {
  74. {
  75. .num = 0,
  76. .name = "LOOPBACK",
  77. .num_elements = 32,
  78. .event_ring = 1,
  79. .dir = DMA_TO_DEVICE,
  80. .ee_mask = 0x4,
  81. .pollcfg = 0,
  82. .doorbell = MHI_DB_BRST_DISABLE,
  83. .lpm_notify = false,
  84. .offload_channel = false,
  85. .doorbell_mode_switch = false,
  86. .auto_queue = false,
  87. },
  88. {
  89. .num = 1,
  90. .name = "LOOPBACK",
  91. .num_elements = 32,
  92. .event_ring = 1,
  93. .dir = DMA_FROM_DEVICE,
  94. .ee_mask = 0x4,
  95. .pollcfg = 0,
  96. .doorbell = MHI_DB_BRST_DISABLE,
  97. .lpm_notify = false,
  98. .offload_channel = false,
  99. .doorbell_mode_switch = false,
  100. .auto_queue = false,
  101. },
  102. {
  103. .num = 4,
  104. .name = "DIAG",
  105. .num_elements = 64,
  106. .event_ring = 1,
  107. .dir = DMA_TO_DEVICE,
  108. .ee_mask = 0x4,
  109. .pollcfg = 0,
  110. .doorbell = MHI_DB_BRST_DISABLE,
  111. .lpm_notify = false,
  112. .offload_channel = false,
  113. .doorbell_mode_switch = false,
  114. .auto_queue = false,
  115. },
  116. {
  117. .num = 5,
  118. .name = "DIAG",
  119. .num_elements = 64,
  120. .event_ring = 1,
  121. .dir = DMA_FROM_DEVICE,
  122. .ee_mask = 0x4,
  123. .pollcfg = 0,
  124. .doorbell = MHI_DB_BRST_DISABLE,
  125. .lpm_notify = false,
  126. .offload_channel = false,
  127. .doorbell_mode_switch = false,
  128. .auto_queue = false,
  129. },
  130. {
  131. .num = 20,
  132. .name = "IPCR",
  133. .num_elements = 64,
  134. .event_ring = 1,
  135. .dir = DMA_TO_DEVICE,
  136. .ee_mask = 0x4,
  137. .pollcfg = 0,
  138. .doorbell = MHI_DB_BRST_DISABLE,
  139. .lpm_notify = false,
  140. .offload_channel = false,
  141. .doorbell_mode_switch = false,
  142. .auto_queue = false,
  143. },
  144. {
  145. .num = 21,
  146. .name = "IPCR",
  147. .num_elements = 64,
  148. .event_ring = 1,
  149. .dir = DMA_FROM_DEVICE,
  150. .ee_mask = 0x4,
  151. .pollcfg = 0,
  152. .doorbell = MHI_DB_BRST_DISABLE,
  153. .lpm_notify = false,
  154. .offload_channel = false,
  155. .doorbell_mode_switch = false,
  156. .auto_queue = true,
  157. },
  158. };
  159. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0))
  160. static struct mhi_event_config cnss_mhi_events[] = {
  161. #else
  162. static const struct mhi_event_config cnss_mhi_events[] = {
  163. #endif
  164. {
  165. .num_elements = 32,
  166. .irq_moderation_ms = 0,
  167. .irq = 1,
  168. .mode = MHI_DB_BRST_DISABLE,
  169. .data_type = MHI_ER_CTRL,
  170. .priority = 0,
  171. .hardware_event = false,
  172. .client_managed = false,
  173. .offload_channel = false,
  174. },
  175. {
  176. .num_elements = 256,
  177. .irq_moderation_ms = 0,
  178. .irq = 2,
  179. .mode = MHI_DB_BRST_DISABLE,
  180. .priority = 1,
  181. .hardware_event = false,
  182. .client_managed = false,
  183. .offload_channel = false,
  184. },
  185. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  186. {
  187. .num_elements = 32,
  188. .irq_moderation_ms = 0,
  189. .irq = 1,
  190. .mode = MHI_DB_BRST_DISABLE,
  191. .data_type = MHI_ER_BW_SCALE,
  192. .priority = 2,
  193. .hardware_event = false,
  194. .client_managed = false,
  195. .offload_channel = false,
  196. },
  197. #endif
  198. };
  199. static const struct mhi_controller_config cnss_mhi_config = {
  200. .max_channels = 32,
  201. .timeout_ms = 10000,
  202. .use_bounce_buf = false,
  203. .buf_len = 0x8000,
  204. .num_channels = ARRAY_SIZE(cnss_mhi_channels),
  205. .ch_cfg = cnss_mhi_channels,
  206. .num_events = ARRAY_SIZE(cnss_mhi_events),
  207. .event_cfg = cnss_mhi_events,
  208. .m2_no_db = true,
  209. };
  210. static struct cnss_pci_reg ce_src[] = {
  211. { "SRC_RING_BASE_LSB", CE_SRC_RING_BASE_LSB_OFFSET },
  212. { "SRC_RING_BASE_MSB", CE_SRC_RING_BASE_MSB_OFFSET },
  213. { "SRC_RING_ID", CE_SRC_RING_ID_OFFSET },
  214. { "SRC_RING_MISC", CE_SRC_RING_MISC_OFFSET },
  215. { "SRC_CTRL", CE_SRC_CTRL_OFFSET },
  216. { "SRC_R0_CE_CH_SRC_IS", CE_SRC_R0_CE_CH_SRC_IS_OFFSET },
  217. { "SRC_RING_HP", CE_SRC_RING_HP_OFFSET },
  218. { "SRC_RING_TP", CE_SRC_RING_TP_OFFSET },
  219. { NULL },
  220. };
  221. static struct cnss_pci_reg ce_dst[] = {
  222. { "DEST_RING_BASE_LSB", CE_DEST_RING_BASE_LSB_OFFSET },
  223. { "DEST_RING_BASE_MSB", CE_DEST_RING_BASE_MSB_OFFSET },
  224. { "DEST_RING_ID", CE_DEST_RING_ID_OFFSET },
  225. { "DEST_RING_MISC", CE_DEST_RING_MISC_OFFSET },
  226. { "DEST_CTRL", CE_DEST_CTRL_OFFSET },
  227. { "CE_CH_DST_IS", CE_CH_DST_IS_OFFSET },
  228. { "CE_CH_DEST_CTRL2", CE_CH_DEST_CTRL2_OFFSET },
  229. { "DEST_RING_HP", CE_DEST_RING_HP_OFFSET },
  230. { "DEST_RING_TP", CE_DEST_RING_TP_OFFSET },
  231. { "STATUS_RING_BASE_LSB", CE_STATUS_RING_BASE_LSB_OFFSET },
  232. { "STATUS_RING_BASE_MSB", CE_STATUS_RING_BASE_MSB_OFFSET },
  233. { "STATUS_RING_ID", CE_STATUS_RING_ID_OFFSET },
  234. { "STATUS_RING_MISC", CE_STATUS_RING_MISC_OFFSET },
  235. { "STATUS_RING_HP", CE_STATUS_RING_HP_OFFSET },
  236. { "STATUS_RING_TP", CE_STATUS_RING_TP_OFFSET },
  237. { NULL },
  238. };
  239. static struct cnss_pci_reg ce_cmn[] = {
  240. { "GXI_ERR_INTS", CE_COMMON_GXI_ERR_INTS },
  241. { "GXI_ERR_STATS", CE_COMMON_GXI_ERR_STATS },
  242. { "GXI_WDOG_STATUS", CE_COMMON_GXI_WDOG_STATUS },
  243. { "TARGET_IE_0", CE_COMMON_TARGET_IE_0 },
  244. { "TARGET_IE_1", CE_COMMON_TARGET_IE_1 },
  245. { NULL },
  246. };
  247. static struct cnss_pci_reg qdss_csr[] = {
  248. { "QDSSCSR_ETRIRQCTRL", QDSS_APB_DEC_CSR_ETRIRQCTRL_OFFSET },
  249. { "QDSSCSR_PRESERVEETF", QDSS_APB_DEC_CSR_PRESERVEETF_OFFSET },
  250. { "QDSSCSR_PRESERVEETR0", QDSS_APB_DEC_CSR_PRESERVEETR0_OFFSET },
  251. { "QDSSCSR_PRESERVEETR1", QDSS_APB_DEC_CSR_PRESERVEETR1_OFFSET },
  252. { NULL },
  253. };
  254. static struct cnss_pci_reg pci_scratch[] = {
  255. { "PCIE_SCRATCH_0", PCIE_SCRATCH_0_SOC_PCIE_REG },
  256. { "PCIE_SCRATCH_1", PCIE_SCRATCH_1_SOC_PCIE_REG },
  257. { "PCIE_SCRATCH_2", PCIE_SCRATCH_2_SOC_PCIE_REG },
  258. { NULL },
  259. };
  260. /* First field of the structure is the device bit mask. Use
  261. * enum cnss_pci_reg_mask as reference for the value.
  262. */
  263. static struct cnss_misc_reg wcss_reg_access_seq[] = {
  264. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  265. {1, 1, QCA6390_GCC_DEBUG_CLK_CTL, 0x802},
  266. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  267. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_PLL_MODE, 0},
  268. {1, 1, QCA6390_GCC_DEBUG_CLK_CTL, 0x805},
  269. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  270. {1, 0, QCA6390_WCSS_WFSS_PMM_WFSS_PMM_R0_PMM_CTRL, 0},
  271. {1, 0, QCA6390_WCSS_PMM_TOP_PMU_CX_CSR, 0},
  272. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_RAW_STAT, 0},
  273. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_EN, 0},
  274. {1, 0, QCA6390_WCSS_PMM_TOP_PMU_TESTBUS_STS, 0},
  275. {1, 1, QCA6390_WCSS_PMM_TOP_PMU_TESTBUS_CTL, 0xD},
  276. {1, 0, QCA6390_WCSS_PMM_TOP_TESTBUS_STS, 0},
  277. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG, 0},
  278. {1, 1, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG, 0},
  279. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x8},
  280. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  281. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_STS, 0},
  282. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_CTL, 0},
  283. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_SLP_SEQ_ENTRY_0, 0},
  284. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_SLP_SEQ_ENTRY_9, 0},
  285. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS0, 0},
  286. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS1, 0},
  287. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS2, 0},
  288. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS3, 0},
  289. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS4, 0},
  290. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS5, 0},
  291. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS6, 0},
  292. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE0, 0},
  293. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE1, 0},
  294. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE2, 0},
  295. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE3, 0},
  296. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE4, 0},
  297. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE5, 0},
  298. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE6, 0},
  299. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING0, 0},
  300. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING1, 0},
  301. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING2, 0},
  302. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING3, 0},
  303. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING4, 0},
  304. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING5, 0},
  305. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING6, 0},
  306. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x30040},
  307. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  308. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  309. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  310. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  311. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x30105},
  312. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  313. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  314. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  315. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  316. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  317. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  318. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  319. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  320. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  321. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_NOC_CBCR, 0},
  322. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_AHB_CBCR, 0},
  323. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_GDSCR, 0},
  324. {1, 0, QCA6390_WCSS_CC_WCSS_WLAN1_GDSCR, 0},
  325. {1, 0, QCA6390_WCSS_CC_WCSS_WLAN2_GDSCR, 0},
  326. {1, 0, QCA6390_WCSS_PMM_TOP_PMM_INT_CLR, 0},
  327. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_STICKY_EN, 0},
  328. };
  329. static struct cnss_misc_reg pcie_reg_access_seq[] = {
  330. {1, 0, QCA6390_PCIE_PCIE_WCSS_STATUS_FOR_DEBUG_LOW_PCIE_LOCAL_REG, 0},
  331. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  332. {1, 1, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0x18},
  333. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  334. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  335. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_STATUS_SOC_PCIE_REG, 0},
  336. {1, 0, QCA6390_PCIE_SOC_COMMIT_REPLAY_SOC_PCIE_REG, 0},
  337. {1, 0, QCA6390_TLMM_GPIO_IN_OUT57, 0},
  338. {1, 0, QCA6390_TLMM_GPIO_INTR_CFG57, 0},
  339. {1, 0, QCA6390_TLMM_GPIO_INTR_STATUS57, 0},
  340. {1, 0, QCA6390_TLMM_GPIO_IN_OUT59, 0},
  341. {1, 0, QCA6390_TLMM_GPIO_INTR_CFG59, 0},
  342. {1, 0, QCA6390_TLMM_GPIO_INTR_STATUS59, 0},
  343. {1, 0, QCA6390_PCIE_PCIE_PARF_LTSSM, 0},
  344. {1, 0, QCA6390_PCIE_PCIE_PARF_PM_STTS, 0},
  345. {1, 0, QCA6390_PCIE_PCIE_PARF_PM_STTS_1, 0},
  346. {1, 0, QCA6390_PCIE_PCIE_PARF_INT_STATUS, 0},
  347. {1, 0, QCA6390_PCIE_PCIE_INT_ALL_STATUS, 0},
  348. {1, 0, QCA6390_PCIE_PCIE_INT_ALL_MASK, 0},
  349. {1, 0, QCA6390_PCIE_PCIE_PARF_BDF_TO_SID_CFG, 0},
  350. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_4, 0},
  351. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_3, 0},
  352. {1, 0, QCA6390_PCIE_PCIE_PARF_MHI_CLOCK_RESET_CTRL, 0},
  353. {1, 0, QCA6390_PCIE_PCIE_PARF_MHI_BASE_ADDR_LOWER, 0},
  354. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_MODE_HANDLER_STATUS, 0},
  355. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_MODE_HANDLER_CFG, 0},
  356. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2, 0},
  357. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L1SUB, 0},
  358. {1, 0, QCA6390_PCIE_PCIE_CORE_CONFIG, 0},
  359. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_4, 0},
  360. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L2, 0},
  361. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L1, 0},
  362. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1, 0},
  363. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2, 0},
  364. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSSAON_PCIE_SR_STATUS_HIGH, 0},
  365. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSSAON_PCIE_SR_STATUS_LOW, 0},
  366. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSS_STATUS_FOR_DEBUG_HIGH, 0},
  367. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSS_STATUS_FOR_DEBUG_LOW, 0},
  368. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN1_STATUS_REG2, 0},
  369. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN2_STATUS_REG2, 0},
  370. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_PMM_WLAN2_CFG_REG1, 0},
  371. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_PMM_WLAN1_CFG_REG1, 0},
  372. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN2_APS_STATUS_REG1, 0},
  373. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN1_APS_STATUS_REG1, 0},
  374. {1, 0, QCA6390_PCIE_PCIE_BHI_EXECENV_REG, 0},
  375. };
  376. static struct cnss_misc_reg wlaon_reg_access_seq[] = {
  377. {3, 0, WLAON_SOC_POWER_CTRL, 0},
  378. {3, 0, WLAON_SOC_PWR_WDG_BARK_THRSHD, 0},
  379. {3, 0, WLAON_SOC_PWR_WDG_BITE_THRSHD, 0},
  380. {3, 0, WLAON_SW_COLD_RESET, 0},
  381. {3, 0, WLAON_RFA_MEM_SLP_NRET_N_OVERRIDE, 0},
  382. {3, 0, WLAON_GDSC_DELAY_SETTING, 0},
  383. {3, 0, WLAON_GDSC_DELAY_SETTING2, 0},
  384. {3, 0, WLAON_WL_PWR_STATUS_REG, 0},
  385. {3, 0, WLAON_WL_AON_DBG_CFG_REG, 0},
  386. {2, 0, WLAON_WL_AON_DBG_ENABLE_GRP0_REG, 0},
  387. {2, 0, WLAON_WL_AON_DBG_ENABLE_GRP1_REG, 0},
  388. {2, 0, WLAON_WL_AON_APM_CFG_CTRL0, 0},
  389. {2, 0, WLAON_WL_AON_APM_CFG_CTRL1, 0},
  390. {2, 0, WLAON_WL_AON_APM_CFG_CTRL2, 0},
  391. {2, 0, WLAON_WL_AON_APM_CFG_CTRL3, 0},
  392. {2, 0, WLAON_WL_AON_APM_CFG_CTRL4, 0},
  393. {2, 0, WLAON_WL_AON_APM_CFG_CTRL5, 0},
  394. {2, 0, WLAON_WL_AON_APM_CFG_CTRL5_1, 0},
  395. {2, 0, WLAON_WL_AON_APM_CFG_CTRL6, 0},
  396. {2, 0, WLAON_WL_AON_APM_CFG_CTRL6_1, 0},
  397. {2, 0, WLAON_WL_AON_APM_CFG_CTRL7, 0},
  398. {2, 0, WLAON_WL_AON_APM_CFG_CTRL8, 0},
  399. {2, 0, WLAON_WL_AON_APM_CFG_CTRL8_1, 0},
  400. {2, 0, WLAON_WL_AON_APM_CFG_CTRL9, 0},
  401. {2, 0, WLAON_WL_AON_APM_CFG_CTRL9_1, 0},
  402. {2, 0, WLAON_WL_AON_APM_CFG_CTRL10, 0},
  403. {2, 0, WLAON_WL_AON_APM_CFG_CTRL11, 0},
  404. {2, 0, WLAON_WL_AON_APM_CFG_CTRL12, 0},
  405. {2, 0, WLAON_WL_AON_APM_OVERRIDE_REG, 0},
  406. {2, 0, WLAON_WL_AON_CXPC_REG, 0},
  407. {2, 0, WLAON_WL_AON_APM_STATUS0, 0},
  408. {2, 0, WLAON_WL_AON_APM_STATUS1, 0},
  409. {2, 0, WLAON_WL_AON_APM_STATUS2, 0},
  410. {2, 0, WLAON_WL_AON_APM_STATUS3, 0},
  411. {2, 0, WLAON_WL_AON_APM_STATUS4, 0},
  412. {2, 0, WLAON_WL_AON_APM_STATUS5, 0},
  413. {2, 0, WLAON_WL_AON_APM_STATUS6, 0},
  414. {3, 0, WLAON_GLOBAL_COUNTER_CTRL1, 0},
  415. {3, 0, WLAON_GLOBAL_COUNTER_CTRL6, 0},
  416. {3, 0, WLAON_GLOBAL_COUNTER_CTRL7, 0},
  417. {3, 0, WLAON_GLOBAL_COUNTER_CTRL3, 0},
  418. {3, 0, WLAON_GLOBAL_COUNTER_CTRL4, 0},
  419. {3, 0, WLAON_GLOBAL_COUNTER_CTRL5, 0},
  420. {3, 0, WLAON_GLOBAL_COUNTER_CTRL8, 0},
  421. {3, 0, WLAON_GLOBAL_COUNTER_CTRL2, 0},
  422. {3, 0, WLAON_GLOBAL_COUNTER_CTRL9, 0},
  423. {3, 0, WLAON_RTC_CLK_CAL_CTRL1, 0},
  424. {3, 0, WLAON_RTC_CLK_CAL_CTRL2, 0},
  425. {3, 0, WLAON_RTC_CLK_CAL_CTRL3, 0},
  426. {3, 0, WLAON_RTC_CLK_CAL_CTRL4, 0},
  427. {3, 0, WLAON_RTC_CLK_CAL_CTRL5, 0},
  428. {3, 0, WLAON_RTC_CLK_CAL_CTRL6, 0},
  429. {3, 0, WLAON_RTC_CLK_CAL_CTRL7, 0},
  430. {3, 0, WLAON_RTC_CLK_CAL_CTRL8, 0},
  431. {3, 0, WLAON_RTC_CLK_CAL_CTRL9, 0},
  432. {3, 0, WLAON_WCSSAON_CONFIG_REG, 0},
  433. {3, 0, WLAON_WLAN_OEM_DEBUG_REG, 0},
  434. {3, 0, WLAON_WLAN_RAM_DUMP_REG, 0},
  435. {3, 0, WLAON_QDSS_WCSS_REG, 0},
  436. {3, 0, WLAON_QDSS_WCSS_ACK, 0},
  437. {3, 0, WLAON_WL_CLK_CNTL_KDF_REG, 0},
  438. {3, 0, WLAON_WL_CLK_CNTL_PMU_HFRC_REG, 0},
  439. {3, 0, WLAON_QFPROM_PWR_CTRL_REG, 0},
  440. {3, 0, WLAON_DLY_CONFIG, 0},
  441. {3, 0, WLAON_WLAON_Q6_IRQ_REG, 0},
  442. {3, 0, WLAON_PCIE_INTF_SW_CFG_REG, 0},
  443. {3, 0, WLAON_PCIE_INTF_STICKY_SW_CFG_REG, 0},
  444. {3, 0, WLAON_PCIE_INTF_PHY_SW_CFG_REG, 0},
  445. {3, 0, WLAON_PCIE_INTF_PHY_NOCSR_SW_CFG_REG, 0},
  446. {3, 0, WLAON_Q6_COOKIE_BIT, 0},
  447. {3, 0, WLAON_WARM_SW_ENTRY, 0},
  448. {3, 0, WLAON_RESET_DBG_SW_ENTRY, 0},
  449. {3, 0, WLAON_WL_PMUNOC_CFG_REG, 0},
  450. {3, 0, WLAON_RESET_CAUSE_CFG_REG, 0},
  451. {3, 0, WLAON_SOC_WCSSAON_WAKEUP_IRQ_7_EN_REG, 0},
  452. {3, 0, WLAON_DEBUG, 0},
  453. {3, 0, WLAON_SOC_PARAMETERS, 0},
  454. {3, 0, WLAON_WLPM_SIGNAL, 0},
  455. {3, 0, WLAON_SOC_RESET_CAUSE_REG, 0},
  456. {3, 0, WLAON_WAKEUP_PCIE_SOC_REG, 0},
  457. {3, 0, WLAON_PBL_STACK_CANARY, 0},
  458. {3, 0, WLAON_MEM_TOT_NUM_GRP_REG, 0},
  459. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP0_REG, 0},
  460. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP1_REG, 0},
  461. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP2_REG, 0},
  462. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP3_REG, 0},
  463. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP0_REG, 0},
  464. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP1_REG, 0},
  465. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP2_REG, 0},
  466. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP3_REG, 0},
  467. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP0_REG, 0},
  468. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP1_REG, 0},
  469. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP2_REG, 0},
  470. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP3_REG, 0},
  471. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP0_REG, 0},
  472. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP1_REG, 0},
  473. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP2_REG, 0},
  474. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP3_REG, 0},
  475. {3, 0, WLAON_MEM_CNT_SEL_REG, 0},
  476. {3, 0, WLAON_MEM_NO_EXTBHS_REG, 0},
  477. {3, 0, WLAON_MEM_DEBUG_REG, 0},
  478. {3, 0, WLAON_MEM_DEBUG_BUS_REG, 0},
  479. {3, 0, WLAON_MEM_REDUN_CFG_REG, 0},
  480. {3, 0, WLAON_WL_AON_SPARE2, 0},
  481. {3, 0, WLAON_VSEL_CFG_FOR_WL_RET_DISABLE_REG, 0},
  482. {3, 0, WLAON_BTFM_WLAN_IPC_STATUS_REG, 0},
  483. {3, 0, WLAON_MPM_COUNTER_CHICKEN_BITS, 0},
  484. {3, 0, WLAON_WLPM_CHICKEN_BITS, 0},
  485. {3, 0, WLAON_PCIE_PHY_PWR_REG, 0},
  486. {3, 0, WLAON_WL_CLK_CNTL_PMU_LPO2M_REG, 0},
  487. {3, 0, WLAON_WL_SS_ROOT_CLK_SWITCH_REG, 0},
  488. {3, 0, WLAON_POWERCTRL_PMU_REG, 0},
  489. {3, 0, WLAON_POWERCTRL_MEM_REG, 0},
  490. {3, 0, WLAON_PCIE_PWR_CTRL_REG, 0},
  491. {3, 0, WLAON_SOC_PWR_PROFILE_REG, 0},
  492. {3, 0, WLAON_WCSSAON_PCIE_SR_STATUS_HI_REG, 0},
  493. {3, 0, WLAON_WCSSAON_PCIE_SR_STATUS_LO_REG, 0},
  494. {3, 0, WLAON_WCSS_TCSR_PMM_SR_STATUS_HI_REG, 0},
  495. {3, 0, WLAON_WCSS_TCSR_PMM_SR_STATUS_LO_REG, 0},
  496. {3, 0, WLAON_MEM_SVS_CFG_REG, 0},
  497. {3, 0, WLAON_CMN_AON_MISC_REG, 0},
  498. {3, 0, WLAON_INTR_STATUS, 0},
  499. {2, 0, WLAON_INTR_ENABLE, 0},
  500. {2, 0, WLAON_NOC_DBG_BUS_SEL_REG, 0},
  501. {2, 0, WLAON_NOC_DBG_BUS_REG, 0},
  502. {2, 0, WLAON_WL_CTRL_MISC_REG, 0},
  503. {2, 0, WLAON_DBG_STATUS0, 0},
  504. {2, 0, WLAON_DBG_STATUS1, 0},
  505. {2, 0, WLAON_TIMERSYNC_OFFSET_L, 0},
  506. {2, 0, WLAON_TIMERSYNC_OFFSET_H, 0},
  507. {2, 0, WLAON_PMU_LDO_SETTLE_REG, 0},
  508. };
  509. static struct cnss_misc_reg syspm_reg_access_seq[] = {
  510. {1, 0, QCA6390_SYSPM_SYSPM_PWR_STATUS, 0},
  511. {1, 0, QCA6390_SYSPM_DBG_BTFM_AON_REG, 0},
  512. {1, 0, QCA6390_SYSPM_DBG_BUS_SEL_REG, 0},
  513. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  514. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  515. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  516. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  517. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  518. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  519. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  520. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  521. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  522. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  523. };
  524. #define WCSS_REG_SIZE ARRAY_SIZE(wcss_reg_access_seq)
  525. #define PCIE_REG_SIZE ARRAY_SIZE(pcie_reg_access_seq)
  526. #define WLAON_REG_SIZE ARRAY_SIZE(wlaon_reg_access_seq)
  527. #define SYSPM_REG_SIZE ARRAY_SIZE(syspm_reg_access_seq)
  528. #if IS_ENABLED(CONFIG_PCI_MSM)
  529. /**
  530. * _cnss_pci_enumerate() - Enumerate PCIe endpoints
  531. * @plat_priv: driver platform context pointer
  532. * @rc_num: root complex index that an endpoint connects to
  533. *
  534. * This function shall call corresponding PCIe root complex driver APIs
  535. * to power on root complex and enumerate the endpoint connected to it.
  536. *
  537. * Return: 0 for success, negative value for error
  538. */
  539. static int _cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  540. {
  541. return msm_pcie_enumerate(rc_num);
  542. }
  543. /**
  544. * cnss_pci_assert_perst() - Assert PCIe PERST GPIO
  545. * @pci_priv: driver PCI bus context pointer
  546. *
  547. * This function shall call corresponding PCIe root complex driver APIs
  548. * to assert PCIe PERST GPIO.
  549. *
  550. * Return: 0 for success, negative value for error
  551. */
  552. static int cnss_pci_assert_perst(struct cnss_pci_data *pci_priv)
  553. {
  554. struct pci_dev *pci_dev = pci_priv->pci_dev;
  555. return msm_pcie_pm_control(MSM_PCIE_HANDLE_LINKDOWN,
  556. pci_dev->bus->number, pci_dev, NULL,
  557. PM_OPTIONS_DEFAULT);
  558. }
  559. /**
  560. * cnss_pci_disable_pc() - Disable PCIe link power collapse from RC driver
  561. * @pci_priv: driver PCI bus context pointer
  562. * @vote: value to indicate disable (true) or enable (false)
  563. *
  564. * This function shall call corresponding PCIe root complex driver APIs
  565. * to disable PCIe power collapse. The purpose of this API is to avoid
  566. * root complex driver still controlling PCIe link from callbacks of
  567. * system suspend/resume. Device driver itself should take full control
  568. * of the link in such cases.
  569. *
  570. * Return: 0 for success, negative value for error
  571. */
  572. static int cnss_pci_disable_pc(struct cnss_pci_data *pci_priv, bool vote)
  573. {
  574. struct pci_dev *pci_dev = pci_priv->pci_dev;
  575. return msm_pcie_pm_control(vote ? MSM_PCIE_DISABLE_PC :
  576. MSM_PCIE_ENABLE_PC,
  577. pci_dev->bus->number, pci_dev, NULL,
  578. PM_OPTIONS_DEFAULT);
  579. }
  580. /**
  581. * cnss_pci_set_link_bandwidth() - Update number of lanes and speed of
  582. * PCIe link
  583. * @pci_priv: driver PCI bus context pointer
  584. * @link_speed: PCIe link gen speed
  585. * @link_width: number of lanes for PCIe link
  586. *
  587. * This function shall call corresponding PCIe root complex driver APIs
  588. * to update number of lanes and speed of the link.
  589. *
  590. * Return: 0 for success, negative value for error
  591. */
  592. static int cnss_pci_set_link_bandwidth(struct cnss_pci_data *pci_priv,
  593. u16 link_speed, u16 link_width)
  594. {
  595. return msm_pcie_set_link_bandwidth(pci_priv->pci_dev,
  596. link_speed, link_width);
  597. }
  598. /**
  599. * cnss_pci_set_max_link_speed() - Set the maximum speed PCIe can link up with
  600. * @pci_priv: driver PCI bus context pointer
  601. * @rc_num: root complex index that an endpoint connects to
  602. * @link_speed: PCIe link gen speed
  603. *
  604. * This function shall call corresponding PCIe root complex driver APIs
  605. * to update the maximum speed that PCIe can link up with.
  606. *
  607. * Return: 0 for success, negative value for error
  608. */
  609. static int cnss_pci_set_max_link_speed(struct cnss_pci_data *pci_priv,
  610. u32 rc_num, u16 link_speed)
  611. {
  612. return msm_pcie_set_target_link_speed(rc_num, link_speed, false);
  613. }
  614. /**
  615. * _cnss_pci_prevent_l1() - Prevent PCIe L1 and L1 sub-states
  616. * @pci_priv: driver PCI bus context pointer
  617. *
  618. * This function shall call corresponding PCIe root complex driver APIs
  619. * to prevent PCIe link enter L1 and L1 sub-states. The APIs should also
  620. * bring link out of L1 or L1 sub-states if any and avoid synchronization
  621. * issues if any.
  622. *
  623. * Return: 0 for success, negative value for error
  624. */
  625. static int _cnss_pci_prevent_l1(struct cnss_pci_data *pci_priv)
  626. {
  627. return msm_pcie_prevent_l1(pci_priv->pci_dev);
  628. }
  629. /**
  630. * _cnss_pci_allow_l1() - Allow PCIe L1 and L1 sub-states
  631. * @pci_priv: driver PCI bus context pointer
  632. *
  633. * This function shall call corresponding PCIe root complex driver APIs
  634. * to allow PCIe link enter L1 and L1 sub-states. The APIs should avoid
  635. * synchronization issues if any.
  636. *
  637. * Return: 0 for success, negative value for error
  638. */
  639. static void _cnss_pci_allow_l1(struct cnss_pci_data *pci_priv)
  640. {
  641. msm_pcie_allow_l1(pci_priv->pci_dev);
  642. }
  643. /**
  644. * cnss_pci_set_link_up() - Power on or resume PCIe link
  645. * @pci_priv: driver PCI bus context pointer
  646. *
  647. * This function shall call corresponding PCIe root complex driver APIs
  648. * to Power on or resume PCIe link.
  649. *
  650. * Return: 0 for success, negative value for error
  651. */
  652. static int cnss_pci_set_link_up(struct cnss_pci_data *pci_priv)
  653. {
  654. struct pci_dev *pci_dev = pci_priv->pci_dev;
  655. enum msm_pcie_pm_opt pm_ops = MSM_PCIE_RESUME;
  656. u32 pm_options = PM_OPTIONS_DEFAULT;
  657. int ret;
  658. ret = msm_pcie_pm_control(pm_ops, pci_dev->bus->number, pci_dev,
  659. NULL, pm_options);
  660. if (ret)
  661. cnss_pr_err("Failed to resume PCI link with default option, err = %d\n",
  662. ret);
  663. return ret;
  664. }
  665. /**
  666. * cnss_pci_set_link_down() - Power off or suspend PCIe link
  667. * @pci_priv: driver PCI bus context pointer
  668. *
  669. * This function shall call corresponding PCIe root complex driver APIs
  670. * to power off or suspend PCIe link.
  671. *
  672. * Return: 0 for success, negative value for error
  673. */
  674. static int cnss_pci_set_link_down(struct cnss_pci_data *pci_priv)
  675. {
  676. struct pci_dev *pci_dev = pci_priv->pci_dev;
  677. enum msm_pcie_pm_opt pm_ops;
  678. u32 pm_options = PM_OPTIONS_DEFAULT;
  679. int ret;
  680. if (pci_priv->drv_connected_last) {
  681. cnss_pr_vdbg("Use PCIe DRV suspend\n");
  682. pm_ops = MSM_PCIE_DRV_SUSPEND;
  683. } else {
  684. pm_ops = MSM_PCIE_SUSPEND;
  685. }
  686. ret = msm_pcie_pm_control(pm_ops, pci_dev->bus->number, pci_dev,
  687. NULL, pm_options);
  688. if (ret)
  689. cnss_pr_err("Failed to suspend PCI link with default option, err = %d\n",
  690. ret);
  691. return ret;
  692. }
  693. #else
  694. static int _cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  695. {
  696. return -EOPNOTSUPP;
  697. }
  698. static int cnss_pci_assert_perst(struct cnss_pci_data *pci_priv)
  699. {
  700. return -EOPNOTSUPP;
  701. }
  702. static int cnss_pci_disable_pc(struct cnss_pci_data *pci_priv, bool vote)
  703. {
  704. return 0;
  705. }
  706. static int cnss_pci_set_link_bandwidth(struct cnss_pci_data *pci_priv,
  707. u16 link_speed, u16 link_width)
  708. {
  709. return 0;
  710. }
  711. static int cnss_pci_set_max_link_speed(struct cnss_pci_data *pci_priv,
  712. u32 rc_num, u16 link_speed)
  713. {
  714. return 0;
  715. }
  716. static int _cnss_pci_prevent_l1(struct cnss_pci_data *pci_priv)
  717. {
  718. return 0;
  719. }
  720. static void _cnss_pci_allow_l1(struct cnss_pci_data *pci_priv) {}
  721. static int cnss_pci_set_link_up(struct cnss_pci_data *pci_priv)
  722. {
  723. return 0;
  724. }
  725. static int cnss_pci_set_link_down(struct cnss_pci_data *pci_priv)
  726. {
  727. return 0;
  728. }
  729. #endif /* CONFIG_PCI_MSM */
  730. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  731. static void cnss_mhi_debug_reg_dump(struct cnss_pci_data *pci_priv)
  732. {
  733. mhi_debug_reg_dump(pci_priv->mhi_ctrl);
  734. }
  735. static void cnss_mhi_dump_sfr(struct cnss_pci_data *pci_priv)
  736. {
  737. mhi_dump_sfr(pci_priv->mhi_ctrl);
  738. }
  739. static bool cnss_mhi_scan_rddm_cookie(struct cnss_pci_data *pci_priv,
  740. u32 cookie)
  741. {
  742. return mhi_scan_rddm_cookie(pci_priv->mhi_ctrl, cookie);
  743. }
  744. static int cnss_mhi_pm_fast_suspend(struct cnss_pci_data *pci_priv,
  745. bool notify_clients)
  746. {
  747. return mhi_pm_fast_suspend(pci_priv->mhi_ctrl, notify_clients);
  748. }
  749. static int cnss_mhi_pm_fast_resume(struct cnss_pci_data *pci_priv,
  750. bool notify_clients)
  751. {
  752. return mhi_pm_fast_resume(pci_priv->mhi_ctrl, notify_clients);
  753. }
  754. static void cnss_mhi_set_m2_timeout_ms(struct cnss_pci_data *pci_priv,
  755. u32 timeout)
  756. {
  757. return mhi_set_m2_timeout_ms(pci_priv->mhi_ctrl, timeout);
  758. }
  759. static int cnss_mhi_device_get_sync_atomic(struct cnss_pci_data *pci_priv,
  760. int timeout_us, bool in_panic)
  761. {
  762. return mhi_device_get_sync_atomic(pci_priv->mhi_ctrl->mhi_dev,
  763. timeout_us, in_panic);
  764. }
  765. static void
  766. cnss_mhi_controller_set_bw_scale_cb(struct cnss_pci_data *pci_priv,
  767. int (*cb)(struct mhi_controller *mhi_ctrl,
  768. struct mhi_link_info *link_info))
  769. {
  770. mhi_controller_set_bw_scale_cb(pci_priv->mhi_ctrl, cb);
  771. }
  772. static int cnss_mhi_force_reset(struct cnss_pci_data *pci_priv)
  773. {
  774. return mhi_force_reset(pci_priv->mhi_ctrl);
  775. }
  776. #else
  777. static void cnss_mhi_debug_reg_dump(struct cnss_pci_data *pci_priv)
  778. {
  779. }
  780. static void cnss_mhi_dump_sfr(struct cnss_pci_data *pci_priv)
  781. {
  782. }
  783. static bool cnss_mhi_scan_rddm_cookie(struct cnss_pci_data *pci_priv,
  784. u32 cookie)
  785. {
  786. return false;
  787. }
  788. static int cnss_mhi_pm_fast_suspend(struct cnss_pci_data *pci_priv,
  789. bool notify_clients)
  790. {
  791. return -EOPNOTSUPP;
  792. }
  793. static int cnss_mhi_pm_fast_resume(struct cnss_pci_data *pci_priv,
  794. bool notify_clients)
  795. {
  796. return -EOPNOTSUPP;
  797. }
  798. static void cnss_mhi_set_m2_timeout_ms(struct cnss_pci_data *pci_priv,
  799. u32 timeout)
  800. {
  801. }
  802. static int cnss_mhi_device_get_sync_atomic(struct cnss_pci_data *pci_priv,
  803. int timeout_us, bool in_panic)
  804. {
  805. return -EOPNOTSUPP;
  806. }
  807. static void
  808. cnss_mhi_controller_set_bw_scale_cb(struct cnss_pci_data *pci_priv,
  809. int (*cb)(struct mhi_controller *mhi_ctrl,
  810. struct mhi_link_info *link_info))
  811. {
  812. }
  813. static int cnss_mhi_force_reset(struct cnss_pci_data *pci_priv)
  814. {
  815. return -EOPNOTSUPP;
  816. }
  817. #endif /* CONFIG_MHI_BUS_MISC */
  818. int cnss_pci_check_link_status(struct cnss_pci_data *pci_priv)
  819. {
  820. u16 device_id;
  821. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  822. cnss_pr_dbg("%ps: PCIe link is in suspend state\n",
  823. (void *)_RET_IP_);
  824. return -EACCES;
  825. }
  826. if (pci_priv->pci_link_down_ind) {
  827. cnss_pr_err("%ps: PCIe link is down\n", (void *)_RET_IP_);
  828. return -EIO;
  829. }
  830. pci_read_config_word(pci_priv->pci_dev, PCI_DEVICE_ID, &device_id);
  831. if (device_id != pci_priv->device_id) {
  832. cnss_fatal_err("%ps: PCI device ID mismatch, link possibly down, current read ID: 0x%x, record ID: 0x%x\n",
  833. (void *)_RET_IP_, device_id,
  834. pci_priv->device_id);
  835. return -EIO;
  836. }
  837. return 0;
  838. }
  839. static void cnss_pci_select_window(struct cnss_pci_data *pci_priv, u32 offset)
  840. {
  841. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  842. u32 window = (offset >> WINDOW_SHIFT) & WINDOW_VALUE_MASK;
  843. u32 window_enable = WINDOW_ENABLE_BIT | window;
  844. u32 val;
  845. writel_relaxed(window_enable, pci_priv->bar +
  846. QCA6390_PCIE_REMAP_BAR_CTRL_OFFSET);
  847. if (window != pci_priv->remap_window) {
  848. pci_priv->remap_window = window;
  849. cnss_pr_dbg("Config PCIe remap window register to 0x%x\n",
  850. window_enable);
  851. }
  852. /* Read it back to make sure the write has taken effect */
  853. val = readl_relaxed(pci_priv->bar + QCA6390_PCIE_REMAP_BAR_CTRL_OFFSET);
  854. if (val != window_enable) {
  855. cnss_pr_err("Failed to config window register to 0x%x, current value: 0x%x\n",
  856. window_enable, val);
  857. if (!cnss_pci_check_link_status(pci_priv) &&
  858. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  859. CNSS_ASSERT(0);
  860. }
  861. }
  862. static int cnss_pci_reg_read(struct cnss_pci_data *pci_priv,
  863. u32 offset, u32 *val)
  864. {
  865. int ret;
  866. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  867. if (!in_interrupt() && !irqs_disabled()) {
  868. ret = cnss_pci_check_link_status(pci_priv);
  869. if (ret)
  870. return ret;
  871. }
  872. if (pci_priv->pci_dev->device == QCA6174_DEVICE_ID ||
  873. offset < MAX_UNWINDOWED_ADDRESS) {
  874. *val = readl_relaxed(pci_priv->bar + offset);
  875. return 0;
  876. }
  877. /* If in panic, assumption is kernel panic handler will hold all threads
  878. * and interrupts. Further pci_reg_window_lock could be held before
  879. * panic. So only lock during normal operation.
  880. */
  881. if (test_bit(CNSS_IN_PANIC, &plat_priv->driver_state)) {
  882. cnss_pci_select_window(pci_priv, offset);
  883. *val = readl_relaxed(pci_priv->bar + WINDOW_START +
  884. (offset & WINDOW_RANGE_MASK));
  885. } else {
  886. spin_lock_bh(&pci_reg_window_lock);
  887. cnss_pci_select_window(pci_priv, offset);
  888. *val = readl_relaxed(pci_priv->bar + WINDOW_START +
  889. (offset & WINDOW_RANGE_MASK));
  890. spin_unlock_bh(&pci_reg_window_lock);
  891. }
  892. return 0;
  893. }
  894. static int cnss_pci_reg_write(struct cnss_pci_data *pci_priv, u32 offset,
  895. u32 val)
  896. {
  897. int ret;
  898. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  899. if (!in_interrupt() && !irqs_disabled()) {
  900. ret = cnss_pci_check_link_status(pci_priv);
  901. if (ret)
  902. return ret;
  903. }
  904. if (pci_priv->pci_dev->device == QCA6174_DEVICE_ID ||
  905. offset < MAX_UNWINDOWED_ADDRESS) {
  906. writel_relaxed(val, pci_priv->bar + offset);
  907. return 0;
  908. }
  909. /* Same constraint as PCI register read in panic */
  910. if (test_bit(CNSS_IN_PANIC, &plat_priv->driver_state)) {
  911. cnss_pci_select_window(pci_priv, offset);
  912. writel_relaxed(val, pci_priv->bar + WINDOW_START +
  913. (offset & WINDOW_RANGE_MASK));
  914. } else {
  915. spin_lock_bh(&pci_reg_window_lock);
  916. cnss_pci_select_window(pci_priv, offset);
  917. writel_relaxed(val, pci_priv->bar + WINDOW_START +
  918. (offset & WINDOW_RANGE_MASK));
  919. spin_unlock_bh(&pci_reg_window_lock);
  920. }
  921. return 0;
  922. }
  923. static int cnss_pci_force_wake_get(struct cnss_pci_data *pci_priv)
  924. {
  925. struct device *dev = &pci_priv->pci_dev->dev;
  926. int ret;
  927. ret = cnss_pci_force_wake_request_sync(dev,
  928. FORCE_WAKE_DELAY_TIMEOUT_US);
  929. if (ret) {
  930. if (ret != -EAGAIN)
  931. cnss_pr_err("Failed to request force wake\n");
  932. return ret;
  933. }
  934. /* If device's M1 state-change event races here, it can be ignored,
  935. * as the device is expected to immediately move from M2 to M0
  936. * without entering low power state.
  937. */
  938. if (cnss_pci_is_device_awake(dev) != true)
  939. cnss_pr_warn("MHI not in M0, while reg still accessible\n");
  940. return 0;
  941. }
  942. static int cnss_pci_force_wake_put(struct cnss_pci_data *pci_priv)
  943. {
  944. struct device *dev = &pci_priv->pci_dev->dev;
  945. int ret;
  946. ret = cnss_pci_force_wake_release(dev);
  947. if (ret && ret != -EAGAIN)
  948. cnss_pr_err("Failed to release force wake\n");
  949. return ret;
  950. }
  951. #if IS_ENABLED(CONFIG_INTERCONNECT)
  952. /**
  953. * cnss_setup_bus_bandwidth() - Setup interconnect vote for given bandwidth
  954. * @plat_priv: Platform private data struct
  955. * @bw: bandwidth
  956. * @save: toggle flag to save bandwidth to current_bw_vote
  957. *
  958. * Setup bandwidth votes for configured interconnect paths
  959. *
  960. * Return: 0 for success
  961. */
  962. static int cnss_setup_bus_bandwidth(struct cnss_plat_data *plat_priv,
  963. u32 bw, bool save)
  964. {
  965. int ret = 0;
  966. struct cnss_bus_bw_info *bus_bw_info;
  967. if (!plat_priv->icc.path_count)
  968. return -EOPNOTSUPP;
  969. if (bw >= plat_priv->icc.bus_bw_cfg_count) {
  970. cnss_pr_err("Invalid bus bandwidth Type: %d", bw);
  971. return -EINVAL;
  972. }
  973. list_for_each_entry(bus_bw_info, &plat_priv->icc.list_head, list) {
  974. ret = icc_set_bw(bus_bw_info->icc_path,
  975. bus_bw_info->cfg_table[bw].avg_bw,
  976. bus_bw_info->cfg_table[bw].peak_bw);
  977. if (ret) {
  978. cnss_pr_err("Could not set BW Cfg: %d, err = %d ICC Path: %s Val: %d %d\n",
  979. bw, ret, bus_bw_info->icc_name,
  980. bus_bw_info->cfg_table[bw].avg_bw,
  981. bus_bw_info->cfg_table[bw].peak_bw);
  982. break;
  983. }
  984. }
  985. if (ret == 0 && save)
  986. plat_priv->icc.current_bw_vote = bw;
  987. return ret;
  988. }
  989. int cnss_request_bus_bandwidth(struct device *dev, int bandwidth)
  990. {
  991. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  992. if (!plat_priv)
  993. return -ENODEV;
  994. if (bandwidth < 0)
  995. return -EINVAL;
  996. return cnss_setup_bus_bandwidth(plat_priv, (u32)bandwidth, true);
  997. }
  998. #else
  999. static int cnss_setup_bus_bandwidth(struct cnss_plat_data *plat_priv,
  1000. u32 bw, bool save)
  1001. {
  1002. return 0;
  1003. }
  1004. int cnss_request_bus_bandwidth(struct device *dev, int bandwidth)
  1005. {
  1006. return 0;
  1007. }
  1008. #endif
  1009. EXPORT_SYMBOL(cnss_request_bus_bandwidth);
  1010. int cnss_pci_debug_reg_read(struct cnss_pci_data *pci_priv, u32 offset,
  1011. u32 *val, bool raw_access)
  1012. {
  1013. int ret = 0;
  1014. bool do_force_wake_put = true;
  1015. if (raw_access) {
  1016. ret = cnss_pci_reg_read(pci_priv, offset, val);
  1017. goto out;
  1018. }
  1019. ret = cnss_pci_is_device_down(&pci_priv->pci_dev->dev);
  1020. if (ret)
  1021. goto out;
  1022. ret = cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS);
  1023. if (ret < 0)
  1024. goto runtime_pm_put;
  1025. ret = cnss_pci_force_wake_get(pci_priv);
  1026. if (ret)
  1027. do_force_wake_put = false;
  1028. ret = cnss_pci_reg_read(pci_priv, offset, val);
  1029. if (ret) {
  1030. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  1031. offset, ret);
  1032. goto force_wake_put;
  1033. }
  1034. force_wake_put:
  1035. if (do_force_wake_put)
  1036. cnss_pci_force_wake_put(pci_priv);
  1037. runtime_pm_put:
  1038. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  1039. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  1040. out:
  1041. return ret;
  1042. }
  1043. int cnss_pci_debug_reg_write(struct cnss_pci_data *pci_priv, u32 offset,
  1044. u32 val, bool raw_access)
  1045. {
  1046. int ret = 0;
  1047. bool do_force_wake_put = true;
  1048. if (raw_access) {
  1049. ret = cnss_pci_reg_write(pci_priv, offset, val);
  1050. goto out;
  1051. }
  1052. ret = cnss_pci_is_device_down(&pci_priv->pci_dev->dev);
  1053. if (ret)
  1054. goto out;
  1055. ret = cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS);
  1056. if (ret < 0)
  1057. goto runtime_pm_put;
  1058. ret = cnss_pci_force_wake_get(pci_priv);
  1059. if (ret)
  1060. do_force_wake_put = false;
  1061. ret = cnss_pci_reg_write(pci_priv, offset, val);
  1062. if (ret) {
  1063. cnss_pr_err("Failed to write 0x%x to register offset 0x%x, err = %d\n",
  1064. val, offset, ret);
  1065. goto force_wake_put;
  1066. }
  1067. force_wake_put:
  1068. if (do_force_wake_put)
  1069. cnss_pci_force_wake_put(pci_priv);
  1070. runtime_pm_put:
  1071. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  1072. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  1073. out:
  1074. return ret;
  1075. }
  1076. static int cnss_set_pci_config_space(struct cnss_pci_data *pci_priv, bool save)
  1077. {
  1078. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1079. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1080. bool link_down_or_recovery;
  1081. if (!plat_priv)
  1082. return -ENODEV;
  1083. link_down_or_recovery = pci_priv->pci_link_down_ind ||
  1084. (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state));
  1085. if (save) {
  1086. if (link_down_or_recovery) {
  1087. pci_priv->saved_state = NULL;
  1088. } else {
  1089. pci_save_state(pci_dev);
  1090. pci_priv->saved_state = pci_store_saved_state(pci_dev);
  1091. }
  1092. } else {
  1093. if (link_down_or_recovery) {
  1094. pci_load_saved_state(pci_dev, pci_priv->default_state);
  1095. pci_restore_state(pci_dev);
  1096. } else if (pci_priv->saved_state) {
  1097. pci_load_and_free_saved_state(pci_dev,
  1098. &pci_priv->saved_state);
  1099. pci_restore_state(pci_dev);
  1100. }
  1101. }
  1102. return 0;
  1103. }
  1104. static int cnss_pci_get_link_status(struct cnss_pci_data *pci_priv)
  1105. {
  1106. u16 link_status;
  1107. int ret;
  1108. ret = pcie_capability_read_word(pci_priv->pci_dev, PCI_EXP_LNKSTA,
  1109. &link_status);
  1110. if (ret)
  1111. return ret;
  1112. cnss_pr_dbg("Get PCI link status register: %u\n", link_status);
  1113. pci_priv->def_link_speed = link_status & PCI_EXP_LNKSTA_CLS;
  1114. pci_priv->def_link_width =
  1115. (link_status & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
  1116. pci_priv->cur_link_speed = pci_priv->def_link_speed;
  1117. cnss_pr_dbg("Default PCI link speed is 0x%x, link width is 0x%x\n",
  1118. pci_priv->def_link_speed, pci_priv->def_link_width);
  1119. return 0;
  1120. }
  1121. static int cnss_set_pci_link_status(struct cnss_pci_data *pci_priv,
  1122. enum pci_link_status status)
  1123. {
  1124. u16 link_speed, link_width;
  1125. int ret;
  1126. cnss_pr_vdbg("Set PCI link status to: %u\n", status);
  1127. switch (status) {
  1128. case PCI_GEN1:
  1129. link_speed = PCI_EXP_LNKSTA_CLS_2_5GB;
  1130. link_width = PCI_EXP_LNKSTA_NLW_X1 >> PCI_EXP_LNKSTA_NLW_SHIFT;
  1131. break;
  1132. case PCI_GEN2:
  1133. link_speed = PCI_EXP_LNKSTA_CLS_5_0GB;
  1134. link_width = PCI_EXP_LNKSTA_NLW_X1 >> PCI_EXP_LNKSTA_NLW_SHIFT;
  1135. break;
  1136. case PCI_DEF:
  1137. link_speed = pci_priv->def_link_speed;
  1138. link_width = pci_priv->def_link_width;
  1139. if (!link_speed && !link_width) {
  1140. cnss_pr_err("PCI link speed or width is not valid\n");
  1141. return -EINVAL;
  1142. }
  1143. break;
  1144. default:
  1145. cnss_pr_err("Unknown PCI link status config: %u\n", status);
  1146. return -EINVAL;
  1147. }
  1148. ret = cnss_pci_set_link_bandwidth(pci_priv, link_speed, link_width);
  1149. if (!ret)
  1150. pci_priv->cur_link_speed = link_speed;
  1151. return ret;
  1152. }
  1153. static int cnss_set_pci_link(struct cnss_pci_data *pci_priv, bool link_up)
  1154. {
  1155. int ret = 0, retry = 0;
  1156. cnss_pr_vdbg("%s PCI link\n", link_up ? "Resuming" : "Suspending");
  1157. if (link_up) {
  1158. retry:
  1159. ret = cnss_pci_set_link_up(pci_priv);
  1160. if (ret && retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  1161. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  1162. if (pci_priv->pci_link_down_ind)
  1163. msleep(LINK_TRAINING_RETRY_DELAY_MS * retry);
  1164. goto retry;
  1165. }
  1166. } else {
  1167. /* Since DRV suspend cannot be done in Gen 3, set it to
  1168. * Gen 2 if current link speed is larger than Gen 2.
  1169. */
  1170. if (pci_priv->drv_connected_last &&
  1171. pci_priv->cur_link_speed > PCI_EXP_LNKSTA_CLS_5_0GB)
  1172. cnss_set_pci_link_status(pci_priv, PCI_GEN2);
  1173. ret = cnss_pci_set_link_down(pci_priv);
  1174. }
  1175. if (pci_priv->drv_connected_last) {
  1176. if ((link_up && !ret) || (!link_up && ret))
  1177. cnss_set_pci_link_status(pci_priv, PCI_DEF);
  1178. }
  1179. return ret;
  1180. }
  1181. static void cnss_pci_soc_scratch_reg_dump(struct cnss_pci_data *pci_priv)
  1182. {
  1183. u32 reg_offset, val;
  1184. int i;
  1185. switch (pci_priv->device_id) {
  1186. case QCA6390_DEVICE_ID:
  1187. case QCA6490_DEVICE_ID:
  1188. break;
  1189. default:
  1190. return;
  1191. }
  1192. if (in_interrupt() || irqs_disabled())
  1193. return;
  1194. if (cnss_pci_check_link_status(pci_priv))
  1195. return;
  1196. cnss_pr_dbg("Start to dump SOC Scratch registers\n");
  1197. for (i = 0; pci_scratch[i].name; i++) {
  1198. reg_offset = pci_scratch[i].offset;
  1199. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  1200. return;
  1201. cnss_pr_dbg("PCIE_SOC_REG_%s = 0x%x\n",
  1202. pci_scratch[i].name, val);
  1203. }
  1204. }
  1205. int cnss_suspend_pci_link(struct cnss_pci_data *pci_priv)
  1206. {
  1207. int ret = 0;
  1208. if (!pci_priv)
  1209. return -ENODEV;
  1210. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1211. cnss_pr_info("PCI link is already suspended\n");
  1212. goto out;
  1213. }
  1214. pci_clear_master(pci_priv->pci_dev);
  1215. ret = cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  1216. if (ret)
  1217. goto out;
  1218. pci_disable_device(pci_priv->pci_dev);
  1219. if (pci_priv->pci_dev->device != QCA6174_DEVICE_ID) {
  1220. if (pci_set_power_state(pci_priv->pci_dev, PCI_D3hot))
  1221. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  1222. }
  1223. /* Always do PCIe L2 suspend during power off/PCIe link recovery */
  1224. pci_priv->drv_connected_last = 0;
  1225. ret = cnss_set_pci_link(pci_priv, PCI_LINK_DOWN);
  1226. if (ret)
  1227. goto out;
  1228. pci_priv->pci_link_state = PCI_LINK_DOWN;
  1229. return 0;
  1230. out:
  1231. return ret;
  1232. }
  1233. int cnss_resume_pci_link(struct cnss_pci_data *pci_priv)
  1234. {
  1235. int ret = 0;
  1236. if (!pci_priv)
  1237. return -ENODEV;
  1238. if (pci_priv->pci_link_state == PCI_LINK_UP) {
  1239. cnss_pr_info("PCI link is already resumed\n");
  1240. goto out;
  1241. }
  1242. ret = cnss_set_pci_link(pci_priv, PCI_LINK_UP);
  1243. if (ret) {
  1244. ret = -EAGAIN;
  1245. goto out;
  1246. }
  1247. pci_priv->pci_link_state = PCI_LINK_UP;
  1248. if (pci_priv->pci_dev->device != QCA6174_DEVICE_ID) {
  1249. ret = pci_set_power_state(pci_priv->pci_dev, PCI_D0);
  1250. if (ret) {
  1251. cnss_pr_err("Failed to set D0, err = %d\n", ret);
  1252. goto out;
  1253. }
  1254. }
  1255. ret = pci_enable_device(pci_priv->pci_dev);
  1256. if (ret) {
  1257. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  1258. goto out;
  1259. }
  1260. ret = cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  1261. if (ret)
  1262. goto out;
  1263. pci_set_master(pci_priv->pci_dev);
  1264. if (pci_priv->pci_link_down_ind)
  1265. pci_priv->pci_link_down_ind = false;
  1266. return 0;
  1267. out:
  1268. return ret;
  1269. }
  1270. int cnss_pci_recover_link_down(struct cnss_pci_data *pci_priv)
  1271. {
  1272. int ret;
  1273. switch (pci_priv->device_id) {
  1274. case QCA6390_DEVICE_ID:
  1275. case QCA6490_DEVICE_ID:
  1276. case KIWI_DEVICE_ID:
  1277. break;
  1278. default:
  1279. return -EOPNOTSUPP;
  1280. }
  1281. /* Always wait here to avoid missing WAKE assert for RDDM
  1282. * before link recovery
  1283. */
  1284. msleep(WAKE_EVENT_TIMEOUT);
  1285. ret = cnss_suspend_pci_link(pci_priv);
  1286. if (ret)
  1287. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  1288. ret = cnss_resume_pci_link(pci_priv);
  1289. if (ret) {
  1290. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  1291. del_timer(&pci_priv->dev_rddm_timer);
  1292. return ret;
  1293. }
  1294. mod_timer(&pci_priv->dev_rddm_timer,
  1295. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1296. cnss_mhi_debug_reg_dump(pci_priv);
  1297. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1298. return 0;
  1299. }
  1300. int cnss_pci_prevent_l1(struct device *dev)
  1301. {
  1302. struct pci_dev *pci_dev = to_pci_dev(dev);
  1303. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1304. int ret;
  1305. if (!pci_priv) {
  1306. cnss_pr_err("pci_priv is NULL\n");
  1307. return -ENODEV;
  1308. }
  1309. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1310. cnss_pr_dbg("PCIe link is in suspend state\n");
  1311. return -EIO;
  1312. }
  1313. if (pci_priv->pci_link_down_ind) {
  1314. cnss_pr_err("PCIe link is down\n");
  1315. return -EIO;
  1316. }
  1317. ret = _cnss_pci_prevent_l1(pci_priv);
  1318. if (ret == -EIO) {
  1319. cnss_pr_err("Failed to prevent PCIe L1, considered as link down\n");
  1320. cnss_pci_link_down(dev);
  1321. }
  1322. return ret;
  1323. }
  1324. EXPORT_SYMBOL(cnss_pci_prevent_l1);
  1325. void cnss_pci_allow_l1(struct device *dev)
  1326. {
  1327. struct pci_dev *pci_dev = to_pci_dev(dev);
  1328. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1329. if (!pci_priv) {
  1330. cnss_pr_err("pci_priv is NULL\n");
  1331. return;
  1332. }
  1333. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1334. cnss_pr_dbg("PCIe link is in suspend state\n");
  1335. return;
  1336. }
  1337. if (pci_priv->pci_link_down_ind) {
  1338. cnss_pr_err("PCIe link is down\n");
  1339. return;
  1340. }
  1341. _cnss_pci_allow_l1(pci_priv);
  1342. }
  1343. EXPORT_SYMBOL(cnss_pci_allow_l1);
  1344. static void cnss_pci_update_link_event(struct cnss_pci_data *pci_priv,
  1345. enum cnss_bus_event_type type,
  1346. void *data)
  1347. {
  1348. struct cnss_bus_event bus_event;
  1349. bus_event.etype = type;
  1350. bus_event.event_data = data;
  1351. cnss_pci_call_driver_uevent(pci_priv, CNSS_BUS_EVENT, &bus_event);
  1352. }
  1353. static void cnss_pci_handle_linkdown(struct cnss_pci_data *pci_priv)
  1354. {
  1355. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1356. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1357. unsigned long flags;
  1358. if (test_bit(ENABLE_PCI_LINK_DOWN_PANIC,
  1359. &plat_priv->ctrl_params.quirks))
  1360. panic("cnss: PCI link is down\n");
  1361. spin_lock_irqsave(&pci_link_down_lock, flags);
  1362. if (pci_priv->pci_link_down_ind) {
  1363. cnss_pr_dbg("PCI link down recovery is in progress, ignore\n");
  1364. spin_unlock_irqrestore(&pci_link_down_lock, flags);
  1365. return;
  1366. }
  1367. pci_priv->pci_link_down_ind = true;
  1368. spin_unlock_irqrestore(&pci_link_down_lock, flags);
  1369. if (pci_dev->device == QCA6174_DEVICE_ID)
  1370. disable_irq(pci_dev->irq);
  1371. /* Notify bus related event. Now for all supported chips.
  1372. * Here PCIe LINK_DOWN notification taken care.
  1373. * uevent buffer can be extended later, to cover more bus info.
  1374. */
  1375. cnss_pci_update_link_event(pci_priv, BUS_EVENT_PCI_LINK_DOWN, NULL);
  1376. cnss_fatal_err("PCI link down, schedule recovery\n");
  1377. cnss_schedule_recovery(&pci_dev->dev, CNSS_REASON_LINK_DOWN);
  1378. }
  1379. int cnss_pci_link_down(struct device *dev)
  1380. {
  1381. struct pci_dev *pci_dev = to_pci_dev(dev);
  1382. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1383. struct cnss_plat_data *plat_priv = NULL;
  1384. int ret;
  1385. if (!pci_priv) {
  1386. cnss_pr_err("pci_priv is NULL\n");
  1387. return -EINVAL;
  1388. }
  1389. plat_priv = pci_priv->plat_priv;
  1390. if (!plat_priv) {
  1391. cnss_pr_err("plat_priv is NULL\n");
  1392. return -ENODEV;
  1393. }
  1394. if (pci_priv->pci_link_down_ind) {
  1395. cnss_pr_dbg("PCI link down recovery is already in progress\n");
  1396. return -EBUSY;
  1397. }
  1398. if (pci_priv->drv_connected_last &&
  1399. of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  1400. "cnss-enable-self-recovery"))
  1401. plat_priv->ctrl_params.quirks |= BIT(LINK_DOWN_SELF_RECOVERY);
  1402. cnss_pr_err("PCI link down is detected by drivers\n");
  1403. ret = cnss_pci_assert_perst(pci_priv);
  1404. if (ret)
  1405. cnss_pci_handle_linkdown(pci_priv);
  1406. return ret;
  1407. }
  1408. EXPORT_SYMBOL(cnss_pci_link_down);
  1409. int cnss_pcie_is_device_down(struct cnss_pci_data *pci_priv)
  1410. {
  1411. struct cnss_plat_data *plat_priv;
  1412. if (!pci_priv) {
  1413. cnss_pr_err("pci_priv is NULL\n");
  1414. return -ENODEV;
  1415. }
  1416. plat_priv = pci_priv->plat_priv;
  1417. if (!plat_priv) {
  1418. cnss_pr_err("plat_priv is NULL\n");
  1419. return -ENODEV;
  1420. }
  1421. return test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) |
  1422. pci_priv->pci_link_down_ind;
  1423. }
  1424. int cnss_pci_is_device_down(struct device *dev)
  1425. {
  1426. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  1427. return cnss_pcie_is_device_down(pci_priv);
  1428. }
  1429. EXPORT_SYMBOL(cnss_pci_is_device_down);
  1430. void cnss_pci_lock_reg_window(struct device *dev, unsigned long *flags)
  1431. {
  1432. spin_lock_bh(&pci_reg_window_lock);
  1433. }
  1434. EXPORT_SYMBOL(cnss_pci_lock_reg_window);
  1435. void cnss_pci_unlock_reg_window(struct device *dev, unsigned long *flags)
  1436. {
  1437. spin_unlock_bh(&pci_reg_window_lock);
  1438. }
  1439. EXPORT_SYMBOL(cnss_pci_unlock_reg_window);
  1440. int cnss_get_pci_slot(struct device *dev)
  1441. {
  1442. struct pci_dev *pci_dev = to_pci_dev(dev);
  1443. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1444. struct cnss_plat_data *plat_priv = NULL;
  1445. if (!pci_priv) {
  1446. cnss_pr_err("pci_priv is NULL\n");
  1447. return -EINVAL;
  1448. }
  1449. plat_priv = pci_priv->plat_priv;
  1450. if (!plat_priv) {
  1451. cnss_pr_err("plat_priv is NULL\n");
  1452. return -ENODEV;
  1453. }
  1454. return plat_priv->rc_num;
  1455. }
  1456. EXPORT_SYMBOL(cnss_get_pci_slot);
  1457. /**
  1458. * cnss_pci_dump_bl_sram_mem - Dump WLAN device bootloader debug log
  1459. * @pci_priv: driver PCI bus context pointer
  1460. *
  1461. * Dump primary and secondary bootloader debug log data. For SBL check the
  1462. * log struct address and size for validity.
  1463. *
  1464. * Return: None
  1465. */
  1466. static void cnss_pci_dump_bl_sram_mem(struct cnss_pci_data *pci_priv)
  1467. {
  1468. u32 mem_addr, val, pbl_log_max_size, sbl_log_max_size;
  1469. u32 pbl_log_sram_start;
  1470. u32 pbl_stage, sbl_log_start, sbl_log_size;
  1471. u32 pbl_wlan_boot_cfg, pbl_bootstrap_status;
  1472. u32 pbl_bootstrap_status_reg = PBL_BOOTSTRAP_STATUS;
  1473. u32 sbl_log_def_start = SRAM_START;
  1474. u32 sbl_log_def_end = SRAM_END;
  1475. int i;
  1476. switch (pci_priv->device_id) {
  1477. case QCA6390_DEVICE_ID:
  1478. pbl_log_sram_start = QCA6390_DEBUG_PBL_LOG_SRAM_START;
  1479. pbl_log_max_size = QCA6390_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1480. sbl_log_max_size = QCA6390_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1481. break;
  1482. case QCA6490_DEVICE_ID:
  1483. pbl_log_sram_start = QCA6490_DEBUG_PBL_LOG_SRAM_START;
  1484. pbl_log_max_size = QCA6490_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1485. sbl_log_max_size = QCA6490_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1486. break;
  1487. case KIWI_DEVICE_ID:
  1488. pbl_bootstrap_status_reg = KIWI_PBL_BOOTSTRAP_STATUS;
  1489. pbl_log_sram_start = KIWI_DEBUG_PBL_LOG_SRAM_START;
  1490. pbl_log_max_size = KIWI_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1491. sbl_log_max_size = KIWI_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1492. default:
  1493. return;
  1494. }
  1495. if (cnss_pci_check_link_status(pci_priv))
  1496. return;
  1497. cnss_pci_reg_read(pci_priv, TCSR_PBL_LOGGING_REG, &pbl_stage);
  1498. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG2_REG, &sbl_log_start);
  1499. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG3_REG, &sbl_log_size);
  1500. cnss_pci_reg_read(pci_priv, PBL_WLAN_BOOT_CFG, &pbl_wlan_boot_cfg);
  1501. cnss_pci_reg_read(pci_priv, pbl_bootstrap_status_reg,
  1502. &pbl_bootstrap_status);
  1503. cnss_pr_dbg("TCSR_PBL_LOGGING: 0x%08x PCIE_BHI_ERRDBG: Start: 0x%08x Size:0x%08x\n",
  1504. pbl_stage, sbl_log_start, sbl_log_size);
  1505. cnss_pr_dbg("PBL_WLAN_BOOT_CFG: 0x%08x PBL_BOOTSTRAP_STATUS: 0x%08x\n",
  1506. pbl_wlan_boot_cfg, pbl_bootstrap_status);
  1507. cnss_pr_dbg("Dumping PBL log data\n");
  1508. for (i = 0; i < pbl_log_max_size; i += sizeof(val)) {
  1509. mem_addr = pbl_log_sram_start + i;
  1510. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1511. break;
  1512. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1513. }
  1514. sbl_log_size = (sbl_log_size > sbl_log_max_size ?
  1515. sbl_log_max_size : sbl_log_size);
  1516. if (sbl_log_start < sbl_log_def_start ||
  1517. sbl_log_start > sbl_log_def_end ||
  1518. (sbl_log_start + sbl_log_size) > sbl_log_def_end) {
  1519. cnss_pr_err("Invalid SBL log data\n");
  1520. return;
  1521. }
  1522. cnss_pr_dbg("Dumping SBL log data\n");
  1523. for (i = 0; i < sbl_log_size; i += sizeof(val)) {
  1524. mem_addr = sbl_log_start + i;
  1525. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1526. break;
  1527. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1528. }
  1529. }
  1530. static int cnss_pci_handle_mhi_poweron_timeout(struct cnss_pci_data *pci_priv)
  1531. {
  1532. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1533. cnss_fatal_err("MHI power up returns timeout\n");
  1534. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE)) {
  1535. /* Wait for RDDM if RDDM cookie is set. If RDDM times out,
  1536. * PBL/SBL error region may have been erased so no need to
  1537. * dump them either.
  1538. */
  1539. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  1540. !pci_priv->pci_link_down_ind) {
  1541. mod_timer(&pci_priv->dev_rddm_timer,
  1542. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1543. }
  1544. } else {
  1545. cnss_pr_dbg("RDDM cookie is not set\n");
  1546. cnss_mhi_debug_reg_dump(pci_priv);
  1547. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1548. /* Dump PBL/SBL error log if RDDM cookie is not set */
  1549. cnss_pci_dump_bl_sram_mem(pci_priv);
  1550. return -ETIMEDOUT;
  1551. }
  1552. return 0;
  1553. }
  1554. static char *cnss_mhi_state_to_str(enum cnss_mhi_state mhi_state)
  1555. {
  1556. switch (mhi_state) {
  1557. case CNSS_MHI_INIT:
  1558. return "INIT";
  1559. case CNSS_MHI_DEINIT:
  1560. return "DEINIT";
  1561. case CNSS_MHI_POWER_ON:
  1562. return "POWER_ON";
  1563. case CNSS_MHI_POWERING_OFF:
  1564. return "POWERING_OFF";
  1565. case CNSS_MHI_POWER_OFF:
  1566. return "POWER_OFF";
  1567. case CNSS_MHI_FORCE_POWER_OFF:
  1568. return "FORCE_POWER_OFF";
  1569. case CNSS_MHI_SUSPEND:
  1570. return "SUSPEND";
  1571. case CNSS_MHI_RESUME:
  1572. return "RESUME";
  1573. case CNSS_MHI_TRIGGER_RDDM:
  1574. return "TRIGGER_RDDM";
  1575. case CNSS_MHI_RDDM_DONE:
  1576. return "RDDM_DONE";
  1577. default:
  1578. return "UNKNOWN";
  1579. }
  1580. };
  1581. static int cnss_pci_check_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1582. enum cnss_mhi_state mhi_state)
  1583. {
  1584. switch (mhi_state) {
  1585. case CNSS_MHI_INIT:
  1586. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state))
  1587. return 0;
  1588. break;
  1589. case CNSS_MHI_DEINIT:
  1590. case CNSS_MHI_POWER_ON:
  1591. if (test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state) &&
  1592. !test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1593. return 0;
  1594. break;
  1595. case CNSS_MHI_FORCE_POWER_OFF:
  1596. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1597. return 0;
  1598. break;
  1599. case CNSS_MHI_POWER_OFF:
  1600. case CNSS_MHI_SUSPEND:
  1601. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1602. !test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1603. return 0;
  1604. break;
  1605. case CNSS_MHI_RESUME:
  1606. if (test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1607. return 0;
  1608. break;
  1609. case CNSS_MHI_TRIGGER_RDDM:
  1610. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1611. !test_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state))
  1612. return 0;
  1613. break;
  1614. case CNSS_MHI_RDDM_DONE:
  1615. return 0;
  1616. default:
  1617. cnss_pr_err("Unhandled MHI state: %s(%d)\n",
  1618. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1619. }
  1620. cnss_pr_err("Cannot set MHI state %s(%d) in current MHI state (0x%lx)\n",
  1621. cnss_mhi_state_to_str(mhi_state), mhi_state,
  1622. pci_priv->mhi_state);
  1623. if (mhi_state != CNSS_MHI_TRIGGER_RDDM)
  1624. CNSS_ASSERT(0);
  1625. return -EINVAL;
  1626. }
  1627. static void cnss_pci_set_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1628. enum cnss_mhi_state mhi_state)
  1629. {
  1630. switch (mhi_state) {
  1631. case CNSS_MHI_INIT:
  1632. set_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1633. break;
  1634. case CNSS_MHI_DEINIT:
  1635. clear_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1636. break;
  1637. case CNSS_MHI_POWER_ON:
  1638. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1639. break;
  1640. case CNSS_MHI_POWERING_OFF:
  1641. set_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1642. break;
  1643. case CNSS_MHI_POWER_OFF:
  1644. case CNSS_MHI_FORCE_POWER_OFF:
  1645. clear_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1646. clear_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1647. clear_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1648. clear_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1649. break;
  1650. case CNSS_MHI_SUSPEND:
  1651. set_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1652. break;
  1653. case CNSS_MHI_RESUME:
  1654. clear_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1655. break;
  1656. case CNSS_MHI_TRIGGER_RDDM:
  1657. set_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1658. break;
  1659. case CNSS_MHI_RDDM_DONE:
  1660. set_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1661. break;
  1662. default:
  1663. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1664. }
  1665. }
  1666. static int cnss_pci_set_mhi_state(struct cnss_pci_data *pci_priv,
  1667. enum cnss_mhi_state mhi_state)
  1668. {
  1669. int ret = 0, retry = 0;
  1670. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  1671. return 0;
  1672. if (mhi_state < 0) {
  1673. cnss_pr_err("Invalid MHI state (%d)\n", mhi_state);
  1674. return -EINVAL;
  1675. }
  1676. ret = cnss_pci_check_mhi_state_bit(pci_priv, mhi_state);
  1677. if (ret)
  1678. goto out;
  1679. cnss_pr_vdbg("Setting MHI state: %s(%d)\n",
  1680. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1681. switch (mhi_state) {
  1682. case CNSS_MHI_INIT:
  1683. ret = mhi_prepare_for_power_up(pci_priv->mhi_ctrl);
  1684. break;
  1685. case CNSS_MHI_DEINIT:
  1686. mhi_unprepare_after_power_down(pci_priv->mhi_ctrl);
  1687. ret = 0;
  1688. break;
  1689. case CNSS_MHI_POWER_ON:
  1690. ret = mhi_sync_power_up(pci_priv->mhi_ctrl);
  1691. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  1692. /* Only set img_pre_alloc when power up succeeds */
  1693. if (!ret && !pci_priv->mhi_ctrl->img_pre_alloc) {
  1694. cnss_pr_dbg("Notify MHI to use already allocated images\n");
  1695. pci_priv->mhi_ctrl->img_pre_alloc = true;
  1696. }
  1697. #endif
  1698. break;
  1699. case CNSS_MHI_POWER_OFF:
  1700. mhi_power_down(pci_priv->mhi_ctrl, true);
  1701. ret = 0;
  1702. break;
  1703. case CNSS_MHI_FORCE_POWER_OFF:
  1704. mhi_power_down(pci_priv->mhi_ctrl, false);
  1705. ret = 0;
  1706. break;
  1707. case CNSS_MHI_SUSPEND:
  1708. retry_mhi_suspend:
  1709. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1710. if (pci_priv->drv_connected_last)
  1711. ret = cnss_mhi_pm_fast_suspend(pci_priv, true);
  1712. else
  1713. ret = mhi_pm_suspend(pci_priv->mhi_ctrl);
  1714. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1715. if (ret == -EBUSY && retry++ < MHI_SUSPEND_RETRY_MAX_TIMES) {
  1716. cnss_pr_dbg("Retry MHI suspend #%d\n", retry);
  1717. usleep_range(MHI_SUSPEND_RETRY_DELAY_US,
  1718. MHI_SUSPEND_RETRY_DELAY_US + 1000);
  1719. goto retry_mhi_suspend;
  1720. }
  1721. break;
  1722. case CNSS_MHI_RESUME:
  1723. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1724. if (pci_priv->drv_connected_last) {
  1725. cnss_pci_prevent_l1(&pci_priv->pci_dev->dev);
  1726. ret = cnss_mhi_pm_fast_resume(pci_priv, true);
  1727. cnss_pci_allow_l1(&pci_priv->pci_dev->dev);
  1728. } else {
  1729. ret = mhi_pm_resume(pci_priv->mhi_ctrl);
  1730. }
  1731. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1732. break;
  1733. case CNSS_MHI_TRIGGER_RDDM:
  1734. ret = mhi_force_rddm_mode(pci_priv->mhi_ctrl);
  1735. if (ret) {
  1736. cnss_pr_err("Failed to trigger RDDM, err = %d\n", ret);
  1737. cnss_pr_dbg("Sending host reset req\n");
  1738. ret = cnss_mhi_force_reset(pci_priv);
  1739. }
  1740. break;
  1741. case CNSS_MHI_RDDM_DONE:
  1742. break;
  1743. default:
  1744. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1745. ret = -EINVAL;
  1746. }
  1747. if (ret)
  1748. goto out;
  1749. cnss_pci_set_mhi_state_bit(pci_priv, mhi_state);
  1750. return 0;
  1751. out:
  1752. cnss_pr_err("Failed to set MHI state: %s(%d), err = %d\n",
  1753. cnss_mhi_state_to_str(mhi_state), mhi_state, ret);
  1754. return ret;
  1755. }
  1756. #if IS_ENABLED(CONFIG_PCI_MSM)
  1757. /**
  1758. * cnss_wlan_adsp_pc_enable: Control ADSP power collapse setup
  1759. * @dev: Platform driver pci private data structure
  1760. * @control: Power collapse enable / disable
  1761. *
  1762. * This function controls ADSP power collapse (PC). It must be called
  1763. * based on wlan state. ADSP power collapse during wlan RTPM suspend state
  1764. * results in delay during periodic QMI stats PCI link up/down. This delay
  1765. * causes additional power consumption.
  1766. * Introduced in SM8350.
  1767. *
  1768. * Result: 0 Success. negative error codes.
  1769. */
  1770. static int cnss_wlan_adsp_pc_enable(struct cnss_pci_data *pci_priv,
  1771. bool control)
  1772. {
  1773. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1774. int ret = 0;
  1775. u32 pm_options = PM_OPTIONS_DEFAULT;
  1776. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1777. if (plat_priv->adsp_pc_enabled == control) {
  1778. cnss_pr_dbg("ADSP power collapse already %s\n",
  1779. control ? "Enabled" : "Disabled");
  1780. return 0;
  1781. }
  1782. if (control)
  1783. pm_options &= ~MSM_PCIE_CONFIG_NO_DRV_PC;
  1784. else
  1785. pm_options |= MSM_PCIE_CONFIG_NO_DRV_PC;
  1786. ret = msm_pcie_pm_control(MSM_PCIE_DRV_PC_CTRL, pci_dev->bus->number,
  1787. pci_dev, NULL, pm_options);
  1788. if (ret)
  1789. return ret;
  1790. cnss_pr_dbg("%s ADSP power collapse\n", control ? "Enable" : "Disable");
  1791. plat_priv->adsp_pc_enabled = control;
  1792. return 0;
  1793. }
  1794. #else
  1795. static int cnss_wlan_adsp_pc_enable(struct cnss_pci_data *pci_priv,
  1796. bool control)
  1797. {
  1798. return 0;
  1799. }
  1800. #endif
  1801. int cnss_pci_start_mhi(struct cnss_pci_data *pci_priv)
  1802. {
  1803. int ret = 0;
  1804. struct cnss_plat_data *plat_priv;
  1805. unsigned int timeout = 0;
  1806. if (!pci_priv) {
  1807. cnss_pr_err("pci_priv is NULL\n");
  1808. return -ENODEV;
  1809. }
  1810. plat_priv = pci_priv->plat_priv;
  1811. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1812. return 0;
  1813. if (MHI_TIMEOUT_OVERWRITE_MS)
  1814. pci_priv->mhi_ctrl->timeout_ms = MHI_TIMEOUT_OVERWRITE_MS;
  1815. cnss_mhi_set_m2_timeout_ms(pci_priv, MHI_M2_TIMEOUT_MS);
  1816. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_INIT);
  1817. if (ret)
  1818. return ret;
  1819. timeout = pci_priv->mhi_ctrl->timeout_ms;
  1820. /* For non-perf builds the timeout is 10 (default) * 6 seconds */
  1821. if (cnss_get_host_build_type() == QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  1822. pci_priv->mhi_ctrl->timeout_ms *= 6;
  1823. else /* For perf builds the timeout is 10 (default) * 3 seconds */
  1824. pci_priv->mhi_ctrl->timeout_ms *= 3;
  1825. /* Start the timer to dump MHI/PBL/SBL debug data periodically */
  1826. mod_timer(&pci_priv->boot_debug_timer,
  1827. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  1828. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_ON);
  1829. del_timer(&pci_priv->boot_debug_timer);
  1830. if (ret == 0)
  1831. cnss_wlan_adsp_pc_enable(pci_priv, false);
  1832. pci_priv->mhi_ctrl->timeout_ms = timeout;
  1833. if (ret == -ETIMEDOUT) {
  1834. /* This is a special case needs to be handled that if MHI
  1835. * power on returns -ETIMEDOUT, controller needs to take care
  1836. * the cleanup by calling MHI power down. Force to set the bit
  1837. * for driver internal MHI state to make sure it can be handled
  1838. * properly later.
  1839. */
  1840. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1841. ret = cnss_pci_handle_mhi_poweron_timeout(pci_priv);
  1842. }
  1843. return ret;
  1844. }
  1845. static void cnss_pci_power_off_mhi(struct cnss_pci_data *pci_priv)
  1846. {
  1847. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1848. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1849. return;
  1850. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state)) {
  1851. cnss_pr_dbg("MHI is already powered off\n");
  1852. return;
  1853. }
  1854. cnss_wlan_adsp_pc_enable(pci_priv, true);
  1855. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_RESUME);
  1856. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_POWERING_OFF);
  1857. if (!pci_priv->pci_link_down_ind)
  1858. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_OFF);
  1859. else
  1860. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_FORCE_POWER_OFF);
  1861. }
  1862. static void cnss_pci_deinit_mhi(struct cnss_pci_data *pci_priv)
  1863. {
  1864. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1865. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1866. return;
  1867. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state)) {
  1868. cnss_pr_dbg("MHI is already deinited\n");
  1869. return;
  1870. }
  1871. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_DEINIT);
  1872. }
  1873. static void cnss_pci_set_wlaon_pwr_ctrl(struct cnss_pci_data *pci_priv,
  1874. bool set_vddd4blow, bool set_shutdown,
  1875. bool do_force_wake)
  1876. {
  1877. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1878. int ret;
  1879. u32 val;
  1880. if (!plat_priv->set_wlaon_pwr_ctrl)
  1881. return;
  1882. if (pci_priv->pci_link_state == PCI_LINK_DOWN ||
  1883. pci_priv->pci_link_down_ind)
  1884. return;
  1885. if (do_force_wake)
  1886. if (cnss_pci_force_wake_get(pci_priv))
  1887. return;
  1888. ret = cnss_pci_reg_read(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, &val);
  1889. if (ret) {
  1890. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  1891. WLAON_QFPROM_PWR_CTRL_REG, ret);
  1892. goto force_wake_put;
  1893. }
  1894. cnss_pr_dbg("Read register offset 0x%x, val = 0x%x\n",
  1895. WLAON_QFPROM_PWR_CTRL_REG, val);
  1896. if (set_vddd4blow)
  1897. val |= QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1898. else
  1899. val &= ~QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1900. if (set_shutdown)
  1901. val |= QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1902. else
  1903. val &= ~QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1904. ret = cnss_pci_reg_write(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, val);
  1905. if (ret) {
  1906. cnss_pr_err("Failed to write register offset 0x%x, err = %d\n",
  1907. WLAON_QFPROM_PWR_CTRL_REG, ret);
  1908. goto force_wake_put;
  1909. }
  1910. cnss_pr_dbg("Write val 0x%x to register offset 0x%x\n", val,
  1911. WLAON_QFPROM_PWR_CTRL_REG);
  1912. if (set_shutdown)
  1913. usleep_range(WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US,
  1914. WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US);
  1915. force_wake_put:
  1916. if (do_force_wake)
  1917. cnss_pci_force_wake_put(pci_priv);
  1918. }
  1919. static int cnss_pci_get_device_timestamp(struct cnss_pci_data *pci_priv,
  1920. u64 *time_us)
  1921. {
  1922. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1923. u32 low, high;
  1924. u64 device_ticks;
  1925. if (!plat_priv->device_freq_hz) {
  1926. cnss_pr_err("Device time clock frequency is not valid\n");
  1927. return -EINVAL;
  1928. }
  1929. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL3, &low);
  1930. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL4, &high);
  1931. device_ticks = (u64)high << 32 | low;
  1932. do_div(device_ticks, plat_priv->device_freq_hz / 100000);
  1933. *time_us = device_ticks * 10;
  1934. return 0;
  1935. }
  1936. static void cnss_pci_enable_time_sync_counter(struct cnss_pci_data *pci_priv)
  1937. {
  1938. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  1939. TIME_SYNC_ENABLE);
  1940. }
  1941. static void cnss_pci_clear_time_sync_counter(struct cnss_pci_data *pci_priv)
  1942. {
  1943. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  1944. TIME_SYNC_CLEAR);
  1945. }
  1946. static int cnss_pci_update_timestamp(struct cnss_pci_data *pci_priv)
  1947. {
  1948. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1949. struct device *dev = &pci_priv->pci_dev->dev;
  1950. unsigned long flags = 0;
  1951. u64 host_time_us, device_time_us, offset;
  1952. u32 low, high;
  1953. int ret;
  1954. ret = cnss_pci_prevent_l1(dev);
  1955. if (ret)
  1956. goto out;
  1957. ret = cnss_pci_force_wake_get(pci_priv);
  1958. if (ret)
  1959. goto allow_l1;
  1960. spin_lock_irqsave(&time_sync_lock, flags);
  1961. cnss_pci_clear_time_sync_counter(pci_priv);
  1962. cnss_pci_enable_time_sync_counter(pci_priv);
  1963. host_time_us = cnss_get_host_timestamp(plat_priv);
  1964. ret = cnss_pci_get_device_timestamp(pci_priv, &device_time_us);
  1965. cnss_pci_clear_time_sync_counter(pci_priv);
  1966. spin_unlock_irqrestore(&time_sync_lock, flags);
  1967. if (ret)
  1968. goto force_wake_put;
  1969. if (host_time_us < device_time_us) {
  1970. cnss_pr_err("Host time (%llu us) is smaller than device time (%llu us), stop\n",
  1971. host_time_us, device_time_us);
  1972. ret = -EINVAL;
  1973. goto force_wake_put;
  1974. }
  1975. offset = host_time_us - device_time_us;
  1976. cnss_pr_dbg("Host time = %llu us, device time = %llu us, offset = %llu us\n",
  1977. host_time_us, device_time_us, offset);
  1978. low = offset & 0xFFFFFFFF;
  1979. high = offset >> 32;
  1980. cnss_pci_reg_write(pci_priv, PCIE_SHADOW_REG_VALUE_34, low);
  1981. cnss_pci_reg_write(pci_priv, PCIE_SHADOW_REG_VALUE_35, high);
  1982. cnss_pci_reg_read(pci_priv, PCIE_SHADOW_REG_VALUE_34, &low);
  1983. cnss_pci_reg_read(pci_priv, PCIE_SHADOW_REG_VALUE_35, &high);
  1984. cnss_pr_dbg("Updated time sync regs [0x%x] = 0x%x, [0x%x] = 0x%x\n",
  1985. PCIE_SHADOW_REG_VALUE_34, low,
  1986. PCIE_SHADOW_REG_VALUE_35, high);
  1987. force_wake_put:
  1988. cnss_pci_force_wake_put(pci_priv);
  1989. allow_l1:
  1990. cnss_pci_allow_l1(dev);
  1991. out:
  1992. return ret;
  1993. }
  1994. static void cnss_pci_time_sync_work_hdlr(struct work_struct *work)
  1995. {
  1996. struct cnss_pci_data *pci_priv =
  1997. container_of(work, struct cnss_pci_data, time_sync_work.work);
  1998. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1999. unsigned int time_sync_period_ms =
  2000. plat_priv->ctrl_params.time_sync_period;
  2001. if (test_bit(DISABLE_TIME_SYNC, &plat_priv->ctrl_params.quirks)) {
  2002. cnss_pr_dbg("Time sync is disabled\n");
  2003. return;
  2004. }
  2005. if (!time_sync_period_ms) {
  2006. cnss_pr_dbg("Skip time sync as time period is 0\n");
  2007. return;
  2008. }
  2009. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  2010. return;
  2011. if (cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS) < 0)
  2012. goto runtime_pm_put;
  2013. mutex_lock(&pci_priv->bus_lock);
  2014. cnss_pci_update_timestamp(pci_priv);
  2015. mutex_unlock(&pci_priv->bus_lock);
  2016. schedule_delayed_work(&pci_priv->time_sync_work,
  2017. msecs_to_jiffies(time_sync_period_ms));
  2018. runtime_pm_put:
  2019. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  2020. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  2021. }
  2022. static int cnss_pci_start_time_sync_update(struct cnss_pci_data *pci_priv)
  2023. {
  2024. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2025. switch (pci_priv->device_id) {
  2026. case QCA6390_DEVICE_ID:
  2027. case QCA6490_DEVICE_ID:
  2028. break;
  2029. default:
  2030. return -EOPNOTSUPP;
  2031. }
  2032. if (!plat_priv->device_freq_hz) {
  2033. cnss_pr_dbg("Device time clock frequency is not valid, skip time sync\n");
  2034. return -EINVAL;
  2035. }
  2036. cnss_pci_time_sync_work_hdlr(&pci_priv->time_sync_work.work);
  2037. return 0;
  2038. }
  2039. static void cnss_pci_stop_time_sync_update(struct cnss_pci_data *pci_priv)
  2040. {
  2041. switch (pci_priv->device_id) {
  2042. case QCA6390_DEVICE_ID:
  2043. case QCA6490_DEVICE_ID:
  2044. break;
  2045. default:
  2046. return;
  2047. }
  2048. cancel_delayed_work_sync(&pci_priv->time_sync_work);
  2049. }
  2050. int cnss_pci_call_driver_probe(struct cnss_pci_data *pci_priv)
  2051. {
  2052. int ret = 0;
  2053. struct cnss_plat_data *plat_priv;
  2054. if (!pci_priv)
  2055. return -ENODEV;
  2056. plat_priv = pci_priv->plat_priv;
  2057. if (test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2058. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2059. cnss_pr_dbg("Skip driver probe\n");
  2060. goto out;
  2061. }
  2062. if (!pci_priv->driver_ops) {
  2063. cnss_pr_err("driver_ops is NULL\n");
  2064. ret = -EINVAL;
  2065. goto out;
  2066. }
  2067. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2068. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2069. ret = pci_priv->driver_ops->reinit(pci_priv->pci_dev,
  2070. pci_priv->pci_device_id);
  2071. if (ret) {
  2072. cnss_pr_err("Failed to reinit host driver, err = %d\n",
  2073. ret);
  2074. goto out;
  2075. }
  2076. complete(&plat_priv->recovery_complete);
  2077. } else if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state)) {
  2078. ret = pci_priv->driver_ops->probe(pci_priv->pci_dev,
  2079. pci_priv->pci_device_id);
  2080. if (ret) {
  2081. cnss_pr_err("Failed to probe host driver, err = %d\n",
  2082. ret);
  2083. goto out;
  2084. }
  2085. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2086. set_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2087. complete_all(&plat_priv->power_up_complete);
  2088. } else if (test_bit(CNSS_DRIVER_IDLE_RESTART,
  2089. &plat_priv->driver_state)) {
  2090. ret = pci_priv->driver_ops->idle_restart(pci_priv->pci_dev,
  2091. pci_priv->pci_device_id);
  2092. if (ret) {
  2093. cnss_pr_err("Failed to idle restart host driver, err = %d\n",
  2094. ret);
  2095. plat_priv->power_up_error = ret;
  2096. complete_all(&plat_priv->power_up_complete);
  2097. goto out;
  2098. }
  2099. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  2100. complete_all(&plat_priv->power_up_complete);
  2101. } else {
  2102. complete(&plat_priv->power_up_complete);
  2103. }
  2104. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  2105. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2106. __pm_relax(plat_priv->recovery_ws);
  2107. }
  2108. cnss_pci_start_time_sync_update(pci_priv);
  2109. return 0;
  2110. out:
  2111. return ret;
  2112. }
  2113. int cnss_pci_call_driver_remove(struct cnss_pci_data *pci_priv)
  2114. {
  2115. struct cnss_plat_data *plat_priv;
  2116. int ret;
  2117. if (!pci_priv)
  2118. return -ENODEV;
  2119. plat_priv = pci_priv->plat_priv;
  2120. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) ||
  2121. test_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state) ||
  2122. test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2123. cnss_pr_dbg("Skip driver remove\n");
  2124. return 0;
  2125. }
  2126. if (!pci_priv->driver_ops) {
  2127. cnss_pr_err("driver_ops is NULL\n");
  2128. return -EINVAL;
  2129. }
  2130. cnss_pci_stop_time_sync_update(pci_priv);
  2131. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2132. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2133. pci_priv->driver_ops->shutdown(pci_priv->pci_dev);
  2134. } else if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state)) {
  2135. pci_priv->driver_ops->remove(pci_priv->pci_dev);
  2136. clear_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2137. } else if (test_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2138. &plat_priv->driver_state)) {
  2139. ret = pci_priv->driver_ops->idle_shutdown(pci_priv->pci_dev);
  2140. if (ret == -EAGAIN) {
  2141. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2142. &plat_priv->driver_state);
  2143. return ret;
  2144. }
  2145. }
  2146. plat_priv->get_info_cb_ctx = NULL;
  2147. plat_priv->get_info_cb = NULL;
  2148. return 0;
  2149. }
  2150. int cnss_pci_call_driver_modem_status(struct cnss_pci_data *pci_priv,
  2151. int modem_current_status)
  2152. {
  2153. struct cnss_wlan_driver *driver_ops;
  2154. if (!pci_priv)
  2155. return -ENODEV;
  2156. driver_ops = pci_priv->driver_ops;
  2157. if (!driver_ops || !driver_ops->modem_status)
  2158. return -EINVAL;
  2159. driver_ops->modem_status(pci_priv->pci_dev, modem_current_status);
  2160. return 0;
  2161. }
  2162. int cnss_pci_update_status(struct cnss_pci_data *pci_priv,
  2163. enum cnss_driver_status status)
  2164. {
  2165. struct cnss_wlan_driver *driver_ops;
  2166. if (!pci_priv)
  2167. return -ENODEV;
  2168. driver_ops = pci_priv->driver_ops;
  2169. if (!driver_ops || !driver_ops->update_status)
  2170. return -EINVAL;
  2171. cnss_pr_dbg("Update driver status: %d\n", status);
  2172. driver_ops->update_status(pci_priv->pci_dev, status);
  2173. return 0;
  2174. }
  2175. static void cnss_pci_misc_reg_dump(struct cnss_pci_data *pci_priv,
  2176. struct cnss_misc_reg *misc_reg,
  2177. u32 misc_reg_size,
  2178. char *reg_name)
  2179. {
  2180. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2181. bool do_force_wake_put = true;
  2182. int i;
  2183. if (!misc_reg)
  2184. return;
  2185. if (in_interrupt() || irqs_disabled())
  2186. return;
  2187. if (cnss_pci_check_link_status(pci_priv))
  2188. return;
  2189. if (cnss_pci_force_wake_get(pci_priv)) {
  2190. /* Continue to dump when device has entered RDDM already */
  2191. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2192. return;
  2193. do_force_wake_put = false;
  2194. }
  2195. cnss_pr_dbg("Start to dump %s registers\n", reg_name);
  2196. for (i = 0; i < misc_reg_size; i++) {
  2197. if (!test_bit(pci_priv->misc_reg_dev_mask,
  2198. &misc_reg[i].dev_mask))
  2199. continue;
  2200. if (misc_reg[i].wr) {
  2201. if (misc_reg[i].offset ==
  2202. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG &&
  2203. i >= 1)
  2204. misc_reg[i].val =
  2205. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG_MSK |
  2206. misc_reg[i - 1].val;
  2207. if (cnss_pci_reg_write(pci_priv,
  2208. misc_reg[i].offset,
  2209. misc_reg[i].val))
  2210. goto force_wake_put;
  2211. cnss_pr_vdbg("Write 0x%X to 0x%X\n",
  2212. misc_reg[i].val,
  2213. misc_reg[i].offset);
  2214. } else {
  2215. if (cnss_pci_reg_read(pci_priv,
  2216. misc_reg[i].offset,
  2217. &misc_reg[i].val))
  2218. goto force_wake_put;
  2219. }
  2220. }
  2221. force_wake_put:
  2222. if (do_force_wake_put)
  2223. cnss_pci_force_wake_put(pci_priv);
  2224. }
  2225. static void cnss_pci_dump_misc_reg(struct cnss_pci_data *pci_priv)
  2226. {
  2227. if (in_interrupt() || irqs_disabled())
  2228. return;
  2229. if (cnss_pci_check_link_status(pci_priv))
  2230. return;
  2231. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wcss_reg,
  2232. WCSS_REG_SIZE, "wcss");
  2233. cnss_pci_misc_reg_dump(pci_priv, pci_priv->pcie_reg,
  2234. PCIE_REG_SIZE, "pcie");
  2235. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wlaon_reg,
  2236. WLAON_REG_SIZE, "wlaon");
  2237. cnss_pci_misc_reg_dump(pci_priv, pci_priv->syspm_reg,
  2238. SYSPM_REG_SIZE, "syspm");
  2239. }
  2240. static void cnss_pci_dump_shadow_reg(struct cnss_pci_data *pci_priv)
  2241. {
  2242. int i, j = 0, array_size = SHADOW_REG_COUNT + SHADOW_REG_INTER_COUNT;
  2243. u32 reg_offset;
  2244. bool do_force_wake_put = true;
  2245. if (in_interrupt() || irqs_disabled())
  2246. return;
  2247. if (cnss_pci_check_link_status(pci_priv))
  2248. return;
  2249. if (!pci_priv->debug_reg) {
  2250. pci_priv->debug_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  2251. sizeof(*pci_priv->debug_reg)
  2252. * array_size, GFP_KERNEL);
  2253. if (!pci_priv->debug_reg)
  2254. return;
  2255. }
  2256. if (cnss_pci_force_wake_get(pci_priv))
  2257. do_force_wake_put = false;
  2258. cnss_pr_dbg("Start to dump shadow registers\n");
  2259. for (i = 0; i < SHADOW_REG_COUNT; i++, j++) {
  2260. reg_offset = PCIE_SHADOW_REG_VALUE_0 + i * 4;
  2261. pci_priv->debug_reg[j].offset = reg_offset;
  2262. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2263. &pci_priv->debug_reg[j].val))
  2264. goto force_wake_put;
  2265. }
  2266. for (i = 0; i < SHADOW_REG_INTER_COUNT; i++, j++) {
  2267. reg_offset = PCIE_SHADOW_REG_INTER_0 + i * 4;
  2268. pci_priv->debug_reg[j].offset = reg_offset;
  2269. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2270. &pci_priv->debug_reg[j].val))
  2271. goto force_wake_put;
  2272. }
  2273. force_wake_put:
  2274. if (do_force_wake_put)
  2275. cnss_pci_force_wake_put(pci_priv);
  2276. }
  2277. static int cnss_qca6174_powerup(struct cnss_pci_data *pci_priv)
  2278. {
  2279. int ret = 0;
  2280. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2281. ret = cnss_power_on_device(plat_priv);
  2282. if (ret) {
  2283. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2284. goto out;
  2285. }
  2286. ret = cnss_resume_pci_link(pci_priv);
  2287. if (ret) {
  2288. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2289. goto power_off;
  2290. }
  2291. ret = cnss_pci_call_driver_probe(pci_priv);
  2292. if (ret)
  2293. goto suspend_link;
  2294. return 0;
  2295. suspend_link:
  2296. cnss_suspend_pci_link(pci_priv);
  2297. power_off:
  2298. cnss_power_off_device(plat_priv);
  2299. out:
  2300. return ret;
  2301. }
  2302. static int cnss_qca6174_shutdown(struct cnss_pci_data *pci_priv)
  2303. {
  2304. int ret = 0;
  2305. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2306. cnss_pci_pm_runtime_resume(pci_priv);
  2307. ret = cnss_pci_call_driver_remove(pci_priv);
  2308. if (ret == -EAGAIN)
  2309. goto out;
  2310. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2311. CNSS_BUS_WIDTH_NONE);
  2312. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2313. cnss_pci_set_auto_suspended(pci_priv, 0);
  2314. ret = cnss_suspend_pci_link(pci_priv);
  2315. if (ret)
  2316. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2317. cnss_power_off_device(plat_priv);
  2318. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2319. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2320. out:
  2321. return ret;
  2322. }
  2323. static void cnss_qca6174_crash_shutdown(struct cnss_pci_data *pci_priv)
  2324. {
  2325. if (pci_priv->driver_ops && pci_priv->driver_ops->crash_shutdown)
  2326. pci_priv->driver_ops->crash_shutdown(pci_priv->pci_dev);
  2327. }
  2328. static int cnss_qca6174_ramdump(struct cnss_pci_data *pci_priv)
  2329. {
  2330. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2331. struct cnss_ramdump_info *ramdump_info;
  2332. ramdump_info = &plat_priv->ramdump_info;
  2333. if (!ramdump_info->ramdump_size)
  2334. return -EINVAL;
  2335. return cnss_do_ramdump(plat_priv);
  2336. }
  2337. static int cnss_qca6290_powerup(struct cnss_pci_data *pci_priv)
  2338. {
  2339. int ret = 0;
  2340. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2341. unsigned int timeout;
  2342. int retry = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  2343. if (plat_priv->ramdump_info_v2.dump_data_valid) {
  2344. cnss_pci_clear_dump_info(pci_priv);
  2345. cnss_pci_power_off_mhi(pci_priv);
  2346. cnss_suspend_pci_link(pci_priv);
  2347. cnss_pci_deinit_mhi(pci_priv);
  2348. cnss_power_off_device(plat_priv);
  2349. }
  2350. /* Clear QMI send usage count during every power up */
  2351. pci_priv->qmi_send_usage_count = 0;
  2352. plat_priv->power_up_error = 0;
  2353. retry:
  2354. ret = cnss_power_on_device(plat_priv);
  2355. if (ret) {
  2356. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2357. goto out;
  2358. }
  2359. ret = cnss_resume_pci_link(pci_priv);
  2360. if (ret) {
  2361. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2362. if (test_bit(IGNORE_PCI_LINK_FAILURE,
  2363. &plat_priv->ctrl_params.quirks)) {
  2364. cnss_pr_dbg("Ignore PCI link resume failure\n");
  2365. ret = 0;
  2366. goto out;
  2367. }
  2368. if (ret == -EAGAIN && retry++ < POWER_ON_RETRY_MAX_TIMES) {
  2369. cnss_power_off_device(plat_priv);
  2370. /* Force toggle BT_EN GPIO low */
  2371. if (retry == POWER_ON_RETRY_MAX_TIMES) {
  2372. cnss_pr_dbg("Retry #%d. Set BT_EN GPIO(%u) low\n",
  2373. retry, bt_en_gpio);
  2374. if (bt_en_gpio >= 0)
  2375. gpio_direction_output(bt_en_gpio, 0);
  2376. cnss_pr_dbg("BT_EN GPIO val: %d\n",
  2377. gpio_get_value(bt_en_gpio));
  2378. }
  2379. cnss_pr_dbg("Retry to resume PCI link #%d\n", retry);
  2380. msleep(POWER_ON_RETRY_DELAY_MS * retry);
  2381. goto retry;
  2382. }
  2383. /* Assert when it reaches maximum retries */
  2384. CNSS_ASSERT(0);
  2385. goto power_off;
  2386. }
  2387. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  2388. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_QMI);
  2389. ret = cnss_pci_start_mhi(pci_priv);
  2390. if (ret) {
  2391. cnss_fatal_err("Failed to start MHI, err = %d\n", ret);
  2392. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  2393. !pci_priv->pci_link_down_ind && timeout) {
  2394. /* Start recovery directly for MHI start failures */
  2395. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  2396. CNSS_REASON_DEFAULT);
  2397. }
  2398. return 0;
  2399. }
  2400. if (test_bit(USE_CORE_ONLY_FW, &plat_priv->ctrl_params.quirks)) {
  2401. clear_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state);
  2402. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2403. return 0;
  2404. }
  2405. cnss_set_pin_connect_status(plat_priv);
  2406. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks)) {
  2407. ret = cnss_pci_call_driver_probe(pci_priv);
  2408. if (ret)
  2409. goto stop_mhi;
  2410. } else if (timeout) {
  2411. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state))
  2412. timeout += WLAN_COLD_BOOT_CAL_TIMEOUT;
  2413. else
  2414. timeout += WLAN_MISSION_MODE_TIMEOUT;
  2415. mod_timer(&plat_priv->fw_boot_timer,
  2416. jiffies + msecs_to_jiffies(timeout));
  2417. }
  2418. return 0;
  2419. stop_mhi:
  2420. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, true);
  2421. cnss_pci_power_off_mhi(pci_priv);
  2422. cnss_suspend_pci_link(pci_priv);
  2423. cnss_pci_deinit_mhi(pci_priv);
  2424. power_off:
  2425. cnss_power_off_device(plat_priv);
  2426. out:
  2427. return ret;
  2428. }
  2429. static int cnss_qca6290_shutdown(struct cnss_pci_data *pci_priv)
  2430. {
  2431. int ret = 0;
  2432. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2433. int do_force_wake = true;
  2434. cnss_pci_pm_runtime_resume(pci_priv);
  2435. ret = cnss_pci_call_driver_remove(pci_priv);
  2436. if (ret == -EAGAIN)
  2437. goto out;
  2438. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2439. CNSS_BUS_WIDTH_NONE);
  2440. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2441. cnss_pci_set_auto_suspended(pci_priv, 0);
  2442. if ((test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  2443. test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2444. test_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state) ||
  2445. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state) ||
  2446. test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) &&
  2447. test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  2448. del_timer(&pci_priv->dev_rddm_timer);
  2449. cnss_pci_collect_dump_info(pci_priv, false);
  2450. CNSS_ASSERT(0);
  2451. }
  2452. if (!cnss_is_device_powered_on(plat_priv)) {
  2453. cnss_pr_dbg("Device is already powered off, ignore\n");
  2454. goto skip_power_off;
  2455. }
  2456. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2457. do_force_wake = false;
  2458. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, do_force_wake);
  2459. /* FBC image will be freed after powering off MHI, so skip
  2460. * if RAM dump data is still valid.
  2461. */
  2462. if (plat_priv->ramdump_info_v2.dump_data_valid)
  2463. goto skip_power_off;
  2464. cnss_pci_power_off_mhi(pci_priv);
  2465. ret = cnss_suspend_pci_link(pci_priv);
  2466. if (ret)
  2467. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2468. cnss_pci_deinit_mhi(pci_priv);
  2469. cnss_power_off_device(plat_priv);
  2470. skip_power_off:
  2471. pci_priv->remap_window = 0;
  2472. clear_bit(CNSS_FW_READY, &plat_priv->driver_state);
  2473. clear_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  2474. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2475. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  2476. clear_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  2477. pci_priv->pci_link_down_ind = false;
  2478. }
  2479. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2480. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2481. out:
  2482. return ret;
  2483. }
  2484. static void cnss_qca6290_crash_shutdown(struct cnss_pci_data *pci_priv)
  2485. {
  2486. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2487. set_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2488. cnss_pr_dbg("Crash shutdown with driver_state 0x%lx\n",
  2489. plat_priv->driver_state);
  2490. cnss_pci_collect_dump_info(pci_priv, true);
  2491. clear_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2492. }
  2493. static int cnss_qca6290_ramdump(struct cnss_pci_data *pci_priv)
  2494. {
  2495. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2496. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2497. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2498. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2499. int ret = 0;
  2500. if (!info_v2->dump_data_valid || !dump_seg ||
  2501. dump_data->nentries == 0)
  2502. return 0;
  2503. ret = cnss_do_elf_ramdump(plat_priv);
  2504. cnss_pci_clear_dump_info(pci_priv);
  2505. cnss_pci_power_off_mhi(pci_priv);
  2506. cnss_suspend_pci_link(pci_priv);
  2507. cnss_pci_deinit_mhi(pci_priv);
  2508. cnss_power_off_device(plat_priv);
  2509. return ret;
  2510. }
  2511. int cnss_pci_dev_powerup(struct cnss_pci_data *pci_priv)
  2512. {
  2513. int ret = 0;
  2514. if (!pci_priv) {
  2515. cnss_pr_err("pci_priv is NULL\n");
  2516. return -ENODEV;
  2517. }
  2518. switch (pci_priv->device_id) {
  2519. case QCA6174_DEVICE_ID:
  2520. ret = cnss_qca6174_powerup(pci_priv);
  2521. break;
  2522. case QCA6290_DEVICE_ID:
  2523. case QCA6390_DEVICE_ID:
  2524. case QCA6490_DEVICE_ID:
  2525. case KIWI_DEVICE_ID:
  2526. ret = cnss_qca6290_powerup(pci_priv);
  2527. break;
  2528. default:
  2529. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2530. pci_priv->device_id);
  2531. ret = -ENODEV;
  2532. }
  2533. return ret;
  2534. }
  2535. int cnss_pci_dev_shutdown(struct cnss_pci_data *pci_priv)
  2536. {
  2537. int ret = 0;
  2538. if (!pci_priv) {
  2539. cnss_pr_err("pci_priv is NULL\n");
  2540. return -ENODEV;
  2541. }
  2542. switch (pci_priv->device_id) {
  2543. case QCA6174_DEVICE_ID:
  2544. ret = cnss_qca6174_shutdown(pci_priv);
  2545. break;
  2546. case QCA6290_DEVICE_ID:
  2547. case QCA6390_DEVICE_ID:
  2548. case QCA6490_DEVICE_ID:
  2549. case KIWI_DEVICE_ID:
  2550. ret = cnss_qca6290_shutdown(pci_priv);
  2551. break;
  2552. default:
  2553. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2554. pci_priv->device_id);
  2555. ret = -ENODEV;
  2556. }
  2557. return ret;
  2558. }
  2559. int cnss_pci_dev_crash_shutdown(struct cnss_pci_data *pci_priv)
  2560. {
  2561. int ret = 0;
  2562. if (!pci_priv) {
  2563. cnss_pr_err("pci_priv is NULL\n");
  2564. return -ENODEV;
  2565. }
  2566. switch (pci_priv->device_id) {
  2567. case QCA6174_DEVICE_ID:
  2568. cnss_qca6174_crash_shutdown(pci_priv);
  2569. break;
  2570. case QCA6290_DEVICE_ID:
  2571. case QCA6390_DEVICE_ID:
  2572. case QCA6490_DEVICE_ID:
  2573. case KIWI_DEVICE_ID:
  2574. cnss_qca6290_crash_shutdown(pci_priv);
  2575. break;
  2576. default:
  2577. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2578. pci_priv->device_id);
  2579. ret = -ENODEV;
  2580. }
  2581. return ret;
  2582. }
  2583. int cnss_pci_dev_ramdump(struct cnss_pci_data *pci_priv)
  2584. {
  2585. int ret = 0;
  2586. if (!pci_priv) {
  2587. cnss_pr_err("pci_priv is NULL\n");
  2588. return -ENODEV;
  2589. }
  2590. switch (pci_priv->device_id) {
  2591. case QCA6174_DEVICE_ID:
  2592. ret = cnss_qca6174_ramdump(pci_priv);
  2593. break;
  2594. case QCA6290_DEVICE_ID:
  2595. case QCA6390_DEVICE_ID:
  2596. case QCA6490_DEVICE_ID:
  2597. case KIWI_DEVICE_ID:
  2598. ret = cnss_qca6290_ramdump(pci_priv);
  2599. break;
  2600. default:
  2601. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2602. pci_priv->device_id);
  2603. ret = -ENODEV;
  2604. }
  2605. return ret;
  2606. }
  2607. int cnss_pci_is_drv_connected(struct device *dev)
  2608. {
  2609. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2610. if (!pci_priv)
  2611. return -ENODEV;
  2612. return pci_priv->drv_connected_last;
  2613. }
  2614. EXPORT_SYMBOL(cnss_pci_is_drv_connected);
  2615. static void cnss_wlan_reg_driver_work(struct work_struct *work)
  2616. {
  2617. struct cnss_plat_data *plat_priv =
  2618. container_of(work, struct cnss_plat_data, wlan_reg_driver_work.work);
  2619. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  2620. struct cnss_cal_info *cal_info;
  2621. if (test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state)) {
  2622. goto reg_driver;
  2623. } else {
  2624. cnss_pr_err("Timeout waiting for calibration to complete\n");
  2625. del_timer(&plat_priv->fw_boot_timer);
  2626. if (!test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state))
  2627. CNSS_ASSERT(0);
  2628. cal_info = kzalloc(sizeof(*cal_info), GFP_KERNEL);
  2629. if (!cal_info)
  2630. return;
  2631. cal_info->cal_status = CNSS_CAL_TIMEOUT;
  2632. cnss_driver_event_post(plat_priv,
  2633. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE,
  2634. 0, cal_info);
  2635. }
  2636. reg_driver:
  2637. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2638. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2639. return;
  2640. }
  2641. reinit_completion(&plat_priv->power_up_complete);
  2642. cnss_driver_event_post(plat_priv,
  2643. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2644. CNSS_EVENT_SYNC_UNKILLABLE,
  2645. pci_priv->driver_ops);
  2646. }
  2647. int cnss_wlan_register_driver(struct cnss_wlan_driver *driver_ops)
  2648. {
  2649. int ret = 0;
  2650. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2651. struct cnss_pci_data *pci_priv;
  2652. const struct pci_device_id *id_table = driver_ops->id_table;
  2653. unsigned int timeout;
  2654. if (!plat_priv) {
  2655. cnss_pr_info("plat_priv is not ready for register driver\n");
  2656. return -EAGAIN;
  2657. }
  2658. if (!test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state)) {
  2659. cnss_pr_info("pci probe not yet done for register driver\n");
  2660. return -EAGAIN;
  2661. }
  2662. pci_priv = plat_priv->bus_priv;
  2663. if (pci_priv->driver_ops) {
  2664. cnss_pr_err("Driver has already registered\n");
  2665. return -EEXIST;
  2666. }
  2667. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2668. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2669. return -EINVAL;
  2670. }
  2671. if (!id_table || !pci_dev_present(id_table)) {
  2672. /* id_table pointer will move from pci_dev_present(),
  2673. * so check again using local pointer.
  2674. */
  2675. id_table = driver_ops->id_table;
  2676. while (id_table && id_table->vendor) {
  2677. cnss_pr_info("Host driver is built for PCIe device ID 0x%x\n",
  2678. id_table->device);
  2679. id_table++;
  2680. }
  2681. cnss_pr_err("Enumerated PCIe device id is 0x%x, reject unsupported driver\n",
  2682. pci_priv->device_id);
  2683. return -ENODEV;
  2684. }
  2685. if (!plat_priv->cbc_enabled ||
  2686. test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state))
  2687. goto register_driver;
  2688. pci_priv->driver_ops = driver_ops;
  2689. /* If Cold Boot Calibration is enabled, it is the 1st step in init
  2690. * sequence.CBC is done on file system_ready trigger. Qcacld will be
  2691. * loaded from vendor_modprobe.sh at early boot and must be deferred
  2692. * until CBC is complete
  2693. */
  2694. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_CALIBRATION);
  2695. INIT_DELAYED_WORK(&plat_priv->wlan_reg_driver_work,
  2696. cnss_wlan_reg_driver_work);
  2697. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2698. msecs_to_jiffies(timeout));
  2699. cnss_pr_info("WLAN register driver deferred for Calibration\n");
  2700. return 0;
  2701. register_driver:
  2702. reinit_completion(&plat_priv->power_up_complete);
  2703. ret = cnss_driver_event_post(plat_priv,
  2704. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2705. CNSS_EVENT_SYNC_UNKILLABLE,
  2706. driver_ops);
  2707. return ret;
  2708. }
  2709. EXPORT_SYMBOL(cnss_wlan_register_driver);
  2710. void cnss_wlan_unregister_driver(struct cnss_wlan_driver *driver_ops)
  2711. {
  2712. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2713. int ret = 0;
  2714. unsigned int timeout;
  2715. if (!plat_priv) {
  2716. cnss_pr_err("plat_priv is NULL\n");
  2717. return;
  2718. }
  2719. mutex_lock(&plat_priv->driver_ops_lock);
  2720. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  2721. goto skip_wait_power_up;
  2722. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_WLAN_WATCHDOG);
  2723. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  2724. msecs_to_jiffies(timeout));
  2725. if (!ret) {
  2726. cnss_pr_err("Timeout (%ums) waiting for driver power up to complete\n",
  2727. timeout);
  2728. CNSS_ASSERT(0);
  2729. }
  2730. skip_wait_power_up:
  2731. if (!test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2732. !test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2733. goto skip_wait_recovery;
  2734. reinit_completion(&plat_priv->recovery_complete);
  2735. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY);
  2736. ret = wait_for_completion_timeout(&plat_priv->recovery_complete,
  2737. msecs_to_jiffies(timeout));
  2738. if (!ret) {
  2739. cnss_pr_err("Timeout (%ums) waiting for recovery to complete\n",
  2740. timeout);
  2741. CNSS_ASSERT(0);
  2742. }
  2743. skip_wait_recovery:
  2744. cnss_driver_event_post(plat_priv,
  2745. CNSS_DRIVER_EVENT_UNREGISTER_DRIVER,
  2746. CNSS_EVENT_SYNC_UNKILLABLE, NULL);
  2747. mutex_unlock(&plat_priv->driver_ops_lock);
  2748. }
  2749. EXPORT_SYMBOL(cnss_wlan_unregister_driver);
  2750. int cnss_pci_register_driver_hdlr(struct cnss_pci_data *pci_priv,
  2751. void *data)
  2752. {
  2753. int ret = 0;
  2754. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2755. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2756. cnss_pr_dbg("Reboot or shutdown is in progress, ignore register driver\n");
  2757. return -EINVAL;
  2758. }
  2759. set_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2760. pci_priv->driver_ops = data;
  2761. ret = cnss_pci_dev_powerup(pci_priv);
  2762. if (ret) {
  2763. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2764. pci_priv->driver_ops = NULL;
  2765. }
  2766. return ret;
  2767. }
  2768. int cnss_pci_unregister_driver_hdlr(struct cnss_pci_data *pci_priv)
  2769. {
  2770. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2771. set_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2772. cnss_pci_dev_shutdown(pci_priv);
  2773. pci_priv->driver_ops = NULL;
  2774. return 0;
  2775. }
  2776. #if IS_ENABLED(CONFIG_PCI_MSM)
  2777. static bool cnss_pci_is_drv_supported(struct cnss_pci_data *pci_priv)
  2778. {
  2779. struct pci_dev *root_port = pcie_find_root_port(pci_priv->pci_dev);
  2780. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2781. struct device_node *root_of_node;
  2782. bool drv_supported = false;
  2783. if (!root_port) {
  2784. cnss_pr_err("PCIe DRV is not supported as root port is null\n");
  2785. pci_priv->drv_supported = false;
  2786. return drv_supported;
  2787. }
  2788. root_of_node = root_port->dev.of_node;
  2789. if (root_of_node->parent) {
  2790. drv_supported = of_property_read_bool(root_of_node->parent,
  2791. "qcom,drv-supported") ||
  2792. of_property_read_bool(root_of_node->parent,
  2793. "qcom,drv-name");
  2794. }
  2795. cnss_pr_dbg("PCIe DRV is %s\n",
  2796. drv_supported ? "supported" : "not supported");
  2797. pci_priv->drv_supported = drv_supported;
  2798. if (drv_supported) {
  2799. plat_priv->cap.cap_flag |= CNSS_HAS_DRV_SUPPORT;
  2800. cnss_set_feature_list(plat_priv, CNSS_DRV_SUPPORT_V01);
  2801. }
  2802. return drv_supported;
  2803. }
  2804. static void cnss_pci_event_cb(struct msm_pcie_notify *notify)
  2805. {
  2806. struct pci_dev *pci_dev;
  2807. struct cnss_pci_data *pci_priv;
  2808. struct device *dev;
  2809. struct cnss_plat_data *plat_priv = NULL;
  2810. int ret = 0;
  2811. if (!notify)
  2812. return;
  2813. pci_dev = notify->user;
  2814. if (!pci_dev)
  2815. return;
  2816. pci_priv = cnss_get_pci_priv(pci_dev);
  2817. if (!pci_priv)
  2818. return;
  2819. dev = &pci_priv->pci_dev->dev;
  2820. switch (notify->event) {
  2821. case MSM_PCIE_EVENT_LINK_RECOVER:
  2822. cnss_pr_dbg("PCI link recover callback\n");
  2823. plat_priv = pci_priv->plat_priv;
  2824. if (!plat_priv) {
  2825. cnss_pr_err("plat_priv is NULL\n");
  2826. return;
  2827. }
  2828. plat_priv->ctrl_params.quirks |= BIT(LINK_DOWN_SELF_RECOVERY);
  2829. ret = msm_pcie_pm_control(MSM_PCIE_HANDLE_LINKDOWN,
  2830. pci_dev->bus->number, pci_dev, NULL,
  2831. PM_OPTIONS_DEFAULT);
  2832. if (ret)
  2833. cnss_pci_handle_linkdown(pci_priv);
  2834. break;
  2835. case MSM_PCIE_EVENT_LINKDOWN:
  2836. cnss_pr_dbg("PCI link down event callback\n");
  2837. cnss_pci_handle_linkdown(pci_priv);
  2838. break;
  2839. case MSM_PCIE_EVENT_WAKEUP:
  2840. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  2841. cnss_pci_get_auto_suspended(pci_priv)) ||
  2842. dev->power.runtime_status == RPM_SUSPENDING) {
  2843. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2844. cnss_pci_pm_request_resume(pci_priv);
  2845. }
  2846. break;
  2847. case MSM_PCIE_EVENT_DRV_CONNECT:
  2848. cnss_pr_dbg("DRV subsystem is connected\n");
  2849. cnss_pci_set_drv_connected(pci_priv, 1);
  2850. break;
  2851. case MSM_PCIE_EVENT_DRV_DISCONNECT:
  2852. cnss_pr_dbg("DRV subsystem is disconnected\n");
  2853. if (cnss_pci_get_auto_suspended(pci_priv))
  2854. cnss_pci_pm_request_resume(pci_priv);
  2855. cnss_pci_set_drv_connected(pci_priv, 0);
  2856. break;
  2857. default:
  2858. cnss_pr_err("Received invalid PCI event: %d\n", notify->event);
  2859. }
  2860. }
  2861. /**
  2862. * cnss_reg_pci_event() - Register for PCIe events
  2863. * @pci_priv: driver PCI bus context pointer
  2864. *
  2865. * This function shall call corresponding PCIe root complex driver APIs
  2866. * to register for PCIe events like link down or WAKE GPIO toggling etc.
  2867. * The events should be based on PCIe root complex driver's capability.
  2868. *
  2869. * Return: 0 for success, negative value for error
  2870. */
  2871. static int cnss_reg_pci_event(struct cnss_pci_data *pci_priv)
  2872. {
  2873. int ret = 0;
  2874. struct msm_pcie_register_event *pci_event;
  2875. pci_event = &pci_priv->msm_pci_event;
  2876. pci_event->events = MSM_PCIE_EVENT_LINK_RECOVER |
  2877. MSM_PCIE_EVENT_LINKDOWN |
  2878. MSM_PCIE_EVENT_WAKEUP;
  2879. if (cnss_pci_is_drv_supported(pci_priv))
  2880. pci_event->events = pci_event->events |
  2881. MSM_PCIE_EVENT_DRV_CONNECT |
  2882. MSM_PCIE_EVENT_DRV_DISCONNECT;
  2883. pci_event->user = pci_priv->pci_dev;
  2884. pci_event->mode = MSM_PCIE_TRIGGER_CALLBACK;
  2885. pci_event->callback = cnss_pci_event_cb;
  2886. pci_event->options = MSM_PCIE_CONFIG_NO_RECOVERY;
  2887. ret = msm_pcie_register_event(pci_event);
  2888. if (ret)
  2889. cnss_pr_err("Failed to register MSM PCI event, err = %d\n",
  2890. ret);
  2891. return ret;
  2892. }
  2893. static void cnss_dereg_pci_event(struct cnss_pci_data *pci_priv)
  2894. {
  2895. msm_pcie_deregister_event(&pci_priv->msm_pci_event);
  2896. }
  2897. #else
  2898. static int cnss_reg_pci_event(struct cnss_pci_data *pci_priv)
  2899. {
  2900. return 0;
  2901. }
  2902. static void cnss_dereg_pci_event(struct cnss_pci_data *pci_priv) {}
  2903. #endif
  2904. static int cnss_pci_suspend_driver(struct cnss_pci_data *pci_priv)
  2905. {
  2906. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2907. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  2908. int ret = 0;
  2909. pm_message_t state = { .event = PM_EVENT_SUSPEND };
  2910. if (driver_ops && driver_ops->suspend) {
  2911. ret = driver_ops->suspend(pci_dev, state);
  2912. if (ret) {
  2913. cnss_pr_err("Failed to suspend host driver, err = %d\n",
  2914. ret);
  2915. ret = -EAGAIN;
  2916. }
  2917. }
  2918. return ret;
  2919. }
  2920. static int cnss_pci_resume_driver(struct cnss_pci_data *pci_priv)
  2921. {
  2922. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2923. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  2924. int ret = 0;
  2925. if (driver_ops && driver_ops->resume) {
  2926. ret = driver_ops->resume(pci_dev);
  2927. if (ret)
  2928. cnss_pr_err("Failed to resume host driver, err = %d\n",
  2929. ret);
  2930. }
  2931. return ret;
  2932. }
  2933. int cnss_pci_suspend_bus(struct cnss_pci_data *pci_priv)
  2934. {
  2935. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2936. int ret = 0;
  2937. if (pci_priv->pci_link_state == PCI_LINK_DOWN)
  2938. goto out;
  2939. if (cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_SUSPEND)) {
  2940. ret = -EAGAIN;
  2941. goto out;
  2942. }
  2943. if (pci_priv->drv_connected_last)
  2944. goto skip_disable_pci;
  2945. pci_clear_master(pci_dev);
  2946. cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  2947. pci_disable_device(pci_dev);
  2948. ret = pci_set_power_state(pci_dev, PCI_D3hot);
  2949. if (ret)
  2950. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  2951. skip_disable_pci:
  2952. if (cnss_set_pci_link(pci_priv, PCI_LINK_DOWN)) {
  2953. ret = -EAGAIN;
  2954. goto resume_mhi;
  2955. }
  2956. pci_priv->pci_link_state = PCI_LINK_DOWN;
  2957. return 0;
  2958. resume_mhi:
  2959. if (!pci_is_enabled(pci_dev))
  2960. if (pci_enable_device(pci_dev))
  2961. cnss_pr_err("Failed to enable PCI device\n");
  2962. if (pci_priv->saved_state)
  2963. cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  2964. pci_set_master(pci_dev);
  2965. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  2966. out:
  2967. return ret;
  2968. }
  2969. int cnss_pci_resume_bus(struct cnss_pci_data *pci_priv)
  2970. {
  2971. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2972. int ret = 0;
  2973. if (pci_priv->pci_link_state == PCI_LINK_UP)
  2974. goto out;
  2975. if (cnss_set_pci_link(pci_priv, PCI_LINK_UP)) {
  2976. cnss_fatal_err("Failed to resume PCI link from suspend\n");
  2977. cnss_pci_link_down(&pci_dev->dev);
  2978. ret = -EAGAIN;
  2979. goto out;
  2980. }
  2981. pci_priv->pci_link_state = PCI_LINK_UP;
  2982. if (pci_priv->drv_connected_last)
  2983. goto skip_enable_pci;
  2984. ret = pci_enable_device(pci_dev);
  2985. if (ret) {
  2986. cnss_pr_err("Failed to enable PCI device, err = %d\n",
  2987. ret);
  2988. goto out;
  2989. }
  2990. if (pci_priv->saved_state)
  2991. cnss_set_pci_config_space(pci_priv,
  2992. RESTORE_PCI_CONFIG_SPACE);
  2993. pci_set_master(pci_dev);
  2994. skip_enable_pci:
  2995. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  2996. out:
  2997. return ret;
  2998. }
  2999. static int cnss_pci_suspend(struct device *dev)
  3000. {
  3001. int ret = 0;
  3002. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3003. struct cnss_plat_data *plat_priv;
  3004. if (!pci_priv)
  3005. goto out;
  3006. plat_priv = pci_priv->plat_priv;
  3007. if (!plat_priv)
  3008. goto out;
  3009. if (!cnss_is_device_powered_on(plat_priv))
  3010. goto out;
  3011. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3012. pci_priv->drv_supported) {
  3013. pci_priv->drv_connected_last =
  3014. cnss_pci_get_drv_connected(pci_priv);
  3015. if (!pci_priv->drv_connected_last) {
  3016. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3017. ret = -EAGAIN;
  3018. goto out;
  3019. }
  3020. }
  3021. set_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3022. ret = cnss_pci_suspend_driver(pci_priv);
  3023. if (ret)
  3024. goto clear_flag;
  3025. if (!pci_priv->disable_pc) {
  3026. mutex_lock(&pci_priv->bus_lock);
  3027. ret = cnss_pci_suspend_bus(pci_priv);
  3028. mutex_unlock(&pci_priv->bus_lock);
  3029. if (ret)
  3030. goto resume_driver;
  3031. }
  3032. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  3033. return 0;
  3034. resume_driver:
  3035. cnss_pci_resume_driver(pci_priv);
  3036. clear_flag:
  3037. pci_priv->drv_connected_last = 0;
  3038. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3039. out:
  3040. return ret;
  3041. }
  3042. static int cnss_pci_resume(struct device *dev)
  3043. {
  3044. int ret = 0;
  3045. struct pci_dev *pci_dev = to_pci_dev(dev);
  3046. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3047. struct cnss_plat_data *plat_priv;
  3048. if (!pci_priv)
  3049. goto out;
  3050. plat_priv = pci_priv->plat_priv;
  3051. if (!plat_priv)
  3052. goto out;
  3053. if (pci_priv->pci_link_down_ind)
  3054. goto out;
  3055. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3056. goto out;
  3057. if (!pci_priv->disable_pc) {
  3058. ret = cnss_pci_resume_bus(pci_priv);
  3059. if (ret)
  3060. goto out;
  3061. }
  3062. ret = cnss_pci_resume_driver(pci_priv);
  3063. pci_priv->drv_connected_last = 0;
  3064. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3065. out:
  3066. return ret;
  3067. }
  3068. static int cnss_pci_suspend_noirq(struct device *dev)
  3069. {
  3070. int ret = 0;
  3071. struct pci_dev *pci_dev = to_pci_dev(dev);
  3072. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3073. struct cnss_wlan_driver *driver_ops;
  3074. if (!pci_priv)
  3075. goto out;
  3076. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3077. goto out;
  3078. driver_ops = pci_priv->driver_ops;
  3079. if (driver_ops && driver_ops->suspend_noirq)
  3080. ret = driver_ops->suspend_noirq(pci_dev);
  3081. if (pci_priv->disable_pc && !pci_dev->state_saved &&
  3082. !pci_priv->plat_priv->use_pm_domain)
  3083. pci_save_state(pci_dev);
  3084. out:
  3085. return ret;
  3086. }
  3087. static int cnss_pci_resume_noirq(struct device *dev)
  3088. {
  3089. int ret = 0;
  3090. struct pci_dev *pci_dev = to_pci_dev(dev);
  3091. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3092. struct cnss_wlan_driver *driver_ops;
  3093. if (!pci_priv)
  3094. goto out;
  3095. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3096. goto out;
  3097. driver_ops = pci_priv->driver_ops;
  3098. if (driver_ops && driver_ops->resume_noirq &&
  3099. !pci_priv->pci_link_down_ind)
  3100. ret = driver_ops->resume_noirq(pci_dev);
  3101. out:
  3102. return ret;
  3103. }
  3104. static int cnss_pci_runtime_suspend(struct device *dev)
  3105. {
  3106. int ret = 0;
  3107. struct pci_dev *pci_dev = to_pci_dev(dev);
  3108. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3109. struct cnss_plat_data *plat_priv;
  3110. struct cnss_wlan_driver *driver_ops;
  3111. if (!pci_priv)
  3112. return -EAGAIN;
  3113. plat_priv = pci_priv->plat_priv;
  3114. if (!plat_priv)
  3115. return -EAGAIN;
  3116. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3117. return -EAGAIN;
  3118. if (pci_priv->pci_link_down_ind) {
  3119. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3120. return -EAGAIN;
  3121. }
  3122. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3123. pci_priv->drv_supported) {
  3124. pci_priv->drv_connected_last =
  3125. cnss_pci_get_drv_connected(pci_priv);
  3126. if (!pci_priv->drv_connected_last) {
  3127. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3128. return -EAGAIN;
  3129. }
  3130. }
  3131. cnss_pr_vdbg("Runtime suspend start\n");
  3132. driver_ops = pci_priv->driver_ops;
  3133. if (driver_ops && driver_ops->runtime_ops &&
  3134. driver_ops->runtime_ops->runtime_suspend)
  3135. ret = driver_ops->runtime_ops->runtime_suspend(pci_dev);
  3136. else
  3137. ret = cnss_auto_suspend(dev);
  3138. if (ret)
  3139. pci_priv->drv_connected_last = 0;
  3140. cnss_pr_vdbg("Runtime suspend status: %d\n", ret);
  3141. return ret;
  3142. }
  3143. static int cnss_pci_runtime_resume(struct device *dev)
  3144. {
  3145. int ret = 0;
  3146. struct pci_dev *pci_dev = to_pci_dev(dev);
  3147. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3148. struct cnss_wlan_driver *driver_ops;
  3149. if (!pci_priv)
  3150. return -EAGAIN;
  3151. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3152. return -EAGAIN;
  3153. if (pci_priv->pci_link_down_ind) {
  3154. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3155. return -EAGAIN;
  3156. }
  3157. cnss_pr_vdbg("Runtime resume start\n");
  3158. driver_ops = pci_priv->driver_ops;
  3159. if (driver_ops && driver_ops->runtime_ops &&
  3160. driver_ops->runtime_ops->runtime_resume)
  3161. ret = driver_ops->runtime_ops->runtime_resume(pci_dev);
  3162. else
  3163. ret = cnss_auto_resume(dev);
  3164. if (!ret)
  3165. pci_priv->drv_connected_last = 0;
  3166. cnss_pr_vdbg("Runtime resume status: %d\n", ret);
  3167. return ret;
  3168. }
  3169. static int cnss_pci_runtime_idle(struct device *dev)
  3170. {
  3171. cnss_pr_vdbg("Runtime idle\n");
  3172. pm_request_autosuspend(dev);
  3173. return -EBUSY;
  3174. }
  3175. int cnss_wlan_pm_control(struct device *dev, bool vote)
  3176. {
  3177. struct pci_dev *pci_dev = to_pci_dev(dev);
  3178. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3179. int ret = 0;
  3180. if (!pci_priv)
  3181. return -ENODEV;
  3182. ret = cnss_pci_disable_pc(pci_priv, vote);
  3183. if (ret)
  3184. return ret;
  3185. pci_priv->disable_pc = vote;
  3186. cnss_pr_dbg("%s PCIe power collapse\n", vote ? "disable" : "enable");
  3187. return 0;
  3188. }
  3189. EXPORT_SYMBOL(cnss_wlan_pm_control);
  3190. static void cnss_pci_pm_runtime_get_record(struct cnss_pci_data *pci_priv,
  3191. enum cnss_rtpm_id id)
  3192. {
  3193. if (id >= RTPM_ID_MAX)
  3194. return;
  3195. atomic_inc(&pci_priv->pm_stats.runtime_get);
  3196. atomic_inc(&pci_priv->pm_stats.runtime_get_id[id]);
  3197. pci_priv->pm_stats.runtime_get_timestamp_id[id] =
  3198. cnss_get_host_timestamp(pci_priv->plat_priv);
  3199. }
  3200. static void cnss_pci_pm_runtime_put_record(struct cnss_pci_data *pci_priv,
  3201. enum cnss_rtpm_id id)
  3202. {
  3203. if (id >= RTPM_ID_MAX)
  3204. return;
  3205. atomic_inc(&pci_priv->pm_stats.runtime_put);
  3206. atomic_inc(&pci_priv->pm_stats.runtime_put_id[id]);
  3207. pci_priv->pm_stats.runtime_put_timestamp_id[id] =
  3208. cnss_get_host_timestamp(pci_priv->plat_priv);
  3209. }
  3210. void cnss_pci_pm_runtime_show_usage_count(struct cnss_pci_data *pci_priv)
  3211. {
  3212. struct device *dev;
  3213. if (!pci_priv)
  3214. return;
  3215. dev = &pci_priv->pci_dev->dev;
  3216. cnss_pr_dbg("Runtime PM usage count: %d\n",
  3217. atomic_read(&dev->power.usage_count));
  3218. }
  3219. int cnss_pci_pm_request_resume(struct cnss_pci_data *pci_priv)
  3220. {
  3221. struct device *dev;
  3222. enum rpm_status status;
  3223. if (!pci_priv)
  3224. return -ENODEV;
  3225. dev = &pci_priv->pci_dev->dev;
  3226. status = dev->power.runtime_status;
  3227. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3228. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3229. (void *)_RET_IP_);
  3230. return pm_request_resume(dev);
  3231. }
  3232. int cnss_pci_pm_runtime_resume(struct cnss_pci_data *pci_priv)
  3233. {
  3234. struct device *dev;
  3235. enum rpm_status status;
  3236. if (!pci_priv)
  3237. return -ENODEV;
  3238. dev = &pci_priv->pci_dev->dev;
  3239. status = dev->power.runtime_status;
  3240. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3241. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3242. (void *)_RET_IP_);
  3243. return pm_runtime_resume(dev);
  3244. }
  3245. int cnss_pci_pm_runtime_get(struct cnss_pci_data *pci_priv,
  3246. enum cnss_rtpm_id id)
  3247. {
  3248. struct device *dev;
  3249. enum rpm_status status;
  3250. if (!pci_priv)
  3251. return -ENODEV;
  3252. dev = &pci_priv->pci_dev->dev;
  3253. status = dev->power.runtime_status;
  3254. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3255. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3256. (void *)_RET_IP_);
  3257. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3258. return pm_runtime_get(dev);
  3259. }
  3260. int cnss_pci_pm_runtime_get_sync(struct cnss_pci_data *pci_priv,
  3261. enum cnss_rtpm_id id)
  3262. {
  3263. struct device *dev;
  3264. enum rpm_status status;
  3265. if (!pci_priv)
  3266. return -ENODEV;
  3267. dev = &pci_priv->pci_dev->dev;
  3268. status = dev->power.runtime_status;
  3269. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3270. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3271. (void *)_RET_IP_);
  3272. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3273. return pm_runtime_get_sync(dev);
  3274. }
  3275. void cnss_pci_pm_runtime_get_noresume(struct cnss_pci_data *pci_priv,
  3276. enum cnss_rtpm_id id)
  3277. {
  3278. if (!pci_priv)
  3279. return;
  3280. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3281. pm_runtime_get_noresume(&pci_priv->pci_dev->dev);
  3282. }
  3283. int cnss_pci_pm_runtime_put_autosuspend(struct cnss_pci_data *pci_priv,
  3284. enum cnss_rtpm_id id)
  3285. {
  3286. struct device *dev;
  3287. if (!pci_priv)
  3288. return -ENODEV;
  3289. dev = &pci_priv->pci_dev->dev;
  3290. if (atomic_read(&dev->power.usage_count) == 0) {
  3291. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3292. return -EINVAL;
  3293. }
  3294. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3295. return pm_runtime_put_autosuspend(&pci_priv->pci_dev->dev);
  3296. }
  3297. void cnss_pci_pm_runtime_put_noidle(struct cnss_pci_data *pci_priv,
  3298. enum cnss_rtpm_id id)
  3299. {
  3300. struct device *dev;
  3301. if (!pci_priv)
  3302. return;
  3303. dev = &pci_priv->pci_dev->dev;
  3304. if (atomic_read(&dev->power.usage_count) == 0) {
  3305. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3306. return;
  3307. }
  3308. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3309. pm_runtime_put_noidle(&pci_priv->pci_dev->dev);
  3310. }
  3311. void cnss_pci_pm_runtime_mark_last_busy(struct cnss_pci_data *pci_priv)
  3312. {
  3313. if (!pci_priv)
  3314. return;
  3315. pm_runtime_mark_last_busy(&pci_priv->pci_dev->dev);
  3316. }
  3317. int cnss_auto_suspend(struct device *dev)
  3318. {
  3319. int ret = 0;
  3320. struct pci_dev *pci_dev = to_pci_dev(dev);
  3321. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3322. struct cnss_plat_data *plat_priv;
  3323. if (!pci_priv)
  3324. return -ENODEV;
  3325. plat_priv = pci_priv->plat_priv;
  3326. if (!plat_priv)
  3327. return -ENODEV;
  3328. mutex_lock(&pci_priv->bus_lock);
  3329. if (!pci_priv->qmi_send_usage_count) {
  3330. ret = cnss_pci_suspend_bus(pci_priv);
  3331. if (ret) {
  3332. mutex_unlock(&pci_priv->bus_lock);
  3333. return ret;
  3334. }
  3335. }
  3336. cnss_pci_set_auto_suspended(pci_priv, 1);
  3337. mutex_unlock(&pci_priv->bus_lock);
  3338. cnss_pci_set_monitor_wake_intr(pci_priv, true);
  3339. /* For suspend temporarily set bandwidth vote to NONE and dont save in
  3340. * current_bw_vote as in resume path we should vote for last used
  3341. * bandwidth vote. Also ignore error if bw voting is not setup.
  3342. */
  3343. cnss_setup_bus_bandwidth(plat_priv, CNSS_BUS_WIDTH_NONE, false);
  3344. return 0;
  3345. }
  3346. EXPORT_SYMBOL(cnss_auto_suspend);
  3347. int cnss_auto_resume(struct device *dev)
  3348. {
  3349. int ret = 0;
  3350. struct pci_dev *pci_dev = to_pci_dev(dev);
  3351. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3352. struct cnss_plat_data *plat_priv;
  3353. if (!pci_priv)
  3354. return -ENODEV;
  3355. plat_priv = pci_priv->plat_priv;
  3356. if (!plat_priv)
  3357. return -ENODEV;
  3358. mutex_lock(&pci_priv->bus_lock);
  3359. ret = cnss_pci_resume_bus(pci_priv);
  3360. if (ret) {
  3361. mutex_unlock(&pci_priv->bus_lock);
  3362. return ret;
  3363. }
  3364. cnss_pci_set_auto_suspended(pci_priv, 0);
  3365. mutex_unlock(&pci_priv->bus_lock);
  3366. cnss_request_bus_bandwidth(dev, plat_priv->icc.current_bw_vote);
  3367. return 0;
  3368. }
  3369. EXPORT_SYMBOL(cnss_auto_resume);
  3370. int cnss_pci_force_wake_request_sync(struct device *dev, int timeout_us)
  3371. {
  3372. struct pci_dev *pci_dev = to_pci_dev(dev);
  3373. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3374. struct cnss_plat_data *plat_priv;
  3375. struct mhi_controller *mhi_ctrl;
  3376. if (!pci_priv)
  3377. return -ENODEV;
  3378. switch (pci_priv->device_id) {
  3379. case QCA6390_DEVICE_ID:
  3380. case QCA6490_DEVICE_ID:
  3381. case KIWI_DEVICE_ID:
  3382. break;
  3383. default:
  3384. return 0;
  3385. }
  3386. mhi_ctrl = pci_priv->mhi_ctrl;
  3387. if (!mhi_ctrl)
  3388. return -EINVAL;
  3389. plat_priv = pci_priv->plat_priv;
  3390. if (!plat_priv)
  3391. return -ENODEV;
  3392. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3393. return -EAGAIN;
  3394. if (timeout_us) {
  3395. /* Busy wait for timeout_us */
  3396. return cnss_mhi_device_get_sync_atomic(pci_priv,
  3397. timeout_us, false);
  3398. } else {
  3399. /* Sleep wait for mhi_ctrl->timeout_ms */
  3400. return mhi_device_get_sync(mhi_ctrl->mhi_dev);
  3401. }
  3402. }
  3403. EXPORT_SYMBOL(cnss_pci_force_wake_request_sync);
  3404. int cnss_pci_force_wake_request(struct device *dev)
  3405. {
  3406. struct pci_dev *pci_dev = to_pci_dev(dev);
  3407. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3408. struct cnss_plat_data *plat_priv;
  3409. struct mhi_controller *mhi_ctrl;
  3410. if (!pci_priv)
  3411. return -ENODEV;
  3412. switch (pci_priv->device_id) {
  3413. case QCA6390_DEVICE_ID:
  3414. case QCA6490_DEVICE_ID:
  3415. case KIWI_DEVICE_ID:
  3416. break;
  3417. default:
  3418. return 0;
  3419. }
  3420. mhi_ctrl = pci_priv->mhi_ctrl;
  3421. if (!mhi_ctrl)
  3422. return -EINVAL;
  3423. plat_priv = pci_priv->plat_priv;
  3424. if (!plat_priv)
  3425. return -ENODEV;
  3426. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3427. return -EAGAIN;
  3428. mhi_device_get(mhi_ctrl->mhi_dev);
  3429. return 0;
  3430. }
  3431. EXPORT_SYMBOL(cnss_pci_force_wake_request);
  3432. int cnss_pci_is_device_awake(struct device *dev)
  3433. {
  3434. struct pci_dev *pci_dev = to_pci_dev(dev);
  3435. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3436. struct mhi_controller *mhi_ctrl;
  3437. if (!pci_priv)
  3438. return -ENODEV;
  3439. switch (pci_priv->device_id) {
  3440. case QCA6390_DEVICE_ID:
  3441. case QCA6490_DEVICE_ID:
  3442. case KIWI_DEVICE_ID:
  3443. break;
  3444. default:
  3445. return 0;
  3446. }
  3447. mhi_ctrl = pci_priv->mhi_ctrl;
  3448. if (!mhi_ctrl)
  3449. return -EINVAL;
  3450. return (mhi_ctrl->dev_state == MHI_STATE_M0);
  3451. }
  3452. EXPORT_SYMBOL(cnss_pci_is_device_awake);
  3453. int cnss_pci_force_wake_release(struct device *dev)
  3454. {
  3455. struct pci_dev *pci_dev = to_pci_dev(dev);
  3456. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3457. struct cnss_plat_data *plat_priv;
  3458. struct mhi_controller *mhi_ctrl;
  3459. if (!pci_priv)
  3460. return -ENODEV;
  3461. switch (pci_priv->device_id) {
  3462. case QCA6390_DEVICE_ID:
  3463. case QCA6490_DEVICE_ID:
  3464. case KIWI_DEVICE_ID:
  3465. break;
  3466. default:
  3467. return 0;
  3468. }
  3469. mhi_ctrl = pci_priv->mhi_ctrl;
  3470. if (!mhi_ctrl)
  3471. return -EINVAL;
  3472. plat_priv = pci_priv->plat_priv;
  3473. if (!plat_priv)
  3474. return -ENODEV;
  3475. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3476. return -EAGAIN;
  3477. mhi_device_put(mhi_ctrl->mhi_dev);
  3478. return 0;
  3479. }
  3480. EXPORT_SYMBOL(cnss_pci_force_wake_release);
  3481. int cnss_pci_qmi_send_get(struct cnss_pci_data *pci_priv)
  3482. {
  3483. int ret = 0;
  3484. if (!pci_priv)
  3485. return -ENODEV;
  3486. mutex_lock(&pci_priv->bus_lock);
  3487. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3488. !pci_priv->qmi_send_usage_count)
  3489. ret = cnss_pci_resume_bus(pci_priv);
  3490. pci_priv->qmi_send_usage_count++;
  3491. cnss_pr_buf("Increased QMI send usage count to %d\n",
  3492. pci_priv->qmi_send_usage_count);
  3493. mutex_unlock(&pci_priv->bus_lock);
  3494. return ret;
  3495. }
  3496. int cnss_pci_qmi_send_put(struct cnss_pci_data *pci_priv)
  3497. {
  3498. int ret = 0;
  3499. if (!pci_priv)
  3500. return -ENODEV;
  3501. mutex_lock(&pci_priv->bus_lock);
  3502. if (pci_priv->qmi_send_usage_count)
  3503. pci_priv->qmi_send_usage_count--;
  3504. cnss_pr_buf("Decreased QMI send usage count to %d\n",
  3505. pci_priv->qmi_send_usage_count);
  3506. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3507. !pci_priv->qmi_send_usage_count &&
  3508. !cnss_pcie_is_device_down(pci_priv))
  3509. ret = cnss_pci_suspend_bus(pci_priv);
  3510. mutex_unlock(&pci_priv->bus_lock);
  3511. return ret;
  3512. }
  3513. int cnss_pci_alloc_fw_mem(struct cnss_pci_data *pci_priv)
  3514. {
  3515. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3516. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3517. struct device *dev = &pci_priv->pci_dev->dev;
  3518. int i;
  3519. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3520. if (!fw_mem[i].va && fw_mem[i].size) {
  3521. fw_mem[i].va =
  3522. dma_alloc_attrs(dev, fw_mem[i].size,
  3523. &fw_mem[i].pa, GFP_KERNEL,
  3524. fw_mem[i].attrs);
  3525. if (!fw_mem[i].va) {
  3526. cnss_pr_err("Failed to allocate memory for FW, size: 0x%zx, type: %u\n",
  3527. fw_mem[i].size, fw_mem[i].type);
  3528. return -ENOMEM;
  3529. }
  3530. }
  3531. }
  3532. return 0;
  3533. }
  3534. static void cnss_pci_free_fw_mem(struct cnss_pci_data *pci_priv)
  3535. {
  3536. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3537. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3538. struct device *dev = &pci_priv->pci_dev->dev;
  3539. int i;
  3540. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3541. if (fw_mem[i].va && fw_mem[i].size) {
  3542. cnss_pr_dbg("Freeing memory for FW, va: 0x%pK, pa: %pa, size: 0x%zx, type: %u\n",
  3543. fw_mem[i].va, &fw_mem[i].pa,
  3544. fw_mem[i].size, fw_mem[i].type);
  3545. dma_free_attrs(dev, fw_mem[i].size,
  3546. fw_mem[i].va, fw_mem[i].pa,
  3547. fw_mem[i].attrs);
  3548. fw_mem[i].va = NULL;
  3549. fw_mem[i].pa = 0;
  3550. fw_mem[i].size = 0;
  3551. fw_mem[i].type = 0;
  3552. }
  3553. }
  3554. plat_priv->fw_mem_seg_len = 0;
  3555. }
  3556. int cnss_pci_alloc_qdss_mem(struct cnss_pci_data *pci_priv)
  3557. {
  3558. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3559. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3560. int i, j;
  3561. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3562. if (!qdss_mem[i].va && qdss_mem[i].size) {
  3563. qdss_mem[i].va =
  3564. dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3565. qdss_mem[i].size,
  3566. &qdss_mem[i].pa,
  3567. GFP_KERNEL);
  3568. if (!qdss_mem[i].va) {
  3569. cnss_pr_err("Failed to allocate QDSS memory for FW, size: 0x%zx, type: %u, chuck-ID: %d\n",
  3570. qdss_mem[i].size,
  3571. qdss_mem[i].type, i);
  3572. break;
  3573. }
  3574. }
  3575. }
  3576. /* Best-effort allocation for QDSS trace */
  3577. if (i < plat_priv->qdss_mem_seg_len) {
  3578. for (j = i; j < plat_priv->qdss_mem_seg_len; j++) {
  3579. qdss_mem[j].type = 0;
  3580. qdss_mem[j].size = 0;
  3581. }
  3582. plat_priv->qdss_mem_seg_len = i;
  3583. }
  3584. return 0;
  3585. }
  3586. void cnss_pci_free_qdss_mem(struct cnss_pci_data *pci_priv)
  3587. {
  3588. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3589. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3590. int i;
  3591. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3592. if (qdss_mem[i].va && qdss_mem[i].size) {
  3593. cnss_pr_dbg("Freeing memory for QDSS: pa: %pa, size: 0x%zx, type: %u\n",
  3594. &qdss_mem[i].pa, qdss_mem[i].size,
  3595. qdss_mem[i].type);
  3596. dma_free_coherent(&pci_priv->pci_dev->dev,
  3597. qdss_mem[i].size, qdss_mem[i].va,
  3598. qdss_mem[i].pa);
  3599. qdss_mem[i].va = NULL;
  3600. qdss_mem[i].pa = 0;
  3601. qdss_mem[i].size = 0;
  3602. qdss_mem[i].type = 0;
  3603. }
  3604. }
  3605. plat_priv->qdss_mem_seg_len = 0;
  3606. }
  3607. int cnss_pci_load_m3(struct cnss_pci_data *pci_priv)
  3608. {
  3609. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3610. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3611. char filename[MAX_FIRMWARE_NAME_LEN];
  3612. char *phy_filename = DEFAULT_PHY_UCODE_FILE_NAME;
  3613. const struct firmware *fw_entry;
  3614. int ret = 0;
  3615. /* Use forward compatibility here since for any recent device
  3616. * it should use DEFAULT_PHY_UCODE_FILE_NAME.
  3617. */
  3618. switch (pci_priv->device_id) {
  3619. case QCA6174_DEVICE_ID:
  3620. cnss_pr_err("Invalid device ID (0x%x) to load phy image\n",
  3621. pci_priv->device_id);
  3622. return -EINVAL;
  3623. case QCA6290_DEVICE_ID:
  3624. case QCA6390_DEVICE_ID:
  3625. case QCA6490_DEVICE_ID:
  3626. phy_filename = DEFAULT_PHY_M3_FILE_NAME;
  3627. break;
  3628. case KIWI_DEVICE_ID:
  3629. switch (plat_priv->device_version.major_version) {
  3630. case FW_V2_NUMBER:
  3631. phy_filename = PHY_UCODE_V2_FILE_NAME;
  3632. break;
  3633. default:
  3634. break;
  3635. }
  3636. break;
  3637. default:
  3638. break;
  3639. }
  3640. if (!m3_mem->va && !m3_mem->size) {
  3641. cnss_pci_add_fw_prefix_name(pci_priv, filename,
  3642. phy_filename);
  3643. ret = firmware_request_nowarn(&fw_entry, filename,
  3644. &pci_priv->pci_dev->dev);
  3645. if (ret) {
  3646. cnss_pr_err("Failed to load M3 image: %s\n", filename);
  3647. return ret;
  3648. }
  3649. m3_mem->va = dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3650. fw_entry->size, &m3_mem->pa,
  3651. GFP_KERNEL);
  3652. if (!m3_mem->va) {
  3653. cnss_pr_err("Failed to allocate memory for M3, size: 0x%zx\n",
  3654. fw_entry->size);
  3655. release_firmware(fw_entry);
  3656. return -ENOMEM;
  3657. }
  3658. memcpy(m3_mem->va, fw_entry->data, fw_entry->size);
  3659. m3_mem->size = fw_entry->size;
  3660. release_firmware(fw_entry);
  3661. }
  3662. return 0;
  3663. }
  3664. static void cnss_pci_free_m3_mem(struct cnss_pci_data *pci_priv)
  3665. {
  3666. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3667. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3668. if (m3_mem->va && m3_mem->size) {
  3669. cnss_pr_dbg("Freeing memory for M3, va: 0x%pK, pa: %pa, size: 0x%zx\n",
  3670. m3_mem->va, &m3_mem->pa, m3_mem->size);
  3671. dma_free_coherent(&pci_priv->pci_dev->dev, m3_mem->size,
  3672. m3_mem->va, m3_mem->pa);
  3673. }
  3674. m3_mem->va = NULL;
  3675. m3_mem->pa = 0;
  3676. m3_mem->size = 0;
  3677. }
  3678. void cnss_pci_fw_boot_timeout_hdlr(struct cnss_pci_data *pci_priv)
  3679. {
  3680. struct cnss_plat_data *plat_priv;
  3681. if (!pci_priv)
  3682. return;
  3683. cnss_fatal_err("Timeout waiting for FW ready indication\n");
  3684. plat_priv = pci_priv->plat_priv;
  3685. if (!plat_priv)
  3686. return;
  3687. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  3688. cnss_pr_dbg("Ignore FW ready timeout for calibration mode\n");
  3689. return;
  3690. }
  3691. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  3692. CNSS_REASON_TIMEOUT);
  3693. }
  3694. static int cnss_pci_smmu_fault_handler(struct iommu_domain *domain,
  3695. struct device *dev, unsigned long iova,
  3696. int flags, void *handler_token)
  3697. {
  3698. struct cnss_pci_data *pci_priv = handler_token;
  3699. cnss_fatal_err("SMMU fault happened with IOVA 0x%lx\n", iova);
  3700. if (!pci_priv) {
  3701. cnss_pr_err("pci_priv is NULL\n");
  3702. return -ENODEV;
  3703. }
  3704. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  3705. cnss_force_fw_assert(&pci_priv->pci_dev->dev);
  3706. /* IOMMU driver requires -ENOSYS to print debug info. */
  3707. return -ENOSYS;
  3708. }
  3709. static int cnss_pci_init_smmu(struct cnss_pci_data *pci_priv)
  3710. {
  3711. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3712. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3713. struct device_node *of_node;
  3714. struct resource *res;
  3715. const char *iommu_dma_type;
  3716. u32 addr_win[2];
  3717. int ret = 0;
  3718. of_node = of_parse_phandle(pci_dev->dev.of_node, "qcom,iommu-group", 0);
  3719. if (!of_node)
  3720. return ret;
  3721. cnss_pr_dbg("Initializing SMMU\n");
  3722. pci_priv->iommu_domain = iommu_get_domain_for_dev(&pci_dev->dev);
  3723. ret = of_property_read_string(of_node, "qcom,iommu-dma",
  3724. &iommu_dma_type);
  3725. if (!ret && !strcmp("fastmap", iommu_dma_type)) {
  3726. cnss_pr_dbg("Enabling SMMU S1 stage\n");
  3727. pci_priv->smmu_s1_enable = true;
  3728. iommu_set_fault_handler(pci_priv->iommu_domain,
  3729. cnss_pci_smmu_fault_handler, pci_priv);
  3730. }
  3731. ret = of_property_read_u32_array(of_node, "qcom,iommu-dma-addr-pool",
  3732. addr_win, ARRAY_SIZE(addr_win));
  3733. if (ret) {
  3734. cnss_pr_err("Invalid SMMU size window, err = %d\n", ret);
  3735. of_node_put(of_node);
  3736. return ret;
  3737. }
  3738. pci_priv->smmu_iova_start = addr_win[0];
  3739. pci_priv->smmu_iova_len = addr_win[1];
  3740. cnss_pr_dbg("smmu_iova_start: %pa, smmu_iova_len: 0x%zx\n",
  3741. &pci_priv->smmu_iova_start,
  3742. pci_priv->smmu_iova_len);
  3743. res = platform_get_resource_byname(plat_priv->plat_dev, IORESOURCE_MEM,
  3744. "smmu_iova_ipa");
  3745. if (res) {
  3746. pci_priv->smmu_iova_ipa_start = res->start;
  3747. pci_priv->smmu_iova_ipa_current = res->start;
  3748. pci_priv->smmu_iova_ipa_len = resource_size(res);
  3749. cnss_pr_dbg("smmu_iova_ipa_start: %pa, smmu_iova_ipa_len: 0x%zx\n",
  3750. &pci_priv->smmu_iova_ipa_start,
  3751. pci_priv->smmu_iova_ipa_len);
  3752. }
  3753. pci_priv->iommu_geometry = of_property_read_bool(of_node,
  3754. "qcom,iommu-geometry");
  3755. cnss_pr_dbg("iommu_geometry: %d\n", pci_priv->iommu_geometry);
  3756. of_node_put(of_node);
  3757. return 0;
  3758. }
  3759. static void cnss_pci_deinit_smmu(struct cnss_pci_data *pci_priv)
  3760. {
  3761. pci_priv->iommu_domain = NULL;
  3762. }
  3763. int cnss_pci_get_iova(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3764. {
  3765. if (!pci_priv)
  3766. return -ENODEV;
  3767. if (!pci_priv->smmu_iova_len)
  3768. return -EINVAL;
  3769. *addr = pci_priv->smmu_iova_start;
  3770. *size = pci_priv->smmu_iova_len;
  3771. return 0;
  3772. }
  3773. int cnss_pci_get_iova_ipa(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3774. {
  3775. if (!pci_priv)
  3776. return -ENODEV;
  3777. if (!pci_priv->smmu_iova_ipa_len)
  3778. return -EINVAL;
  3779. *addr = pci_priv->smmu_iova_ipa_start;
  3780. *size = pci_priv->smmu_iova_ipa_len;
  3781. return 0;
  3782. }
  3783. struct iommu_domain *cnss_smmu_get_domain(struct device *dev)
  3784. {
  3785. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3786. if (!pci_priv)
  3787. return NULL;
  3788. return pci_priv->iommu_domain;
  3789. }
  3790. EXPORT_SYMBOL(cnss_smmu_get_domain);
  3791. int cnss_smmu_map(struct device *dev,
  3792. phys_addr_t paddr, uint32_t *iova_addr, size_t size)
  3793. {
  3794. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3795. struct cnss_plat_data *plat_priv;
  3796. unsigned long iova;
  3797. size_t len;
  3798. int ret = 0;
  3799. int flag = IOMMU_READ | IOMMU_WRITE;
  3800. struct pci_dev *root_port;
  3801. struct device_node *root_of_node;
  3802. bool dma_coherent = false;
  3803. if (!pci_priv)
  3804. return -ENODEV;
  3805. if (!iova_addr) {
  3806. cnss_pr_err("iova_addr is NULL, paddr %pa, size %zu\n",
  3807. &paddr, size);
  3808. return -EINVAL;
  3809. }
  3810. plat_priv = pci_priv->plat_priv;
  3811. len = roundup(size + paddr - rounddown(paddr, PAGE_SIZE), PAGE_SIZE);
  3812. iova = roundup(pci_priv->smmu_iova_ipa_current, PAGE_SIZE);
  3813. if (pci_priv->iommu_geometry &&
  3814. iova >= pci_priv->smmu_iova_ipa_start +
  3815. pci_priv->smmu_iova_ipa_len) {
  3816. cnss_pr_err("No IOVA space to map, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3817. iova,
  3818. &pci_priv->smmu_iova_ipa_start,
  3819. pci_priv->smmu_iova_ipa_len);
  3820. return -ENOMEM;
  3821. }
  3822. if (!test_bit(DISABLE_IO_COHERENCY,
  3823. &plat_priv->ctrl_params.quirks)) {
  3824. root_port = pcie_find_root_port(pci_priv->pci_dev);
  3825. if (!root_port) {
  3826. cnss_pr_err("Root port is null, so dma_coherent is disabled\n");
  3827. } else {
  3828. root_of_node = root_port->dev.of_node;
  3829. if (root_of_node && root_of_node->parent) {
  3830. dma_coherent =
  3831. of_property_read_bool(root_of_node->parent,
  3832. "dma-coherent");
  3833. cnss_pr_dbg("dma-coherent is %s\n",
  3834. dma_coherent ? "enabled" : "disabled");
  3835. if (dma_coherent)
  3836. flag |= IOMMU_CACHE;
  3837. }
  3838. }
  3839. }
  3840. cnss_pr_dbg("IOMMU map: iova %lx, len %zu\n", iova, len);
  3841. ret = iommu_map(pci_priv->iommu_domain, iova,
  3842. rounddown(paddr, PAGE_SIZE), len, flag);
  3843. if (ret) {
  3844. cnss_pr_err("PA to IOVA mapping failed, ret %d\n", ret);
  3845. return ret;
  3846. }
  3847. pci_priv->smmu_iova_ipa_current = iova + len;
  3848. *iova_addr = (uint32_t)(iova + paddr - rounddown(paddr, PAGE_SIZE));
  3849. cnss_pr_dbg("IOMMU map: iova_addr %lx\n", *iova_addr);
  3850. return 0;
  3851. }
  3852. EXPORT_SYMBOL(cnss_smmu_map);
  3853. int cnss_smmu_unmap(struct device *dev, uint32_t iova_addr, size_t size)
  3854. {
  3855. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3856. unsigned long iova;
  3857. size_t unmapped;
  3858. size_t len;
  3859. if (!pci_priv)
  3860. return -ENODEV;
  3861. iova = rounddown(iova_addr, PAGE_SIZE);
  3862. len = roundup(size + iova_addr - iova, PAGE_SIZE);
  3863. if (iova >= pci_priv->smmu_iova_ipa_start +
  3864. pci_priv->smmu_iova_ipa_len) {
  3865. cnss_pr_err("Out of IOVA space to unmap, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3866. iova,
  3867. &pci_priv->smmu_iova_ipa_start,
  3868. pci_priv->smmu_iova_ipa_len);
  3869. return -ENOMEM;
  3870. }
  3871. cnss_pr_dbg("IOMMU unmap: iova %lx, len %zu\n", iova, len);
  3872. unmapped = iommu_unmap(pci_priv->iommu_domain, iova, len);
  3873. if (unmapped != len) {
  3874. cnss_pr_err("IOMMU unmap failed, unmapped = %zu, requested = %zu\n",
  3875. unmapped, len);
  3876. return -EINVAL;
  3877. }
  3878. pci_priv->smmu_iova_ipa_current = iova;
  3879. return 0;
  3880. }
  3881. EXPORT_SYMBOL(cnss_smmu_unmap);
  3882. int cnss_get_soc_info(struct device *dev, struct cnss_soc_info *info)
  3883. {
  3884. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3885. struct cnss_plat_data *plat_priv;
  3886. if (!pci_priv)
  3887. return -ENODEV;
  3888. plat_priv = pci_priv->plat_priv;
  3889. if (!plat_priv)
  3890. return -ENODEV;
  3891. info->va = pci_priv->bar;
  3892. info->pa = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  3893. info->chip_id = plat_priv->chip_info.chip_id;
  3894. info->chip_family = plat_priv->chip_info.chip_family;
  3895. info->board_id = plat_priv->board_info.board_id;
  3896. info->soc_id = plat_priv->soc_info.soc_id;
  3897. info->fw_version = plat_priv->fw_version_info.fw_version;
  3898. strlcpy(info->fw_build_timestamp,
  3899. plat_priv->fw_version_info.fw_build_timestamp,
  3900. sizeof(info->fw_build_timestamp));
  3901. memcpy(&info->device_version, &plat_priv->device_version,
  3902. sizeof(info->device_version));
  3903. memcpy(&info->dev_mem_info, &plat_priv->dev_mem_info,
  3904. sizeof(info->dev_mem_info));
  3905. return 0;
  3906. }
  3907. EXPORT_SYMBOL(cnss_get_soc_info);
  3908. static struct cnss_msi_config msi_config = {
  3909. .total_vectors = 32,
  3910. .total_users = 4,
  3911. .users = (struct cnss_msi_user[]) {
  3912. { .name = "MHI", .num_vectors = 3, .base_vector = 0 },
  3913. { .name = "CE", .num_vectors = 10, .base_vector = 3 },
  3914. { .name = "WAKE", .num_vectors = 1, .base_vector = 13 },
  3915. { .name = "DP", .num_vectors = 18, .base_vector = 14 },
  3916. },
  3917. };
  3918. static int cnss_pci_get_msi_assignment(struct cnss_pci_data *pci_priv)
  3919. {
  3920. pci_priv->msi_config = &msi_config;
  3921. return 0;
  3922. }
  3923. static int cnss_pci_enable_msi(struct cnss_pci_data *pci_priv)
  3924. {
  3925. int ret = 0;
  3926. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3927. int num_vectors;
  3928. struct cnss_msi_config *msi_config;
  3929. struct msi_desc *msi_desc;
  3930. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  3931. return 0;
  3932. ret = cnss_pci_get_msi_assignment(pci_priv);
  3933. if (ret) {
  3934. cnss_pr_err("Failed to get MSI assignment, err = %d\n", ret);
  3935. goto out;
  3936. }
  3937. msi_config = pci_priv->msi_config;
  3938. if (!msi_config) {
  3939. cnss_pr_err("msi_config is NULL!\n");
  3940. ret = -EINVAL;
  3941. goto out;
  3942. }
  3943. num_vectors = pci_alloc_irq_vectors(pci_dev,
  3944. msi_config->total_vectors,
  3945. msi_config->total_vectors,
  3946. PCI_IRQ_MSI);
  3947. if (num_vectors != msi_config->total_vectors) {
  3948. cnss_pr_err("Failed to get enough MSI vectors (%d), available vectors = %d",
  3949. msi_config->total_vectors, num_vectors);
  3950. if (num_vectors >= 0)
  3951. ret = -EINVAL;
  3952. goto reset_msi_config;
  3953. }
  3954. msi_desc = irq_get_msi_desc(pci_dev->irq);
  3955. if (!msi_desc) {
  3956. cnss_pr_err("msi_desc is NULL!\n");
  3957. ret = -EINVAL;
  3958. goto free_msi_vector;
  3959. }
  3960. pci_priv->msi_ep_base_data = msi_desc->msg.data;
  3961. cnss_pr_dbg("MSI base data is %d\n", pci_priv->msi_ep_base_data);
  3962. return 0;
  3963. free_msi_vector:
  3964. pci_free_irq_vectors(pci_priv->pci_dev);
  3965. reset_msi_config:
  3966. pci_priv->msi_config = NULL;
  3967. out:
  3968. return ret;
  3969. }
  3970. static void cnss_pci_disable_msi(struct cnss_pci_data *pci_priv)
  3971. {
  3972. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  3973. return;
  3974. pci_free_irq_vectors(pci_priv->pci_dev);
  3975. }
  3976. int cnss_get_user_msi_assignment(struct device *dev, char *user_name,
  3977. int *num_vectors, u32 *user_base_data,
  3978. u32 *base_vector)
  3979. {
  3980. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3981. struct cnss_msi_config *msi_config;
  3982. int idx;
  3983. if (!pci_priv)
  3984. return -ENODEV;
  3985. msi_config = pci_priv->msi_config;
  3986. if (!msi_config) {
  3987. cnss_pr_err("MSI is not supported.\n");
  3988. return -EINVAL;
  3989. }
  3990. for (idx = 0; idx < msi_config->total_users; idx++) {
  3991. if (strcmp(user_name, msi_config->users[idx].name) == 0) {
  3992. *num_vectors = msi_config->users[idx].num_vectors;
  3993. *user_base_data = msi_config->users[idx].base_vector
  3994. + pci_priv->msi_ep_base_data;
  3995. *base_vector = msi_config->users[idx].base_vector;
  3996. cnss_pr_dbg("Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
  3997. user_name, *num_vectors, *user_base_data,
  3998. *base_vector);
  3999. return 0;
  4000. }
  4001. }
  4002. cnss_pr_err("Failed to find MSI assignment for %s!\n", user_name);
  4003. return -EINVAL;
  4004. }
  4005. EXPORT_SYMBOL(cnss_get_user_msi_assignment);
  4006. int cnss_get_msi_irq(struct device *dev, unsigned int vector)
  4007. {
  4008. struct pci_dev *pci_dev = to_pci_dev(dev);
  4009. int irq_num;
  4010. irq_num = pci_irq_vector(pci_dev, vector);
  4011. cnss_pr_dbg("Get IRQ number %d for vector index %d\n", irq_num, vector);
  4012. return irq_num;
  4013. }
  4014. EXPORT_SYMBOL(cnss_get_msi_irq);
  4015. void cnss_get_msi_address(struct device *dev, u32 *msi_addr_low,
  4016. u32 *msi_addr_high)
  4017. {
  4018. struct pci_dev *pci_dev = to_pci_dev(dev);
  4019. u16 control;
  4020. pci_read_config_word(pci_dev, pci_dev->msi_cap + PCI_MSI_FLAGS,
  4021. &control);
  4022. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  4023. msi_addr_low);
  4024. /* Return MSI high address only when device supports 64-bit MSI */
  4025. if (control & PCI_MSI_FLAGS_64BIT)
  4026. pci_read_config_dword(pci_dev,
  4027. pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  4028. msi_addr_high);
  4029. else
  4030. *msi_addr_high = 0;
  4031. cnss_pr_dbg("Get MSI low addr = 0x%x, high addr = 0x%x\n",
  4032. *msi_addr_low, *msi_addr_high);
  4033. }
  4034. EXPORT_SYMBOL(cnss_get_msi_address);
  4035. u32 cnss_pci_get_wake_msi(struct cnss_pci_data *pci_priv)
  4036. {
  4037. int ret, num_vectors;
  4038. u32 user_base_data, base_vector;
  4039. if (!pci_priv)
  4040. return -ENODEV;
  4041. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4042. WAKE_MSI_NAME, &num_vectors,
  4043. &user_base_data, &base_vector);
  4044. if (ret) {
  4045. cnss_pr_err("WAKE MSI is not valid\n");
  4046. return 0;
  4047. }
  4048. return user_base_data;
  4049. }
  4050. static int cnss_pci_enable_bus(struct cnss_pci_data *pci_priv)
  4051. {
  4052. int ret = 0;
  4053. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4054. u16 device_id;
  4055. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &device_id);
  4056. if (device_id != pci_priv->pci_device_id->device) {
  4057. cnss_pr_err("PCI device ID mismatch, config ID: 0x%x, probe ID: 0x%x\n",
  4058. device_id, pci_priv->pci_device_id->device);
  4059. ret = -EIO;
  4060. goto out;
  4061. }
  4062. ret = pci_assign_resource(pci_dev, PCI_BAR_NUM);
  4063. if (ret) {
  4064. pr_err("Failed to assign PCI resource, err = %d\n", ret);
  4065. goto out;
  4066. }
  4067. ret = pci_enable_device(pci_dev);
  4068. if (ret) {
  4069. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  4070. goto out;
  4071. }
  4072. ret = pci_request_region(pci_dev, PCI_BAR_NUM, "cnss");
  4073. if (ret) {
  4074. cnss_pr_err("Failed to request PCI region, err = %d\n", ret);
  4075. goto disable_device;
  4076. }
  4077. switch (device_id) {
  4078. case QCA6174_DEVICE_ID:
  4079. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4080. break;
  4081. case QCA6390_DEVICE_ID:
  4082. case QCA6490_DEVICE_ID:
  4083. case KIWI_DEVICE_ID:
  4084. pci_priv->dma_bit_mask = PCI_DMA_MASK_36_BIT;
  4085. break;
  4086. default:
  4087. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4088. break;
  4089. }
  4090. cnss_pr_dbg("Set PCI DMA MASK (0x%llx)\n", pci_priv->dma_bit_mask);
  4091. ret = pci_set_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4092. if (ret) {
  4093. cnss_pr_err("Failed to set PCI DMA mask, err = %d\n", ret);
  4094. goto release_region;
  4095. }
  4096. ret = pci_set_consistent_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4097. if (ret) {
  4098. cnss_pr_err("Failed to set PCI consistent DMA mask, err = %d\n",
  4099. ret);
  4100. goto release_region;
  4101. }
  4102. pci_priv->bar = pci_iomap(pci_dev, PCI_BAR_NUM, 0);
  4103. if (!pci_priv->bar) {
  4104. cnss_pr_err("Failed to do PCI IO map!\n");
  4105. ret = -EIO;
  4106. goto release_region;
  4107. }
  4108. /* Save default config space without BME enabled */
  4109. pci_save_state(pci_dev);
  4110. pci_priv->default_state = pci_store_saved_state(pci_dev);
  4111. pci_set_master(pci_dev);
  4112. return 0;
  4113. release_region:
  4114. pci_release_region(pci_dev, PCI_BAR_NUM);
  4115. disable_device:
  4116. pci_disable_device(pci_dev);
  4117. out:
  4118. return ret;
  4119. }
  4120. static void cnss_pci_disable_bus(struct cnss_pci_data *pci_priv)
  4121. {
  4122. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4123. pci_clear_master(pci_dev);
  4124. pci_load_and_free_saved_state(pci_dev, &pci_priv->saved_state);
  4125. pci_load_and_free_saved_state(pci_dev, &pci_priv->default_state);
  4126. if (pci_priv->bar) {
  4127. pci_iounmap(pci_dev, pci_priv->bar);
  4128. pci_priv->bar = NULL;
  4129. }
  4130. pci_release_region(pci_dev, PCI_BAR_NUM);
  4131. if (pci_is_enabled(pci_dev))
  4132. pci_disable_device(pci_dev);
  4133. }
  4134. static void cnss_pci_dump_qdss_reg(struct cnss_pci_data *pci_priv)
  4135. {
  4136. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4137. int i, array_size = ARRAY_SIZE(qdss_csr) - 1;
  4138. gfp_t gfp = GFP_KERNEL;
  4139. u32 reg_offset;
  4140. if (in_interrupt() || irqs_disabled())
  4141. gfp = GFP_ATOMIC;
  4142. if (!plat_priv->qdss_reg) {
  4143. plat_priv->qdss_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  4144. sizeof(*plat_priv->qdss_reg)
  4145. * array_size, gfp);
  4146. if (!plat_priv->qdss_reg)
  4147. return;
  4148. }
  4149. cnss_pr_dbg("Start to dump qdss registers\n");
  4150. for (i = 0; qdss_csr[i].name; i++) {
  4151. reg_offset = QDSS_APB_DEC_CSR_BASE + qdss_csr[i].offset;
  4152. if (cnss_pci_reg_read(pci_priv, reg_offset,
  4153. &plat_priv->qdss_reg[i]))
  4154. return;
  4155. cnss_pr_dbg("%s[0x%x] = 0x%x\n", qdss_csr[i].name, reg_offset,
  4156. plat_priv->qdss_reg[i]);
  4157. }
  4158. }
  4159. static void cnss_pci_dump_ce_reg(struct cnss_pci_data *pci_priv,
  4160. enum cnss_ce_index ce)
  4161. {
  4162. int i;
  4163. u32 ce_base = ce * CE_REG_INTERVAL;
  4164. u32 reg_offset, src_ring_base, dst_ring_base, cmn_base, val;
  4165. switch (pci_priv->device_id) {
  4166. case QCA6390_DEVICE_ID:
  4167. src_ring_base = QCA6390_CE_SRC_RING_REG_BASE;
  4168. dst_ring_base = QCA6390_CE_DST_RING_REG_BASE;
  4169. cmn_base = QCA6390_CE_COMMON_REG_BASE;
  4170. break;
  4171. case QCA6490_DEVICE_ID:
  4172. src_ring_base = QCA6490_CE_SRC_RING_REG_BASE;
  4173. dst_ring_base = QCA6490_CE_DST_RING_REG_BASE;
  4174. cmn_base = QCA6490_CE_COMMON_REG_BASE;
  4175. break;
  4176. default:
  4177. return;
  4178. }
  4179. switch (ce) {
  4180. case CNSS_CE_09:
  4181. case CNSS_CE_10:
  4182. for (i = 0; ce_src[i].name; i++) {
  4183. reg_offset = src_ring_base + ce_base + ce_src[i].offset;
  4184. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4185. return;
  4186. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4187. ce, ce_src[i].name, reg_offset, val);
  4188. }
  4189. for (i = 0; ce_dst[i].name; i++) {
  4190. reg_offset = dst_ring_base + ce_base + ce_dst[i].offset;
  4191. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4192. return;
  4193. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4194. ce, ce_dst[i].name, reg_offset, val);
  4195. }
  4196. break;
  4197. case CNSS_CE_COMMON:
  4198. for (i = 0; ce_cmn[i].name; i++) {
  4199. reg_offset = cmn_base + ce_cmn[i].offset;
  4200. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4201. return;
  4202. cnss_pr_dbg("CE_COMMON_%s[0x%x] = 0x%x\n",
  4203. ce_cmn[i].name, reg_offset, val);
  4204. }
  4205. break;
  4206. default:
  4207. cnss_pr_err("Unsupported CE[%d] registers dump\n", ce);
  4208. }
  4209. }
  4210. static void cnss_pci_dump_debug_reg(struct cnss_pci_data *pci_priv)
  4211. {
  4212. if (cnss_pci_check_link_status(pci_priv))
  4213. return;
  4214. cnss_pr_dbg("Start to dump debug registers\n");
  4215. cnss_mhi_debug_reg_dump(pci_priv);
  4216. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4217. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_COMMON);
  4218. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_09);
  4219. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_10);
  4220. }
  4221. int cnss_pci_force_fw_assert_hdlr(struct cnss_pci_data *pci_priv)
  4222. {
  4223. int ret;
  4224. struct cnss_plat_data *plat_priv;
  4225. if (!pci_priv)
  4226. return -ENODEV;
  4227. plat_priv = pci_priv->plat_priv;
  4228. if (!plat_priv)
  4229. return -ENODEV;
  4230. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4231. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state))
  4232. return -EINVAL;
  4233. cnss_auto_resume(&pci_priv->pci_dev->dev);
  4234. if (!cnss_pci_check_link_status(pci_priv))
  4235. cnss_mhi_debug_reg_dump(pci_priv);
  4236. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4237. cnss_pci_dump_misc_reg(pci_priv);
  4238. cnss_pci_dump_shadow_reg(pci_priv);
  4239. /* If link is still down here, directly trigger link down recovery */
  4240. ret = cnss_pci_check_link_status(pci_priv);
  4241. if (ret) {
  4242. cnss_pci_link_down(&pci_priv->pci_dev->dev);
  4243. return 0;
  4244. }
  4245. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_TRIGGER_RDDM);
  4246. if (ret) {
  4247. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4248. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state)) {
  4249. cnss_pr_dbg("MHI is not powered on, ignore RDDM failure\n");
  4250. return 0;
  4251. }
  4252. cnss_fatal_err("Failed to trigger RDDM, err = %d\n", ret);
  4253. cnss_pci_dump_debug_reg(pci_priv);
  4254. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4255. CNSS_REASON_DEFAULT);
  4256. return ret;
  4257. }
  4258. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  4259. mod_timer(&pci_priv->dev_rddm_timer,
  4260. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4261. }
  4262. return 0;
  4263. }
  4264. static void cnss_pci_add_dump_seg(struct cnss_pci_data *pci_priv,
  4265. struct cnss_dump_seg *dump_seg,
  4266. enum cnss_fw_dump_type type, int seg_no,
  4267. void *va, dma_addr_t dma, size_t size)
  4268. {
  4269. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4270. struct device *dev = &pci_priv->pci_dev->dev;
  4271. phys_addr_t pa;
  4272. dump_seg->address = dma;
  4273. dump_seg->v_address = va;
  4274. dump_seg->size = size;
  4275. dump_seg->type = type;
  4276. cnss_pr_dbg("Seg: %x, va: %pK, dma: %pa, size: 0x%zx\n",
  4277. seg_no, va, &dma, size);
  4278. if (cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS))
  4279. return;
  4280. cnss_minidump_add_region(plat_priv, type, seg_no, va, pa, size);
  4281. }
  4282. static void cnss_pci_remove_dump_seg(struct cnss_pci_data *pci_priv,
  4283. struct cnss_dump_seg *dump_seg,
  4284. enum cnss_fw_dump_type type, int seg_no,
  4285. void *va, dma_addr_t dma, size_t size)
  4286. {
  4287. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4288. struct device *dev = &pci_priv->pci_dev->dev;
  4289. phys_addr_t pa;
  4290. cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS);
  4291. cnss_minidump_remove_region(plat_priv, type, seg_no, va, pa, size);
  4292. }
  4293. int cnss_pci_call_driver_uevent(struct cnss_pci_data *pci_priv,
  4294. enum cnss_driver_status status, void *data)
  4295. {
  4296. struct cnss_uevent_data uevent_data;
  4297. struct cnss_wlan_driver *driver_ops;
  4298. driver_ops = pci_priv->driver_ops;
  4299. if (!driver_ops || !driver_ops->update_event) {
  4300. cnss_pr_dbg("Hang event driver ops is NULL\n");
  4301. return -EINVAL;
  4302. }
  4303. cnss_pr_dbg("Calling driver uevent: %d\n", status);
  4304. uevent_data.status = status;
  4305. uevent_data.data = data;
  4306. return driver_ops->update_event(pci_priv->pci_dev, &uevent_data);
  4307. }
  4308. static void cnss_pci_send_hang_event(struct cnss_pci_data *pci_priv)
  4309. {
  4310. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4311. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4312. struct cnss_hang_event hang_event;
  4313. void *hang_data_va = NULL;
  4314. u64 offset = 0;
  4315. int i = 0;
  4316. if (!fw_mem || !plat_priv->fw_mem_seg_len)
  4317. return;
  4318. memset(&hang_event, 0, sizeof(hang_event));
  4319. switch (pci_priv->device_id) {
  4320. case QCA6390_DEVICE_ID:
  4321. offset = HST_HANG_DATA_OFFSET;
  4322. break;
  4323. case QCA6490_DEVICE_ID:
  4324. offset = HSP_HANG_DATA_OFFSET;
  4325. break;
  4326. default:
  4327. cnss_pr_err("Skip Hang Event Data as unsupported Device ID received: %d\n",
  4328. pci_priv->device_id);
  4329. return;
  4330. }
  4331. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4332. if (fw_mem[i].type == QMI_WLFW_MEM_TYPE_DDR_V01 &&
  4333. fw_mem[i].va) {
  4334. hang_data_va = fw_mem[i].va + offset;
  4335. hang_event.hang_event_data = kmemdup(hang_data_va,
  4336. HANG_DATA_LENGTH,
  4337. GFP_ATOMIC);
  4338. if (!hang_event.hang_event_data) {
  4339. cnss_pr_dbg("Hang data memory alloc failed\n");
  4340. return;
  4341. }
  4342. hang_event.hang_event_data_len = HANG_DATA_LENGTH;
  4343. break;
  4344. }
  4345. }
  4346. cnss_pci_call_driver_uevent(pci_priv, CNSS_HANG_EVENT, &hang_event);
  4347. kfree(hang_event.hang_event_data);
  4348. hang_event.hang_event_data = NULL;
  4349. }
  4350. void cnss_pci_collect_dump_info(struct cnss_pci_data *pci_priv, bool in_panic)
  4351. {
  4352. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4353. struct cnss_dump_data *dump_data =
  4354. &plat_priv->ramdump_info_v2.dump_data;
  4355. struct cnss_dump_seg *dump_seg =
  4356. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4357. struct image_info *fw_image, *rddm_image;
  4358. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4359. int ret, i, j;
  4360. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  4361. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  4362. cnss_pci_send_hang_event(pci_priv);
  4363. if (test_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state)) {
  4364. cnss_pr_dbg("RAM dump is already collected, skip\n");
  4365. return;
  4366. }
  4367. if (!cnss_is_device_powered_on(plat_priv)) {
  4368. cnss_pr_dbg("Device is already powered off, skip\n");
  4369. return;
  4370. }
  4371. if (!in_panic) {
  4372. mutex_lock(&pci_priv->bus_lock);
  4373. ret = cnss_pci_check_link_status(pci_priv);
  4374. if (ret) {
  4375. if (ret != -EACCES) {
  4376. mutex_unlock(&pci_priv->bus_lock);
  4377. return;
  4378. }
  4379. if (cnss_pci_resume_bus(pci_priv)) {
  4380. mutex_unlock(&pci_priv->bus_lock);
  4381. return;
  4382. }
  4383. }
  4384. mutex_unlock(&pci_priv->bus_lock);
  4385. } else {
  4386. if (cnss_pci_check_link_status(pci_priv))
  4387. return;
  4388. }
  4389. cnss_mhi_debug_reg_dump(pci_priv);
  4390. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4391. cnss_pci_dump_misc_reg(pci_priv);
  4392. cnss_pci_dump_shadow_reg(pci_priv);
  4393. cnss_pci_dump_qdss_reg(pci_priv);
  4394. ret = mhi_download_rddm_image(pci_priv->mhi_ctrl, in_panic);
  4395. if (ret) {
  4396. cnss_fatal_err("Failed to download RDDM image, err = %d\n",
  4397. ret);
  4398. cnss_pci_dump_debug_reg(pci_priv);
  4399. return;
  4400. }
  4401. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4402. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4403. dump_data->nentries = 0;
  4404. cnss_mhi_dump_sfr(pci_priv);
  4405. if (!dump_seg) {
  4406. cnss_pr_warn("FW image dump collection not setup");
  4407. goto skip_dump;
  4408. }
  4409. cnss_pr_dbg("Collect FW image dump segment, nentries %d\n",
  4410. fw_image->entries);
  4411. for (i = 0; i < fw_image->entries; i++) {
  4412. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4413. fw_image->mhi_buf[i].buf,
  4414. fw_image->mhi_buf[i].dma_addr,
  4415. fw_image->mhi_buf[i].len);
  4416. dump_seg++;
  4417. }
  4418. dump_data->nentries += fw_image->entries;
  4419. cnss_pr_dbg("Collect RDDM image dump segment, nentries %d\n",
  4420. rddm_image->entries);
  4421. for (i = 0; i < rddm_image->entries; i++) {
  4422. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4423. rddm_image->mhi_buf[i].buf,
  4424. rddm_image->mhi_buf[i].dma_addr,
  4425. rddm_image->mhi_buf[i].len);
  4426. dump_seg++;
  4427. }
  4428. dump_data->nentries += rddm_image->entries;
  4429. cnss_pr_dbg("Collect remote heap dump segment\n");
  4430. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4431. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4432. cnss_pci_add_dump_seg(pci_priv, dump_seg,
  4433. CNSS_FW_REMOTE_HEAP, j,
  4434. fw_mem[i].va, fw_mem[i].pa,
  4435. fw_mem[i].size);
  4436. dump_seg++;
  4437. dump_data->nentries++;
  4438. j++;
  4439. }
  4440. }
  4441. if (dump_data->nentries > 0)
  4442. plat_priv->ramdump_info_v2.dump_data_valid = true;
  4443. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RDDM_DONE);
  4444. skip_dump:
  4445. complete(&plat_priv->rddm_complete);
  4446. }
  4447. void cnss_pci_clear_dump_info(struct cnss_pci_data *pci_priv)
  4448. {
  4449. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4450. struct cnss_dump_seg *dump_seg =
  4451. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4452. struct image_info *fw_image, *rddm_image;
  4453. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4454. int i, j;
  4455. if (!dump_seg)
  4456. return;
  4457. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4458. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4459. for (i = 0; i < fw_image->entries; i++) {
  4460. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4461. fw_image->mhi_buf[i].buf,
  4462. fw_image->mhi_buf[i].dma_addr,
  4463. fw_image->mhi_buf[i].len);
  4464. dump_seg++;
  4465. }
  4466. for (i = 0; i < rddm_image->entries; i++) {
  4467. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4468. rddm_image->mhi_buf[i].buf,
  4469. rddm_image->mhi_buf[i].dma_addr,
  4470. rddm_image->mhi_buf[i].len);
  4471. dump_seg++;
  4472. }
  4473. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4474. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4475. cnss_pci_remove_dump_seg(pci_priv, dump_seg,
  4476. CNSS_FW_REMOTE_HEAP, j,
  4477. fw_mem[i].va, fw_mem[i].pa,
  4478. fw_mem[i].size);
  4479. dump_seg++;
  4480. j++;
  4481. }
  4482. }
  4483. plat_priv->ramdump_info_v2.dump_data.nentries = 0;
  4484. plat_priv->ramdump_info_v2.dump_data_valid = false;
  4485. }
  4486. void cnss_pci_device_crashed(struct cnss_pci_data *pci_priv)
  4487. {
  4488. if (!pci_priv)
  4489. return;
  4490. cnss_device_crashed(&pci_priv->pci_dev->dev);
  4491. }
  4492. static int cnss_mhi_pm_runtime_get(struct mhi_controller *mhi_ctrl)
  4493. {
  4494. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4495. return cnss_pci_pm_runtime_get(pci_priv, RTPM_ID_MHI);
  4496. }
  4497. static void cnss_mhi_pm_runtime_put_noidle(struct mhi_controller *mhi_ctrl)
  4498. {
  4499. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4500. cnss_pci_pm_runtime_put_noidle(pci_priv, RTPM_ID_MHI);
  4501. }
  4502. void cnss_pci_add_fw_prefix_name(struct cnss_pci_data *pci_priv,
  4503. char *prefix_name, char *name)
  4504. {
  4505. struct cnss_plat_data *plat_priv;
  4506. if (!pci_priv)
  4507. return;
  4508. plat_priv = pci_priv->plat_priv;
  4509. if (!plat_priv->use_fw_path_with_prefix) {
  4510. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4511. return;
  4512. }
  4513. switch (pci_priv->device_id) {
  4514. case QCA6390_DEVICE_ID:
  4515. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4516. QCA6390_PATH_PREFIX "%s", name);
  4517. break;
  4518. case QCA6490_DEVICE_ID:
  4519. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4520. QCA6490_PATH_PREFIX "%s", name);
  4521. break;
  4522. case KIWI_DEVICE_ID:
  4523. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4524. KIWI_PATH_PREFIX "%s", name);
  4525. break;
  4526. default:
  4527. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4528. break;
  4529. }
  4530. cnss_pr_dbg("FW name added with prefix: %s\n", prefix_name);
  4531. }
  4532. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv)
  4533. {
  4534. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4535. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  4536. plat_priv->device_version.family_number = mhi_ctrl->family_number;
  4537. plat_priv->device_version.device_number = mhi_ctrl->device_number;
  4538. plat_priv->device_version.major_version = mhi_ctrl->major_version;
  4539. plat_priv->device_version.minor_version = mhi_ctrl->minor_version;
  4540. cnss_pr_dbg("Get device version info, family number: 0x%x, device number: 0x%x, major version: 0x%x, minor version: 0x%x\n",
  4541. plat_priv->device_version.family_number,
  4542. plat_priv->device_version.device_number,
  4543. plat_priv->device_version.major_version,
  4544. plat_priv->device_version.minor_version);
  4545. /* Only keep lower 4 bits as real device major version */
  4546. plat_priv->device_version.major_version &= DEVICE_MAJOR_VERSION_MASK;
  4547. switch (pci_priv->device_id) {
  4548. case QCA6390_DEVICE_ID:
  4549. if (plat_priv->device_version.major_version < FW_V2_NUMBER) {
  4550. cnss_pr_dbg("Device ID:version (0x%lx:%d) is not supported\n",
  4551. pci_priv->device_id,
  4552. plat_priv->device_version.major_version);
  4553. return -EINVAL;
  4554. }
  4555. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4556. FW_V2_FILE_NAME);
  4557. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4558. FW_V2_FILE_NAME);
  4559. break;
  4560. case QCA6490_DEVICE_ID:
  4561. case KIWI_DEVICE_ID:
  4562. switch (plat_priv->device_version.major_version) {
  4563. case FW_V2_NUMBER:
  4564. cnss_pci_add_fw_prefix_name(pci_priv,
  4565. plat_priv->firmware_name,
  4566. FW_V2_FILE_NAME);
  4567. snprintf(plat_priv->fw_fallback_name,
  4568. MAX_FIRMWARE_NAME_LEN,
  4569. FW_V2_FILE_NAME);
  4570. break;
  4571. default:
  4572. cnss_pci_add_fw_prefix_name(pci_priv,
  4573. plat_priv->firmware_name,
  4574. DEFAULT_FW_FILE_NAME);
  4575. snprintf(plat_priv->fw_fallback_name,
  4576. MAX_FIRMWARE_NAME_LEN,
  4577. DEFAULT_FW_FILE_NAME);
  4578. break;
  4579. }
  4580. break;
  4581. default:
  4582. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4583. DEFAULT_FW_FILE_NAME);
  4584. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4585. DEFAULT_FW_FILE_NAME);
  4586. break;
  4587. }
  4588. cnss_pr_dbg("FW name is %s, FW fallback name is %s\n",
  4589. plat_priv->firmware_name, plat_priv->fw_fallback_name);
  4590. return 0;
  4591. }
  4592. static char *cnss_mhi_notify_status_to_str(enum mhi_callback status)
  4593. {
  4594. switch (status) {
  4595. case MHI_CB_IDLE:
  4596. return "IDLE";
  4597. case MHI_CB_EE_RDDM:
  4598. return "RDDM";
  4599. case MHI_CB_SYS_ERROR:
  4600. return "SYS_ERROR";
  4601. case MHI_CB_FATAL_ERROR:
  4602. return "FATAL_ERROR";
  4603. case MHI_CB_EE_MISSION_MODE:
  4604. return "MISSION_MODE";
  4605. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4606. case MHI_CB_FALLBACK_IMG:
  4607. return "FW_FALLBACK";
  4608. #endif
  4609. default:
  4610. return "UNKNOWN";
  4611. }
  4612. };
  4613. static void cnss_dev_rddm_timeout_hdlr(struct timer_list *t)
  4614. {
  4615. struct cnss_pci_data *pci_priv =
  4616. from_timer(pci_priv, t, dev_rddm_timer);
  4617. if (!pci_priv)
  4618. return;
  4619. cnss_fatal_err("Timeout waiting for RDDM notification\n");
  4620. if (mhi_get_exec_env(pci_priv->mhi_ctrl) == MHI_EE_PBL)
  4621. cnss_pr_err("Unable to collect ramdumps due to abrupt reset\n");
  4622. cnss_mhi_debug_reg_dump(pci_priv);
  4623. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4624. cnss_schedule_recovery(&pci_priv->pci_dev->dev, CNSS_REASON_TIMEOUT);
  4625. }
  4626. static void cnss_boot_debug_timeout_hdlr(struct timer_list *t)
  4627. {
  4628. struct cnss_pci_data *pci_priv =
  4629. from_timer(pci_priv, t, boot_debug_timer);
  4630. if (!pci_priv)
  4631. return;
  4632. if (cnss_pci_check_link_status(pci_priv))
  4633. return;
  4634. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  4635. return;
  4636. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  4637. return;
  4638. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE))
  4639. return;
  4640. cnss_pr_dbg("Dump MHI/PBL/SBL debug data every %ds during MHI power on\n",
  4641. BOOT_DEBUG_TIMEOUT_MS / 1000);
  4642. cnss_mhi_debug_reg_dump(pci_priv);
  4643. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4644. cnss_pci_dump_bl_sram_mem(pci_priv);
  4645. mod_timer(&pci_priv->boot_debug_timer,
  4646. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  4647. }
  4648. static void cnss_mhi_notify_status(struct mhi_controller *mhi_ctrl,
  4649. enum mhi_callback reason)
  4650. {
  4651. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4652. struct cnss_plat_data *plat_priv;
  4653. enum cnss_recovery_reason cnss_reason;
  4654. if (!pci_priv) {
  4655. cnss_pr_err("pci_priv is NULL");
  4656. return;
  4657. }
  4658. plat_priv = pci_priv->plat_priv;
  4659. if (reason != MHI_CB_IDLE)
  4660. cnss_pr_dbg("MHI status cb is called with reason %s(%d)\n",
  4661. cnss_mhi_notify_status_to_str(reason), reason);
  4662. switch (reason) {
  4663. case MHI_CB_IDLE:
  4664. case MHI_CB_EE_MISSION_MODE:
  4665. return;
  4666. case MHI_CB_FATAL_ERROR:
  4667. cnss_ignore_qmi_failure(true);
  4668. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4669. del_timer(&plat_priv->fw_boot_timer);
  4670. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4671. cnss_reason = CNSS_REASON_DEFAULT;
  4672. break;
  4673. case MHI_CB_SYS_ERROR:
  4674. cnss_ignore_qmi_failure(true);
  4675. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4676. del_timer(&plat_priv->fw_boot_timer);
  4677. mod_timer(&pci_priv->dev_rddm_timer,
  4678. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4679. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4680. return;
  4681. case MHI_CB_EE_RDDM:
  4682. cnss_ignore_qmi_failure(true);
  4683. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4684. del_timer(&plat_priv->fw_boot_timer);
  4685. del_timer(&pci_priv->dev_rddm_timer);
  4686. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4687. cnss_reason = CNSS_REASON_RDDM;
  4688. break;
  4689. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4690. case MHI_CB_FALLBACK_IMG:
  4691. plat_priv->use_fw_path_with_prefix = false;
  4692. cnss_pci_update_fw_name(pci_priv);
  4693. return;
  4694. #endif
  4695. default:
  4696. cnss_pr_err("Unsupported MHI status cb reason: %d\n", reason);
  4697. return;
  4698. }
  4699. cnss_schedule_recovery(&pci_priv->pci_dev->dev, cnss_reason);
  4700. }
  4701. static int cnss_pci_get_mhi_msi(struct cnss_pci_data *pci_priv)
  4702. {
  4703. int ret, num_vectors, i;
  4704. u32 user_base_data, base_vector;
  4705. int *irq;
  4706. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4707. MHI_MSI_NAME, &num_vectors,
  4708. &user_base_data, &base_vector);
  4709. if (ret)
  4710. return ret;
  4711. cnss_pr_dbg("Number of assigned MSI for MHI is %d, base vector is %d\n",
  4712. num_vectors, base_vector);
  4713. irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
  4714. if (!irq)
  4715. return -ENOMEM;
  4716. for (i = 0; i < num_vectors; i++)
  4717. irq[i] = cnss_get_msi_irq(&pci_priv->pci_dev->dev,
  4718. base_vector + i);
  4719. pci_priv->mhi_ctrl->irq = irq;
  4720. pci_priv->mhi_ctrl->nr_irqs = num_vectors;
  4721. return 0;
  4722. }
  4723. static int cnss_mhi_bw_scale(struct mhi_controller *mhi_ctrl,
  4724. struct mhi_link_info *link_info)
  4725. {
  4726. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4727. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4728. int ret = 0;
  4729. cnss_pr_dbg("Setting link speed:0x%x, width:0x%x\n",
  4730. link_info->target_link_speed,
  4731. link_info->target_link_width);
  4732. /* It has to set target link speed here before setting link bandwidth
  4733. * when device requests link speed change. This can avoid setting link
  4734. * bandwidth getting rejected if requested link speed is higher than
  4735. * current one.
  4736. */
  4737. ret = cnss_pci_set_max_link_speed(pci_priv, plat_priv->rc_num,
  4738. link_info->target_link_speed);
  4739. if (ret)
  4740. cnss_pr_err("Failed to set target link speed to 0x%x, err = %d\n",
  4741. link_info->target_link_speed, ret);
  4742. ret = cnss_pci_set_link_bandwidth(pci_priv,
  4743. link_info->target_link_speed,
  4744. link_info->target_link_width);
  4745. if (ret) {
  4746. cnss_pr_err("Failed to set link bandwidth, err = %d\n", ret);
  4747. return ret;
  4748. }
  4749. pci_priv->def_link_speed = link_info->target_link_speed;
  4750. pci_priv->def_link_width = link_info->target_link_width;
  4751. return 0;
  4752. }
  4753. static int cnss_mhi_read_reg(struct mhi_controller *mhi_ctrl,
  4754. void __iomem *addr, u32 *out)
  4755. {
  4756. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4757. u32 tmp = readl_relaxed(addr);
  4758. /* Unexpected value, query the link status */
  4759. if (PCI_INVALID_READ(tmp) &&
  4760. cnss_pci_check_link_status(pci_priv))
  4761. return -EIO;
  4762. *out = tmp;
  4763. return 0;
  4764. }
  4765. static void cnss_mhi_write_reg(struct mhi_controller *mhi_ctrl,
  4766. void __iomem *addr, u32 val)
  4767. {
  4768. writel_relaxed(val, addr);
  4769. }
  4770. static int cnss_pci_register_mhi(struct cnss_pci_data *pci_priv)
  4771. {
  4772. int ret = 0;
  4773. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4774. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4775. struct mhi_controller *mhi_ctrl;
  4776. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4777. return 0;
  4778. mhi_ctrl = mhi_alloc_controller();
  4779. if (!mhi_ctrl) {
  4780. cnss_pr_err("Invalid MHI controller context\n");
  4781. return -EINVAL;
  4782. }
  4783. pci_priv->mhi_ctrl = mhi_ctrl;
  4784. mhi_ctrl->cntrl_dev = &pci_dev->dev;
  4785. mhi_ctrl->fw_image = plat_priv->firmware_name;
  4786. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4787. mhi_ctrl->fallback_fw_image = plat_priv->fw_fallback_name;
  4788. #endif
  4789. mhi_ctrl->regs = pci_priv->bar;
  4790. mhi_ctrl->reg_len = pci_resource_len(pci_priv->pci_dev, PCI_BAR_NUM);
  4791. cnss_pr_dbg("BAR starts at %pa, length is %x\n",
  4792. &pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM),
  4793. mhi_ctrl->reg_len);
  4794. ret = cnss_pci_get_mhi_msi(pci_priv);
  4795. if (ret) {
  4796. cnss_pr_err("Failed to get MSI for MHI, err = %d\n", ret);
  4797. goto free_mhi_ctrl;
  4798. }
  4799. if (pci_priv->smmu_s1_enable) {
  4800. mhi_ctrl->iova_start = pci_priv->smmu_iova_start;
  4801. mhi_ctrl->iova_stop = pci_priv->smmu_iova_start +
  4802. pci_priv->smmu_iova_len;
  4803. } else {
  4804. mhi_ctrl->iova_start = 0;
  4805. mhi_ctrl->iova_stop = pci_priv->dma_bit_mask;
  4806. }
  4807. mhi_ctrl->status_cb = cnss_mhi_notify_status;
  4808. mhi_ctrl->runtime_get = cnss_mhi_pm_runtime_get;
  4809. mhi_ctrl->runtime_put = cnss_mhi_pm_runtime_put_noidle;
  4810. mhi_ctrl->read_reg = cnss_mhi_read_reg;
  4811. mhi_ctrl->write_reg = cnss_mhi_write_reg;
  4812. mhi_ctrl->rddm_size = pci_priv->plat_priv->ramdump_info_v2.ramdump_size;
  4813. if (!mhi_ctrl->rddm_size)
  4814. mhi_ctrl->rddm_size = RAMDUMP_SIZE_DEFAULT;
  4815. mhi_ctrl->sbl_size = SZ_512K;
  4816. mhi_ctrl->seg_len = SZ_512K;
  4817. mhi_ctrl->fbc_download = true;
  4818. ret = mhi_register_controller(mhi_ctrl, &cnss_mhi_config);
  4819. if (ret) {
  4820. cnss_pr_err("Failed to register to MHI bus, err = %d\n", ret);
  4821. goto free_mhi_irq;
  4822. }
  4823. /* BW scale CB needs to be set after registering MHI per requirement */
  4824. cnss_mhi_controller_set_bw_scale_cb(pci_priv, cnss_mhi_bw_scale);
  4825. ret = cnss_pci_update_fw_name(pci_priv);
  4826. if (ret)
  4827. goto unreg_mhi;
  4828. return 0;
  4829. unreg_mhi:
  4830. mhi_unregister_controller(mhi_ctrl);
  4831. free_mhi_irq:
  4832. kfree(mhi_ctrl->irq);
  4833. free_mhi_ctrl:
  4834. mhi_free_controller(mhi_ctrl);
  4835. return ret;
  4836. }
  4837. static void cnss_pci_unregister_mhi(struct cnss_pci_data *pci_priv)
  4838. {
  4839. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  4840. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4841. return;
  4842. mhi_unregister_controller(mhi_ctrl);
  4843. kfree(mhi_ctrl->irq);
  4844. mhi_free_controller(mhi_ctrl);
  4845. }
  4846. static void cnss_pci_config_regs(struct cnss_pci_data *pci_priv)
  4847. {
  4848. switch (pci_priv->device_id) {
  4849. case QCA6390_DEVICE_ID:
  4850. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6390;
  4851. pci_priv->wcss_reg = wcss_reg_access_seq;
  4852. pci_priv->pcie_reg = pcie_reg_access_seq;
  4853. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  4854. pci_priv->syspm_reg = syspm_reg_access_seq;
  4855. /* Configure WDOG register with specific value so that we can
  4856. * know if HW is in the process of WDOG reset recovery or not
  4857. * when reading the registers.
  4858. */
  4859. cnss_pci_reg_write
  4860. (pci_priv,
  4861. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG,
  4862. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG_VAL);
  4863. break;
  4864. case QCA6490_DEVICE_ID:
  4865. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6490;
  4866. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  4867. break;
  4868. default:
  4869. return;
  4870. }
  4871. }
  4872. #if !IS_ENABLED(CONFIG_ARCH_QCOM)
  4873. static irqreturn_t cnss_pci_wake_handler(int irq, void *data)
  4874. {
  4875. struct cnss_pci_data *pci_priv = data;
  4876. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4877. enum rpm_status status;
  4878. struct device *dev;
  4879. pci_priv->wake_counter++;
  4880. cnss_pr_dbg("WLAN PCI wake IRQ (%u) is asserted #%u\n",
  4881. pci_priv->wake_irq, pci_priv->wake_counter);
  4882. /* Make sure abort current suspend */
  4883. cnss_pm_stay_awake(plat_priv);
  4884. cnss_pm_relax(plat_priv);
  4885. /* Above two pm* API calls will abort system suspend only when
  4886. * plat_dev->dev->ws is initiated by device_init_wakeup() API, and
  4887. * calling pm_system_wakeup() is just to guarantee system suspend
  4888. * can be aborted if it is not initiated in any case.
  4889. */
  4890. pm_system_wakeup();
  4891. dev = &pci_priv->pci_dev->dev;
  4892. status = dev->power.runtime_status;
  4893. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  4894. cnss_pci_get_auto_suspended(pci_priv)) ||
  4895. (status == RPM_SUSPENDING || status == RPM_SUSPENDED)) {
  4896. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  4897. cnss_pci_pm_request_resume(pci_priv);
  4898. }
  4899. return IRQ_HANDLED;
  4900. }
  4901. /**
  4902. * cnss_pci_wake_gpio_init() - Setup PCI wake GPIO for WLAN
  4903. * @pci_priv: driver PCI bus context pointer
  4904. *
  4905. * This function initializes WLAN PCI wake GPIO and corresponding
  4906. * interrupt. It should be used in non-MSM platforms whose PCIe
  4907. * root complex driver doesn't handle the GPIO.
  4908. *
  4909. * Return: 0 for success or skip, negative value for error
  4910. */
  4911. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  4912. {
  4913. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4914. struct device *dev = &plat_priv->plat_dev->dev;
  4915. int ret = 0;
  4916. pci_priv->wake_gpio = of_get_named_gpio(dev->of_node,
  4917. "wlan-pci-wake-gpio", 0);
  4918. if (pci_priv->wake_gpio < 0)
  4919. goto out;
  4920. cnss_pr_dbg("Get PCI wake GPIO (%d) from device node\n",
  4921. pci_priv->wake_gpio);
  4922. ret = gpio_request(pci_priv->wake_gpio, "wlan_pci_wake_gpio");
  4923. if (ret) {
  4924. cnss_pr_err("Failed to request PCI wake GPIO, err = %d\n",
  4925. ret);
  4926. goto out;
  4927. }
  4928. gpio_direction_input(pci_priv->wake_gpio);
  4929. pci_priv->wake_irq = gpio_to_irq(pci_priv->wake_gpio);
  4930. ret = request_irq(pci_priv->wake_irq, cnss_pci_wake_handler,
  4931. IRQF_TRIGGER_FALLING, "wlan_pci_wake_irq", pci_priv);
  4932. if (ret) {
  4933. cnss_pr_err("Failed to request PCI wake IRQ, err = %d\n", ret);
  4934. goto free_gpio;
  4935. }
  4936. ret = enable_irq_wake(pci_priv->wake_irq);
  4937. if (ret) {
  4938. cnss_pr_err("Failed to enable PCI wake IRQ, err = %d\n", ret);
  4939. goto free_irq;
  4940. }
  4941. return 0;
  4942. free_irq:
  4943. free_irq(pci_priv->wake_irq, pci_priv);
  4944. free_gpio:
  4945. gpio_free(pci_priv->wake_gpio);
  4946. out:
  4947. return ret;
  4948. }
  4949. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  4950. {
  4951. if (pci_priv->wake_gpio < 0)
  4952. return;
  4953. disable_irq_wake(pci_priv->wake_irq);
  4954. free_irq(pci_priv->wake_irq, pci_priv);
  4955. gpio_free(pci_priv->wake_gpio);
  4956. }
  4957. #else
  4958. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  4959. {
  4960. return 0;
  4961. }
  4962. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  4963. {
  4964. }
  4965. #endif
  4966. #if IS_ENABLED(CONFIG_ARCH_QCOM)
  4967. /**
  4968. * cnss_pci_of_reserved_mem_device_init() - Assign reserved memory region
  4969. * to given PCI device
  4970. * @pci_priv: driver PCI bus context pointer
  4971. *
  4972. * This function shall call corresponding of_reserved_mem_device* API to
  4973. * assign reserved memory region to PCI device based on where the memory is
  4974. * defined and attached to (platform device of_node or PCI device of_node)
  4975. * in device tree.
  4976. *
  4977. * Return: 0 for success, negative value for error
  4978. */
  4979. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  4980. {
  4981. struct device *dev_pci = &pci_priv->pci_dev->dev;
  4982. int ret;
  4983. /* Use of_reserved_mem_device_init_by_idx() if reserved memory is
  4984. * attached to platform device of_node.
  4985. */
  4986. ret = of_reserved_mem_device_init(dev_pci);
  4987. if (ret)
  4988. cnss_pr_err("Failed to init reserved mem device, err = %d\n",
  4989. ret);
  4990. if (dev_pci->cma_area)
  4991. cnss_pr_dbg("CMA area is %s\n",
  4992. cma_get_name(dev_pci->cma_area));
  4993. return ret;
  4994. }
  4995. #else
  4996. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  4997. {
  4998. return 0;
  4999. }
  5000. #endif
  5001. /* Setting to use this cnss_pm_domain ops will let PM framework override the
  5002. * ops from dev->bus->pm which is pci_dev_pm_ops from pci-driver.c. This ops
  5003. * has to take care everything device driver needed which is currently done
  5004. * from pci_dev_pm_ops.
  5005. */
  5006. static struct dev_pm_domain cnss_pm_domain = {
  5007. .ops = {
  5008. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5009. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5010. cnss_pci_resume_noirq)
  5011. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend,
  5012. cnss_pci_runtime_resume,
  5013. cnss_pci_runtime_idle)
  5014. }
  5015. };
  5016. static int cnss_pci_probe(struct pci_dev *pci_dev,
  5017. const struct pci_device_id *id)
  5018. {
  5019. int ret = 0;
  5020. struct cnss_pci_data *pci_priv;
  5021. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  5022. struct device *dev = &pci_dev->dev;
  5023. cnss_pr_dbg("PCI is probing, vendor ID: 0x%x, device ID: 0x%x\n",
  5024. id->vendor, pci_dev->device);
  5025. pci_priv = devm_kzalloc(dev, sizeof(*pci_priv), GFP_KERNEL);
  5026. if (!pci_priv) {
  5027. ret = -ENOMEM;
  5028. goto out;
  5029. }
  5030. pci_priv->pci_link_state = PCI_LINK_UP;
  5031. pci_priv->plat_priv = plat_priv;
  5032. pci_priv->pci_dev = pci_dev;
  5033. pci_priv->pci_device_id = id;
  5034. pci_priv->device_id = pci_dev->device;
  5035. cnss_set_pci_priv(pci_dev, pci_priv);
  5036. plat_priv->device_id = pci_dev->device;
  5037. plat_priv->bus_priv = pci_priv;
  5038. mutex_init(&pci_priv->bus_lock);
  5039. if (plat_priv->use_pm_domain)
  5040. dev->pm_domain = &cnss_pm_domain;
  5041. cnss_pci_of_reserved_mem_device_init(pci_priv);
  5042. ret = cnss_register_subsys(plat_priv);
  5043. if (ret)
  5044. goto reset_ctx;
  5045. ret = cnss_register_ramdump(plat_priv);
  5046. if (ret)
  5047. goto unregister_subsys;
  5048. ret = cnss_pci_init_smmu(pci_priv);
  5049. if (ret)
  5050. goto unregister_ramdump;
  5051. ret = cnss_reg_pci_event(pci_priv);
  5052. if (ret) {
  5053. cnss_pr_err("Failed to register PCI event, err = %d\n", ret);
  5054. goto deinit_smmu;
  5055. }
  5056. ret = cnss_pci_enable_bus(pci_priv);
  5057. if (ret)
  5058. goto dereg_pci_event;
  5059. ret = cnss_pci_enable_msi(pci_priv);
  5060. if (ret)
  5061. goto disable_bus;
  5062. ret = cnss_pci_register_mhi(pci_priv);
  5063. if (ret)
  5064. goto disable_msi;
  5065. switch (pci_dev->device) {
  5066. case QCA6174_DEVICE_ID:
  5067. pci_read_config_word(pci_dev, QCA6174_REV_ID_OFFSET,
  5068. &pci_priv->revision_id);
  5069. break;
  5070. case QCA6290_DEVICE_ID:
  5071. case QCA6390_DEVICE_ID:
  5072. case QCA6490_DEVICE_ID:
  5073. case KIWI_DEVICE_ID:
  5074. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  5075. timer_setup(&pci_priv->dev_rddm_timer,
  5076. cnss_dev_rddm_timeout_hdlr, 0);
  5077. timer_setup(&pci_priv->boot_debug_timer,
  5078. cnss_boot_debug_timeout_hdlr, 0);
  5079. INIT_DELAYED_WORK(&pci_priv->time_sync_work,
  5080. cnss_pci_time_sync_work_hdlr);
  5081. cnss_pci_get_link_status(pci_priv);
  5082. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, false);
  5083. cnss_pci_wake_gpio_init(pci_priv);
  5084. break;
  5085. default:
  5086. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  5087. pci_dev->device);
  5088. ret = -ENODEV;
  5089. goto unreg_mhi;
  5090. }
  5091. cnss_pci_config_regs(pci_priv);
  5092. if (EMULATION_HW)
  5093. goto out;
  5094. ret = cnss_suspend_pci_link(pci_priv);
  5095. if (ret)
  5096. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  5097. cnss_power_off_device(plat_priv);
  5098. set_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5099. return 0;
  5100. unreg_mhi:
  5101. cnss_pci_unregister_mhi(pci_priv);
  5102. disable_msi:
  5103. cnss_pci_disable_msi(pci_priv);
  5104. disable_bus:
  5105. cnss_pci_disable_bus(pci_priv);
  5106. dereg_pci_event:
  5107. cnss_dereg_pci_event(pci_priv);
  5108. deinit_smmu:
  5109. cnss_pci_deinit_smmu(pci_priv);
  5110. unregister_ramdump:
  5111. cnss_unregister_ramdump(plat_priv);
  5112. unregister_subsys:
  5113. cnss_unregister_subsys(plat_priv);
  5114. reset_ctx:
  5115. plat_priv->bus_priv = NULL;
  5116. out:
  5117. return ret;
  5118. }
  5119. static void cnss_pci_remove(struct pci_dev *pci_dev)
  5120. {
  5121. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  5122. struct cnss_plat_data *plat_priv =
  5123. cnss_bus_dev_to_plat_priv(&pci_dev->dev);
  5124. clear_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5125. cnss_pci_free_m3_mem(pci_priv);
  5126. cnss_pci_free_fw_mem(pci_priv);
  5127. cnss_pci_free_qdss_mem(pci_priv);
  5128. switch (pci_dev->device) {
  5129. case QCA6290_DEVICE_ID:
  5130. case QCA6390_DEVICE_ID:
  5131. case QCA6490_DEVICE_ID:
  5132. case KIWI_DEVICE_ID:
  5133. cnss_pci_wake_gpio_deinit(pci_priv);
  5134. del_timer(&pci_priv->boot_debug_timer);
  5135. del_timer(&pci_priv->dev_rddm_timer);
  5136. break;
  5137. default:
  5138. break;
  5139. }
  5140. cnss_pci_unregister_mhi(pci_priv);
  5141. cnss_pci_disable_msi(pci_priv);
  5142. cnss_pci_disable_bus(pci_priv);
  5143. cnss_dereg_pci_event(pci_priv);
  5144. cnss_pci_deinit_smmu(pci_priv);
  5145. if (plat_priv) {
  5146. cnss_unregister_ramdump(plat_priv);
  5147. cnss_unregister_subsys(plat_priv);
  5148. plat_priv->bus_priv = NULL;
  5149. } else {
  5150. cnss_pr_err("Plat_priv is null, Unable to unregister ramdump,subsys\n");
  5151. }
  5152. }
  5153. static const struct pci_device_id cnss_pci_id_table[] = {
  5154. { QCA6174_VENDOR_ID, QCA6174_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5155. { QCA6290_VENDOR_ID, QCA6290_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5156. { QCA6390_VENDOR_ID, QCA6390_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5157. { QCA6490_VENDOR_ID, QCA6490_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5158. { KIWI_VENDOR_ID, KIWI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5159. { 0 }
  5160. };
  5161. MODULE_DEVICE_TABLE(pci, cnss_pci_id_table);
  5162. static const struct dev_pm_ops cnss_pm_ops = {
  5163. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5164. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5165. cnss_pci_resume_noirq)
  5166. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend, cnss_pci_runtime_resume,
  5167. cnss_pci_runtime_idle)
  5168. };
  5169. struct pci_driver cnss_pci_driver = {
  5170. .name = "cnss_pci",
  5171. .id_table = cnss_pci_id_table,
  5172. .probe = cnss_pci_probe,
  5173. .remove = cnss_pci_remove,
  5174. .driver = {
  5175. .pm = &cnss_pm_ops,
  5176. },
  5177. };
  5178. static int cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  5179. {
  5180. int ret, retry = 0;
  5181. /* Always set initial target PCIe link speed to Gen2 for QCA6490 device
  5182. * since there may be link issues if it boots up with Gen3 link speed.
  5183. * Device is able to change it later at any time. It will be rejected
  5184. * if requested speed is higher than the one specified in PCIe DT.
  5185. */
  5186. if (plat_priv->device_id == QCA6490_DEVICE_ID) {
  5187. ret = cnss_pci_set_max_link_speed(plat_priv->bus_priv, rc_num,
  5188. PCI_EXP_LNKSTA_CLS_5_0GB);
  5189. if (ret && ret != -EPROBE_DEFER)
  5190. cnss_pr_err("Failed to set max PCIe RC%x link speed to Gen2, err = %d\n",
  5191. rc_num, ret);
  5192. }
  5193. cnss_pr_dbg("Trying to enumerate with PCIe RC%x\n", rc_num);
  5194. retry:
  5195. ret = _cnss_pci_enumerate(plat_priv, rc_num);
  5196. if (ret) {
  5197. if (ret == -EPROBE_DEFER) {
  5198. cnss_pr_dbg("PCIe RC driver is not ready, defer probe\n");
  5199. goto out;
  5200. }
  5201. cnss_pr_err("Failed to enable PCIe RC%x, err = %d\n",
  5202. rc_num, ret);
  5203. if (retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  5204. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  5205. goto retry;
  5206. } else {
  5207. goto out;
  5208. }
  5209. }
  5210. plat_priv->rc_num = rc_num;
  5211. out:
  5212. return ret;
  5213. }
  5214. int cnss_pci_init(struct cnss_plat_data *plat_priv)
  5215. {
  5216. struct device *dev = &plat_priv->plat_dev->dev;
  5217. const __be32 *prop;
  5218. int ret = 0, prop_len = 0, rc_count, i;
  5219. prop = of_get_property(dev->of_node, "qcom,wlan-rc-num", &prop_len);
  5220. if (!prop || !prop_len) {
  5221. cnss_pr_err("Failed to get PCIe RC number from DT\n");
  5222. goto out;
  5223. }
  5224. rc_count = prop_len / sizeof(__be32);
  5225. for (i = 0; i < rc_count; i++) {
  5226. ret = cnss_pci_enumerate(plat_priv, be32_to_cpup(&prop[i]));
  5227. if (!ret)
  5228. break;
  5229. else if (ret == -EPROBE_DEFER || (ret && i == rc_count - 1))
  5230. goto out;
  5231. }
  5232. ret = pci_register_driver(&cnss_pci_driver);
  5233. if (ret) {
  5234. cnss_pr_err("Failed to register to PCI framework, err = %d\n",
  5235. ret);
  5236. goto out;
  5237. }
  5238. if (!plat_priv->bus_priv) {
  5239. cnss_pr_err("Failed to probe PCI driver\n");
  5240. ret = -ENODEV;
  5241. goto unreg_pci;
  5242. }
  5243. return 0;
  5244. unreg_pci:
  5245. pci_unregister_driver(&cnss_pci_driver);
  5246. out:
  5247. return ret;
  5248. }
  5249. void cnss_pci_deinit(struct cnss_plat_data *plat_priv)
  5250. {
  5251. pci_unregister_driver(&cnss_pci_driver);
  5252. }