pci.c 183 KB

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