pci.c 178 KB

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