pci.c 186 KB

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