pci.c 194 KB

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