pci.c 167 KB

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