pci.c 179 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811
  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_set_therm_cdev_state(struct cnss_pci_data *pci_priv,
  2149. unsigned long thermal_state,
  2150. int tcdev_id)
  2151. {
  2152. if (!pci_priv) {
  2153. cnss_pr_err("pci_priv is NULL!\n");
  2154. return -ENODEV;
  2155. }
  2156. if (!pci_priv->driver_ops || !pci_priv->driver_ops->set_therm_cdev_state) {
  2157. cnss_pr_err("driver_ops or set_therm_cdev_state is NULL\n");
  2158. return -EINVAL;
  2159. }
  2160. return pci_priv->driver_ops->set_therm_cdev_state(pci_priv->pci_dev,
  2161. thermal_state,
  2162. tcdev_id);
  2163. }
  2164. int cnss_pci_update_time_sync_period(struct cnss_pci_data *pci_priv,
  2165. unsigned int time_sync_period)
  2166. {
  2167. struct cnss_plat_data *plat_priv;
  2168. if (!pci_priv)
  2169. return -ENODEV;
  2170. plat_priv = pci_priv->plat_priv;
  2171. cnss_pci_stop_time_sync_update(pci_priv);
  2172. plat_priv->ctrl_params.time_sync_period = time_sync_period;
  2173. cnss_pci_start_time_sync_update(pci_priv);
  2174. cnss_pr_dbg("WLAN time sync period %u ms\n",
  2175. plat_priv->ctrl_params.time_sync_period);
  2176. return 0;
  2177. }
  2178. int cnss_pci_call_driver_probe(struct cnss_pci_data *pci_priv)
  2179. {
  2180. int ret = 0;
  2181. struct cnss_plat_data *plat_priv;
  2182. if (!pci_priv)
  2183. return -ENODEV;
  2184. plat_priv = pci_priv->plat_priv;
  2185. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2186. cnss_pr_err("Reboot is in progress, skip driver probe\n");
  2187. return -EINVAL;
  2188. }
  2189. if (test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2190. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2191. cnss_pr_dbg("Skip driver probe\n");
  2192. goto out;
  2193. }
  2194. if (!pci_priv->driver_ops) {
  2195. cnss_pr_err("driver_ops is NULL\n");
  2196. ret = -EINVAL;
  2197. goto out;
  2198. }
  2199. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2200. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2201. ret = pci_priv->driver_ops->reinit(pci_priv->pci_dev,
  2202. pci_priv->pci_device_id);
  2203. if (ret) {
  2204. cnss_pr_err("Failed to reinit host driver, err = %d\n",
  2205. ret);
  2206. goto out;
  2207. }
  2208. complete(&plat_priv->recovery_complete);
  2209. } else if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state)) {
  2210. ret = pci_priv->driver_ops->probe(pci_priv->pci_dev,
  2211. pci_priv->pci_device_id);
  2212. if (ret) {
  2213. cnss_pr_err("Failed to probe host driver, err = %d\n",
  2214. ret);
  2215. goto out;
  2216. }
  2217. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2218. set_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2219. cnss_pci_free_blob_mem(pci_priv);
  2220. complete_all(&plat_priv->power_up_complete);
  2221. } else if (test_bit(CNSS_DRIVER_IDLE_RESTART,
  2222. &plat_priv->driver_state)) {
  2223. ret = pci_priv->driver_ops->idle_restart(pci_priv->pci_dev,
  2224. pci_priv->pci_device_id);
  2225. if (ret) {
  2226. cnss_pr_err("Failed to idle restart host driver, err = %d\n",
  2227. ret);
  2228. plat_priv->power_up_error = ret;
  2229. complete_all(&plat_priv->power_up_complete);
  2230. goto out;
  2231. }
  2232. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  2233. complete_all(&plat_priv->power_up_complete);
  2234. } else {
  2235. complete(&plat_priv->power_up_complete);
  2236. }
  2237. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  2238. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2239. __pm_relax(plat_priv->recovery_ws);
  2240. }
  2241. cnss_pci_start_time_sync_update(pci_priv);
  2242. return 0;
  2243. out:
  2244. return ret;
  2245. }
  2246. int cnss_pci_call_driver_remove(struct cnss_pci_data *pci_priv)
  2247. {
  2248. struct cnss_plat_data *plat_priv;
  2249. int ret;
  2250. if (!pci_priv)
  2251. return -ENODEV;
  2252. plat_priv = pci_priv->plat_priv;
  2253. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) ||
  2254. test_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state) ||
  2255. test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2256. cnss_pr_dbg("Skip driver remove\n");
  2257. return 0;
  2258. }
  2259. if (!pci_priv->driver_ops) {
  2260. cnss_pr_err("driver_ops is NULL\n");
  2261. return -EINVAL;
  2262. }
  2263. cnss_pci_stop_time_sync_update(pci_priv);
  2264. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2265. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2266. pci_priv->driver_ops->shutdown(pci_priv->pci_dev);
  2267. } else if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state)) {
  2268. pci_priv->driver_ops->remove(pci_priv->pci_dev);
  2269. clear_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2270. } else if (test_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2271. &plat_priv->driver_state)) {
  2272. ret = pci_priv->driver_ops->idle_shutdown(pci_priv->pci_dev);
  2273. if (ret == -EAGAIN) {
  2274. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2275. &plat_priv->driver_state);
  2276. return ret;
  2277. }
  2278. }
  2279. plat_priv->get_info_cb_ctx = NULL;
  2280. plat_priv->get_info_cb = NULL;
  2281. return 0;
  2282. }
  2283. int cnss_pci_call_driver_modem_status(struct cnss_pci_data *pci_priv,
  2284. int modem_current_status)
  2285. {
  2286. struct cnss_wlan_driver *driver_ops;
  2287. if (!pci_priv)
  2288. return -ENODEV;
  2289. driver_ops = pci_priv->driver_ops;
  2290. if (!driver_ops || !driver_ops->modem_status)
  2291. return -EINVAL;
  2292. driver_ops->modem_status(pci_priv->pci_dev, modem_current_status);
  2293. return 0;
  2294. }
  2295. int cnss_pci_update_status(struct cnss_pci_data *pci_priv,
  2296. enum cnss_driver_status status)
  2297. {
  2298. struct cnss_wlan_driver *driver_ops;
  2299. if (!pci_priv)
  2300. return -ENODEV;
  2301. driver_ops = pci_priv->driver_ops;
  2302. if (!driver_ops || !driver_ops->update_status)
  2303. return -EINVAL;
  2304. cnss_pr_dbg("Update driver status: %d\n", status);
  2305. driver_ops->update_status(pci_priv->pci_dev, status);
  2306. return 0;
  2307. }
  2308. static void cnss_pci_misc_reg_dump(struct cnss_pci_data *pci_priv,
  2309. struct cnss_misc_reg *misc_reg,
  2310. u32 misc_reg_size,
  2311. char *reg_name)
  2312. {
  2313. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2314. bool do_force_wake_put = true;
  2315. int i;
  2316. if (!misc_reg)
  2317. return;
  2318. if (in_interrupt() || irqs_disabled())
  2319. return;
  2320. if (cnss_pci_check_link_status(pci_priv))
  2321. return;
  2322. if (cnss_pci_force_wake_get(pci_priv)) {
  2323. /* Continue to dump when device has entered RDDM already */
  2324. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2325. return;
  2326. do_force_wake_put = false;
  2327. }
  2328. cnss_pr_dbg("Start to dump %s registers\n", reg_name);
  2329. for (i = 0; i < misc_reg_size; i++) {
  2330. if (!test_bit(pci_priv->misc_reg_dev_mask,
  2331. &misc_reg[i].dev_mask))
  2332. continue;
  2333. if (misc_reg[i].wr) {
  2334. if (misc_reg[i].offset ==
  2335. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG &&
  2336. i >= 1)
  2337. misc_reg[i].val =
  2338. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG_MSK |
  2339. misc_reg[i - 1].val;
  2340. if (cnss_pci_reg_write(pci_priv,
  2341. misc_reg[i].offset,
  2342. misc_reg[i].val))
  2343. goto force_wake_put;
  2344. cnss_pr_vdbg("Write 0x%X to 0x%X\n",
  2345. misc_reg[i].val,
  2346. misc_reg[i].offset);
  2347. } else {
  2348. if (cnss_pci_reg_read(pci_priv,
  2349. misc_reg[i].offset,
  2350. &misc_reg[i].val))
  2351. goto force_wake_put;
  2352. }
  2353. }
  2354. force_wake_put:
  2355. if (do_force_wake_put)
  2356. cnss_pci_force_wake_put(pci_priv);
  2357. }
  2358. static void cnss_pci_dump_misc_reg(struct cnss_pci_data *pci_priv)
  2359. {
  2360. if (in_interrupt() || irqs_disabled())
  2361. return;
  2362. if (cnss_pci_check_link_status(pci_priv))
  2363. return;
  2364. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wcss_reg,
  2365. WCSS_REG_SIZE, "wcss");
  2366. cnss_pci_misc_reg_dump(pci_priv, pci_priv->pcie_reg,
  2367. PCIE_REG_SIZE, "pcie");
  2368. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wlaon_reg,
  2369. WLAON_REG_SIZE, "wlaon");
  2370. cnss_pci_misc_reg_dump(pci_priv, pci_priv->syspm_reg,
  2371. SYSPM_REG_SIZE, "syspm");
  2372. }
  2373. static void cnss_pci_dump_shadow_reg(struct cnss_pci_data *pci_priv)
  2374. {
  2375. int i, j = 0, array_size = SHADOW_REG_COUNT + SHADOW_REG_INTER_COUNT;
  2376. u32 reg_offset;
  2377. bool do_force_wake_put = true;
  2378. if (in_interrupt() || irqs_disabled())
  2379. return;
  2380. if (cnss_pci_check_link_status(pci_priv))
  2381. return;
  2382. if (!pci_priv->debug_reg) {
  2383. pci_priv->debug_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  2384. sizeof(*pci_priv->debug_reg)
  2385. * array_size, GFP_KERNEL);
  2386. if (!pci_priv->debug_reg)
  2387. return;
  2388. }
  2389. if (cnss_pci_force_wake_get(pci_priv))
  2390. do_force_wake_put = false;
  2391. cnss_pr_dbg("Start to dump shadow registers\n");
  2392. for (i = 0; i < SHADOW_REG_COUNT; i++, j++) {
  2393. reg_offset = PCIE_SHADOW_REG_VALUE_0 + i * 4;
  2394. pci_priv->debug_reg[j].offset = reg_offset;
  2395. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2396. &pci_priv->debug_reg[j].val))
  2397. goto force_wake_put;
  2398. }
  2399. for (i = 0; i < SHADOW_REG_INTER_COUNT; i++, j++) {
  2400. reg_offset = PCIE_SHADOW_REG_INTER_0 + i * 4;
  2401. pci_priv->debug_reg[j].offset = reg_offset;
  2402. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2403. &pci_priv->debug_reg[j].val))
  2404. goto force_wake_put;
  2405. }
  2406. force_wake_put:
  2407. if (do_force_wake_put)
  2408. cnss_pci_force_wake_put(pci_priv);
  2409. }
  2410. static int cnss_qca6174_powerup(struct cnss_pci_data *pci_priv)
  2411. {
  2412. int ret = 0;
  2413. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2414. ret = cnss_power_on_device(plat_priv, false);
  2415. if (ret) {
  2416. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2417. goto out;
  2418. }
  2419. ret = cnss_resume_pci_link(pci_priv);
  2420. if (ret) {
  2421. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2422. goto power_off;
  2423. }
  2424. ret = cnss_pci_call_driver_probe(pci_priv);
  2425. if (ret)
  2426. goto suspend_link;
  2427. return 0;
  2428. suspend_link:
  2429. cnss_suspend_pci_link(pci_priv);
  2430. power_off:
  2431. cnss_power_off_device(plat_priv);
  2432. out:
  2433. return ret;
  2434. }
  2435. static int cnss_qca6174_shutdown(struct cnss_pci_data *pci_priv)
  2436. {
  2437. int ret = 0;
  2438. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2439. cnss_pci_pm_runtime_resume(pci_priv);
  2440. ret = cnss_pci_call_driver_remove(pci_priv);
  2441. if (ret == -EAGAIN)
  2442. goto out;
  2443. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2444. CNSS_BUS_WIDTH_NONE);
  2445. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2446. cnss_pci_set_auto_suspended(pci_priv, 0);
  2447. ret = cnss_suspend_pci_link(pci_priv);
  2448. if (ret)
  2449. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2450. cnss_power_off_device(plat_priv);
  2451. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2452. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2453. out:
  2454. return ret;
  2455. }
  2456. static void cnss_qca6174_crash_shutdown(struct cnss_pci_data *pci_priv)
  2457. {
  2458. if (pci_priv->driver_ops && pci_priv->driver_ops->crash_shutdown)
  2459. pci_priv->driver_ops->crash_shutdown(pci_priv->pci_dev);
  2460. }
  2461. static int cnss_qca6174_ramdump(struct cnss_pci_data *pci_priv)
  2462. {
  2463. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2464. struct cnss_ramdump_info *ramdump_info;
  2465. ramdump_info = &plat_priv->ramdump_info;
  2466. if (!ramdump_info->ramdump_size)
  2467. return -EINVAL;
  2468. return cnss_do_ramdump(plat_priv);
  2469. }
  2470. static void cnss_get_driver_mode_update_fw_name(struct cnss_plat_data *plat_priv)
  2471. {
  2472. struct cnss_pci_data *pci_priv;
  2473. struct cnss_wlan_driver *driver_ops;
  2474. pci_priv = plat_priv->bus_priv;
  2475. driver_ops = pci_priv->driver_ops;
  2476. if (driver_ops && driver_ops->get_driver_mode) {
  2477. plat_priv->driver_mode = driver_ops->get_driver_mode();
  2478. cnss_pci_update_fw_name(pci_priv);
  2479. cnss_pr_dbg("New driver mode is %d", plat_priv->driver_mode);
  2480. }
  2481. }
  2482. static int cnss_qca6290_powerup(struct cnss_pci_data *pci_priv)
  2483. {
  2484. int ret = 0;
  2485. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2486. unsigned int timeout;
  2487. int retry = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  2488. int sw_ctrl_gpio = plat_priv->pinctrl_info.sw_ctrl_gpio;
  2489. if (plat_priv->ramdump_info_v2.dump_data_valid) {
  2490. cnss_pci_clear_dump_info(pci_priv);
  2491. cnss_pci_power_off_mhi(pci_priv);
  2492. cnss_suspend_pci_link(pci_priv);
  2493. cnss_pci_deinit_mhi(pci_priv);
  2494. cnss_power_off_device(plat_priv);
  2495. }
  2496. /* Clear QMI send usage count during every power up */
  2497. pci_priv->qmi_send_usage_count = 0;
  2498. plat_priv->power_up_error = 0;
  2499. cnss_get_driver_mode_update_fw_name(plat_priv);
  2500. retry:
  2501. ret = cnss_power_on_device(plat_priv, false);
  2502. if (ret) {
  2503. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2504. goto out;
  2505. }
  2506. ret = cnss_resume_pci_link(pci_priv);
  2507. if (ret) {
  2508. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2509. cnss_pr_dbg("Value of SW_CTRL GPIO: %d\n",
  2510. cnss_get_input_gpio_value(plat_priv, sw_ctrl_gpio));
  2511. if (test_bit(IGNORE_PCI_LINK_FAILURE,
  2512. &plat_priv->ctrl_params.quirks)) {
  2513. cnss_pr_dbg("Ignore PCI link resume failure\n");
  2514. ret = 0;
  2515. goto out;
  2516. }
  2517. if (ret == -EAGAIN && retry++ < POWER_ON_RETRY_MAX_TIMES) {
  2518. cnss_power_off_device(plat_priv);
  2519. /* Force toggle BT_EN GPIO low */
  2520. if (retry == POWER_ON_RETRY_MAX_TIMES) {
  2521. cnss_pr_dbg("Retry #%d. Set BT_EN GPIO(%u) low\n",
  2522. retry, bt_en_gpio);
  2523. if (bt_en_gpio >= 0)
  2524. gpio_direction_output(bt_en_gpio, 0);
  2525. cnss_pr_dbg("BT_EN GPIO val: %d\n",
  2526. gpio_get_value(bt_en_gpio));
  2527. }
  2528. cnss_pr_dbg("Retry to resume PCI link #%d\n", retry);
  2529. cnss_pr_dbg("Value of SW_CTRL GPIO: %d\n",
  2530. cnss_get_input_gpio_value(plat_priv,
  2531. sw_ctrl_gpio));
  2532. msleep(POWER_ON_RETRY_DELAY_MS * retry);
  2533. goto retry;
  2534. }
  2535. /* Assert when it reaches maximum retries */
  2536. CNSS_ASSERT(0);
  2537. goto power_off;
  2538. }
  2539. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  2540. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_QMI);
  2541. ret = cnss_pci_start_mhi(pci_priv);
  2542. if (ret) {
  2543. cnss_fatal_err("Failed to start MHI, err = %d\n", ret);
  2544. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  2545. !pci_priv->pci_link_down_ind && timeout) {
  2546. /* Start recovery directly for MHI start failures */
  2547. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  2548. CNSS_REASON_DEFAULT);
  2549. }
  2550. return 0;
  2551. }
  2552. if (test_bit(USE_CORE_ONLY_FW, &plat_priv->ctrl_params.quirks)) {
  2553. clear_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state);
  2554. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2555. return 0;
  2556. }
  2557. cnss_set_pin_connect_status(plat_priv);
  2558. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks)) {
  2559. ret = cnss_pci_call_driver_probe(pci_priv);
  2560. if (ret)
  2561. goto stop_mhi;
  2562. } else if (timeout) {
  2563. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state))
  2564. timeout += WLAN_COLD_BOOT_CAL_TIMEOUT;
  2565. else
  2566. timeout += WLAN_MISSION_MODE_TIMEOUT;
  2567. mod_timer(&plat_priv->fw_boot_timer,
  2568. jiffies + msecs_to_jiffies(timeout));
  2569. }
  2570. return 0;
  2571. stop_mhi:
  2572. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, true);
  2573. cnss_pci_power_off_mhi(pci_priv);
  2574. cnss_suspend_pci_link(pci_priv);
  2575. cnss_pci_deinit_mhi(pci_priv);
  2576. power_off:
  2577. cnss_power_off_device(plat_priv);
  2578. out:
  2579. return ret;
  2580. }
  2581. static int cnss_qca6290_shutdown(struct cnss_pci_data *pci_priv)
  2582. {
  2583. int ret = 0;
  2584. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2585. int do_force_wake = true;
  2586. cnss_pci_pm_runtime_resume(pci_priv);
  2587. ret = cnss_pci_call_driver_remove(pci_priv);
  2588. if (ret == -EAGAIN)
  2589. goto out;
  2590. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2591. CNSS_BUS_WIDTH_NONE);
  2592. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2593. cnss_pci_set_auto_suspended(pci_priv, 0);
  2594. if ((test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  2595. test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2596. test_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state) ||
  2597. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state) ||
  2598. test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) &&
  2599. test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  2600. del_timer(&pci_priv->dev_rddm_timer);
  2601. cnss_pci_collect_dump_info(pci_priv, false);
  2602. if (!plat_priv->recovery_enabled)
  2603. CNSS_ASSERT(0);
  2604. }
  2605. if (!cnss_is_device_powered_on(plat_priv)) {
  2606. cnss_pr_dbg("Device is already powered off, ignore\n");
  2607. goto skip_power_off;
  2608. }
  2609. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2610. do_force_wake = false;
  2611. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, do_force_wake);
  2612. /* FBC image will be freed after powering off MHI, so skip
  2613. * if RAM dump data is still valid.
  2614. */
  2615. if (plat_priv->ramdump_info_v2.dump_data_valid)
  2616. goto skip_power_off;
  2617. cnss_pci_power_off_mhi(pci_priv);
  2618. ret = cnss_suspend_pci_link(pci_priv);
  2619. if (ret)
  2620. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2621. cnss_pci_deinit_mhi(pci_priv);
  2622. cnss_power_off_device(plat_priv);
  2623. skip_power_off:
  2624. pci_priv->remap_window = 0;
  2625. clear_bit(CNSS_FW_READY, &plat_priv->driver_state);
  2626. clear_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  2627. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2628. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  2629. clear_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  2630. pci_priv->pci_link_down_ind = false;
  2631. }
  2632. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2633. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2634. memset(&print_optimize, 0, sizeof(print_optimize));
  2635. out:
  2636. return ret;
  2637. }
  2638. static void cnss_qca6290_crash_shutdown(struct cnss_pci_data *pci_priv)
  2639. {
  2640. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2641. set_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2642. cnss_pr_dbg("Crash shutdown with driver_state 0x%lx\n",
  2643. plat_priv->driver_state);
  2644. cnss_pci_collect_dump_info(pci_priv, true);
  2645. clear_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2646. }
  2647. static int cnss_qca6290_ramdump(struct cnss_pci_data *pci_priv)
  2648. {
  2649. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2650. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2651. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2652. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2653. int ret = 0;
  2654. if (!info_v2->dump_data_valid || !dump_seg ||
  2655. dump_data->nentries == 0)
  2656. return 0;
  2657. ret = cnss_do_elf_ramdump(plat_priv);
  2658. cnss_pci_clear_dump_info(pci_priv);
  2659. cnss_pci_power_off_mhi(pci_priv);
  2660. cnss_suspend_pci_link(pci_priv);
  2661. cnss_pci_deinit_mhi(pci_priv);
  2662. cnss_power_off_device(plat_priv);
  2663. return ret;
  2664. }
  2665. int cnss_pci_dev_powerup(struct cnss_pci_data *pci_priv)
  2666. {
  2667. int ret = 0;
  2668. if (!pci_priv) {
  2669. cnss_pr_err("pci_priv is NULL\n");
  2670. return -ENODEV;
  2671. }
  2672. switch (pci_priv->device_id) {
  2673. case QCA6174_DEVICE_ID:
  2674. ret = cnss_qca6174_powerup(pci_priv);
  2675. break;
  2676. case QCA6290_DEVICE_ID:
  2677. case QCA6390_DEVICE_ID:
  2678. case QCA6490_DEVICE_ID:
  2679. case KIWI_DEVICE_ID:
  2680. case MANGO_DEVICE_ID:
  2681. case PEACH_DEVICE_ID:
  2682. ret = cnss_qca6290_powerup(pci_priv);
  2683. break;
  2684. default:
  2685. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2686. pci_priv->device_id);
  2687. ret = -ENODEV;
  2688. }
  2689. return ret;
  2690. }
  2691. int cnss_pci_dev_shutdown(struct cnss_pci_data *pci_priv)
  2692. {
  2693. int ret = 0;
  2694. if (!pci_priv) {
  2695. cnss_pr_err("pci_priv is NULL\n");
  2696. return -ENODEV;
  2697. }
  2698. switch (pci_priv->device_id) {
  2699. case QCA6174_DEVICE_ID:
  2700. ret = cnss_qca6174_shutdown(pci_priv);
  2701. break;
  2702. case QCA6290_DEVICE_ID:
  2703. case QCA6390_DEVICE_ID:
  2704. case QCA6490_DEVICE_ID:
  2705. case KIWI_DEVICE_ID:
  2706. case MANGO_DEVICE_ID:
  2707. case PEACH_DEVICE_ID:
  2708. ret = cnss_qca6290_shutdown(pci_priv);
  2709. break;
  2710. default:
  2711. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2712. pci_priv->device_id);
  2713. ret = -ENODEV;
  2714. }
  2715. return ret;
  2716. }
  2717. int cnss_pci_dev_crash_shutdown(struct cnss_pci_data *pci_priv)
  2718. {
  2719. int ret = 0;
  2720. if (!pci_priv) {
  2721. cnss_pr_err("pci_priv is NULL\n");
  2722. return -ENODEV;
  2723. }
  2724. switch (pci_priv->device_id) {
  2725. case QCA6174_DEVICE_ID:
  2726. cnss_qca6174_crash_shutdown(pci_priv);
  2727. break;
  2728. case QCA6290_DEVICE_ID:
  2729. case QCA6390_DEVICE_ID:
  2730. case QCA6490_DEVICE_ID:
  2731. case KIWI_DEVICE_ID:
  2732. case MANGO_DEVICE_ID:
  2733. case PEACH_DEVICE_ID:
  2734. cnss_qca6290_crash_shutdown(pci_priv);
  2735. break;
  2736. default:
  2737. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2738. pci_priv->device_id);
  2739. ret = -ENODEV;
  2740. }
  2741. return ret;
  2742. }
  2743. int cnss_pci_dev_ramdump(struct cnss_pci_data *pci_priv)
  2744. {
  2745. int ret = 0;
  2746. if (!pci_priv) {
  2747. cnss_pr_err("pci_priv is NULL\n");
  2748. return -ENODEV;
  2749. }
  2750. switch (pci_priv->device_id) {
  2751. case QCA6174_DEVICE_ID:
  2752. ret = cnss_qca6174_ramdump(pci_priv);
  2753. break;
  2754. case QCA6290_DEVICE_ID:
  2755. case QCA6390_DEVICE_ID:
  2756. case QCA6490_DEVICE_ID:
  2757. case KIWI_DEVICE_ID:
  2758. case MANGO_DEVICE_ID:
  2759. case PEACH_DEVICE_ID:
  2760. ret = cnss_qca6290_ramdump(pci_priv);
  2761. break;
  2762. default:
  2763. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2764. pci_priv->device_id);
  2765. ret = -ENODEV;
  2766. }
  2767. return ret;
  2768. }
  2769. int cnss_pci_is_drv_connected(struct device *dev)
  2770. {
  2771. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2772. if (!pci_priv)
  2773. return -ENODEV;
  2774. return pci_priv->drv_connected_last;
  2775. }
  2776. EXPORT_SYMBOL(cnss_pci_is_drv_connected);
  2777. static void cnss_wlan_reg_driver_work(struct work_struct *work)
  2778. {
  2779. struct cnss_plat_data *plat_priv =
  2780. container_of(work, struct cnss_plat_data, wlan_reg_driver_work.work);
  2781. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  2782. struct cnss_cal_info *cal_info;
  2783. unsigned int timeout;
  2784. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state))
  2785. return;
  2786. if (test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state)) {
  2787. goto reg_driver;
  2788. } else {
  2789. if (plat_priv->charger_mode) {
  2790. cnss_pr_err("Ignore calibration timeout in charger mode\n");
  2791. return;
  2792. }
  2793. if (!test_bit(CNSS_IN_COLD_BOOT_CAL,
  2794. &plat_priv->driver_state)) {
  2795. timeout = cnss_get_timeout(plat_priv,
  2796. CNSS_TIMEOUT_CALIBRATION);
  2797. cnss_pr_dbg("File system not ready to start calibration. Wait for %ds..\n",
  2798. timeout / 1000);
  2799. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2800. msecs_to_jiffies(timeout));
  2801. return;
  2802. }
  2803. del_timer(&plat_priv->fw_boot_timer);
  2804. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) &&
  2805. !test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2806. cnss_pr_err("Timeout waiting for calibration to complete\n");
  2807. CNSS_ASSERT(0);
  2808. }
  2809. cal_info = kzalloc(sizeof(*cal_info), GFP_KERNEL);
  2810. if (!cal_info)
  2811. return;
  2812. cal_info->cal_status = CNSS_CAL_TIMEOUT;
  2813. cnss_driver_event_post(plat_priv,
  2814. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE,
  2815. 0, cal_info);
  2816. }
  2817. reg_driver:
  2818. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2819. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2820. return;
  2821. }
  2822. reinit_completion(&plat_priv->power_up_complete);
  2823. cnss_driver_event_post(plat_priv,
  2824. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2825. CNSS_EVENT_SYNC_UNKILLABLE,
  2826. pci_priv->driver_ops);
  2827. }
  2828. int cnss_wlan_register_driver(struct cnss_wlan_driver *driver_ops)
  2829. {
  2830. int ret = 0;
  2831. struct cnss_plat_data *plat_priv;
  2832. struct cnss_pci_data *pci_priv;
  2833. const struct pci_device_id *id_table = driver_ops->id_table;
  2834. unsigned int timeout;
  2835. if (!cnss_check_driver_loading_allowed()) {
  2836. cnss_pr_info("No cnss2 dtsi entry present");
  2837. return -ENODEV;
  2838. }
  2839. plat_priv = cnss_get_plat_priv_by_driver_ops(driver_ops);
  2840. if (!plat_priv) {
  2841. cnss_pr_buf("plat_priv is not ready for register driver\n");
  2842. return -EAGAIN;
  2843. }
  2844. pci_priv = plat_priv->bus_priv;
  2845. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  2846. while (id_table && id_table->device) {
  2847. if (plat_priv->device_id == id_table->device) {
  2848. if (plat_priv->device_id == KIWI_DEVICE_ID &&
  2849. driver_ops->chip_version != 2) {
  2850. cnss_pr_err("WLAN HW disabled. kiwi_v2 only supported\n");
  2851. return -ENODEV;
  2852. }
  2853. cnss_pr_info("WLAN register driver deferred for device ID: 0x%x due to HW disable\n",
  2854. id_table->device);
  2855. plat_priv->driver_ops = driver_ops;
  2856. return 0;
  2857. }
  2858. id_table++;
  2859. }
  2860. return -ENODEV;
  2861. }
  2862. if (!test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state)) {
  2863. cnss_pr_info("pci probe not yet done for register driver\n");
  2864. return -EAGAIN;
  2865. }
  2866. if (test_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state)) {
  2867. cnss_pr_err("Driver has already registered\n");
  2868. return -EEXIST;
  2869. }
  2870. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2871. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2872. return -EINVAL;
  2873. }
  2874. if (!id_table || !pci_dev_present(id_table)) {
  2875. /* id_table pointer will move from pci_dev_present(),
  2876. * so check again using local pointer.
  2877. */
  2878. id_table = driver_ops->id_table;
  2879. while (id_table && id_table->vendor) {
  2880. cnss_pr_info("Host driver is built for PCIe device ID 0x%x\n",
  2881. id_table->device);
  2882. id_table++;
  2883. }
  2884. cnss_pr_err("Enumerated PCIe device id is 0x%x, reject unsupported driver\n",
  2885. pci_priv->device_id);
  2886. return -ENODEV;
  2887. }
  2888. if (driver_ops->chip_version != CNSS_CHIP_VER_ANY &&
  2889. driver_ops->chip_version != plat_priv->device_version.major_version) {
  2890. cnss_pr_err("Driver built for chip ver 0x%x, enumerated ver 0x%x, reject unsupported driver\n",
  2891. driver_ops->chip_version,
  2892. plat_priv->device_version.major_version);
  2893. return -ENODEV;
  2894. }
  2895. cnss_get_driver_mode_update_fw_name(plat_priv);
  2896. set_bit(CNSS_DRIVER_REGISTER, &plat_priv->driver_state);
  2897. if (!plat_priv->cbc_enabled ||
  2898. test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state))
  2899. goto register_driver;
  2900. pci_priv->driver_ops = driver_ops;
  2901. /* If Cold Boot Calibration is enabled, it is the 1st step in init
  2902. * sequence.CBC is done on file system_ready trigger. Qcacld will be
  2903. * loaded from vendor_modprobe.sh at early boot and must be deferred
  2904. * until CBC is complete
  2905. */
  2906. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_CALIBRATION);
  2907. INIT_DELAYED_WORK(&plat_priv->wlan_reg_driver_work,
  2908. cnss_wlan_reg_driver_work);
  2909. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2910. msecs_to_jiffies(timeout));
  2911. cnss_pr_info("WLAN register driver deferred for Calibration\n");
  2912. return 0;
  2913. register_driver:
  2914. reinit_completion(&plat_priv->power_up_complete);
  2915. ret = cnss_driver_event_post(plat_priv,
  2916. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2917. CNSS_EVENT_SYNC_UNKILLABLE,
  2918. driver_ops);
  2919. return ret;
  2920. }
  2921. EXPORT_SYMBOL(cnss_wlan_register_driver);
  2922. void cnss_wlan_unregister_driver(struct cnss_wlan_driver *driver_ops)
  2923. {
  2924. struct cnss_plat_data *plat_priv;
  2925. int ret = 0;
  2926. unsigned int timeout;
  2927. plat_priv = cnss_get_plat_priv_by_driver_ops(driver_ops);
  2928. if (!plat_priv) {
  2929. cnss_pr_err("plat_priv is NULL\n");
  2930. return;
  2931. }
  2932. mutex_lock(&plat_priv->driver_ops_lock);
  2933. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  2934. goto skip_wait_power_up;
  2935. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_WLAN_WATCHDOG);
  2936. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  2937. msecs_to_jiffies(timeout));
  2938. if (!ret) {
  2939. cnss_pr_err("Timeout (%ums) waiting for driver power up to complete\n",
  2940. timeout);
  2941. CNSS_ASSERT(0);
  2942. }
  2943. skip_wait_power_up:
  2944. if (!test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2945. !test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2946. goto skip_wait_recovery;
  2947. reinit_completion(&plat_priv->recovery_complete);
  2948. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY);
  2949. ret = wait_for_completion_timeout(&plat_priv->recovery_complete,
  2950. msecs_to_jiffies(timeout));
  2951. if (!ret) {
  2952. cnss_pr_err("Timeout (%ums) waiting for recovery to complete\n",
  2953. timeout);
  2954. CNSS_ASSERT(0);
  2955. }
  2956. skip_wait_recovery:
  2957. cnss_driver_event_post(plat_priv,
  2958. CNSS_DRIVER_EVENT_UNREGISTER_DRIVER,
  2959. CNSS_EVENT_SYNC_UNKILLABLE, NULL);
  2960. mutex_unlock(&plat_priv->driver_ops_lock);
  2961. }
  2962. EXPORT_SYMBOL(cnss_wlan_unregister_driver);
  2963. int cnss_pci_register_driver_hdlr(struct cnss_pci_data *pci_priv,
  2964. void *data)
  2965. {
  2966. int ret = 0;
  2967. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2968. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2969. cnss_pr_dbg("Reboot or shutdown is in progress, ignore register driver\n");
  2970. return -EINVAL;
  2971. }
  2972. set_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2973. pci_priv->driver_ops = data;
  2974. ret = cnss_pci_dev_powerup(pci_priv);
  2975. if (ret) {
  2976. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2977. pci_priv->driver_ops = NULL;
  2978. } else {
  2979. set_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state);
  2980. }
  2981. return ret;
  2982. }
  2983. int cnss_pci_unregister_driver_hdlr(struct cnss_pci_data *pci_priv)
  2984. {
  2985. struct cnss_plat_data *plat_priv;
  2986. if (!pci_priv)
  2987. return -EINVAL;
  2988. plat_priv = pci_priv->plat_priv;
  2989. set_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2990. cnss_pci_dev_shutdown(pci_priv);
  2991. pci_priv->driver_ops = NULL;
  2992. clear_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state);
  2993. return 0;
  2994. }
  2995. static int cnss_pci_suspend_driver(struct cnss_pci_data *pci_priv)
  2996. {
  2997. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2998. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  2999. int ret = 0;
  3000. pm_message_t state = { .event = PM_EVENT_SUSPEND };
  3001. if (driver_ops && driver_ops->suspend) {
  3002. ret = driver_ops->suspend(pci_dev, state);
  3003. if (ret) {
  3004. cnss_pr_err("Failed to suspend host driver, err = %d\n",
  3005. ret);
  3006. ret = -EAGAIN;
  3007. }
  3008. }
  3009. return ret;
  3010. }
  3011. static int cnss_pci_resume_driver(struct cnss_pci_data *pci_priv)
  3012. {
  3013. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3014. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  3015. int ret = 0;
  3016. if (driver_ops && driver_ops->resume) {
  3017. ret = driver_ops->resume(pci_dev);
  3018. if (ret)
  3019. cnss_pr_err("Failed to resume host driver, err = %d\n",
  3020. ret);
  3021. }
  3022. return ret;
  3023. }
  3024. int cnss_pci_suspend_bus(struct cnss_pci_data *pci_priv)
  3025. {
  3026. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3027. int ret = 0;
  3028. if (pci_priv->pci_link_state == PCI_LINK_DOWN)
  3029. goto out;
  3030. if (cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_SUSPEND)) {
  3031. ret = -EAGAIN;
  3032. goto out;
  3033. }
  3034. if (pci_priv->drv_connected_last)
  3035. goto skip_disable_pci;
  3036. pci_clear_master(pci_dev);
  3037. cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  3038. pci_disable_device(pci_dev);
  3039. ret = pci_set_power_state(pci_dev, PCI_D3hot);
  3040. if (ret)
  3041. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  3042. skip_disable_pci:
  3043. if (cnss_set_pci_link(pci_priv, PCI_LINK_DOWN)) {
  3044. ret = -EAGAIN;
  3045. goto resume_mhi;
  3046. }
  3047. pci_priv->pci_link_state = PCI_LINK_DOWN;
  3048. return 0;
  3049. resume_mhi:
  3050. if (!pci_is_enabled(pci_dev))
  3051. if (pci_enable_device(pci_dev))
  3052. cnss_pr_err("Failed to enable PCI device\n");
  3053. if (pci_priv->saved_state)
  3054. cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  3055. pci_set_master(pci_dev);
  3056. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3057. out:
  3058. return ret;
  3059. }
  3060. int cnss_pci_resume_bus(struct cnss_pci_data *pci_priv)
  3061. {
  3062. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3063. int ret = 0;
  3064. if (pci_priv->pci_link_state == PCI_LINK_UP)
  3065. goto out;
  3066. if (cnss_set_pci_link(pci_priv, PCI_LINK_UP)) {
  3067. cnss_fatal_err("Failed to resume PCI link from suspend\n");
  3068. cnss_pci_link_down(&pci_dev->dev);
  3069. ret = -EAGAIN;
  3070. goto out;
  3071. }
  3072. pci_priv->pci_link_state = PCI_LINK_UP;
  3073. if (pci_priv->drv_connected_last)
  3074. goto skip_enable_pci;
  3075. ret = pci_enable_device(pci_dev);
  3076. if (ret) {
  3077. cnss_pr_err("Failed to enable PCI device, err = %d\n",
  3078. ret);
  3079. goto out;
  3080. }
  3081. if (pci_priv->saved_state)
  3082. cnss_set_pci_config_space(pci_priv,
  3083. RESTORE_PCI_CONFIG_SPACE);
  3084. pci_set_master(pci_dev);
  3085. skip_enable_pci:
  3086. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3087. out:
  3088. return ret;
  3089. }
  3090. static int cnss_pci_suspend(struct device *dev)
  3091. {
  3092. int ret = 0;
  3093. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3094. struct cnss_plat_data *plat_priv;
  3095. if (!pci_priv)
  3096. goto out;
  3097. plat_priv = pci_priv->plat_priv;
  3098. if (!plat_priv)
  3099. goto out;
  3100. if (!cnss_is_device_powered_on(plat_priv))
  3101. goto out;
  3102. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3103. pci_priv->drv_supported) {
  3104. pci_priv->drv_connected_last =
  3105. cnss_pci_get_drv_connected(pci_priv);
  3106. if (!pci_priv->drv_connected_last) {
  3107. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3108. ret = -EAGAIN;
  3109. goto out;
  3110. }
  3111. }
  3112. set_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3113. ret = cnss_pci_suspend_driver(pci_priv);
  3114. if (ret)
  3115. goto clear_flag;
  3116. if (!pci_priv->disable_pc) {
  3117. mutex_lock(&pci_priv->bus_lock);
  3118. ret = cnss_pci_suspend_bus(pci_priv);
  3119. mutex_unlock(&pci_priv->bus_lock);
  3120. if (ret)
  3121. goto resume_driver;
  3122. }
  3123. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  3124. return 0;
  3125. resume_driver:
  3126. cnss_pci_resume_driver(pci_priv);
  3127. clear_flag:
  3128. pci_priv->drv_connected_last = 0;
  3129. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3130. out:
  3131. return ret;
  3132. }
  3133. static int cnss_pci_resume(struct device *dev)
  3134. {
  3135. int ret = 0;
  3136. struct pci_dev *pci_dev = to_pci_dev(dev);
  3137. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3138. struct cnss_plat_data *plat_priv;
  3139. if (!pci_priv)
  3140. goto out;
  3141. plat_priv = pci_priv->plat_priv;
  3142. if (!plat_priv)
  3143. goto out;
  3144. if (pci_priv->pci_link_down_ind)
  3145. goto out;
  3146. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3147. goto out;
  3148. if (!pci_priv->disable_pc) {
  3149. ret = cnss_pci_resume_bus(pci_priv);
  3150. if (ret)
  3151. goto out;
  3152. }
  3153. ret = cnss_pci_resume_driver(pci_priv);
  3154. pci_priv->drv_connected_last = 0;
  3155. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3156. out:
  3157. return ret;
  3158. }
  3159. static int cnss_pci_suspend_noirq(struct device *dev)
  3160. {
  3161. int ret = 0;
  3162. struct pci_dev *pci_dev = to_pci_dev(dev);
  3163. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3164. struct cnss_wlan_driver *driver_ops;
  3165. if (!pci_priv)
  3166. goto out;
  3167. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3168. goto out;
  3169. driver_ops = pci_priv->driver_ops;
  3170. if (driver_ops && driver_ops->suspend_noirq)
  3171. ret = driver_ops->suspend_noirq(pci_dev);
  3172. if (pci_priv->disable_pc && !pci_dev->state_saved &&
  3173. !pci_priv->plat_priv->use_pm_domain)
  3174. pci_save_state(pci_dev);
  3175. out:
  3176. return ret;
  3177. }
  3178. static int cnss_pci_resume_noirq(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_wlan_driver *driver_ops;
  3184. if (!pci_priv)
  3185. goto out;
  3186. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3187. goto out;
  3188. driver_ops = pci_priv->driver_ops;
  3189. if (driver_ops && driver_ops->resume_noirq &&
  3190. !pci_priv->pci_link_down_ind)
  3191. ret = driver_ops->resume_noirq(pci_dev);
  3192. out:
  3193. return ret;
  3194. }
  3195. static int cnss_pci_runtime_suspend(struct device *dev)
  3196. {
  3197. int ret = 0;
  3198. struct pci_dev *pci_dev = to_pci_dev(dev);
  3199. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3200. struct cnss_plat_data *plat_priv;
  3201. struct cnss_wlan_driver *driver_ops;
  3202. if (!pci_priv)
  3203. return -EAGAIN;
  3204. plat_priv = pci_priv->plat_priv;
  3205. if (!plat_priv)
  3206. return -EAGAIN;
  3207. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3208. return -EAGAIN;
  3209. if (pci_priv->pci_link_down_ind) {
  3210. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3211. return -EAGAIN;
  3212. }
  3213. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3214. pci_priv->drv_supported) {
  3215. pci_priv->drv_connected_last =
  3216. cnss_pci_get_drv_connected(pci_priv);
  3217. if (!pci_priv->drv_connected_last) {
  3218. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3219. return -EAGAIN;
  3220. }
  3221. }
  3222. cnss_pr_vdbg("Runtime suspend start\n");
  3223. driver_ops = pci_priv->driver_ops;
  3224. if (driver_ops && driver_ops->runtime_ops &&
  3225. driver_ops->runtime_ops->runtime_suspend)
  3226. ret = driver_ops->runtime_ops->runtime_suspend(pci_dev);
  3227. else
  3228. ret = cnss_auto_suspend(dev);
  3229. if (ret)
  3230. pci_priv->drv_connected_last = 0;
  3231. cnss_pr_vdbg("Runtime suspend status: %d\n", ret);
  3232. return ret;
  3233. }
  3234. static int cnss_pci_runtime_resume(struct device *dev)
  3235. {
  3236. int ret = 0;
  3237. struct pci_dev *pci_dev = to_pci_dev(dev);
  3238. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3239. struct cnss_wlan_driver *driver_ops;
  3240. if (!pci_priv)
  3241. return -EAGAIN;
  3242. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3243. return -EAGAIN;
  3244. if (pci_priv->pci_link_down_ind) {
  3245. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3246. return -EAGAIN;
  3247. }
  3248. cnss_pr_vdbg("Runtime resume start\n");
  3249. driver_ops = pci_priv->driver_ops;
  3250. if (driver_ops && driver_ops->runtime_ops &&
  3251. driver_ops->runtime_ops->runtime_resume)
  3252. ret = driver_ops->runtime_ops->runtime_resume(pci_dev);
  3253. else
  3254. ret = cnss_auto_resume(dev);
  3255. if (!ret)
  3256. pci_priv->drv_connected_last = 0;
  3257. cnss_pr_vdbg("Runtime resume status: %d\n", ret);
  3258. return ret;
  3259. }
  3260. static int cnss_pci_runtime_idle(struct device *dev)
  3261. {
  3262. cnss_pr_vdbg("Runtime idle\n");
  3263. pm_request_autosuspend(dev);
  3264. return -EBUSY;
  3265. }
  3266. int cnss_wlan_pm_control(struct device *dev, bool vote)
  3267. {
  3268. struct pci_dev *pci_dev = to_pci_dev(dev);
  3269. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3270. int ret = 0;
  3271. if (!pci_priv)
  3272. return -ENODEV;
  3273. ret = cnss_pci_disable_pc(pci_priv, vote);
  3274. if (ret)
  3275. return ret;
  3276. pci_priv->disable_pc = vote;
  3277. cnss_pr_dbg("%s PCIe power collapse\n", vote ? "disable" : "enable");
  3278. return 0;
  3279. }
  3280. EXPORT_SYMBOL(cnss_wlan_pm_control);
  3281. static void cnss_pci_pm_runtime_get_record(struct cnss_pci_data *pci_priv,
  3282. enum cnss_rtpm_id id)
  3283. {
  3284. if (id >= RTPM_ID_MAX)
  3285. return;
  3286. atomic_inc(&pci_priv->pm_stats.runtime_get);
  3287. atomic_inc(&pci_priv->pm_stats.runtime_get_id[id]);
  3288. pci_priv->pm_stats.runtime_get_timestamp_id[id] =
  3289. cnss_get_host_timestamp(pci_priv->plat_priv);
  3290. }
  3291. static void cnss_pci_pm_runtime_put_record(struct cnss_pci_data *pci_priv,
  3292. enum cnss_rtpm_id id)
  3293. {
  3294. if (id >= RTPM_ID_MAX)
  3295. return;
  3296. atomic_inc(&pci_priv->pm_stats.runtime_put);
  3297. atomic_inc(&pci_priv->pm_stats.runtime_put_id[id]);
  3298. pci_priv->pm_stats.runtime_put_timestamp_id[id] =
  3299. cnss_get_host_timestamp(pci_priv->plat_priv);
  3300. }
  3301. void cnss_pci_pm_runtime_show_usage_count(struct cnss_pci_data *pci_priv)
  3302. {
  3303. struct device *dev;
  3304. if (!pci_priv)
  3305. return;
  3306. dev = &pci_priv->pci_dev->dev;
  3307. cnss_pr_dbg("Runtime PM usage count: %d\n",
  3308. atomic_read(&dev->power.usage_count));
  3309. }
  3310. int cnss_pci_pm_request_resume(struct cnss_pci_data *pci_priv)
  3311. {
  3312. struct device *dev;
  3313. enum rpm_status status;
  3314. if (!pci_priv)
  3315. return -ENODEV;
  3316. dev = &pci_priv->pci_dev->dev;
  3317. status = dev->power.runtime_status;
  3318. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3319. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3320. (void *)_RET_IP_);
  3321. return pm_request_resume(dev);
  3322. }
  3323. int cnss_pci_pm_runtime_resume(struct cnss_pci_data *pci_priv)
  3324. {
  3325. struct device *dev;
  3326. enum rpm_status status;
  3327. if (!pci_priv)
  3328. return -ENODEV;
  3329. dev = &pci_priv->pci_dev->dev;
  3330. status = dev->power.runtime_status;
  3331. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3332. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3333. (void *)_RET_IP_);
  3334. return pm_runtime_resume(dev);
  3335. }
  3336. int cnss_pci_pm_runtime_get(struct cnss_pci_data *pci_priv,
  3337. enum cnss_rtpm_id id)
  3338. {
  3339. struct device *dev;
  3340. enum rpm_status status;
  3341. if (!pci_priv)
  3342. return -ENODEV;
  3343. dev = &pci_priv->pci_dev->dev;
  3344. status = dev->power.runtime_status;
  3345. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3346. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3347. (void *)_RET_IP_);
  3348. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3349. return pm_runtime_get(dev);
  3350. }
  3351. int cnss_pci_pm_runtime_get_sync(struct cnss_pci_data *pci_priv,
  3352. enum cnss_rtpm_id id)
  3353. {
  3354. struct device *dev;
  3355. enum rpm_status status;
  3356. if (!pci_priv)
  3357. return -ENODEV;
  3358. dev = &pci_priv->pci_dev->dev;
  3359. status = dev->power.runtime_status;
  3360. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3361. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3362. (void *)_RET_IP_);
  3363. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3364. return pm_runtime_get_sync(dev);
  3365. }
  3366. void cnss_pci_pm_runtime_get_noresume(struct cnss_pci_data *pci_priv,
  3367. enum cnss_rtpm_id id)
  3368. {
  3369. if (!pci_priv)
  3370. return;
  3371. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3372. pm_runtime_get_noresume(&pci_priv->pci_dev->dev);
  3373. }
  3374. int cnss_pci_pm_runtime_put_autosuspend(struct cnss_pci_data *pci_priv,
  3375. enum cnss_rtpm_id id)
  3376. {
  3377. struct device *dev;
  3378. if (!pci_priv)
  3379. return -ENODEV;
  3380. dev = &pci_priv->pci_dev->dev;
  3381. if (atomic_read(&dev->power.usage_count) == 0) {
  3382. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3383. return -EINVAL;
  3384. }
  3385. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3386. return pm_runtime_put_autosuspend(&pci_priv->pci_dev->dev);
  3387. }
  3388. void cnss_pci_pm_runtime_put_noidle(struct cnss_pci_data *pci_priv,
  3389. enum cnss_rtpm_id id)
  3390. {
  3391. struct device *dev;
  3392. if (!pci_priv)
  3393. return;
  3394. dev = &pci_priv->pci_dev->dev;
  3395. if (atomic_read(&dev->power.usage_count) == 0) {
  3396. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3397. return;
  3398. }
  3399. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3400. pm_runtime_put_noidle(&pci_priv->pci_dev->dev);
  3401. }
  3402. void cnss_pci_pm_runtime_mark_last_busy(struct cnss_pci_data *pci_priv)
  3403. {
  3404. if (!pci_priv)
  3405. return;
  3406. pm_runtime_mark_last_busy(&pci_priv->pci_dev->dev);
  3407. }
  3408. int cnss_auto_suspend(struct device *dev)
  3409. {
  3410. int ret = 0;
  3411. struct pci_dev *pci_dev = to_pci_dev(dev);
  3412. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3413. struct cnss_plat_data *plat_priv;
  3414. if (!pci_priv)
  3415. return -ENODEV;
  3416. plat_priv = pci_priv->plat_priv;
  3417. if (!plat_priv)
  3418. return -ENODEV;
  3419. mutex_lock(&pci_priv->bus_lock);
  3420. if (!pci_priv->qmi_send_usage_count) {
  3421. ret = cnss_pci_suspend_bus(pci_priv);
  3422. if (ret) {
  3423. mutex_unlock(&pci_priv->bus_lock);
  3424. return ret;
  3425. }
  3426. }
  3427. cnss_pci_set_auto_suspended(pci_priv, 1);
  3428. mutex_unlock(&pci_priv->bus_lock);
  3429. cnss_pci_set_monitor_wake_intr(pci_priv, true);
  3430. /* For suspend temporarily set bandwidth vote to NONE and dont save in
  3431. * current_bw_vote as in resume path we should vote for last used
  3432. * bandwidth vote. Also ignore error if bw voting is not setup.
  3433. */
  3434. cnss_setup_bus_bandwidth(plat_priv, CNSS_BUS_WIDTH_NONE, false);
  3435. return 0;
  3436. }
  3437. EXPORT_SYMBOL(cnss_auto_suspend);
  3438. int cnss_auto_resume(struct device *dev)
  3439. {
  3440. int ret = 0;
  3441. struct pci_dev *pci_dev = to_pci_dev(dev);
  3442. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3443. struct cnss_plat_data *plat_priv;
  3444. if (!pci_priv)
  3445. return -ENODEV;
  3446. plat_priv = pci_priv->plat_priv;
  3447. if (!plat_priv)
  3448. return -ENODEV;
  3449. mutex_lock(&pci_priv->bus_lock);
  3450. ret = cnss_pci_resume_bus(pci_priv);
  3451. if (ret) {
  3452. mutex_unlock(&pci_priv->bus_lock);
  3453. return ret;
  3454. }
  3455. cnss_pci_set_auto_suspended(pci_priv, 0);
  3456. mutex_unlock(&pci_priv->bus_lock);
  3457. cnss_request_bus_bandwidth(dev, plat_priv->icc.current_bw_vote);
  3458. return 0;
  3459. }
  3460. EXPORT_SYMBOL(cnss_auto_resume);
  3461. int cnss_pci_force_wake_request_sync(struct device *dev, int timeout_us)
  3462. {
  3463. struct pci_dev *pci_dev = to_pci_dev(dev);
  3464. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3465. struct cnss_plat_data *plat_priv;
  3466. struct mhi_controller *mhi_ctrl;
  3467. if (!pci_priv)
  3468. return -ENODEV;
  3469. switch (pci_priv->device_id) {
  3470. case QCA6390_DEVICE_ID:
  3471. case QCA6490_DEVICE_ID:
  3472. case KIWI_DEVICE_ID:
  3473. case MANGO_DEVICE_ID:
  3474. case PEACH_DEVICE_ID:
  3475. break;
  3476. default:
  3477. return 0;
  3478. }
  3479. mhi_ctrl = pci_priv->mhi_ctrl;
  3480. if (!mhi_ctrl)
  3481. return -EINVAL;
  3482. plat_priv = pci_priv->plat_priv;
  3483. if (!plat_priv)
  3484. return -ENODEV;
  3485. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3486. return -EAGAIN;
  3487. if (timeout_us) {
  3488. /* Busy wait for timeout_us */
  3489. return cnss_mhi_device_get_sync_atomic(pci_priv,
  3490. timeout_us, false);
  3491. } else {
  3492. /* Sleep wait for mhi_ctrl->timeout_ms */
  3493. return mhi_device_get_sync(mhi_ctrl->mhi_dev);
  3494. }
  3495. }
  3496. EXPORT_SYMBOL(cnss_pci_force_wake_request_sync);
  3497. int cnss_pci_force_wake_request(struct device *dev)
  3498. {
  3499. struct pci_dev *pci_dev = to_pci_dev(dev);
  3500. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3501. struct cnss_plat_data *plat_priv;
  3502. struct mhi_controller *mhi_ctrl;
  3503. if (!pci_priv)
  3504. return -ENODEV;
  3505. switch (pci_priv->device_id) {
  3506. case QCA6390_DEVICE_ID:
  3507. case QCA6490_DEVICE_ID:
  3508. case KIWI_DEVICE_ID:
  3509. case MANGO_DEVICE_ID:
  3510. case PEACH_DEVICE_ID:
  3511. break;
  3512. default:
  3513. return 0;
  3514. }
  3515. mhi_ctrl = pci_priv->mhi_ctrl;
  3516. if (!mhi_ctrl)
  3517. return -EINVAL;
  3518. plat_priv = pci_priv->plat_priv;
  3519. if (!plat_priv)
  3520. return -ENODEV;
  3521. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3522. return -EAGAIN;
  3523. mhi_device_get(mhi_ctrl->mhi_dev);
  3524. return 0;
  3525. }
  3526. EXPORT_SYMBOL(cnss_pci_force_wake_request);
  3527. int cnss_pci_is_device_awake(struct device *dev)
  3528. {
  3529. struct pci_dev *pci_dev = to_pci_dev(dev);
  3530. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3531. struct mhi_controller *mhi_ctrl;
  3532. if (!pci_priv)
  3533. return -ENODEV;
  3534. switch (pci_priv->device_id) {
  3535. case QCA6390_DEVICE_ID:
  3536. case QCA6490_DEVICE_ID:
  3537. case KIWI_DEVICE_ID:
  3538. case MANGO_DEVICE_ID:
  3539. case PEACH_DEVICE_ID:
  3540. break;
  3541. default:
  3542. return 0;
  3543. }
  3544. mhi_ctrl = pci_priv->mhi_ctrl;
  3545. if (!mhi_ctrl)
  3546. return -EINVAL;
  3547. return (mhi_ctrl->dev_state == MHI_STATE_M0);
  3548. }
  3549. EXPORT_SYMBOL(cnss_pci_is_device_awake);
  3550. int cnss_pci_force_wake_release(struct device *dev)
  3551. {
  3552. struct pci_dev *pci_dev = to_pci_dev(dev);
  3553. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3554. struct cnss_plat_data *plat_priv;
  3555. struct mhi_controller *mhi_ctrl;
  3556. if (!pci_priv)
  3557. return -ENODEV;
  3558. switch (pci_priv->device_id) {
  3559. case QCA6390_DEVICE_ID:
  3560. case QCA6490_DEVICE_ID:
  3561. case KIWI_DEVICE_ID:
  3562. case MANGO_DEVICE_ID:
  3563. case PEACH_DEVICE_ID:
  3564. break;
  3565. default:
  3566. return 0;
  3567. }
  3568. mhi_ctrl = pci_priv->mhi_ctrl;
  3569. if (!mhi_ctrl)
  3570. return -EINVAL;
  3571. plat_priv = pci_priv->plat_priv;
  3572. if (!plat_priv)
  3573. return -ENODEV;
  3574. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3575. return -EAGAIN;
  3576. mhi_device_put(mhi_ctrl->mhi_dev);
  3577. return 0;
  3578. }
  3579. EXPORT_SYMBOL(cnss_pci_force_wake_release);
  3580. int cnss_pci_qmi_send_get(struct cnss_pci_data *pci_priv)
  3581. {
  3582. int ret = 0;
  3583. if (!pci_priv)
  3584. return -ENODEV;
  3585. mutex_lock(&pci_priv->bus_lock);
  3586. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3587. !pci_priv->qmi_send_usage_count)
  3588. ret = cnss_pci_resume_bus(pci_priv);
  3589. pci_priv->qmi_send_usage_count++;
  3590. cnss_pr_buf("Increased QMI send usage count to %d\n",
  3591. pci_priv->qmi_send_usage_count);
  3592. mutex_unlock(&pci_priv->bus_lock);
  3593. return ret;
  3594. }
  3595. int cnss_pci_qmi_send_put(struct cnss_pci_data *pci_priv)
  3596. {
  3597. int ret = 0;
  3598. if (!pci_priv)
  3599. return -ENODEV;
  3600. mutex_lock(&pci_priv->bus_lock);
  3601. if (pci_priv->qmi_send_usage_count)
  3602. pci_priv->qmi_send_usage_count--;
  3603. cnss_pr_buf("Decreased QMI send usage count to %d\n",
  3604. pci_priv->qmi_send_usage_count);
  3605. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3606. !pci_priv->qmi_send_usage_count &&
  3607. !cnss_pcie_is_device_down(pci_priv))
  3608. ret = cnss_pci_suspend_bus(pci_priv);
  3609. mutex_unlock(&pci_priv->bus_lock);
  3610. return ret;
  3611. }
  3612. int cnss_send_buffer_to_afcmem(struct device *dev, char *afcdb, uint32_t len,
  3613. uint8_t slotid)
  3614. {
  3615. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  3616. struct cnss_fw_mem *fw_mem;
  3617. void *mem = NULL;
  3618. int i, ret;
  3619. u32 *status;
  3620. if (!plat_priv)
  3621. return -EINVAL;
  3622. fw_mem = plat_priv->fw_mem;
  3623. if (slotid >= AFC_MAX_SLOT) {
  3624. cnss_pr_err("Invalid slot id %d\n", slotid);
  3625. ret = -EINVAL;
  3626. goto err;
  3627. }
  3628. if (len > AFC_SLOT_SIZE) {
  3629. cnss_pr_err("len %d greater than slot size", len);
  3630. ret = -EINVAL;
  3631. goto err;
  3632. }
  3633. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3634. if (fw_mem[i].type == QMI_WLFW_AFC_MEM_V01) {
  3635. mem = fw_mem[i].va;
  3636. status = mem + (slotid * AFC_SLOT_SIZE);
  3637. break;
  3638. }
  3639. }
  3640. if (!mem) {
  3641. cnss_pr_err("AFC mem is not available\n");
  3642. ret = -ENOMEM;
  3643. goto err;
  3644. }
  3645. memcpy(mem + (slotid * AFC_SLOT_SIZE), afcdb, len);
  3646. if (len < AFC_SLOT_SIZE)
  3647. memset(mem + (slotid * AFC_SLOT_SIZE) + len,
  3648. 0, AFC_SLOT_SIZE - len);
  3649. status[AFC_AUTH_STATUS_OFFSET] = cpu_to_le32(AFC_AUTH_SUCCESS);
  3650. return 0;
  3651. err:
  3652. return ret;
  3653. }
  3654. EXPORT_SYMBOL(cnss_send_buffer_to_afcmem);
  3655. int cnss_reset_afcmem(struct device *dev, uint8_t slotid)
  3656. {
  3657. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  3658. struct cnss_fw_mem *fw_mem;
  3659. void *mem = NULL;
  3660. int i, ret;
  3661. if (!plat_priv)
  3662. return -EINVAL;
  3663. fw_mem = plat_priv->fw_mem;
  3664. if (slotid >= AFC_MAX_SLOT) {
  3665. cnss_pr_err("Invalid slot id %d\n", slotid);
  3666. ret = -EINVAL;
  3667. goto err;
  3668. }
  3669. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3670. if (fw_mem[i].type == QMI_WLFW_AFC_MEM_V01) {
  3671. mem = fw_mem[i].va;
  3672. break;
  3673. }
  3674. }
  3675. if (!mem) {
  3676. cnss_pr_err("AFC mem is not available\n");
  3677. ret = -ENOMEM;
  3678. goto err;
  3679. }
  3680. memset(mem + (slotid * AFC_SLOT_SIZE), 0, AFC_SLOT_SIZE);
  3681. return 0;
  3682. err:
  3683. return ret;
  3684. }
  3685. EXPORT_SYMBOL(cnss_reset_afcmem);
  3686. int cnss_pci_alloc_fw_mem(struct cnss_pci_data *pci_priv)
  3687. {
  3688. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3689. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3690. struct device *dev = &pci_priv->pci_dev->dev;
  3691. int i;
  3692. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3693. if (!fw_mem[i].va && fw_mem[i].size) {
  3694. retry:
  3695. fw_mem[i].va =
  3696. dma_alloc_attrs(dev, fw_mem[i].size,
  3697. &fw_mem[i].pa, GFP_KERNEL,
  3698. fw_mem[i].attrs);
  3699. if (!fw_mem[i].va) {
  3700. if ((fw_mem[i].attrs &
  3701. DMA_ATTR_FORCE_CONTIGUOUS)) {
  3702. fw_mem[i].attrs &=
  3703. ~DMA_ATTR_FORCE_CONTIGUOUS;
  3704. cnss_pr_dbg("Fallback to non-contiguous memory for FW, Mem type: %u\n",
  3705. fw_mem[i].type);
  3706. goto retry;
  3707. }
  3708. cnss_pr_err("Failed to allocate memory for FW, size: 0x%zx, type: %u\n",
  3709. fw_mem[i].size, fw_mem[i].type);
  3710. CNSS_ASSERT(0);
  3711. return -ENOMEM;
  3712. }
  3713. }
  3714. }
  3715. return 0;
  3716. }
  3717. static void cnss_pci_free_fw_mem(struct cnss_pci_data *pci_priv)
  3718. {
  3719. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3720. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3721. struct device *dev = &pci_priv->pci_dev->dev;
  3722. int i;
  3723. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3724. if (fw_mem[i].va && fw_mem[i].size) {
  3725. cnss_pr_dbg("Freeing memory for FW, va: 0x%pK, pa: %pa, size: 0x%zx, type: %u\n",
  3726. fw_mem[i].va, &fw_mem[i].pa,
  3727. fw_mem[i].size, fw_mem[i].type);
  3728. dma_free_attrs(dev, fw_mem[i].size,
  3729. fw_mem[i].va, fw_mem[i].pa,
  3730. fw_mem[i].attrs);
  3731. fw_mem[i].va = NULL;
  3732. fw_mem[i].pa = 0;
  3733. fw_mem[i].size = 0;
  3734. fw_mem[i].type = 0;
  3735. }
  3736. }
  3737. plat_priv->fw_mem_seg_len = 0;
  3738. }
  3739. int cnss_pci_alloc_qdss_mem(struct cnss_pci_data *pci_priv)
  3740. {
  3741. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3742. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3743. int i, j;
  3744. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3745. if (!qdss_mem[i].va && qdss_mem[i].size) {
  3746. qdss_mem[i].va =
  3747. dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3748. qdss_mem[i].size,
  3749. &qdss_mem[i].pa,
  3750. GFP_KERNEL);
  3751. if (!qdss_mem[i].va) {
  3752. cnss_pr_err("Failed to allocate QDSS memory for FW, size: 0x%zx, type: %u, chuck-ID: %d\n",
  3753. qdss_mem[i].size,
  3754. qdss_mem[i].type, i);
  3755. break;
  3756. }
  3757. }
  3758. }
  3759. /* Best-effort allocation for QDSS trace */
  3760. if (i < plat_priv->qdss_mem_seg_len) {
  3761. for (j = i; j < plat_priv->qdss_mem_seg_len; j++) {
  3762. qdss_mem[j].type = 0;
  3763. qdss_mem[j].size = 0;
  3764. }
  3765. plat_priv->qdss_mem_seg_len = i;
  3766. }
  3767. return 0;
  3768. }
  3769. void cnss_pci_free_qdss_mem(struct cnss_pci_data *pci_priv)
  3770. {
  3771. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3772. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3773. int i;
  3774. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3775. if (qdss_mem[i].va && qdss_mem[i].size) {
  3776. cnss_pr_dbg("Freeing memory for QDSS: pa: %pa, size: 0x%zx, type: %u\n",
  3777. &qdss_mem[i].pa, qdss_mem[i].size,
  3778. qdss_mem[i].type);
  3779. dma_free_coherent(&pci_priv->pci_dev->dev,
  3780. qdss_mem[i].size, qdss_mem[i].va,
  3781. qdss_mem[i].pa);
  3782. qdss_mem[i].va = NULL;
  3783. qdss_mem[i].pa = 0;
  3784. qdss_mem[i].size = 0;
  3785. qdss_mem[i].type = 0;
  3786. }
  3787. }
  3788. plat_priv->qdss_mem_seg_len = 0;
  3789. }
  3790. int cnss_pci_load_m3(struct cnss_pci_data *pci_priv)
  3791. {
  3792. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3793. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3794. char filename[MAX_FIRMWARE_NAME_LEN];
  3795. char *phy_filename = DEFAULT_PHY_UCODE_FILE_NAME;
  3796. const struct firmware *fw_entry;
  3797. int ret = 0;
  3798. /* Use forward compatibility here since for any recent device
  3799. * it should use DEFAULT_PHY_UCODE_FILE_NAME.
  3800. */
  3801. switch (pci_priv->device_id) {
  3802. case QCA6174_DEVICE_ID:
  3803. cnss_pr_err("Invalid device ID (0x%x) to load phy image\n",
  3804. pci_priv->device_id);
  3805. return -EINVAL;
  3806. case QCA6290_DEVICE_ID:
  3807. case QCA6390_DEVICE_ID:
  3808. case QCA6490_DEVICE_ID:
  3809. phy_filename = DEFAULT_PHY_M3_FILE_NAME;
  3810. break;
  3811. case KIWI_DEVICE_ID:
  3812. case MANGO_DEVICE_ID:
  3813. case PEACH_DEVICE_ID:
  3814. switch (plat_priv->device_version.major_version) {
  3815. case FW_V2_NUMBER:
  3816. phy_filename = PHY_UCODE_V2_FILE_NAME;
  3817. break;
  3818. default:
  3819. break;
  3820. }
  3821. break;
  3822. default:
  3823. break;
  3824. }
  3825. if (!m3_mem->va && !m3_mem->size) {
  3826. cnss_pci_add_fw_prefix_name(pci_priv, filename,
  3827. phy_filename);
  3828. ret = firmware_request_nowarn(&fw_entry, filename,
  3829. &pci_priv->pci_dev->dev);
  3830. if (ret) {
  3831. cnss_pr_err("Failed to load M3 image: %s\n", filename);
  3832. return ret;
  3833. }
  3834. m3_mem->va = dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3835. fw_entry->size, &m3_mem->pa,
  3836. GFP_KERNEL);
  3837. if (!m3_mem->va) {
  3838. cnss_pr_err("Failed to allocate memory for M3, size: 0x%zx\n",
  3839. fw_entry->size);
  3840. release_firmware(fw_entry);
  3841. return -ENOMEM;
  3842. }
  3843. memcpy(m3_mem->va, fw_entry->data, fw_entry->size);
  3844. m3_mem->size = fw_entry->size;
  3845. release_firmware(fw_entry);
  3846. }
  3847. return 0;
  3848. }
  3849. static void cnss_pci_free_m3_mem(struct cnss_pci_data *pci_priv)
  3850. {
  3851. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3852. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3853. if (m3_mem->va && m3_mem->size) {
  3854. cnss_pr_dbg("Freeing memory for M3, va: 0x%pK, pa: %pa, size: 0x%zx\n",
  3855. m3_mem->va, &m3_mem->pa, m3_mem->size);
  3856. dma_free_coherent(&pci_priv->pci_dev->dev, m3_mem->size,
  3857. m3_mem->va, m3_mem->pa);
  3858. }
  3859. m3_mem->va = NULL;
  3860. m3_mem->pa = 0;
  3861. m3_mem->size = 0;
  3862. }
  3863. #ifdef CONFIG_FREE_M3_BLOB_MEM
  3864. void cnss_pci_free_blob_mem(struct cnss_pci_data *pci_priv)
  3865. {
  3866. cnss_pci_free_m3_mem(pci_priv);
  3867. }
  3868. #else
  3869. void cnss_pci_free_blob_mem(struct cnss_pci_data *pci_priv)
  3870. {
  3871. }
  3872. #endif
  3873. void cnss_pci_fw_boot_timeout_hdlr(struct cnss_pci_data *pci_priv)
  3874. {
  3875. struct cnss_plat_data *plat_priv;
  3876. if (!pci_priv)
  3877. return;
  3878. cnss_fatal_err("Timeout waiting for FW ready indication\n");
  3879. plat_priv = pci_priv->plat_priv;
  3880. if (!plat_priv)
  3881. return;
  3882. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  3883. cnss_pr_dbg("Ignore FW ready timeout for calibration mode\n");
  3884. return;
  3885. }
  3886. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  3887. CNSS_REASON_TIMEOUT);
  3888. }
  3889. static void cnss_pci_deinit_smmu(struct cnss_pci_data *pci_priv)
  3890. {
  3891. pci_priv->iommu_domain = NULL;
  3892. }
  3893. int cnss_pci_get_iova(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3894. {
  3895. if (!pci_priv)
  3896. return -ENODEV;
  3897. if (!pci_priv->smmu_iova_len)
  3898. return -EINVAL;
  3899. *addr = pci_priv->smmu_iova_start;
  3900. *size = pci_priv->smmu_iova_len;
  3901. return 0;
  3902. }
  3903. int cnss_pci_get_iova_ipa(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3904. {
  3905. if (!pci_priv)
  3906. return -ENODEV;
  3907. if (!pci_priv->smmu_iova_ipa_len)
  3908. return -EINVAL;
  3909. *addr = pci_priv->smmu_iova_ipa_start;
  3910. *size = pci_priv->smmu_iova_ipa_len;
  3911. return 0;
  3912. }
  3913. bool cnss_pci_is_smmu_s1_enabled(struct cnss_pci_data *pci_priv)
  3914. {
  3915. if (pci_priv)
  3916. return pci_priv->smmu_s1_enable;
  3917. return false;
  3918. }
  3919. struct iommu_domain *cnss_smmu_get_domain(struct device *dev)
  3920. {
  3921. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3922. if (!pci_priv)
  3923. return NULL;
  3924. return pci_priv->iommu_domain;
  3925. }
  3926. EXPORT_SYMBOL(cnss_smmu_get_domain);
  3927. int cnss_smmu_map(struct device *dev,
  3928. phys_addr_t paddr, uint32_t *iova_addr, size_t size)
  3929. {
  3930. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3931. struct cnss_plat_data *plat_priv;
  3932. unsigned long iova;
  3933. size_t len;
  3934. int ret = 0;
  3935. int flag = IOMMU_READ | IOMMU_WRITE;
  3936. struct pci_dev *root_port;
  3937. struct device_node *root_of_node;
  3938. bool dma_coherent = false;
  3939. if (!pci_priv)
  3940. return -ENODEV;
  3941. if (!iova_addr) {
  3942. cnss_pr_err("iova_addr is NULL, paddr %pa, size %zu\n",
  3943. &paddr, size);
  3944. return -EINVAL;
  3945. }
  3946. plat_priv = pci_priv->plat_priv;
  3947. len = roundup(size + paddr - rounddown(paddr, PAGE_SIZE), PAGE_SIZE);
  3948. iova = roundup(pci_priv->smmu_iova_ipa_current, PAGE_SIZE);
  3949. if (pci_priv->iommu_geometry &&
  3950. iova >= pci_priv->smmu_iova_ipa_start +
  3951. pci_priv->smmu_iova_ipa_len) {
  3952. cnss_pr_err("No IOVA space to map, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3953. iova,
  3954. &pci_priv->smmu_iova_ipa_start,
  3955. pci_priv->smmu_iova_ipa_len);
  3956. return -ENOMEM;
  3957. }
  3958. if (!test_bit(DISABLE_IO_COHERENCY,
  3959. &plat_priv->ctrl_params.quirks)) {
  3960. root_port = pcie_find_root_port(pci_priv->pci_dev);
  3961. if (!root_port) {
  3962. cnss_pr_err("Root port is null, so dma_coherent is disabled\n");
  3963. } else {
  3964. root_of_node = root_port->dev.of_node;
  3965. if (root_of_node && root_of_node->parent) {
  3966. dma_coherent =
  3967. of_property_read_bool(root_of_node->parent,
  3968. "dma-coherent");
  3969. cnss_pr_dbg("dma-coherent is %s\n",
  3970. dma_coherent ? "enabled" : "disabled");
  3971. if (dma_coherent)
  3972. flag |= IOMMU_CACHE;
  3973. }
  3974. }
  3975. }
  3976. cnss_pr_dbg("IOMMU map: iova %lx, len %zu\n", iova, len);
  3977. ret = iommu_map(pci_priv->iommu_domain, iova,
  3978. rounddown(paddr, PAGE_SIZE), len, flag);
  3979. if (ret) {
  3980. cnss_pr_err("PA to IOVA mapping failed, ret %d\n", ret);
  3981. return ret;
  3982. }
  3983. pci_priv->smmu_iova_ipa_current = iova + len;
  3984. *iova_addr = (uint32_t)(iova + paddr - rounddown(paddr, PAGE_SIZE));
  3985. cnss_pr_dbg("IOMMU map: iova_addr %lx\n", *iova_addr);
  3986. return 0;
  3987. }
  3988. EXPORT_SYMBOL(cnss_smmu_map);
  3989. int cnss_smmu_unmap(struct device *dev, uint32_t iova_addr, size_t size)
  3990. {
  3991. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3992. unsigned long iova;
  3993. size_t unmapped;
  3994. size_t len;
  3995. if (!pci_priv)
  3996. return -ENODEV;
  3997. iova = rounddown(iova_addr, PAGE_SIZE);
  3998. len = roundup(size + iova_addr - iova, PAGE_SIZE);
  3999. if (iova >= pci_priv->smmu_iova_ipa_start +
  4000. pci_priv->smmu_iova_ipa_len) {
  4001. cnss_pr_err("Out of IOVA space to unmap, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  4002. iova,
  4003. &pci_priv->smmu_iova_ipa_start,
  4004. pci_priv->smmu_iova_ipa_len);
  4005. return -ENOMEM;
  4006. }
  4007. cnss_pr_dbg("IOMMU unmap: iova %lx, len %zu\n", iova, len);
  4008. unmapped = iommu_unmap(pci_priv->iommu_domain, iova, len);
  4009. if (unmapped != len) {
  4010. cnss_pr_err("IOMMU unmap failed, unmapped = %zu, requested = %zu\n",
  4011. unmapped, len);
  4012. return -EINVAL;
  4013. }
  4014. pci_priv->smmu_iova_ipa_current = iova;
  4015. return 0;
  4016. }
  4017. EXPORT_SYMBOL(cnss_smmu_unmap);
  4018. int cnss_get_soc_info(struct device *dev, struct cnss_soc_info *info)
  4019. {
  4020. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4021. struct cnss_plat_data *plat_priv;
  4022. if (!pci_priv)
  4023. return -ENODEV;
  4024. plat_priv = pci_priv->plat_priv;
  4025. if (!plat_priv)
  4026. return -ENODEV;
  4027. info->va = pci_priv->bar;
  4028. info->pa = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  4029. info->chip_id = plat_priv->chip_info.chip_id;
  4030. info->chip_family = plat_priv->chip_info.chip_family;
  4031. info->board_id = plat_priv->board_info.board_id;
  4032. info->soc_id = plat_priv->soc_info.soc_id;
  4033. info->fw_version = plat_priv->fw_version_info.fw_version;
  4034. strlcpy(info->fw_build_timestamp,
  4035. plat_priv->fw_version_info.fw_build_timestamp,
  4036. sizeof(info->fw_build_timestamp));
  4037. memcpy(&info->device_version, &plat_priv->device_version,
  4038. sizeof(info->device_version));
  4039. memcpy(&info->dev_mem_info, &plat_priv->dev_mem_info,
  4040. sizeof(info->dev_mem_info));
  4041. memcpy(&info->fw_build_id, &plat_priv->fw_build_id,
  4042. sizeof(info->fw_build_id));
  4043. return 0;
  4044. }
  4045. EXPORT_SYMBOL(cnss_get_soc_info);
  4046. static int cnss_pci_enable_msi(struct cnss_pci_data *pci_priv)
  4047. {
  4048. int ret = 0;
  4049. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4050. int num_vectors;
  4051. struct cnss_msi_config *msi_config;
  4052. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4053. return 0;
  4054. if (cnss_pci_is_force_one_msi(pci_priv)) {
  4055. ret = cnss_pci_get_one_msi_assignment(pci_priv);
  4056. cnss_pr_dbg("force one msi\n");
  4057. } else {
  4058. ret = cnss_pci_get_msi_assignment(pci_priv);
  4059. }
  4060. if (ret) {
  4061. cnss_pr_err("Failed to get MSI assignment, err = %d\n", ret);
  4062. goto out;
  4063. }
  4064. msi_config = pci_priv->msi_config;
  4065. if (!msi_config) {
  4066. cnss_pr_err("msi_config is NULL!\n");
  4067. ret = -EINVAL;
  4068. goto out;
  4069. }
  4070. num_vectors = pci_alloc_irq_vectors(pci_dev,
  4071. msi_config->total_vectors,
  4072. msi_config->total_vectors,
  4073. PCI_IRQ_MSI);
  4074. if ((num_vectors != msi_config->total_vectors) &&
  4075. !cnss_pci_fallback_one_msi(pci_priv, &num_vectors)) {
  4076. cnss_pr_err("Failed to get enough MSI vectors (%d), available vectors = %d",
  4077. msi_config->total_vectors, num_vectors);
  4078. if (num_vectors >= 0)
  4079. ret = -EINVAL;
  4080. goto reset_msi_config;
  4081. }
  4082. if (cnss_pci_config_msi_data(pci_priv)) {
  4083. ret = -EINVAL;
  4084. goto free_msi_vector;
  4085. }
  4086. return 0;
  4087. free_msi_vector:
  4088. pci_free_irq_vectors(pci_priv->pci_dev);
  4089. reset_msi_config:
  4090. pci_priv->msi_config = NULL;
  4091. out:
  4092. return ret;
  4093. }
  4094. static void cnss_pci_disable_msi(struct cnss_pci_data *pci_priv)
  4095. {
  4096. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4097. return;
  4098. pci_free_irq_vectors(pci_priv->pci_dev);
  4099. }
  4100. int cnss_get_user_msi_assignment(struct device *dev, char *user_name,
  4101. int *num_vectors, u32 *user_base_data,
  4102. u32 *base_vector)
  4103. {
  4104. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4105. struct cnss_msi_config *msi_config;
  4106. int idx;
  4107. if (!pci_priv)
  4108. return -ENODEV;
  4109. msi_config = pci_priv->msi_config;
  4110. if (!msi_config) {
  4111. cnss_pr_err("MSI is not supported.\n");
  4112. return -EINVAL;
  4113. }
  4114. for (idx = 0; idx < msi_config->total_users; idx++) {
  4115. if (strcmp(user_name, msi_config->users[idx].name) == 0) {
  4116. *num_vectors = msi_config->users[idx].num_vectors;
  4117. *user_base_data = msi_config->users[idx].base_vector
  4118. + pci_priv->msi_ep_base_data;
  4119. *base_vector = msi_config->users[idx].base_vector;
  4120. /*Add only single print for each user*/
  4121. if (print_optimize.msi_log_chk[idx]++)
  4122. goto skip_print;
  4123. cnss_pr_dbg("Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
  4124. user_name, *num_vectors, *user_base_data,
  4125. *base_vector);
  4126. skip_print:
  4127. return 0;
  4128. }
  4129. }
  4130. cnss_pr_err("Failed to find MSI assignment for %s!\n", user_name);
  4131. return -EINVAL;
  4132. }
  4133. EXPORT_SYMBOL(cnss_get_user_msi_assignment);
  4134. int cnss_get_msi_irq(struct device *dev, unsigned int vector)
  4135. {
  4136. struct pci_dev *pci_dev = to_pci_dev(dev);
  4137. int irq_num;
  4138. irq_num = pci_irq_vector(pci_dev, vector);
  4139. cnss_pr_dbg("Get IRQ number %d for vector index %d\n", irq_num, vector);
  4140. return irq_num;
  4141. }
  4142. EXPORT_SYMBOL(cnss_get_msi_irq);
  4143. bool cnss_is_one_msi(struct device *dev)
  4144. {
  4145. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4146. if (!pci_priv)
  4147. return false;
  4148. return cnss_pci_is_one_msi(pci_priv);
  4149. }
  4150. EXPORT_SYMBOL(cnss_is_one_msi);
  4151. void cnss_get_msi_address(struct device *dev, u32 *msi_addr_low,
  4152. u32 *msi_addr_high)
  4153. {
  4154. struct pci_dev *pci_dev = to_pci_dev(dev);
  4155. u16 control;
  4156. pci_read_config_word(pci_dev, pci_dev->msi_cap + PCI_MSI_FLAGS,
  4157. &control);
  4158. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  4159. msi_addr_low);
  4160. /* Return MSI high address only when device supports 64-bit MSI */
  4161. if (control & PCI_MSI_FLAGS_64BIT)
  4162. pci_read_config_dword(pci_dev,
  4163. pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  4164. msi_addr_high);
  4165. else
  4166. *msi_addr_high = 0;
  4167. /*Add only single print as the address is constant*/
  4168. if (!print_optimize.msi_addr_chk++)
  4169. cnss_pr_dbg("Get MSI low addr = 0x%x, high addr = 0x%x\n",
  4170. *msi_addr_low, *msi_addr_high);
  4171. }
  4172. EXPORT_SYMBOL(cnss_get_msi_address);
  4173. u32 cnss_pci_get_wake_msi(struct cnss_pci_data *pci_priv)
  4174. {
  4175. int ret, num_vectors;
  4176. u32 user_base_data, base_vector;
  4177. if (!pci_priv)
  4178. return -ENODEV;
  4179. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4180. WAKE_MSI_NAME, &num_vectors,
  4181. &user_base_data, &base_vector);
  4182. if (ret) {
  4183. cnss_pr_err("WAKE MSI is not valid\n");
  4184. return 0;
  4185. }
  4186. return user_base_data;
  4187. }
  4188. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0))
  4189. static inline int cnss_pci_set_dma_mask(struct pci_dev *pci_dev, u64 mask)
  4190. {
  4191. return dma_set_mask(&pci_dev->dev, mask);
  4192. }
  4193. static inline int cnss_pci_set_coherent_dma_mask(struct pci_dev *pci_dev,
  4194. u64 mask)
  4195. {
  4196. return dma_set_coherent_mask(&pci_dev->dev, mask);
  4197. }
  4198. #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)) */
  4199. static inline int cnss_pci_set_dma_mask(struct pci_dev *pci_dev, u64 mask)
  4200. {
  4201. return pci_set_dma_mask(pci_dev, mask);
  4202. }
  4203. static inline int cnss_pci_set_coherent_dma_mask(struct pci_dev *pci_dev,
  4204. u64 mask)
  4205. {
  4206. return pci_set_consistent_dma_mask(pci_dev, mask);
  4207. }
  4208. #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)) */
  4209. static int cnss_pci_enable_bus(struct cnss_pci_data *pci_priv)
  4210. {
  4211. int ret = 0;
  4212. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4213. u16 device_id;
  4214. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &device_id);
  4215. if (device_id != pci_priv->pci_device_id->device) {
  4216. cnss_pr_err("PCI device ID mismatch, config ID: 0x%x, probe ID: 0x%x\n",
  4217. device_id, pci_priv->pci_device_id->device);
  4218. ret = -EIO;
  4219. goto out;
  4220. }
  4221. ret = pci_assign_resource(pci_dev, PCI_BAR_NUM);
  4222. if (ret) {
  4223. pr_err("Failed to assign PCI resource, err = %d\n", ret);
  4224. goto out;
  4225. }
  4226. ret = pci_enable_device(pci_dev);
  4227. if (ret) {
  4228. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  4229. goto out;
  4230. }
  4231. ret = pci_request_region(pci_dev, PCI_BAR_NUM, "cnss");
  4232. if (ret) {
  4233. cnss_pr_err("Failed to request PCI region, err = %d\n", ret);
  4234. goto disable_device;
  4235. }
  4236. switch (device_id) {
  4237. case QCA6174_DEVICE_ID:
  4238. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4239. break;
  4240. case QCA6390_DEVICE_ID:
  4241. case QCA6490_DEVICE_ID:
  4242. case KIWI_DEVICE_ID:
  4243. case MANGO_DEVICE_ID:
  4244. case PEACH_DEVICE_ID:
  4245. pci_priv->dma_bit_mask = PCI_DMA_MASK_36_BIT;
  4246. break;
  4247. default:
  4248. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4249. break;
  4250. }
  4251. cnss_pr_dbg("Set PCI DMA MASK (0x%llx)\n", pci_priv->dma_bit_mask);
  4252. ret = cnss_pci_set_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4253. if (ret) {
  4254. cnss_pr_err("Failed to set PCI DMA mask, err = %d\n", ret);
  4255. goto release_region;
  4256. }
  4257. ret = cnss_pci_set_coherent_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4258. if (ret) {
  4259. cnss_pr_err("Failed to set PCI coherent DMA mask, err = %d\n",
  4260. ret);
  4261. goto release_region;
  4262. }
  4263. pci_priv->bar = pci_iomap(pci_dev, PCI_BAR_NUM, 0);
  4264. if (!pci_priv->bar) {
  4265. cnss_pr_err("Failed to do PCI IO map!\n");
  4266. ret = -EIO;
  4267. goto release_region;
  4268. }
  4269. /* Save default config space without BME enabled */
  4270. pci_save_state(pci_dev);
  4271. pci_priv->default_state = pci_store_saved_state(pci_dev);
  4272. pci_set_master(pci_dev);
  4273. return 0;
  4274. release_region:
  4275. pci_release_region(pci_dev, PCI_BAR_NUM);
  4276. disable_device:
  4277. pci_disable_device(pci_dev);
  4278. out:
  4279. return ret;
  4280. }
  4281. static void cnss_pci_disable_bus(struct cnss_pci_data *pci_priv)
  4282. {
  4283. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4284. pci_clear_master(pci_dev);
  4285. pci_load_and_free_saved_state(pci_dev, &pci_priv->saved_state);
  4286. pci_load_and_free_saved_state(pci_dev, &pci_priv->default_state);
  4287. if (pci_priv->bar) {
  4288. pci_iounmap(pci_dev, pci_priv->bar);
  4289. pci_priv->bar = NULL;
  4290. }
  4291. pci_release_region(pci_dev, PCI_BAR_NUM);
  4292. if (pci_is_enabled(pci_dev))
  4293. pci_disable_device(pci_dev);
  4294. }
  4295. static void cnss_pci_dump_qdss_reg(struct cnss_pci_data *pci_priv)
  4296. {
  4297. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4298. int i, array_size = ARRAY_SIZE(qdss_csr) - 1;
  4299. gfp_t gfp = GFP_KERNEL;
  4300. u32 reg_offset;
  4301. if (in_interrupt() || irqs_disabled())
  4302. gfp = GFP_ATOMIC;
  4303. if (!plat_priv->qdss_reg) {
  4304. plat_priv->qdss_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  4305. sizeof(*plat_priv->qdss_reg)
  4306. * array_size, gfp);
  4307. if (!plat_priv->qdss_reg)
  4308. return;
  4309. }
  4310. cnss_pr_dbg("Start to dump qdss registers\n");
  4311. for (i = 0; qdss_csr[i].name; i++) {
  4312. reg_offset = QDSS_APB_DEC_CSR_BASE + qdss_csr[i].offset;
  4313. if (cnss_pci_reg_read(pci_priv, reg_offset,
  4314. &plat_priv->qdss_reg[i]))
  4315. return;
  4316. cnss_pr_dbg("%s[0x%x] = 0x%x\n", qdss_csr[i].name, reg_offset,
  4317. plat_priv->qdss_reg[i]);
  4318. }
  4319. }
  4320. static void cnss_pci_dump_ce_reg(struct cnss_pci_data *pci_priv,
  4321. enum cnss_ce_index ce)
  4322. {
  4323. int i;
  4324. u32 ce_base = ce * CE_REG_INTERVAL;
  4325. u32 reg_offset, src_ring_base, dst_ring_base, cmn_base, val;
  4326. switch (pci_priv->device_id) {
  4327. case QCA6390_DEVICE_ID:
  4328. src_ring_base = QCA6390_CE_SRC_RING_REG_BASE;
  4329. dst_ring_base = QCA6390_CE_DST_RING_REG_BASE;
  4330. cmn_base = QCA6390_CE_COMMON_REG_BASE;
  4331. break;
  4332. case QCA6490_DEVICE_ID:
  4333. src_ring_base = QCA6490_CE_SRC_RING_REG_BASE;
  4334. dst_ring_base = QCA6490_CE_DST_RING_REG_BASE;
  4335. cmn_base = QCA6490_CE_COMMON_REG_BASE;
  4336. break;
  4337. default:
  4338. return;
  4339. }
  4340. switch (ce) {
  4341. case CNSS_CE_09:
  4342. case CNSS_CE_10:
  4343. for (i = 0; ce_src[i].name; i++) {
  4344. reg_offset = src_ring_base + ce_base + ce_src[i].offset;
  4345. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4346. return;
  4347. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4348. ce, ce_src[i].name, reg_offset, val);
  4349. }
  4350. for (i = 0; ce_dst[i].name; i++) {
  4351. reg_offset = dst_ring_base + ce_base + ce_dst[i].offset;
  4352. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4353. return;
  4354. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4355. ce, ce_dst[i].name, reg_offset, val);
  4356. }
  4357. break;
  4358. case CNSS_CE_COMMON:
  4359. for (i = 0; ce_cmn[i].name; i++) {
  4360. reg_offset = cmn_base + ce_cmn[i].offset;
  4361. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4362. return;
  4363. cnss_pr_dbg("CE_COMMON_%s[0x%x] = 0x%x\n",
  4364. ce_cmn[i].name, reg_offset, val);
  4365. }
  4366. break;
  4367. default:
  4368. cnss_pr_err("Unsupported CE[%d] registers dump\n", ce);
  4369. }
  4370. }
  4371. static void cnss_pci_dump_debug_reg(struct cnss_pci_data *pci_priv)
  4372. {
  4373. if (cnss_pci_check_link_status(pci_priv))
  4374. return;
  4375. cnss_pr_dbg("Start to dump debug registers\n");
  4376. cnss_mhi_debug_reg_dump(pci_priv);
  4377. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4378. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_COMMON);
  4379. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_09);
  4380. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_10);
  4381. }
  4382. static int cnss_pci_assert_host_sol(struct cnss_pci_data *pci_priv)
  4383. {
  4384. if (cnss_get_host_sol_value(pci_priv->plat_priv))
  4385. return -EINVAL;
  4386. cnss_pr_dbg("Assert host SOL GPIO to retry RDDM, expecting link down\n");
  4387. cnss_set_host_sol_value(pci_priv->plat_priv, 1);
  4388. return 0;
  4389. }
  4390. static void cnss_pci_mhi_reg_dump(struct cnss_pci_data *pci_priv)
  4391. {
  4392. if (!cnss_pci_check_link_status(pci_priv))
  4393. cnss_mhi_debug_reg_dump(pci_priv);
  4394. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4395. cnss_pci_dump_misc_reg(pci_priv);
  4396. cnss_pci_dump_shadow_reg(pci_priv);
  4397. }
  4398. int cnss_pci_force_fw_assert_hdlr(struct cnss_pci_data *pci_priv)
  4399. {
  4400. int ret;
  4401. struct cnss_plat_data *plat_priv;
  4402. if (!pci_priv)
  4403. return -ENODEV;
  4404. plat_priv = pci_priv->plat_priv;
  4405. if (!plat_priv)
  4406. return -ENODEV;
  4407. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4408. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state))
  4409. return -EINVAL;
  4410. cnss_auto_resume(&pci_priv->pci_dev->dev);
  4411. if (!pci_priv->is_smmu_fault)
  4412. cnss_pci_mhi_reg_dump(pci_priv);
  4413. /* If link is still down here, directly trigger link down recovery */
  4414. ret = cnss_pci_check_link_status(pci_priv);
  4415. if (ret) {
  4416. cnss_pci_link_down(&pci_priv->pci_dev->dev);
  4417. return 0;
  4418. }
  4419. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_TRIGGER_RDDM);
  4420. if (ret) {
  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_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4426. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state)) {
  4427. cnss_pr_dbg("MHI is not powered on, ignore RDDM failure\n");
  4428. return 0;
  4429. }
  4430. cnss_fatal_err("Failed to trigger RDDM, err = %d\n", ret);
  4431. if (!cnss_pci_assert_host_sol(pci_priv))
  4432. return 0;
  4433. cnss_pci_dump_debug_reg(pci_priv);
  4434. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4435. CNSS_REASON_DEFAULT);
  4436. return ret;
  4437. }
  4438. if (pci_priv->is_smmu_fault) {
  4439. cnss_pci_mhi_reg_dump(pci_priv);
  4440. pci_priv->is_smmu_fault = false;
  4441. }
  4442. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  4443. mod_timer(&pci_priv->dev_rddm_timer,
  4444. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4445. }
  4446. return 0;
  4447. }
  4448. static void cnss_pci_add_dump_seg(struct cnss_pci_data *pci_priv,
  4449. struct cnss_dump_seg *dump_seg,
  4450. enum cnss_fw_dump_type type, int seg_no,
  4451. void *va, dma_addr_t dma, size_t size)
  4452. {
  4453. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4454. struct device *dev = &pci_priv->pci_dev->dev;
  4455. phys_addr_t pa;
  4456. dump_seg->address = dma;
  4457. dump_seg->v_address = va;
  4458. dump_seg->size = size;
  4459. dump_seg->type = type;
  4460. cnss_pr_dbg("Seg: %x, va: %pK, dma: %pa, size: 0x%zx\n",
  4461. seg_no, va, &dma, size);
  4462. if (cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS))
  4463. return;
  4464. cnss_minidump_add_region(plat_priv, type, seg_no, va, pa, size);
  4465. }
  4466. static void cnss_pci_remove_dump_seg(struct cnss_pci_data *pci_priv,
  4467. struct cnss_dump_seg *dump_seg,
  4468. enum cnss_fw_dump_type type, int seg_no,
  4469. void *va, dma_addr_t dma, size_t size)
  4470. {
  4471. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4472. struct device *dev = &pci_priv->pci_dev->dev;
  4473. phys_addr_t pa;
  4474. cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS);
  4475. cnss_minidump_remove_region(plat_priv, type, seg_no, va, pa, size);
  4476. }
  4477. int cnss_pci_call_driver_uevent(struct cnss_pci_data *pci_priv,
  4478. enum cnss_driver_status status, void *data)
  4479. {
  4480. struct cnss_uevent_data uevent_data;
  4481. struct cnss_wlan_driver *driver_ops;
  4482. driver_ops = pci_priv->driver_ops;
  4483. if (!driver_ops || !driver_ops->update_event) {
  4484. cnss_pr_dbg("Hang event driver ops is NULL\n");
  4485. return -EINVAL;
  4486. }
  4487. cnss_pr_dbg("Calling driver uevent: %d\n", status);
  4488. uevent_data.status = status;
  4489. uevent_data.data = data;
  4490. return driver_ops->update_event(pci_priv->pci_dev, &uevent_data);
  4491. }
  4492. static void cnss_pci_send_hang_event(struct cnss_pci_data *pci_priv)
  4493. {
  4494. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4495. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4496. struct cnss_hang_event hang_event;
  4497. void *hang_data_va = NULL;
  4498. u64 offset = 0;
  4499. u16 length = 0;
  4500. int i = 0;
  4501. if (!fw_mem || !plat_priv->fw_mem_seg_len)
  4502. return;
  4503. memset(&hang_event, 0, sizeof(hang_event));
  4504. switch (pci_priv->device_id) {
  4505. case QCA6390_DEVICE_ID:
  4506. offset = HST_HANG_DATA_OFFSET;
  4507. length = HANG_DATA_LENGTH;
  4508. break;
  4509. case QCA6490_DEVICE_ID:
  4510. /* Fallback to hard-coded values if hang event params not
  4511. * present in QMI. Once all the firmware branches have the
  4512. * fix to send params over QMI, this can be removed.
  4513. */
  4514. if (plat_priv->hang_event_data_len) {
  4515. offset = plat_priv->hang_data_addr_offset;
  4516. length = plat_priv->hang_event_data_len;
  4517. } else {
  4518. offset = HSP_HANG_DATA_OFFSET;
  4519. length = HANG_DATA_LENGTH;
  4520. }
  4521. break;
  4522. case KIWI_DEVICE_ID:
  4523. case MANGO_DEVICE_ID:
  4524. case PEACH_DEVICE_ID:
  4525. offset = plat_priv->hang_data_addr_offset;
  4526. length = plat_priv->hang_event_data_len;
  4527. break;
  4528. default:
  4529. cnss_pr_err("Skip Hang Event Data as unsupported Device ID received: %d\n",
  4530. pci_priv->device_id);
  4531. return;
  4532. }
  4533. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4534. if (fw_mem[i].type == QMI_WLFW_MEM_TYPE_DDR_V01 &&
  4535. fw_mem[i].va) {
  4536. /* The offset must be < (fw_mem size- hangdata length) */
  4537. if (!(offset <= fw_mem[i].size - length))
  4538. goto exit;
  4539. hang_data_va = fw_mem[i].va + offset;
  4540. hang_event.hang_event_data = kmemdup(hang_data_va,
  4541. length,
  4542. GFP_ATOMIC);
  4543. if (!hang_event.hang_event_data) {
  4544. cnss_pr_dbg("Hang data memory alloc failed\n");
  4545. return;
  4546. }
  4547. hang_event.hang_event_data_len = length;
  4548. break;
  4549. }
  4550. }
  4551. cnss_pci_call_driver_uevent(pci_priv, CNSS_HANG_EVENT, &hang_event);
  4552. kfree(hang_event.hang_event_data);
  4553. hang_event.hang_event_data = NULL;
  4554. return;
  4555. exit:
  4556. cnss_pr_dbg("Invalid hang event params, offset:0x%x, length:0x%x\n",
  4557. plat_priv->hang_data_addr_offset,
  4558. plat_priv->hang_event_data_len);
  4559. }
  4560. #ifdef CONFIG_CNSS2_SSR_DRIVER_DUMP
  4561. void cnss_pci_collect_host_dump_info(struct cnss_pci_data *pci_priv)
  4562. {
  4563. struct cnss_ssr_driver_dump_entry ssr_entry[CNSS_HOST_DUMP_TYPE_MAX] = {0};
  4564. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4565. size_t num_entries_loaded = 0;
  4566. int x;
  4567. int ret = -1;
  4568. if (pci_priv->driver_ops &&
  4569. pci_priv->driver_ops->collect_driver_dump) {
  4570. ret = pci_priv->driver_ops->collect_driver_dump(pci_priv->pci_dev,
  4571. ssr_entry,
  4572. &num_entries_loaded);
  4573. }
  4574. if (!ret) {
  4575. for (x = 0; x < num_entries_loaded; x++) {
  4576. cnss_pr_info("Idx:%d, ptr: %p, name: %s, size: %d\n",
  4577. x, ssr_entry[x].buffer_pointer,
  4578. ssr_entry[x].region_name,
  4579. ssr_entry[x].buffer_size);
  4580. }
  4581. cnss_do_host_ramdump(plat_priv, ssr_entry, num_entries_loaded);
  4582. } else {
  4583. cnss_pr_info("Host SSR elf dump collection feature disabled\n");
  4584. }
  4585. }
  4586. #endif
  4587. void cnss_pci_collect_dump_info(struct cnss_pci_data *pci_priv, bool in_panic)
  4588. {
  4589. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4590. struct cnss_dump_data *dump_data =
  4591. &plat_priv->ramdump_info_v2.dump_data;
  4592. struct cnss_dump_seg *dump_seg =
  4593. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4594. struct image_info *fw_image, *rddm_image;
  4595. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4596. int ret, i, j;
  4597. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  4598. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  4599. cnss_pci_send_hang_event(pci_priv);
  4600. if (test_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state)) {
  4601. cnss_pr_dbg("RAM dump is already collected, skip\n");
  4602. return;
  4603. }
  4604. if (!cnss_is_device_powered_on(plat_priv)) {
  4605. cnss_pr_dbg("Device is already powered off, skip\n");
  4606. return;
  4607. }
  4608. if (!in_panic) {
  4609. mutex_lock(&pci_priv->bus_lock);
  4610. ret = cnss_pci_check_link_status(pci_priv);
  4611. if (ret) {
  4612. if (ret != -EACCES) {
  4613. mutex_unlock(&pci_priv->bus_lock);
  4614. return;
  4615. }
  4616. if (cnss_pci_resume_bus(pci_priv)) {
  4617. mutex_unlock(&pci_priv->bus_lock);
  4618. return;
  4619. }
  4620. }
  4621. mutex_unlock(&pci_priv->bus_lock);
  4622. } else {
  4623. if (cnss_pci_check_link_status(pci_priv))
  4624. return;
  4625. /* Inside panic handler, reduce timeout for RDDM to avoid
  4626. * unnecessary hypervisor watchdog bite.
  4627. */
  4628. pci_priv->mhi_ctrl->timeout_ms /= 2;
  4629. }
  4630. cnss_mhi_debug_reg_dump(pci_priv);
  4631. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4632. cnss_pci_dump_misc_reg(pci_priv);
  4633. cnss_rddm_trigger_debug(pci_priv);
  4634. ret = mhi_download_rddm_image(pci_priv->mhi_ctrl, in_panic);
  4635. if (ret) {
  4636. cnss_fatal_err("Failed to download RDDM image, err = %d\n",
  4637. ret);
  4638. if (!cnss_pci_assert_host_sol(pci_priv))
  4639. return;
  4640. cnss_rddm_trigger_check(pci_priv);
  4641. cnss_pci_dump_debug_reg(pci_priv);
  4642. return;
  4643. }
  4644. cnss_rddm_trigger_check(pci_priv);
  4645. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4646. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4647. dump_data->nentries = 0;
  4648. if (plat_priv->qdss_mem_seg_len)
  4649. cnss_pci_dump_qdss_reg(pci_priv);
  4650. cnss_mhi_dump_sfr(pci_priv);
  4651. if (!dump_seg) {
  4652. cnss_pr_warn("FW image dump collection not setup");
  4653. goto skip_dump;
  4654. }
  4655. cnss_pr_dbg("Collect FW image dump segment, nentries %d\n",
  4656. fw_image->entries);
  4657. for (i = 0; i < fw_image->entries; i++) {
  4658. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4659. fw_image->mhi_buf[i].buf,
  4660. fw_image->mhi_buf[i].dma_addr,
  4661. fw_image->mhi_buf[i].len);
  4662. dump_seg++;
  4663. }
  4664. dump_data->nentries += fw_image->entries;
  4665. cnss_pr_dbg("Collect RDDM image dump segment, nentries %d\n",
  4666. rddm_image->entries);
  4667. for (i = 0; i < rddm_image->entries; i++) {
  4668. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4669. rddm_image->mhi_buf[i].buf,
  4670. rddm_image->mhi_buf[i].dma_addr,
  4671. rddm_image->mhi_buf[i].len);
  4672. dump_seg++;
  4673. }
  4674. dump_data->nentries += rddm_image->entries;
  4675. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4676. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4677. if (fw_mem[i].attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  4678. cnss_pr_dbg("Collect remote heap dump segment\n");
  4679. cnss_pci_add_dump_seg(pci_priv, dump_seg,
  4680. CNSS_FW_REMOTE_HEAP, j,
  4681. fw_mem[i].va,
  4682. fw_mem[i].pa,
  4683. fw_mem[i].size);
  4684. dump_seg++;
  4685. dump_data->nentries++;
  4686. j++;
  4687. } else {
  4688. cnss_pr_dbg("Skip remote heap dumps as it is non-contiguous\n");
  4689. }
  4690. }
  4691. }
  4692. if (dump_data->nentries > 0)
  4693. plat_priv->ramdump_info_v2.dump_data_valid = true;
  4694. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RDDM_DONE);
  4695. skip_dump:
  4696. complete(&plat_priv->rddm_complete);
  4697. }
  4698. void cnss_pci_clear_dump_info(struct cnss_pci_data *pci_priv)
  4699. {
  4700. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4701. struct cnss_dump_seg *dump_seg =
  4702. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4703. struct image_info *fw_image, *rddm_image;
  4704. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4705. int i, j;
  4706. if (!dump_seg)
  4707. return;
  4708. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4709. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4710. for (i = 0; i < fw_image->entries; i++) {
  4711. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4712. fw_image->mhi_buf[i].buf,
  4713. fw_image->mhi_buf[i].dma_addr,
  4714. fw_image->mhi_buf[i].len);
  4715. dump_seg++;
  4716. }
  4717. for (i = 0; i < rddm_image->entries; i++) {
  4718. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4719. rddm_image->mhi_buf[i].buf,
  4720. rddm_image->mhi_buf[i].dma_addr,
  4721. rddm_image->mhi_buf[i].len);
  4722. dump_seg++;
  4723. }
  4724. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4725. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR &&
  4726. (fw_mem[i].attrs & DMA_ATTR_FORCE_CONTIGUOUS)) {
  4727. cnss_pci_remove_dump_seg(pci_priv, dump_seg,
  4728. CNSS_FW_REMOTE_HEAP, j,
  4729. fw_mem[i].va, fw_mem[i].pa,
  4730. fw_mem[i].size);
  4731. dump_seg++;
  4732. j++;
  4733. }
  4734. }
  4735. plat_priv->ramdump_info_v2.dump_data.nentries = 0;
  4736. plat_priv->ramdump_info_v2.dump_data_valid = false;
  4737. }
  4738. void cnss_pci_device_crashed(struct cnss_pci_data *pci_priv)
  4739. {
  4740. struct cnss_plat_data *plat_priv;
  4741. if (!pci_priv) {
  4742. cnss_pr_err("pci_priv is NULL\n");
  4743. return;
  4744. }
  4745. plat_priv = pci_priv->plat_priv;
  4746. if (!plat_priv) {
  4747. cnss_pr_err("plat_priv is NULL\n");
  4748. return;
  4749. }
  4750. if (plat_priv->recovery_enabled)
  4751. cnss_pci_collect_host_dump_info(pci_priv);
  4752. cnss_device_crashed(&pci_priv->pci_dev->dev);
  4753. }
  4754. static int cnss_mhi_pm_runtime_get(struct mhi_controller *mhi_ctrl)
  4755. {
  4756. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4757. return cnss_pci_pm_runtime_get(pci_priv, RTPM_ID_MHI);
  4758. }
  4759. static void cnss_mhi_pm_runtime_put_noidle(struct mhi_controller *mhi_ctrl)
  4760. {
  4761. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4762. cnss_pci_pm_runtime_put_noidle(pci_priv, RTPM_ID_MHI);
  4763. }
  4764. void cnss_pci_add_fw_prefix_name(struct cnss_pci_data *pci_priv,
  4765. char *prefix_name, char *name)
  4766. {
  4767. struct cnss_plat_data *plat_priv;
  4768. if (!pci_priv)
  4769. return;
  4770. plat_priv = pci_priv->plat_priv;
  4771. if (!plat_priv->use_fw_path_with_prefix) {
  4772. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4773. return;
  4774. }
  4775. switch (pci_priv->device_id) {
  4776. case QCA6390_DEVICE_ID:
  4777. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4778. QCA6390_PATH_PREFIX "%s", name);
  4779. break;
  4780. case QCA6490_DEVICE_ID:
  4781. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4782. QCA6490_PATH_PREFIX "%s", name);
  4783. break;
  4784. case KIWI_DEVICE_ID:
  4785. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4786. KIWI_PATH_PREFIX "%s", name);
  4787. break;
  4788. case MANGO_DEVICE_ID:
  4789. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4790. MANGO_PATH_PREFIX "%s", name);
  4791. break;
  4792. case PEACH_DEVICE_ID:
  4793. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4794. PEACH_PATH_PREFIX "%s", name);
  4795. break;
  4796. default:
  4797. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4798. break;
  4799. }
  4800. cnss_pr_dbg("FW name added with prefix: %s\n", prefix_name);
  4801. }
  4802. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv)
  4803. {
  4804. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4805. switch (pci_priv->device_id) {
  4806. case QCA6390_DEVICE_ID:
  4807. if (plat_priv->device_version.major_version < FW_V2_NUMBER) {
  4808. cnss_pr_dbg("Device ID:version (0x%lx:%d) is not supported\n",
  4809. pci_priv->device_id,
  4810. plat_priv->device_version.major_version);
  4811. return -EINVAL;
  4812. }
  4813. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4814. FW_V2_FILE_NAME);
  4815. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4816. FW_V2_FILE_NAME);
  4817. break;
  4818. case QCA6490_DEVICE_ID:
  4819. switch (plat_priv->device_version.major_version) {
  4820. case FW_V2_NUMBER:
  4821. cnss_pci_add_fw_prefix_name(pci_priv,
  4822. plat_priv->firmware_name,
  4823. FW_V2_FILE_NAME);
  4824. snprintf(plat_priv->fw_fallback_name,
  4825. MAX_FIRMWARE_NAME_LEN,
  4826. FW_V2_FILE_NAME);
  4827. break;
  4828. default:
  4829. cnss_pci_add_fw_prefix_name(pci_priv,
  4830. plat_priv->firmware_name,
  4831. DEFAULT_FW_FILE_NAME);
  4832. snprintf(plat_priv->fw_fallback_name,
  4833. MAX_FIRMWARE_NAME_LEN,
  4834. DEFAULT_FW_FILE_NAME);
  4835. break;
  4836. }
  4837. break;
  4838. case KIWI_DEVICE_ID:
  4839. case MANGO_DEVICE_ID:
  4840. case PEACH_DEVICE_ID:
  4841. switch (plat_priv->device_version.major_version) {
  4842. case FW_V2_NUMBER:
  4843. /*
  4844. * kiwiv2 using seprate fw binary for MM and FTM mode,
  4845. * platform driver loads corresponding binary according
  4846. * to current mode indicated by wlan driver. Otherwise
  4847. * use default binary.
  4848. * Mission mode using same binary name as before,
  4849. * if seprate binary is not there, fall back to default.
  4850. */
  4851. if (plat_priv->driver_mode == CNSS_MISSION) {
  4852. cnss_pci_add_fw_prefix_name(pci_priv,
  4853. plat_priv->firmware_name,
  4854. FW_V2_FILE_NAME);
  4855. cnss_pci_add_fw_prefix_name(pci_priv,
  4856. plat_priv->fw_fallback_name,
  4857. FW_V2_FILE_NAME);
  4858. } else if (plat_priv->driver_mode == CNSS_FTM) {
  4859. cnss_pci_add_fw_prefix_name(pci_priv,
  4860. plat_priv->firmware_name,
  4861. FW_V2_FTM_FILE_NAME);
  4862. cnss_pci_add_fw_prefix_name(pci_priv,
  4863. plat_priv->fw_fallback_name,
  4864. FW_V2_FILE_NAME);
  4865. } else {
  4866. /*
  4867. * Since during cold boot calibration phase,
  4868. * wlan driver has not registered, so default
  4869. * fw binary will be used.
  4870. */
  4871. cnss_pci_add_fw_prefix_name(pci_priv,
  4872. plat_priv->firmware_name,
  4873. FW_V2_FILE_NAME);
  4874. snprintf(plat_priv->fw_fallback_name,
  4875. MAX_FIRMWARE_NAME_LEN,
  4876. FW_V2_FILE_NAME);
  4877. }
  4878. break;
  4879. default:
  4880. cnss_pci_add_fw_prefix_name(pci_priv,
  4881. plat_priv->firmware_name,
  4882. DEFAULT_FW_FILE_NAME);
  4883. snprintf(plat_priv->fw_fallback_name,
  4884. MAX_FIRMWARE_NAME_LEN,
  4885. DEFAULT_FW_FILE_NAME);
  4886. break;
  4887. }
  4888. break;
  4889. default:
  4890. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4891. DEFAULT_FW_FILE_NAME);
  4892. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4893. DEFAULT_FW_FILE_NAME);
  4894. break;
  4895. }
  4896. cnss_pr_dbg("FW name is %s, FW fallback name is %s\n",
  4897. plat_priv->firmware_name, plat_priv->fw_fallback_name);
  4898. return 0;
  4899. }
  4900. static char *cnss_mhi_notify_status_to_str(enum mhi_callback status)
  4901. {
  4902. switch (status) {
  4903. case MHI_CB_IDLE:
  4904. return "IDLE";
  4905. case MHI_CB_EE_RDDM:
  4906. return "RDDM";
  4907. case MHI_CB_SYS_ERROR:
  4908. return "SYS_ERROR";
  4909. case MHI_CB_FATAL_ERROR:
  4910. return "FATAL_ERROR";
  4911. case MHI_CB_EE_MISSION_MODE:
  4912. return "MISSION_MODE";
  4913. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4914. case MHI_CB_FALLBACK_IMG:
  4915. return "FW_FALLBACK";
  4916. #endif
  4917. default:
  4918. return "UNKNOWN";
  4919. }
  4920. };
  4921. static void cnss_dev_rddm_timeout_hdlr(struct timer_list *t)
  4922. {
  4923. struct cnss_pci_data *pci_priv =
  4924. from_timer(pci_priv, t, dev_rddm_timer);
  4925. enum mhi_ee_type mhi_ee;
  4926. if (!pci_priv)
  4927. return;
  4928. cnss_fatal_err("Timeout waiting for RDDM notification\n");
  4929. if (!cnss_pci_assert_host_sol(pci_priv))
  4930. return;
  4931. mhi_ee = mhi_get_exec_env(pci_priv->mhi_ctrl);
  4932. if (mhi_ee == MHI_EE_PBL)
  4933. cnss_pr_err("Unable to collect ramdumps due to abrupt reset\n");
  4934. if (mhi_ee == MHI_EE_RDDM) {
  4935. cnss_pr_info("Device MHI EE is RDDM, try to collect dump\n");
  4936. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4937. CNSS_REASON_RDDM);
  4938. } else {
  4939. cnss_mhi_debug_reg_dump(pci_priv);
  4940. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4941. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4942. CNSS_REASON_TIMEOUT);
  4943. }
  4944. }
  4945. static void cnss_boot_debug_timeout_hdlr(struct timer_list *t)
  4946. {
  4947. struct cnss_pci_data *pci_priv =
  4948. from_timer(pci_priv, t, boot_debug_timer);
  4949. if (!pci_priv)
  4950. return;
  4951. if (cnss_pci_check_link_status(pci_priv))
  4952. return;
  4953. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  4954. return;
  4955. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  4956. return;
  4957. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE))
  4958. return;
  4959. cnss_pr_dbg("Dump MHI/PBL/SBL debug data every %ds during MHI power on\n",
  4960. BOOT_DEBUG_TIMEOUT_MS / 1000);
  4961. cnss_mhi_debug_reg_dump(pci_priv);
  4962. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4963. cnss_pci_dump_bl_sram_mem(pci_priv);
  4964. mod_timer(&pci_priv->boot_debug_timer,
  4965. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  4966. }
  4967. static int cnss_pci_handle_mhi_sys_err(struct cnss_pci_data *pci_priv)
  4968. {
  4969. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4970. cnss_ignore_qmi_failure(true);
  4971. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4972. del_timer(&plat_priv->fw_boot_timer);
  4973. mod_timer(&pci_priv->dev_rddm_timer,
  4974. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4975. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4976. return 0;
  4977. }
  4978. int cnss_pci_handle_dev_sol_irq(struct cnss_pci_data *pci_priv)
  4979. {
  4980. return cnss_pci_handle_mhi_sys_err(pci_priv);
  4981. }
  4982. static void cnss_mhi_notify_status(struct mhi_controller *mhi_ctrl,
  4983. enum mhi_callback reason)
  4984. {
  4985. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4986. struct cnss_plat_data *plat_priv;
  4987. enum cnss_recovery_reason cnss_reason;
  4988. if (!pci_priv) {
  4989. cnss_pr_err("pci_priv is NULL");
  4990. return;
  4991. }
  4992. plat_priv = pci_priv->plat_priv;
  4993. if (reason != MHI_CB_IDLE)
  4994. cnss_pr_dbg("MHI status cb is called with reason %s(%d)\n",
  4995. cnss_mhi_notify_status_to_str(reason), reason);
  4996. switch (reason) {
  4997. case MHI_CB_IDLE:
  4998. case MHI_CB_EE_MISSION_MODE:
  4999. return;
  5000. case MHI_CB_FATAL_ERROR:
  5001. cnss_ignore_qmi_failure(true);
  5002. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  5003. del_timer(&plat_priv->fw_boot_timer);
  5004. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  5005. cnss_reason = CNSS_REASON_DEFAULT;
  5006. break;
  5007. case MHI_CB_SYS_ERROR:
  5008. cnss_pci_handle_mhi_sys_err(pci_priv);
  5009. return;
  5010. case MHI_CB_EE_RDDM:
  5011. cnss_ignore_qmi_failure(true);
  5012. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  5013. del_timer(&plat_priv->fw_boot_timer);
  5014. del_timer(&pci_priv->dev_rddm_timer);
  5015. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  5016. cnss_reason = CNSS_REASON_RDDM;
  5017. break;
  5018. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  5019. case MHI_CB_FALLBACK_IMG:
  5020. /* for kiwi_v2 binary fallback is used, skip path fallback here */
  5021. if (!(pci_priv->device_id == KIWI_DEVICE_ID &&
  5022. plat_priv->device_version.major_version == FW_V2_NUMBER)) {
  5023. plat_priv->use_fw_path_with_prefix = false;
  5024. cnss_pci_update_fw_name(pci_priv);
  5025. }
  5026. return;
  5027. #endif
  5028. default:
  5029. cnss_pr_err("Unsupported MHI status cb reason: %d\n", reason);
  5030. return;
  5031. }
  5032. cnss_schedule_recovery(&pci_priv->pci_dev->dev, cnss_reason);
  5033. }
  5034. static int cnss_pci_get_mhi_msi(struct cnss_pci_data *pci_priv)
  5035. {
  5036. int ret, num_vectors, i;
  5037. u32 user_base_data, base_vector;
  5038. int *irq;
  5039. unsigned int msi_data;
  5040. bool is_one_msi = false;
  5041. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  5042. MHI_MSI_NAME, &num_vectors,
  5043. &user_base_data, &base_vector);
  5044. if (ret)
  5045. return ret;
  5046. if (cnss_pci_is_one_msi(pci_priv)) {
  5047. is_one_msi = true;
  5048. num_vectors = cnss_pci_get_one_msi_mhi_irq_array_size(pci_priv);
  5049. }
  5050. cnss_pr_dbg("Number of assigned MSI for MHI is %d, base vector is %d\n",
  5051. num_vectors, base_vector);
  5052. irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
  5053. if (!irq)
  5054. return -ENOMEM;
  5055. for (i = 0; i < num_vectors; i++) {
  5056. msi_data = base_vector;
  5057. if (!is_one_msi)
  5058. msi_data += i;
  5059. irq[i] = cnss_get_msi_irq(&pci_priv->pci_dev->dev, msi_data);
  5060. }
  5061. pci_priv->mhi_ctrl->irq = irq;
  5062. pci_priv->mhi_ctrl->nr_irqs = num_vectors;
  5063. return 0;
  5064. }
  5065. static int cnss_mhi_bw_scale(struct mhi_controller *mhi_ctrl,
  5066. struct mhi_link_info *link_info)
  5067. {
  5068. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  5069. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5070. int ret = 0;
  5071. cnss_pr_dbg("Setting link speed:0x%x, width:0x%x\n",
  5072. link_info->target_link_speed,
  5073. link_info->target_link_width);
  5074. /* It has to set target link speed here before setting link bandwidth
  5075. * when device requests link speed change. This can avoid setting link
  5076. * bandwidth getting rejected if requested link speed is higher than
  5077. * current one.
  5078. */
  5079. ret = cnss_pci_set_max_link_speed(pci_priv, plat_priv->rc_num,
  5080. link_info->target_link_speed);
  5081. if (ret)
  5082. cnss_pr_err("Failed to set target link speed to 0x%x, err = %d\n",
  5083. link_info->target_link_speed, ret);
  5084. ret = cnss_pci_set_link_bandwidth(pci_priv,
  5085. link_info->target_link_speed,
  5086. link_info->target_link_width);
  5087. if (ret) {
  5088. cnss_pr_err("Failed to set link bandwidth, err = %d\n", ret);
  5089. return ret;
  5090. }
  5091. pci_priv->def_link_speed = link_info->target_link_speed;
  5092. pci_priv->def_link_width = link_info->target_link_width;
  5093. return 0;
  5094. }
  5095. static int cnss_mhi_read_reg(struct mhi_controller *mhi_ctrl,
  5096. void __iomem *addr, u32 *out)
  5097. {
  5098. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  5099. u32 tmp = readl_relaxed(addr);
  5100. /* Unexpected value, query the link status */
  5101. if (PCI_INVALID_READ(tmp) &&
  5102. cnss_pci_check_link_status(pci_priv))
  5103. return -EIO;
  5104. *out = tmp;
  5105. return 0;
  5106. }
  5107. static void cnss_mhi_write_reg(struct mhi_controller *mhi_ctrl,
  5108. void __iomem *addr, u32 val)
  5109. {
  5110. writel_relaxed(val, addr);
  5111. }
  5112. static int cnss_get_mhi_soc_info(struct cnss_plat_data *plat_priv,
  5113. struct mhi_controller *mhi_ctrl)
  5114. {
  5115. int ret = 0;
  5116. ret = mhi_get_soc_info(mhi_ctrl);
  5117. if (ret)
  5118. goto exit;
  5119. plat_priv->device_version.family_number = mhi_ctrl->family_number;
  5120. plat_priv->device_version.device_number = mhi_ctrl->device_number;
  5121. plat_priv->device_version.major_version = mhi_ctrl->major_version;
  5122. plat_priv->device_version.minor_version = mhi_ctrl->minor_version;
  5123. cnss_pr_dbg("Get device version info, family number: 0x%x, device number: 0x%x, major version: 0x%x, minor version: 0x%x\n",
  5124. plat_priv->device_version.family_number,
  5125. plat_priv->device_version.device_number,
  5126. plat_priv->device_version.major_version,
  5127. plat_priv->device_version.minor_version);
  5128. /* Only keep lower 4 bits as real device major version */
  5129. plat_priv->device_version.major_version &= DEVICE_MAJOR_VERSION_MASK;
  5130. exit:
  5131. return ret;
  5132. }
  5133. static int cnss_pci_register_mhi(struct cnss_pci_data *pci_priv)
  5134. {
  5135. int ret = 0;
  5136. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5137. struct pci_dev *pci_dev = pci_priv->pci_dev;
  5138. struct mhi_controller *mhi_ctrl;
  5139. phys_addr_t bar_start;
  5140. const struct mhi_controller_config *cnss_mhi_config =
  5141. &cnss_mhi_config_default;
  5142. ret = cnss_qmi_init(plat_priv);
  5143. if (ret)
  5144. return -EINVAL;
  5145. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  5146. return 0;
  5147. mhi_ctrl = mhi_alloc_controller();
  5148. if (!mhi_ctrl) {
  5149. cnss_pr_err("Invalid MHI controller context\n");
  5150. return -EINVAL;
  5151. }
  5152. pci_priv->mhi_ctrl = mhi_ctrl;
  5153. mhi_ctrl->cntrl_dev = &pci_dev->dev;
  5154. mhi_ctrl->fw_image = plat_priv->firmware_name;
  5155. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  5156. mhi_ctrl->fallback_fw_image = plat_priv->fw_fallback_name;
  5157. #endif
  5158. mhi_ctrl->regs = pci_priv->bar;
  5159. mhi_ctrl->reg_len = pci_resource_len(pci_priv->pci_dev, PCI_BAR_NUM);
  5160. bar_start = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  5161. cnss_pr_dbg("BAR starts at %pa, length is %x\n",
  5162. &bar_start, mhi_ctrl->reg_len);
  5163. ret = cnss_pci_get_mhi_msi(pci_priv);
  5164. if (ret) {
  5165. cnss_pr_err("Failed to get MSI for MHI, err = %d\n", ret);
  5166. goto free_mhi_ctrl;
  5167. }
  5168. if (cnss_pci_is_one_msi(pci_priv))
  5169. mhi_ctrl->irq_flags = IRQF_SHARED | IRQF_NOBALANCING;
  5170. if (pci_priv->smmu_s1_enable) {
  5171. mhi_ctrl->iova_start = pci_priv->smmu_iova_start;
  5172. mhi_ctrl->iova_stop = pci_priv->smmu_iova_start +
  5173. pci_priv->smmu_iova_len;
  5174. } else {
  5175. mhi_ctrl->iova_start = 0;
  5176. mhi_ctrl->iova_stop = pci_priv->dma_bit_mask;
  5177. }
  5178. mhi_ctrl->status_cb = cnss_mhi_notify_status;
  5179. mhi_ctrl->runtime_get = cnss_mhi_pm_runtime_get;
  5180. mhi_ctrl->runtime_put = cnss_mhi_pm_runtime_put_noidle;
  5181. mhi_ctrl->read_reg = cnss_mhi_read_reg;
  5182. mhi_ctrl->write_reg = cnss_mhi_write_reg;
  5183. mhi_ctrl->rddm_size = pci_priv->plat_priv->ramdump_info_v2.ramdump_size;
  5184. if (!mhi_ctrl->rddm_size)
  5185. mhi_ctrl->rddm_size = RAMDUMP_SIZE_DEFAULT;
  5186. mhi_ctrl->sbl_size = SZ_512K;
  5187. mhi_ctrl->seg_len = SZ_512K;
  5188. mhi_ctrl->fbc_download = true;
  5189. ret = cnss_get_mhi_soc_info(plat_priv, mhi_ctrl);
  5190. if (ret)
  5191. goto free_mhi_irq;
  5192. /* Satellite config only supported on KIWI V2 and later chipset */
  5193. if (plat_priv->device_id <= QCA6490_DEVICE_ID ||
  5194. (plat_priv->device_id == KIWI_DEVICE_ID &&
  5195. plat_priv->device_version.major_version == 1))
  5196. cnss_mhi_config = &cnss_mhi_config_no_satellite;
  5197. ret = mhi_register_controller(mhi_ctrl, cnss_mhi_config);
  5198. if (ret) {
  5199. cnss_pr_err("Failed to register to MHI bus, err = %d\n", ret);
  5200. goto free_mhi_irq;
  5201. }
  5202. /* MHI satellite driver only needs to connect when DRV is supported */
  5203. if (cnss_pci_is_drv_supported(pci_priv))
  5204. cnss_mhi_controller_set_base(pci_priv, bar_start);
  5205. /* BW scale CB needs to be set after registering MHI per requirement */
  5206. cnss_mhi_controller_set_bw_scale_cb(pci_priv, cnss_mhi_bw_scale);
  5207. ret = cnss_pci_update_fw_name(pci_priv);
  5208. if (ret)
  5209. goto unreg_mhi;
  5210. return 0;
  5211. unreg_mhi:
  5212. mhi_unregister_controller(mhi_ctrl);
  5213. free_mhi_irq:
  5214. kfree(mhi_ctrl->irq);
  5215. free_mhi_ctrl:
  5216. mhi_free_controller(mhi_ctrl);
  5217. return ret;
  5218. }
  5219. static void cnss_pci_unregister_mhi(struct cnss_pci_data *pci_priv)
  5220. {
  5221. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  5222. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  5223. return;
  5224. mhi_unregister_controller(mhi_ctrl);
  5225. kfree(mhi_ctrl->irq);
  5226. mhi_ctrl->irq = NULL;
  5227. mhi_free_controller(mhi_ctrl);
  5228. pci_priv->mhi_ctrl = NULL;
  5229. }
  5230. static void cnss_pci_config_regs(struct cnss_pci_data *pci_priv)
  5231. {
  5232. switch (pci_priv->device_id) {
  5233. case QCA6390_DEVICE_ID:
  5234. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6390;
  5235. pci_priv->wcss_reg = wcss_reg_access_seq;
  5236. pci_priv->pcie_reg = pcie_reg_access_seq;
  5237. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  5238. pci_priv->syspm_reg = syspm_reg_access_seq;
  5239. /* Configure WDOG register with specific value so that we can
  5240. * know if HW is in the process of WDOG reset recovery or not
  5241. * when reading the registers.
  5242. */
  5243. cnss_pci_reg_write
  5244. (pci_priv,
  5245. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG,
  5246. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG_VAL);
  5247. break;
  5248. case QCA6490_DEVICE_ID:
  5249. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6490;
  5250. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  5251. break;
  5252. default:
  5253. return;
  5254. }
  5255. }
  5256. #if !IS_ENABLED(CONFIG_ARCH_QCOM)
  5257. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  5258. {
  5259. return 0;
  5260. }
  5261. static irqreturn_t cnss_pci_wake_handler(int irq, void *data)
  5262. {
  5263. struct cnss_pci_data *pci_priv = data;
  5264. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5265. enum rpm_status status;
  5266. struct device *dev;
  5267. pci_priv->wake_counter++;
  5268. cnss_pr_dbg("WLAN PCI wake IRQ (%u) is asserted #%u\n",
  5269. pci_priv->wake_irq, pci_priv->wake_counter);
  5270. /* Make sure abort current suspend */
  5271. cnss_pm_stay_awake(plat_priv);
  5272. cnss_pm_relax(plat_priv);
  5273. /* Above two pm* API calls will abort system suspend only when
  5274. * plat_dev->dev->ws is initiated by device_init_wakeup() API, and
  5275. * calling pm_system_wakeup() is just to guarantee system suspend
  5276. * can be aborted if it is not initiated in any case.
  5277. */
  5278. pm_system_wakeup();
  5279. dev = &pci_priv->pci_dev->dev;
  5280. status = dev->power.runtime_status;
  5281. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  5282. cnss_pci_get_auto_suspended(pci_priv)) ||
  5283. (status == RPM_SUSPENDING || status == RPM_SUSPENDED)) {
  5284. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  5285. cnss_pci_pm_request_resume(pci_priv);
  5286. }
  5287. return IRQ_HANDLED;
  5288. }
  5289. /**
  5290. * cnss_pci_wake_gpio_init() - Setup PCI wake GPIO for WLAN
  5291. * @pci_priv: driver PCI bus context pointer
  5292. *
  5293. * This function initializes WLAN PCI wake GPIO and corresponding
  5294. * interrupt. It should be used in non-MSM platforms whose PCIe
  5295. * root complex driver doesn't handle the GPIO.
  5296. *
  5297. * Return: 0 for success or skip, negative value for error
  5298. */
  5299. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  5300. {
  5301. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5302. struct device *dev = &plat_priv->plat_dev->dev;
  5303. int ret = 0;
  5304. pci_priv->wake_gpio = of_get_named_gpio(dev->of_node,
  5305. "wlan-pci-wake-gpio", 0);
  5306. if (pci_priv->wake_gpio < 0)
  5307. goto out;
  5308. cnss_pr_dbg("Get PCI wake GPIO (%d) from device node\n",
  5309. pci_priv->wake_gpio);
  5310. ret = gpio_request(pci_priv->wake_gpio, "wlan_pci_wake_gpio");
  5311. if (ret) {
  5312. cnss_pr_err("Failed to request PCI wake GPIO, err = %d\n",
  5313. ret);
  5314. goto out;
  5315. }
  5316. gpio_direction_input(pci_priv->wake_gpio);
  5317. pci_priv->wake_irq = gpio_to_irq(pci_priv->wake_gpio);
  5318. ret = request_irq(pci_priv->wake_irq, cnss_pci_wake_handler,
  5319. IRQF_TRIGGER_FALLING, "wlan_pci_wake_irq", pci_priv);
  5320. if (ret) {
  5321. cnss_pr_err("Failed to request PCI wake IRQ, err = %d\n", ret);
  5322. goto free_gpio;
  5323. }
  5324. ret = enable_irq_wake(pci_priv->wake_irq);
  5325. if (ret) {
  5326. cnss_pr_err("Failed to enable PCI wake IRQ, err = %d\n", ret);
  5327. goto free_irq;
  5328. }
  5329. return 0;
  5330. free_irq:
  5331. free_irq(pci_priv->wake_irq, pci_priv);
  5332. free_gpio:
  5333. gpio_free(pci_priv->wake_gpio);
  5334. out:
  5335. return ret;
  5336. }
  5337. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  5338. {
  5339. if (pci_priv->wake_gpio < 0)
  5340. return;
  5341. disable_irq_wake(pci_priv->wake_irq);
  5342. free_irq(pci_priv->wake_irq, pci_priv);
  5343. gpio_free(pci_priv->wake_gpio);
  5344. }
  5345. #endif
  5346. #ifdef CONFIG_CNSS_SUPPORT_DUAL_DEV
  5347. static int cnss_try_suspend(struct cnss_plat_data *plat_priv)
  5348. {
  5349. int ret = 0;
  5350. /* in the dual wlan card case, if call pci_register_driver after
  5351. * finishing the first pcie device enumeration, it will cause
  5352. * the cnss_pci_probe called in advance with the second wlan card,
  5353. * and the sequence like this:
  5354. * enter msm_pcie_enumerate -> pci_bus_add_devices -> cnss_pci_probe
  5355. * -> exit msm_pcie_enumerate.
  5356. * But the correct sequence we expected is like this:
  5357. * enter msm_pcie_enumerate -> pci_bus_add_devices ->
  5358. * exit msm_pcie_enumerate -> cnss_pci_probe.
  5359. * And this unexpected sequence will make the second wlan card do
  5360. * pcie link suspend while the pcie enumeration not finished.
  5361. * So need to add below logical to avoid doing pcie link suspend
  5362. * if the enumeration has not finish.
  5363. */
  5364. plat_priv->enumerate_done = true;
  5365. /* Now enumeration is finished, try to suspend PCIe link */
  5366. if (plat_priv->bus_priv) {
  5367. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  5368. struct pci_dev *pci_dev = pci_priv->pci_dev;
  5369. switch (pci_dev->device) {
  5370. case QCA6390_DEVICE_ID:
  5371. cnss_pci_set_wlaon_pwr_ctrl(pci_priv,
  5372. false,
  5373. true,
  5374. false);
  5375. cnss_pci_suspend_pwroff(pci_dev);
  5376. break;
  5377. default:
  5378. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  5379. pci_dev->device);
  5380. ret = -ENODEV;
  5381. }
  5382. }
  5383. return ret;
  5384. }
  5385. #else
  5386. static int cnss_try_suspend(struct cnss_plat_data *plat_priv)
  5387. {
  5388. return 0;
  5389. }
  5390. #endif
  5391. /* Setting to use this cnss_pm_domain ops will let PM framework override the
  5392. * ops from dev->bus->pm which is pci_dev_pm_ops from pci-driver.c. This ops
  5393. * has to take care everything device driver needed which is currently done
  5394. * from pci_dev_pm_ops.
  5395. */
  5396. static struct dev_pm_domain cnss_pm_domain = {
  5397. .ops = {
  5398. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5399. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5400. cnss_pci_resume_noirq)
  5401. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend,
  5402. cnss_pci_runtime_resume,
  5403. cnss_pci_runtime_idle)
  5404. }
  5405. };
  5406. static int cnss_pci_get_dev_cfg_node(struct cnss_plat_data *plat_priv)
  5407. {
  5408. struct device_node *child;
  5409. u32 id, i;
  5410. int id_n, ret;
  5411. if (plat_priv->dt_type != CNSS_DTT_MULTIEXCHG)
  5412. return 0;
  5413. if (!plat_priv->device_id) {
  5414. cnss_pr_err("Invalid device id\n");
  5415. return -EINVAL;
  5416. }
  5417. for_each_available_child_of_node(plat_priv->plat_dev->dev.of_node,
  5418. child) {
  5419. if (strcmp(child->name, "chip_cfg"))
  5420. continue;
  5421. id_n = of_property_count_u32_elems(child, "supported-ids");
  5422. if (id_n <= 0) {
  5423. cnss_pr_err("Device id is NOT set\n");
  5424. return -EINVAL;
  5425. }
  5426. for (i = 0; i < id_n; i++) {
  5427. ret = of_property_read_u32_index(child,
  5428. "supported-ids",
  5429. i, &id);
  5430. if (ret) {
  5431. cnss_pr_err("Failed to read supported ids\n");
  5432. return -EINVAL;
  5433. }
  5434. if (id == plat_priv->device_id) {
  5435. plat_priv->dev_node = child;
  5436. cnss_pr_dbg("got node[%s@%d] for device[0x%x]\n",
  5437. child->name, i, id);
  5438. return 0;
  5439. }
  5440. }
  5441. }
  5442. return -EINVAL;
  5443. }
  5444. #ifdef CONFIG_CNSS2_CONDITIONAL_POWEROFF
  5445. static bool cnss_should_suspend_pwroff(struct pci_dev *pci_dev)
  5446. {
  5447. bool suspend_pwroff;
  5448. switch (pci_dev->device) {
  5449. case QCA6390_DEVICE_ID:
  5450. case QCA6490_DEVICE_ID:
  5451. suspend_pwroff = false;
  5452. break;
  5453. default:
  5454. suspend_pwroff = true;
  5455. }
  5456. return suspend_pwroff;
  5457. }
  5458. #else
  5459. static bool cnss_should_suspend_pwroff(struct pci_dev *pci_dev)
  5460. {
  5461. return true;
  5462. }
  5463. #endif
  5464. static void cnss_pci_suspend_pwroff(struct pci_dev *pci_dev)
  5465. {
  5466. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  5467. int rc_num = pci_dev->bus->domain_nr;
  5468. struct cnss_plat_data *plat_priv;
  5469. int ret = 0;
  5470. bool suspend_pwroff = cnss_should_suspend_pwroff(pci_dev);
  5471. plat_priv = cnss_get_plat_priv_by_rc_num(rc_num);
  5472. if (suspend_pwroff) {
  5473. ret = cnss_suspend_pci_link(pci_priv);
  5474. if (ret)
  5475. cnss_pr_err("Failed to suspend PCI link, err = %d\n",
  5476. ret);
  5477. cnss_power_off_device(plat_priv);
  5478. } else {
  5479. cnss_pr_dbg("bus suspend and dev power off disabled for device [0x%x]\n",
  5480. pci_dev->device);
  5481. }
  5482. }
  5483. static int cnss_pci_probe(struct pci_dev *pci_dev,
  5484. const struct pci_device_id *id)
  5485. {
  5486. int ret = 0;
  5487. struct cnss_pci_data *pci_priv;
  5488. struct device *dev = &pci_dev->dev;
  5489. int rc_num = pci_dev->bus->domain_nr;
  5490. struct cnss_plat_data *plat_priv = cnss_get_plat_priv_by_rc_num(rc_num);
  5491. cnss_pr_dbg("PCI is probing, vendor ID: 0x%x, device ID: 0x%x rc_num %d\n",
  5492. id->vendor, pci_dev->device, rc_num);
  5493. if (!plat_priv) {
  5494. cnss_pr_err("Find match plat_priv with rc number failure\n");
  5495. ret = -ENODEV;
  5496. goto out;
  5497. }
  5498. pci_priv = devm_kzalloc(dev, sizeof(*pci_priv), GFP_KERNEL);
  5499. if (!pci_priv) {
  5500. ret = -ENOMEM;
  5501. goto out;
  5502. }
  5503. pci_priv->pci_link_state = PCI_LINK_UP;
  5504. pci_priv->plat_priv = plat_priv;
  5505. pci_priv->pci_dev = pci_dev;
  5506. pci_priv->pci_device_id = id;
  5507. pci_priv->device_id = pci_dev->device;
  5508. cnss_set_pci_priv(pci_dev, pci_priv);
  5509. plat_priv->device_id = pci_dev->device;
  5510. plat_priv->bus_priv = pci_priv;
  5511. mutex_init(&pci_priv->bus_lock);
  5512. if (plat_priv->use_pm_domain)
  5513. dev->pm_domain = &cnss_pm_domain;
  5514. ret = cnss_pci_get_dev_cfg_node(plat_priv);
  5515. if (ret) {
  5516. cnss_pr_err("Failed to get device cfg node, err = %d\n", ret);
  5517. goto reset_ctx;
  5518. }
  5519. ret = cnss_dev_specific_power_on(plat_priv);
  5520. if (ret < 0)
  5521. goto reset_ctx;
  5522. cnss_pci_of_reserved_mem_device_init(pci_priv);
  5523. ret = cnss_register_subsys(plat_priv);
  5524. if (ret)
  5525. goto reset_ctx;
  5526. ret = cnss_register_ramdump(plat_priv);
  5527. if (ret)
  5528. goto unregister_subsys;
  5529. ret = cnss_pci_init_smmu(pci_priv);
  5530. if (ret)
  5531. goto unregister_ramdump;
  5532. ret = cnss_reg_pci_event(pci_priv);
  5533. if (ret) {
  5534. cnss_pr_err("Failed to register PCI event, err = %d\n", ret);
  5535. goto deinit_smmu;
  5536. }
  5537. ret = cnss_pci_enable_bus(pci_priv);
  5538. if (ret)
  5539. goto dereg_pci_event;
  5540. ret = cnss_pci_enable_msi(pci_priv);
  5541. if (ret)
  5542. goto disable_bus;
  5543. ret = cnss_pci_register_mhi(pci_priv);
  5544. if (ret)
  5545. goto disable_msi;
  5546. switch (pci_dev->device) {
  5547. case QCA6174_DEVICE_ID:
  5548. pci_read_config_word(pci_dev, QCA6174_REV_ID_OFFSET,
  5549. &pci_priv->revision_id);
  5550. break;
  5551. case QCA6290_DEVICE_ID:
  5552. case QCA6390_DEVICE_ID:
  5553. case QCA6490_DEVICE_ID:
  5554. case KIWI_DEVICE_ID:
  5555. case MANGO_DEVICE_ID:
  5556. case PEACH_DEVICE_ID:
  5557. if ((cnss_is_dual_wlan_enabled() &&
  5558. plat_priv->enumerate_done) || !cnss_is_dual_wlan_enabled())
  5559. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false,
  5560. false);
  5561. timer_setup(&pci_priv->dev_rddm_timer,
  5562. cnss_dev_rddm_timeout_hdlr, 0);
  5563. timer_setup(&pci_priv->boot_debug_timer,
  5564. cnss_boot_debug_timeout_hdlr, 0);
  5565. INIT_DELAYED_WORK(&pci_priv->time_sync_work,
  5566. cnss_pci_time_sync_work_hdlr);
  5567. cnss_pci_get_link_status(pci_priv);
  5568. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, false);
  5569. cnss_pci_wake_gpio_init(pci_priv);
  5570. break;
  5571. default:
  5572. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  5573. pci_dev->device);
  5574. ret = -ENODEV;
  5575. goto unreg_mhi;
  5576. }
  5577. cnss_pci_config_regs(pci_priv);
  5578. if (EMULATION_HW)
  5579. goto out;
  5580. if (cnss_is_dual_wlan_enabled() && !plat_priv->enumerate_done)
  5581. goto probe_done;
  5582. cnss_pci_suspend_pwroff(pci_dev);
  5583. probe_done:
  5584. set_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5585. return 0;
  5586. unreg_mhi:
  5587. cnss_pci_unregister_mhi(pci_priv);
  5588. disable_msi:
  5589. cnss_pci_disable_msi(pci_priv);
  5590. disable_bus:
  5591. cnss_pci_disable_bus(pci_priv);
  5592. dereg_pci_event:
  5593. cnss_dereg_pci_event(pci_priv);
  5594. deinit_smmu:
  5595. cnss_pci_deinit_smmu(pci_priv);
  5596. unregister_ramdump:
  5597. cnss_unregister_ramdump(plat_priv);
  5598. unregister_subsys:
  5599. cnss_unregister_subsys(plat_priv);
  5600. reset_ctx:
  5601. plat_priv->bus_priv = NULL;
  5602. out:
  5603. return ret;
  5604. }
  5605. static void cnss_pci_remove(struct pci_dev *pci_dev)
  5606. {
  5607. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  5608. struct cnss_plat_data *plat_priv =
  5609. cnss_bus_dev_to_plat_priv(&pci_dev->dev);
  5610. clear_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5611. cnss_pci_unregister_driver_hdlr(pci_priv);
  5612. cnss_pci_free_m3_mem(pci_priv);
  5613. cnss_pci_free_fw_mem(pci_priv);
  5614. cnss_pci_free_qdss_mem(pci_priv);
  5615. switch (pci_dev->device) {
  5616. case QCA6290_DEVICE_ID:
  5617. case QCA6390_DEVICE_ID:
  5618. case QCA6490_DEVICE_ID:
  5619. case KIWI_DEVICE_ID:
  5620. case MANGO_DEVICE_ID:
  5621. case PEACH_DEVICE_ID:
  5622. cnss_pci_wake_gpio_deinit(pci_priv);
  5623. del_timer(&pci_priv->boot_debug_timer);
  5624. del_timer(&pci_priv->dev_rddm_timer);
  5625. break;
  5626. default:
  5627. break;
  5628. }
  5629. cnss_pci_unregister_mhi(pci_priv);
  5630. cnss_pci_disable_msi(pci_priv);
  5631. cnss_pci_disable_bus(pci_priv);
  5632. cnss_dereg_pci_event(pci_priv);
  5633. cnss_pci_deinit_smmu(pci_priv);
  5634. if (plat_priv) {
  5635. cnss_unregister_ramdump(plat_priv);
  5636. cnss_unregister_subsys(plat_priv);
  5637. plat_priv->bus_priv = NULL;
  5638. } else {
  5639. cnss_pr_err("Plat_priv is null, Unable to unregister ramdump,subsys\n");
  5640. }
  5641. }
  5642. static const struct pci_device_id cnss_pci_id_table[] = {
  5643. { QCA6174_VENDOR_ID, QCA6174_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5644. { QCA6290_VENDOR_ID, QCA6290_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5645. { QCA6390_VENDOR_ID, QCA6390_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5646. { QCA6490_VENDOR_ID, QCA6490_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5647. { KIWI_VENDOR_ID, KIWI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5648. { MANGO_VENDOR_ID, MANGO_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5649. { PEACH_VENDOR_ID, PEACH_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5650. { 0 }
  5651. };
  5652. MODULE_DEVICE_TABLE(pci, cnss_pci_id_table);
  5653. static const struct dev_pm_ops cnss_pm_ops = {
  5654. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5655. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5656. cnss_pci_resume_noirq)
  5657. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend, cnss_pci_runtime_resume,
  5658. cnss_pci_runtime_idle)
  5659. };
  5660. static struct pci_driver cnss_pci_driver = {
  5661. .name = "cnss_pci",
  5662. .id_table = cnss_pci_id_table,
  5663. .probe = cnss_pci_probe,
  5664. .remove = cnss_pci_remove,
  5665. .driver = {
  5666. .pm = &cnss_pm_ops,
  5667. },
  5668. };
  5669. static int cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  5670. {
  5671. int ret, retry = 0;
  5672. /* Always set initial target PCIe link speed to Gen2 for QCA6490 device
  5673. * since there may be link issues if it boots up with Gen3 link speed.
  5674. * Device is able to change it later at any time. It will be rejected
  5675. * if requested speed is higher than the one specified in PCIe DT.
  5676. */
  5677. if (plat_priv->device_id == QCA6490_DEVICE_ID) {
  5678. ret = cnss_pci_set_max_link_speed(plat_priv->bus_priv, rc_num,
  5679. PCI_EXP_LNKSTA_CLS_5_0GB);
  5680. if (ret && ret != -EPROBE_DEFER)
  5681. cnss_pr_err("Failed to set max PCIe RC%x link speed to Gen2, err = %d\n",
  5682. rc_num, ret);
  5683. }
  5684. cnss_pr_dbg("Trying to enumerate with PCIe RC%x\n", rc_num);
  5685. retry:
  5686. ret = _cnss_pci_enumerate(plat_priv, rc_num);
  5687. if (ret) {
  5688. if (ret == -EPROBE_DEFER) {
  5689. cnss_pr_dbg("PCIe RC driver is not ready, defer probe\n");
  5690. goto out;
  5691. }
  5692. cnss_pr_err("Failed to enable PCIe RC%x, err = %d\n",
  5693. rc_num, ret);
  5694. if (retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  5695. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  5696. goto retry;
  5697. } else {
  5698. goto out;
  5699. }
  5700. }
  5701. plat_priv->rc_num = rc_num;
  5702. out:
  5703. return ret;
  5704. }
  5705. int cnss_pci_init(struct cnss_plat_data *plat_priv)
  5706. {
  5707. struct device *dev = &plat_priv->plat_dev->dev;
  5708. const __be32 *prop;
  5709. int ret = 0, prop_len = 0, rc_count, i;
  5710. prop = of_get_property(dev->of_node, "qcom,wlan-rc-num", &prop_len);
  5711. if (!prop || !prop_len) {
  5712. cnss_pr_err("Failed to get PCIe RC number from DT\n");
  5713. goto out;
  5714. }
  5715. rc_count = prop_len / sizeof(__be32);
  5716. for (i = 0; i < rc_count; i++) {
  5717. ret = cnss_pci_enumerate(plat_priv, be32_to_cpup(&prop[i]));
  5718. if (!ret)
  5719. break;
  5720. else if (ret == -EPROBE_DEFER || (ret && i == rc_count - 1))
  5721. goto out;
  5722. }
  5723. ret = cnss_try_suspend(plat_priv);
  5724. if (ret) {
  5725. cnss_pr_err("Failed to suspend, ret: %d\n", ret);
  5726. goto out;
  5727. }
  5728. if (!cnss_driver_registered) {
  5729. ret = pci_register_driver(&cnss_pci_driver);
  5730. if (ret) {
  5731. cnss_pr_err("Failed to register to PCI framework, err = %d\n",
  5732. ret);
  5733. goto out;
  5734. }
  5735. if (!plat_priv->bus_priv) {
  5736. cnss_pr_err("Failed to probe PCI driver\n");
  5737. ret = -ENODEV;
  5738. goto unreg_pci;
  5739. }
  5740. cnss_driver_registered = true;
  5741. }
  5742. return 0;
  5743. unreg_pci:
  5744. pci_unregister_driver(&cnss_pci_driver);
  5745. out:
  5746. return ret;
  5747. }
  5748. void cnss_pci_deinit(struct cnss_plat_data *plat_priv)
  5749. {
  5750. if (cnss_driver_registered) {
  5751. pci_unregister_driver(&cnss_pci_driver);
  5752. cnss_driver_registered = false;
  5753. }
  5754. }