pci.c 198 KB

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