pci.c 196 KB

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