dhd_sdio.c 292 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141
  1. /*
  2. * DHD Bus Module for SDIO
  3. *
  4. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  5. *
  6. * Copyright (C) 1999-2020, Broadcom Corporation
  7. *
  8. * Unless you and Broadcom execute a separate written software license
  9. * agreement governing use of this software, this software is licensed to you
  10. * under the terms of the GNU General Public License version 2 (the "GPL"),
  11. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  12. * following added to such license:
  13. *
  14. * As a special exception, the copyright holders of this software give you
  15. * permission to link this software with independent modules, and to copy and
  16. * distribute the resulting executable under terms of your choice, provided that
  17. * you also meet, for each linked independent module, the terms and conditions of
  18. * the license of that module. An independent module is a module which is not
  19. * derived from this software. The special exception does not apply to any
  20. * modifications of the software.
  21. *
  22. * Notwithstanding the above, under no circumstances may you combine this
  23. * software in any way with any other Broadcom software provided under a license
  24. * other than the GPL, without Broadcom's express prior written consent.
  25. *
  26. *
  27. * <<Broadcom-WL-IPTag/Open:>>
  28. *
  29. * $Id: dhd_sdio.c 701844 2017-05-26 18:56:40Z $
  30. */
  31. #include <typedefs.h>
  32. #include <osl.h>
  33. #include <bcmsdh.h>
  34. #include <bcmdefs.h>
  35. #include <bcmutils.h>
  36. #include <bcmendian.h>
  37. #include <bcmdevs.h>
  38. #include <siutils.h>
  39. #include <hndpmu.h>
  40. #include <hndsoc.h>
  41. #include <bcmsdpcm.h>
  42. #include <hnd_armtrap.h>
  43. #include <hnd_cons.h>
  44. #include <sbchipc.h>
  45. #include <sbhnddma.h>
  46. #include <sdio.h>
  47. #ifdef BCMSPI
  48. #include <spid.h>
  49. #endif /* BCMSPI */
  50. #include <sbsdio.h>
  51. #include <sbsdpcmdev.h>
  52. #include <bcmsdpcm.h>
  53. #include <bcmsdbus.h>
  54. #include <ethernet.h>
  55. #include <802.1d.h>
  56. #include <802.11.h>
  57. #include <dngl_stats.h>
  58. #include <dhd.h>
  59. #include <dhd_bus.h>
  60. #include <dhd_proto.h>
  61. #include <dhd_dbg.h>
  62. #include <dhdioctl.h>
  63. #include <sdiovar.h>
  64. #ifdef PROP_TXSTATUS
  65. #include <dhd_wlfc.h>
  66. #endif // endif
  67. #ifdef DHDTCPACK_SUPPRESS
  68. #include <dhd_ip.h>
  69. #endif /* DHDTCPACK_SUPPRESS */
  70. #ifdef BT_OVER_SDIO
  71. #include <dhd_bt_interface.h>
  72. #endif /* BT_OVER_SDIO */
  73. #if defined(DEBUGGER) || defined(DHD_DSCOPE)
  74. #include <debugger.h>
  75. #endif /* DEBUGGER || DHD_DSCOPE */
  76. bool dhd_mp_halting(dhd_pub_t *dhdp);
  77. extern void bcmsdh_waitfor_iodrain(void *sdh);
  78. extern void bcmsdh_reject_ioreqs(void *sdh, bool reject);
  79. extern bool bcmsdh_fatal_error(void *sdh);
  80. static int dhdsdio_suspend(void *context);
  81. static int dhdsdio_resume(void *context);
  82. #ifndef DHDSDIO_MEM_DUMP_FNAME
  83. #define DHDSDIO_MEM_DUMP_FNAME "mem_dump"
  84. #endif // endif
  85. #define QLEN (1024) /* bulk rx and tx queue lengths */
  86. #define FCHI (QLEN - 10)
  87. #define FCLOW (FCHI / 2)
  88. #define PRIOMASK 7
  89. #define F0_BLOCK_SIZE 32
  90. #define TXRETRIES 2 /* # of retries for tx frames */
  91. #define READ_FRM_CNT_RETRIES 3
  92. #ifndef DHD_RXBOUND
  93. #define DHD_RXBOUND 50 /* Default for max rx frames in one scheduling */
  94. #endif // endif
  95. #ifndef DHD_TXBOUND
  96. #define DHD_TXBOUND 20 /* Default for max tx frames in one scheduling */
  97. #endif // endif
  98. #define DHD_TXMINMAX 1 /* Max tx frames if rx still pending */
  99. #define MEMBLOCK 2048 /* Block size used for downloading of dongle image */
  100. #define MAX_MEMBLOCK (32 * 1024) /* Block size used for downloading of dongle image */
  101. #define MAX_DATA_BUF (64 * 1024) /* Must be large enough to hold biggest possible glom */
  102. #ifndef DHD_FIRSTREAD
  103. #define DHD_FIRSTREAD 32
  104. #endif // endif
  105. #if !ISPOWEROF2(DHD_FIRSTREAD)
  106. #error DHD_FIRSTREAD is not a power of 2!
  107. #endif // endif
  108. /* Total length of frame header for dongle protocol */
  109. #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
  110. #define SDPCM_HDRLEN_TXGLOM (SDPCM_HDRLEN + SDPCM_HWEXT_LEN)
  111. #define MAX_TX_PKTCHAIN_CNT SDPCM_MAXGLOM_SIZE
  112. #ifdef SDTEST
  113. #define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN)
  114. #else
  115. #define SDPCM_RESERVE (SDPCM_HDRLEN + DHD_SDALIGN)
  116. #endif // endif
  117. /* Space for header read, limit for data packets */
  118. #ifndef MAX_HDR_READ
  119. #define MAX_HDR_READ 32
  120. #endif // endif
  121. #if !ISPOWEROF2(MAX_HDR_READ)
  122. #error MAX_HDR_READ is not a power of 2!
  123. #endif // endif
  124. #define MAX_RX_DATASZ 2048
  125. #define SDIO_FUNC_BLOCK_SIZE_SHIFT 16
  126. #define F2_BLOCK_SIZE_256 256
  127. /* Maximum milliseconds to wait for F2 to come up */
  128. #define DHD_WAIT_F2RDY 3000
  129. /* Maximum usec to wait for HTAVAIL to come up */
  130. #define DHD_WAIT_HTAVAIL 10000
  131. /* Bump up limit on waiting for HT to account for first startup;
  132. * if the image is doing a CRC calculation before programming the PMU
  133. * for HT availability, it could take a couple hundred ms more, so
  134. * max out at a 1 second (1000000us).
  135. */
  136. #if (PMU_MAX_TRANSITION_DLY <= 1000000)
  137. #undef PMU_MAX_TRANSITION_DLY
  138. #define PMU_MAX_TRANSITION_DLY 1000000
  139. #endif // endif
  140. /* hooks for limiting threshold custom tx num in rx processing */
  141. #define DEFAULT_TXINRX_THRES 0
  142. #ifndef CUSTOM_TXINRX_THRES
  143. #define CUSTOM_TXINRX_THRES DEFAULT_TXINRX_THRES
  144. #endif // endif
  145. /* Value for ChipClockCSR during initial setup */
  146. #define DHD_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ)
  147. #define DHD_INIT_CLKCTL2 (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP)
  148. /* Flags for SDH calls */
  149. #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
  150. /* Packet free applicable unconditionally for sdio and sdspi. Conditional if
  151. * bufpool was present for gspi bus.
  152. */
  153. #define PKTFREE2() if ((bus->bus != SPI_BUS) || bus->usebufpool) \
  154. PKTFREE(bus->dhd->osh, pkt, FALSE);
  155. DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
  156. #if defined(MULTIPLE_SUPPLICANT)
  157. DEFINE_MUTEX(_dhd_sdio_mutex_lock_);
  158. #endif // endif
  159. #ifdef SUPPORT_MULTIPLE_BOARD_REV_FROM_HW
  160. extern unsigned int system_hw_rev;
  161. #endif /* SUPPORT_MULTIPLE_BOARD_REV_FROM_HW */
  162. /* Device console log buffer state */
  163. #define CONSOLE_LINE_MAX 192
  164. #define CONSOLE_BUFFER_MAX 2024
  165. typedef struct dhd_console {
  166. uint count; /* Poll interval msec counter */
  167. uint log_addr; /* Log struct address (fixed) */
  168. hnd_log_t log; /* Log struct (host copy) */
  169. uint bufsize; /* Size of log buffer */
  170. uint8 *buf; /* Log buffer (host copy) */
  171. uint last; /* Last buffer read index */
  172. } dhd_console_t;
  173. #define REMAP_ENAB(bus) ((bus)->remap)
  174. #define REMAP_ISADDR(bus, a) (((a) >= ((bus)->orig_ramsize)) && ((a) < ((bus)->ramsize)))
  175. #define KSO_ENAB(bus) ((bus)->kso)
  176. #define SR_ENAB(bus) ((bus)->_srenab)
  177. #define SLPAUTO_ENAB(bus) ((SR_ENAB(bus)) && ((bus)->_slpauto))
  178. #define MIN_RSRC_SR 0x3
  179. #define CORE_CAPEXT_ADDR_OFFSET (0x64c)
  180. #define CORE_CAPEXT_SR_SUPPORTED_MASK (1 << 1)
  181. #define RCTL_MACPHY_DISABLE_MASK (1 << 26)
  182. #define RCTL_LOGIC_DISABLE_MASK (1 << 27)
  183. #define OOB_WAKEUP_ENAB(bus) ((bus)->_oobwakeup)
  184. #define GPIO_DEV_SRSTATE 16 /* Host gpio17 mapped to device gpio0 SR state */
  185. #define GPIO_DEV_SRSTATE_TIMEOUT 320000 /* 320ms */
  186. #define GPIO_DEV_WAKEUP 17 /* Host gpio17 mapped to device gpio1 wakeup */
  187. #define CC_CHIPCTRL2_GPIO1_WAKEUP (1 << 0)
  188. #define CC_CHIPCTRL3_SR_ENG_ENABLE (1 << 2)
  189. #define OVERFLOW_BLKSZ512_WM 96
  190. #define OVERFLOW_BLKSZ512_MES 80
  191. #define CC_PMUCC3 (0x3)
  192. #ifdef DHD_UCODE_DOWNLOAD
  193. /* Ucode host download related macros */
  194. #define UCODE_DOWNLOAD_REQUEST 0xCAFECAFE
  195. #define UCODE_DOWNLOAD_COMPLETE 0xABCDABCD
  196. #endif /* DHD_UCODE_DOWNLOAD */
  197. #if defined(BT_OVER_SDIO)
  198. #define BTMEM_OFFSET 0x19000000
  199. /* BIT0 => WLAN Power UP and BIT1=> WLAN Wake */
  200. #define BT2WLAN_PWRUP_WAKE 0x03
  201. #define BT2WLAN_PWRUP_ADDR 0x640894 /* This address is specific to 43012B0 */
  202. #define BTFW_MAX_STR_LEN 600
  203. #define BTFW_DOWNLOAD_BLK_SIZE (BTFW_MAX_STR_LEN/2 + 8)
  204. #define BTFW_ADDR_MODE_UNKNOWN 0
  205. #define BTFW_ADDR_MODE_EXTENDED 1
  206. #define BTFW_ADDR_MODE_SEGMENT 2
  207. #define BTFW_ADDR_MODE_LINEAR32 3
  208. #define BTFW_HEX_LINE_TYPE_DATA 0
  209. #define BTFW_HEX_LINE_TYPE_END_OF_DATA 1
  210. #define BTFW_HEX_LINE_TYPE_EXTENDED_SEGMENT_ADDRESS 2
  211. #define BTFW_HEX_LINE_TYPE_EXTENDED_ADDRESS 4
  212. #define BTFW_HEX_LINE_TYPE_ABSOLUTE_32BIT_ADDRESS 5
  213. #endif /* defined (BT_OVER_SDIO) */
  214. /* Private data for SDIO bus interaction */
  215. typedef struct dhd_bus {
  216. dhd_pub_t *dhd;
  217. bcmsdh_info_t *sdh; /* Handle for BCMSDH calls */
  218. si_t *sih; /* Handle for SI calls */
  219. char *vars; /* Variables (from CIS and/or other) */
  220. uint varsz; /* Size of variables buffer */
  221. uint32 sbaddr; /* Current SB window pointer (-1, invalid) */
  222. sdpcmd_regs_t *regs; /* Registers for SDIO core */
  223. uint sdpcmrev; /* SDIO core revision */
  224. uint armrev; /* CPU core revision */
  225. uint ramrev; /* SOCRAM core revision */
  226. uint32 ramsize; /* Size of RAM in SOCRAM (bytes) */
  227. uint32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
  228. uint32 srmemsize; /* Size of SRMEM */
  229. uint32 bus; /* gSPI or SDIO bus */
  230. uint32 bus_num; /* bus number */
  231. uint32 slot_num; /* slot ID */
  232. uint32 hostintmask; /* Copy of Host Interrupt Mask */
  233. uint32 intstatus; /* Intstatus bits (events) pending */
  234. bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
  235. bool fcstate; /* State of dongle flow-control */
  236. uint16 cl_devid; /* cached devid for dhdsdio_probe_attach() */
  237. char *fw_path; /* module_param: path to firmware image */
  238. char *nv_path; /* module_param: path to nvram vars file */
  239. uint blocksize; /* Block size of SDIO transfers */
  240. uint roundup; /* Max roundup limit */
  241. struct pktq txq; /* Queue length used for flow-control */
  242. uint8 flowcontrol; /* per prio flow control bitmask */
  243. uint8 tx_seq; /* Transmit sequence number (next) */
  244. uint8 tx_max; /* Maximum transmit sequence allowed */
  245. uint8 hdrbuf[MAX_HDR_READ + DHD_SDALIGN];
  246. uint8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
  247. uint16 nextlen; /* Next Read Len from last header */
  248. uint8 rx_seq; /* Receive sequence number (expected) */
  249. bool rxskip; /* Skip receive (awaiting NAK ACK) */
  250. void *glomd; /* Packet containing glomming descriptor */
  251. void *glom; /* Packet chain for glommed superframe */
  252. uint glomerr; /* Glom packet read errors */
  253. uint8 *rxbuf; /* Buffer for receiving control packets */
  254. uint rxblen; /* Allocated length of rxbuf */
  255. uint8 *rxctl; /* Aligned pointer into rxbuf */
  256. uint8 *databuf; /* Buffer for receiving big glom packet */
  257. uint8 *dataptr; /* Aligned pointer into databuf */
  258. uint rxlen; /* Length of valid data in buffer */
  259. uint8 sdpcm_ver; /* Bus protocol reported by dongle */
  260. bool intr; /* Use interrupts */
  261. bool poll; /* Use polling */
  262. bool ipend; /* Device interrupt is pending */
  263. bool intdis; /* Interrupts disabled by isr */
  264. uint intrcount; /* Count of device interrupt callbacks */
  265. uint lastintrs; /* Count as of last watchdog timer */
  266. uint spurious; /* Count of spurious interrupts */
  267. uint pollrate; /* Ticks between device polls */
  268. uint polltick; /* Tick counter */
  269. uint pollcnt; /* Count of active polls */
  270. dhd_console_t console; /* Console output polling support */
  271. uint console_addr; /* Console address from shared struct */
  272. uint regfails; /* Count of R_REG/W_REG failures */
  273. uint clkstate; /* State of sd and backplane clock(s) */
  274. bool activity; /* Activity flag for clock down */
  275. int32 idletime; /* Control for activity timeout */
  276. int32 idlecount; /* Activity timeout counter */
  277. int32 idleclock; /* How to set bus driver when idle */
  278. int32 sd_divisor; /* Speed control to bus driver */
  279. int32 sd_mode; /* Mode control to bus driver */
  280. int32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
  281. bool use_rxchain; /* If dhd should use PKT chains */
  282. bool sleeping; /* Is SDIO bus sleeping? */
  283. #if defined(SUPPORT_P2P_GO_PS)
  284. wait_queue_head_t bus_sleep;
  285. #endif /* LINUX && SUPPORT_P2P_GO_PS */
  286. uint rxflow_mode; /* Rx flow control mode */
  287. bool rxflow; /* Is rx flow control on */
  288. uint prev_rxlim_hit; /* Is prev rx limit exceeded (per dpc schedule) */
  289. bool alp_only; /* Don't use HT clock (ALP only) */
  290. /* Field to decide if rx of control frames happen in rxbuf or lb-pool */
  291. bool usebufpool;
  292. int32 txinrx_thres; /* num of in-queued pkts */
  293. int32 dotxinrx; /* tx first in dhdsdio_readframes */
  294. #ifdef SDTEST
  295. /* external loopback */
  296. bool ext_loop;
  297. uint8 loopid;
  298. /* pktgen configuration */
  299. uint pktgen_freq; /* Ticks between bursts */
  300. uint pktgen_count; /* Packets to send each burst */
  301. uint pktgen_print; /* Bursts between count displays */
  302. uint pktgen_total; /* Stop after this many */
  303. uint pktgen_minlen; /* Minimum packet data len */
  304. uint pktgen_maxlen; /* Maximum packet data len */
  305. uint pktgen_mode; /* Configured mode: tx, rx, or echo */
  306. uint pktgen_stop; /* Number of tx failures causing stop */
  307. /* active pktgen fields */
  308. uint pktgen_tick; /* Tick counter for bursts */
  309. uint pktgen_ptick; /* Burst counter for printing */
  310. uint pktgen_sent; /* Number of test packets generated */
  311. uint pktgen_rcvd; /* Number of test packets received */
  312. uint pktgen_prev_time; /* Time at which previous stats where printed */
  313. uint pktgen_prev_sent; /* Number of test packets generated when
  314. * previous stats were printed
  315. */
  316. uint pktgen_prev_rcvd; /* Number of test packets received when
  317. * previous stats were printed
  318. */
  319. uint pktgen_fail; /* Number of failed send attempts */
  320. uint16 pktgen_len; /* Length of next packet to send */
  321. #define PKTGEN_RCV_IDLE (0)
  322. #define PKTGEN_RCV_ONGOING (1)
  323. uint16 pktgen_rcv_state; /* receive state */
  324. uint pktgen_rcvd_rcvsession; /* test pkts rcvd per rcv session. */
  325. #endif /* SDTEST */
  326. /* Some additional counters */
  327. uint tx_sderrs; /* Count of tx attempts with sd errors */
  328. uint fcqueued; /* Tx packets that got queued */
  329. uint rxrtx; /* Count of rtx requests (NAK to dongle) */
  330. uint rx_toolong; /* Receive frames too long to receive */
  331. uint rxc_errors; /* SDIO errors when reading control frames */
  332. uint rx_hdrfail; /* SDIO errors on header reads */
  333. uint rx_badhdr; /* Bad received headers (roosync?) */
  334. uint rx_badseq; /* Mismatched rx sequence number */
  335. uint fc_rcvd; /* Number of flow-control events received */
  336. uint fc_xoff; /* Number which turned on flow-control */
  337. uint fc_xon; /* Number which turned off flow-control */
  338. uint rxglomfail; /* Failed deglom attempts */
  339. uint rxglomframes; /* Number of glom frames (superframes) */
  340. uint rxglompkts; /* Number of packets from glom frames */
  341. uint f2rxhdrs; /* Number of header reads */
  342. uint f2rxdata; /* Number of frame data reads */
  343. uint f2txdata; /* Number of f2 frame writes */
  344. uint f1regdata; /* Number of f1 register accesses */
  345. wake_counts_t wake_counts; /* Wake up counter */
  346. #ifdef BCMSPI
  347. bool dwordmode;
  348. #endif /* BCMSPI */
  349. #ifdef DHDENABLE_TAILPAD
  350. uint tx_tailpad_chain; /* Number of tail padding by chaining pad_pkt */
  351. uint tx_tailpad_pktget; /* Number of tail padding by new PKTGET */
  352. #endif /* DHDENABLE_TAILPAD */
  353. uint8 *ctrl_frame_buf;
  354. uint32 ctrl_frame_len;
  355. bool ctrl_frame_stat;
  356. #ifndef BCMSPI
  357. uint32 rxint_mode; /* rx interrupt mode */
  358. #endif /* BCMSPI */
  359. bool remap; /* Contiguous 1MB RAM: 512K socram + 512K devram
  360. * Available with socram rev 16
  361. * Remap region not DMA-able
  362. */
  363. bool kso;
  364. bool _slpauto;
  365. bool _oobwakeup;
  366. bool _srenab;
  367. bool readframes;
  368. bool reqbussleep;
  369. uint32 resetinstr;
  370. uint32 dongle_ram_base;
  371. void *glom_pkt_arr[SDPCM_MAXGLOM_SIZE]; /* Array of pkts for glomming */
  372. uint32 txglom_cnt; /* Number of pkts in the glom array */
  373. uint32 txglom_total_len; /* Total length of pkts in glom array */
  374. bool txglom_enable; /* Flag to indicate whether tx glom is enabled/disabled */
  375. uint32 txglomsize; /* Glom size limitation */
  376. #ifdef DHDENABLE_TAILPAD
  377. void *pad_pkt;
  378. #endif /* DHDENABLE_TAILPAD */
  379. uint32 dongle_trap_addr; /* device trap addr location in device memory */
  380. #if defined(BT_OVER_SDIO)
  381. char *btfw_path; /* module_param: path to BT firmware image */
  382. uint32 bt_use_count; /* Counter that tracks whether BT is using the bus */
  383. #endif /* defined (BT_OVER_SDIO) */
  384. } dhd_bus_t;
  385. /*
  386. * Whenever DHD_IDLE_IMMEDIATE condition is handled, we have to now check if
  387. * BT is active too. Instead of adding #ifdef code in all the places, we thought
  388. * of adding one macro check as part of the if condition that checks for DHD_IDLE_IMMEDIATE
  389. * In case of non BT over SDIO builds, this macro will always return TRUE. In case
  390. * of the builds where BT_OVER_SDIO is enabled, it will expand to a condition check
  391. * that checks if bt_use_count is zero. So this macro will return equate to 1 if
  392. * bt_use_count is 0, indicating that there are no active users and if bt_use_count
  393. * is non zero it would return 0 there by preventing the caller from executing the
  394. * sleep calls.
  395. */
  396. #ifdef BT_OVER_SDIO
  397. #define NO_OTHER_ACTIVE_BUS_USER(bus) (bus->bt_use_count == 0)
  398. #else
  399. #define NO_OTHER_ACTIVE_BUS_USER(bus) (1)
  400. #endif /* BT_OVER_SDIO */
  401. /* clkstate */
  402. #define CLK_NONE 0
  403. #define CLK_SDONLY 1
  404. #define CLK_PENDING 2 /* Not used yet */
  405. #define CLK_AVAIL 3
  406. #define DHD_NOPMU(dhd) (FALSE)
  407. #if defined(BCMSDIOH_STD)
  408. #define BLK_64_MAXTXGLOM 20
  409. #endif /* BCMSDIOH_STD */
  410. #ifdef DHD_DEBUG
  411. static int qcount[NUMPRIO];
  412. static int tx_packets[NUMPRIO];
  413. #endif /* DHD_DEBUG */
  414. /* Deferred transmit */
  415. const uint dhd_deferred_tx = 1;
  416. extern uint dhd_watchdog_ms;
  417. //extern uint sd_f1_blocksize;
  418. uint sd_f1_blocksize;//add 20210511
  419. #ifdef BCMSPI_ANDROID
  420. extern uint *dhd_spi_lockcount;
  421. #endif /* BCMSPI_ANDROID */
  422. extern void dhd_os_wd_timer(void *bus, uint wdtick);
  423. int dhd_enableOOB(dhd_pub_t *dhd, bool sleep);
  424. #ifdef DHD_PM_CONTROL_FROM_FILE
  425. extern bool g_pm_control;
  426. #endif /* DHD_PM_CONTROL_FROM_FILE */
  427. /* Tx/Rx bounds */
  428. uint dhd_txbound;
  429. uint dhd_rxbound;
  430. uint dhd_txminmax = DHD_TXMINMAX;
  431. /* override the RAM size if possible */
  432. #define DONGLE_MIN_RAMSIZE (128 *1024)
  433. int dhd_dongle_ramsize;
  434. uint dhd_doflow = TRUE;
  435. uint dhd_dpcpoll = FALSE;
  436. module_param(dhd_doflow, uint, 0644);
  437. module_param(dhd_dpcpoll, uint, 0644);
  438. static bool dhd_alignctl;
  439. static bool sd1idle;
  440. static bool retrydata;
  441. #define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
  442. #ifdef BCMSPI
  443. /* At a watermark around 8 the spid hits underflow error. */
  444. static uint watermark = 32;
  445. static uint mesbusyctrl = 0;
  446. #else
  447. static uint watermark = 8;
  448. static uint mesbusyctrl = 0;
  449. #endif /* BCMSPI */
  450. static const uint firstread = DHD_FIRSTREAD;
  451. /* Retry count for register access failures */
  452. static const uint retry_limit = 2;
  453. /* Force even SD lengths (some host controllers mess up on odd bytes) */
  454. static bool forcealign;
  455. #if defined(DEBUGGER)
  456. static uint32 dhd_sdio_reg_read(struct dhd_bus *bus, ulong addr);
  457. static void dhd_sdio_reg_write(struct dhd_bus *bus, ulong addr, uint32 val);
  458. /** the debugger layer will call back into this (bus) layer to read/write dongle memory */
  459. static struct dhd_dbg_bus_ops_s bus_ops = {
  460. .read_u16 = NULL,
  461. .read_u32 = dhd_sdio_reg_read,
  462. .write_u32 = dhd_sdio_reg_write,
  463. };
  464. #endif /* DEBUGGER */
  465. #define ALIGNMENT 4
  466. #if defined(OOB_INTR_ONLY) && defined(HW_OOB)
  467. extern void bcmsdh_enable_hw_oob_intr(void *sdh, bool enable);
  468. #endif // endif
  469. #if defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD)
  470. #error OOB_INTR_ONLY is NOT working with SDIO_ISR_THREAD
  471. #endif /* defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD) */
  472. #define PKTALIGN(osh, p, len, align) \
  473. do { \
  474. uintptr datalign; \
  475. datalign = (uintptr)PKTDATA((osh), (p)); \
  476. datalign = ROUNDUP(datalign, (align)) - datalign; \
  477. ASSERT(datalign < (align)); \
  478. ASSERT(PKTLEN((osh), (p)) >= ((len) + datalign)); \
  479. if (datalign) \
  480. PKTPULL((osh), (p), (uint)datalign); \
  481. PKTSETLEN((osh), (p), (len)); \
  482. } while (0)
  483. /* Limit on rounding up frames */
  484. static const uint max_roundup = 512;
  485. /* Try doing readahead */
  486. static bool dhd_readahead;
  487. /* To check if there's window offered */
  488. #define DATAOK(bus) \
  489. (((uint8)(bus->tx_max - bus->tx_seq) > 1) && \
  490. (((uint8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
  491. /* To check if there's window offered for ctrl frame */
  492. #define TXCTLOK(bus) \
  493. (((uint8)(bus->tx_max - bus->tx_seq) != 0) && \
  494. (((uint8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
  495. /* Number of pkts available in dongle for data RX */
  496. #define DATABUFCNT(bus) \
  497. ((uint8)(bus->tx_max - bus->tx_seq) - 1)
  498. /* Macros to get register read/write status */
  499. /* NOTE: these assume a local dhdsdio_bus_t *bus! */
  500. #define R_SDREG(regvar, regaddr, retryvar) \
  501. do { \
  502. retryvar = 0; \
  503. do { \
  504. regvar = R_REG(bus->dhd->osh, regaddr); \
  505. } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
  506. if (retryvar) { \
  507. bus->regfails += (retryvar-1); \
  508. if (retryvar > retry_limit) { \
  509. DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
  510. __FUNCTION__, __LINE__)); \
  511. regvar = 0; \
  512. } \
  513. } \
  514. } while (0)
  515. #define W_SDREG(regval, regaddr, retryvar) \
  516. do { \
  517. retryvar = 0; \
  518. do { \
  519. W_REG(bus->dhd->osh, regaddr, regval); \
  520. } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
  521. if (retryvar) { \
  522. bus->regfails += (retryvar-1); \
  523. if (retryvar > retry_limit) \
  524. DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
  525. __FUNCTION__, __LINE__)); \
  526. } \
  527. } while (0)
  528. #define BUS_WAKE(bus) \
  529. do { \
  530. bus->idlecount = 0; \
  531. if ((bus)->sleeping) \
  532. dhdsdio_bussleep((bus), FALSE); \
  533. } while (0);
  534. /*
  535. * pktavail interrupts from dongle to host can be managed in 3 different ways
  536. * whenever there is a packet available in dongle to transmit to host.
  537. *
  538. * Mode 0: Dongle writes the software host mailbox and host is interrupted.
  539. * Mode 1: (sdiod core rev >= 4)
  540. * Device sets a new bit in the intstatus whenever there is a packet
  541. * available in fifo. Host can't clear this specific status bit until all the
  542. * packets are read from the FIFO. No need to ack dongle intstatus.
  543. * Mode 2: (sdiod core rev >= 4)
  544. * Device sets a bit in the intstatus, and host acks this by writing
  545. * one to this bit. Dongle won't generate anymore packet interrupts
  546. * until host reads all the packets from the dongle and reads a zero to
  547. * figure that there are no more packets. No need to disable host ints.
  548. * Need to ack the intstatus.
  549. */
  550. #define SDIO_DEVICE_HMB_RXINT 0 /* default old way */
  551. #define SDIO_DEVICE_RXDATAINT_MODE_0 1 /* from sdiod rev 4 */
  552. #define SDIO_DEVICE_RXDATAINT_MODE_1 2 /* from sdiod rev 4 */
  553. #ifdef BCMSPI
  554. #define FRAME_AVAIL_MASK(bus) I_HMB_FRAME_IND
  555. #define DHD_BUS SPI_BUS
  556. /* check packet-available-interrupt in piggybacked dstatus */
  557. #define PKT_AVAILABLE(bus, intstatus) (bcmsdh_get_dstatus(bus->sdh) & STATUS_F2_PKT_AVAILABLE)
  558. #define HOSTINTMASK (I_HMB_FC_CHANGE | I_HMB_HOST_INT)
  559. #define GSPI_PR55150_BAILOUT \
  560. do { \
  561. uint32 dstatussw = bcmsdh_get_dstatus((void *)bus->sdh); \
  562. uint32 dstatushw = bcmsdh_cfg_read_word(bus->sdh, SDIO_FUNC_0, SPID_STATUS_REG, NULL); \
  563. uint32 intstatuserr = 0; \
  564. uint retries = 0; \
  565. \
  566. R_SDREG(intstatuserr, &bus->regs->intstatus, retries); \
  567. printf("dstatussw = 0x%x, dstatushw = 0x%x, intstatus = 0x%x\n", \
  568. dstatussw, dstatushw, intstatuserr); \
  569. \
  570. bus->nextlen = 0; \
  571. *finished = TRUE; \
  572. } while (0)
  573. #else /* BCMSDIO */
  574. #define FRAME_AVAIL_MASK(bus) \
  575. ((bus->rxint_mode == SDIO_DEVICE_HMB_RXINT) ? I_HMB_FRAME_IND : I_XMTDATA_AVAIL)
  576. #define DHD_BUS SDIO_BUS
  577. #define PKT_AVAILABLE(bus, intstatus) ((intstatus) & (FRAME_AVAIL_MASK(bus)))
  578. #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
  579. #define GSPI_PR55150_BAILOUT
  580. #endif /* BCMSPI */
  581. #ifdef SDTEST
  582. static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq);
  583. static void dhdsdio_sdtest_set(dhd_bus_t *bus, uint count);
  584. #endif // endif
  585. static int dhdsdio_checkdied(dhd_bus_t *bus, char *data, uint size);
  586. #ifdef DHD_DEBUG
  587. static int dhd_serialconsole(dhd_bus_t *bus, bool get, bool enable, int *bcmerror);
  588. #endif /* DHD_DEBUG */
  589. #if defined(DHD_FW_COREDUMP)
  590. static int dhdsdio_mem_dump(dhd_bus_t *bus);
  591. static int dhdsdio_get_mem_dump(dhd_bus_t *bus);
  592. #endif /* DHD_FW_COREDUMP */
  593. static int dhdsdio_devcap_set(dhd_bus_t *bus, uint8 cap);
  594. static int dhdsdio_download_state(dhd_bus_t *bus, bool enter);
  595. static void dhdsdio_release(dhd_bus_t *bus, osl_t *osh);
  596. static void dhdsdio_release_malloc(dhd_bus_t *bus, osl_t *osh);
  597. static void dhdsdio_disconnect(void *ptr);
  598. static bool dhdsdio_chipmatch(uint16 chipid);
  599. static bool dhdsdio_probe_attach(dhd_bus_t *bus, osl_t *osh, void *sdh,
  600. void * regsva, uint16 devid);
  601. static bool dhdsdio_probe_malloc(dhd_bus_t *bus, osl_t *osh, void *sdh);
  602. static bool dhdsdio_probe_init(dhd_bus_t *bus, osl_t *osh, void *sdh);
  603. static void dhdsdio_release_dongle(dhd_bus_t *bus, osl_t *osh, bool dongle_isolation,
  604. bool reset_flag);
  605. static void dhd_dongle_setramsize(struct dhd_bus *bus, int mem_size);
  606. static int dhd_bcmsdh_recv_buf(dhd_bus_t *bus, uint32 addr, uint fn, uint flags,
  607. uint8 *buf, uint nbytes,
  608. void *pkt, bcmsdh_cmplt_fn_t complete, void *handle);
  609. static int dhd_bcmsdh_send_buf(dhd_bus_t *bus, uint32 addr, uint fn, uint flags,
  610. uint8 *buf, uint nbytes,
  611. void *pkt, bcmsdh_cmplt_fn_t complete, void *handle, int max_retry);
  612. static int dhdsdio_txpkt(dhd_bus_t *bus, uint chan, void** pkts, int num_pkt, bool free_pkt);
  613. static int dhdsdio_txpkt_preprocess(dhd_bus_t *bus, void *pkt, int chan, int txseq,
  614. int prev_chain_total_len, bool last_chained_pkt,
  615. int *pad_pkt_len, void **new_pkt);
  616. static int dhdsdio_txpkt_postprocess(dhd_bus_t *bus, void *pkt);
  617. static int dhdsdio_download_firmware(dhd_bus_t *bus, osl_t *osh, void *sdh);
  618. static int _dhdsdio_download_firmware(dhd_bus_t *bus);
  619. #ifdef DHD_UCODE_DOWNLOAD
  620. static int dhdsdio_download_ucode_file(struct dhd_bus *bus, char *ucode_path);
  621. #endif /* DHD_UCODE_DOWNLOAD */
  622. static int dhdsdio_download_code_file(dhd_bus_t *bus, char *image_path);
  623. static int dhdsdio_download_nvram(dhd_bus_t *bus);
  624. static int dhdsdio_bussleep(dhd_bus_t *bus, bool sleep);
  625. static int dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok);
  626. static uint8 dhdsdio_sleepcsr_get(dhd_bus_t *bus);
  627. static bool dhdsdio_dpc(dhd_bus_t *bus);
  628. static int dhd_bcmsdh_send_buffer(void *bus, uint8 *frame, uint16 len);
  629. static int dhdsdio_set_sdmode(dhd_bus_t *bus, int32 sd_mode);
  630. static int dhdsdio_sdclk(dhd_bus_t *bus, bool on);
  631. static void dhdsdio_advertise_bus_cleanup(dhd_pub_t *dhdp);
  632. #if defined(BT_OVER_SDIO)
  633. static int extract_hex_field(char * line, uint16 start_pos, uint16 num_chars, uint16 * value);
  634. static int read_more_btbytes(struct dhd_bus *bus, void * file, char *line, int * addr_mode,
  635. uint16 * hi_addr, uint32 * dest_addr, uint8 *data_bytes, uint32 * num_bytes);
  636. static int dhdsdio_download_btfw(struct dhd_bus *bus, osl_t *osh, void *sdh);
  637. static int _dhdsdio_download_btfw(struct dhd_bus *bus);
  638. #endif /* defined (BT_OVER_SDIO) */
  639. #ifdef DHD_ULP
  640. #include <dhd_ulp.h>
  641. static int dhd_bus_ulp_reinit_fw(dhd_bus_t *bus);
  642. #endif /* DHD_ULP */
  643. static void
  644. dhdsdio_tune_fifoparam(struct dhd_bus *bus)
  645. {
  646. int err;
  647. uint8 devctl, wm, mes;
  648. if (bus->sih->buscorerev >= 15) {
  649. /* See .ppt in PR for these recommended values */
  650. if (bus->blocksize == 512) {
  651. wm = OVERFLOW_BLKSZ512_WM;
  652. mes = OVERFLOW_BLKSZ512_MES;
  653. } else {
  654. mes = bus->blocksize/4;
  655. wm = bus->blocksize/4;
  656. }
  657. watermark = wm;
  658. mesbusyctrl = mes;
  659. } else {
  660. DHD_INFO(("skip fifotune: SdioRev(%d) is lower than minimal requested ver\n",
  661. bus->sih->buscorerev));
  662. return;
  663. }
  664. /* Update watermark */
  665. if (wm > 0) {
  666. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK, wm, &err);
  667. devctl = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
  668. devctl |= SBSDIO_DEVCTL_F2WM_ENAB;
  669. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, devctl, &err);
  670. }
  671. /* Update MES */
  672. if (mes > 0) {
  673. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_MESBUSYCTRL,
  674. (mes | SBSDIO_MESBUSYCTRL_ENAB), &err);
  675. }
  676. DHD_INFO(("Apply overflow WAR: 0x%02x 0x%02x 0x%02x\n",
  677. bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err),
  678. bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK, &err),
  679. bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_MESBUSYCTRL, &err)));
  680. }
  681. static void
  682. dhd_dongle_setramsize(struct dhd_bus *bus, int mem_size)
  683. {
  684. int32 min_size = DONGLE_MIN_RAMSIZE;
  685. /* Restrict the ramsize to user specified limit */
  686. DHD_ERROR(("user: Restrict the dongle ram size to %d, min accepted %d\n",
  687. dhd_dongle_ramsize, min_size));
  688. if ((dhd_dongle_ramsize > min_size) &&
  689. (dhd_dongle_ramsize < (int32)bus->orig_ramsize))
  690. bus->ramsize = dhd_dongle_ramsize;
  691. }
  692. static int
  693. dhdsdio_set_siaddr_window(dhd_bus_t *bus, uint32 address)
  694. {
  695. int err = 0;
  696. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
  697. (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
  698. if (!err)
  699. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
  700. (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
  701. if (!err)
  702. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
  703. (address >> 24) & SBSDIO_SBADDRHIGH_MASK, &err);
  704. return err;
  705. }
  706. #ifdef BCMSPI
  707. static void
  708. dhdsdio_wkwlan(dhd_bus_t *bus, bool on)
  709. {
  710. int err;
  711. uint32 regdata;
  712. bcmsdh_info_t *sdh = bus->sdh;
  713. if (bus->sih->buscoretype == SDIOD_CORE_ID) {
  714. /* wake up wlan function :WAKE_UP goes as ht_avail_request and alp_avail_request */
  715. regdata = bcmsdh_cfg_read_word(sdh, SDIO_FUNC_0, SPID_CONFIG, NULL);
  716. DHD_INFO(("F0 REG0 rd = 0x%x\n", regdata));
  717. if (on == TRUE)
  718. regdata |= WAKE_UP;
  719. else
  720. regdata &= ~WAKE_UP;
  721. bcmsdh_cfg_write_word(sdh, SDIO_FUNC_0, SPID_CONFIG, regdata, &err);
  722. }
  723. }
  724. #endif /* BCMSPI */
  725. #ifdef USE_OOB_GPIO1
  726. static int
  727. dhdsdio_oobwakeup_init(dhd_bus_t *bus)
  728. {
  729. uint32 val, addr, data;
  730. bcmsdh_gpioouten(bus->sdh, GPIO_DEV_WAKEUP);
  731. addr = SI_ENUM_BASE(bus->sih) + OFFSETOF(chipcregs_t, chipcontrol_addr);
  732. data = SI_ENUM_BASE(bus->sih) + OFFSETOF(chipcregs_t, chipcontrol_data);
  733. /* Set device for gpio1 wakeup */
  734. bcmsdh_reg_write(bus->sdh, addr, 4, 2);
  735. val = bcmsdh_reg_read(bus->sdh, data, 4);
  736. val |= CC_CHIPCTRL2_GPIO1_WAKEUP;
  737. bcmsdh_reg_write(bus->sdh, data, 4, val);
  738. bus->_oobwakeup = TRUE;
  739. return 0;
  740. }
  741. #endif /* USE_OOB_GPIO1 */
  742. #ifndef BCMSPI
  743. /*
  744. * Query if FW is in SR mode
  745. */
  746. static bool
  747. dhdsdio_sr_cap(dhd_bus_t *bus)
  748. {
  749. bool cap = FALSE;
  750. uint32 core_capext, addr, data;
  751. if (bus->sih->chip == BCM43430_CHIP_ID ||
  752. bus->sih->chip == BCM43018_CHIP_ID) {
  753. /* check if fw initialized sr engine */
  754. addr = SI_ENUM_BASE(bus->sih) + OFFSETOF(chipcregs_t, sr_control1);
  755. if (bcmsdh_reg_read(bus->sdh, addr, 4) != 0)
  756. cap = TRUE;
  757. return cap;
  758. }
  759. if (
  760. 0) {
  761. core_capext = FALSE;
  762. } else if ((bus->sih->chip == BCM4335_CHIP_ID) ||
  763. (bus->sih->chip == BCM4339_CHIP_ID) ||
  764. BCM4345_CHIP(bus->sih->chip) ||
  765. (bus->sih->chip == BCM4354_CHIP_ID) ||
  766. (bus->sih->chip == BCM4358_CHIP_ID) ||
  767. (BCM4349_CHIP(bus->sih->chip)) ||
  768. (bus->sih->chip == BCM4350_CHIP_ID) ||
  769. (bus->sih->chip == BCM4362_CHIP_ID) ||
  770. (bus->sih->chip == BCM43012_CHIP_ID) ||
  771. (bus->sih->chip == BCM43014_CHIP_ID) ||
  772. (bus->sih->chip == BCM43751_CHIP_ID) ||
  773. (bus->sih->chip == BCM4373_CHIP_ID)) {
  774. core_capext = TRUE;
  775. } else {
  776. core_capext = bcmsdh_reg_read(bus->sdh,
  777. si_get_pmu_reg_addr(bus->sih, OFFSETOF(chipcregs_t, core_cap_ext)),
  778. 4);
  779. core_capext = (core_capext & CORE_CAPEXT_SR_SUPPORTED_MASK);
  780. }
  781. if (!(core_capext))
  782. return FALSE;
  783. if ((bus->sih->chip == BCM4335_CHIP_ID) ||
  784. (bus->sih->chip == BCM4339_CHIP_ID) ||
  785. BCM4345_CHIP(bus->sih->chip) ||
  786. (bus->sih->chip == BCM4354_CHIP_ID) ||
  787. (bus->sih->chip == BCM4358_CHIP_ID) ||
  788. (bus->sih->chip == BCM4350_CHIP_ID)) {
  789. uint32 enabval = 0;
  790. addr = SI_ENUM_BASE(bus->sih) + OFFSETOF(chipcregs_t, chipcontrol_addr);
  791. data = SI_ENUM_BASE(bus->sih) + OFFSETOF(chipcregs_t, chipcontrol_data);
  792. bcmsdh_reg_write(bus->sdh, addr, 4, CC_PMUCC3);
  793. enabval = bcmsdh_reg_read(bus->sdh, data, 4);
  794. if ((bus->sih->chip == BCM4350_CHIP_ID) ||
  795. BCM4345_CHIP(bus->sih->chip) ||
  796. (bus->sih->chip == BCM4354_CHIP_ID) ||
  797. (bus->sih->chip == BCM4358_CHIP_ID))
  798. enabval &= CC_CHIPCTRL3_SR_ENG_ENABLE;
  799. if (enabval)
  800. cap = TRUE;
  801. } else {
  802. data = bcmsdh_reg_read(bus->sdh,
  803. si_get_pmu_reg_addr(bus->sih, OFFSETOF(chipcregs_t, retention_ctl)),
  804. 4);
  805. if ((data & (RCTL_MACPHY_DISABLE_MASK | RCTL_LOGIC_DISABLE_MASK)) == 0)
  806. cap = TRUE;
  807. }
  808. return cap;
  809. }
  810. static int
  811. dhdsdio_sr_init(dhd_bus_t *bus)
  812. {
  813. uint8 val;
  814. int err = 0;
  815. if (bus->sih->chip == BCM43012_CHIP_ID) {
  816. val = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_WAKEUPCTRL, NULL);
  817. val |= 1 << SBSDIO_FUNC1_WCTRL_ALPWAIT_SHIFT;
  818. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_WAKEUPCTRL,
  819. 1 << SBSDIO_FUNC1_WCTRL_ALPWAIT_SHIFT, &err);
  820. val = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_WAKEUPCTRL, NULL);
  821. } else {
  822. val = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_WAKEUPCTRL, NULL);
  823. val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
  824. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_WAKEUPCTRL,
  825. 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT, &err);
  826. val = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_WAKEUPCTRL, NULL);
  827. }
  828. #ifdef USE_CMD14
  829. /* Add CMD14 Support */
  830. dhdsdio_devcap_set(bus,
  831. (SDIOD_CCCR_BRCM_CARDCAP_CMD14_SUPPORT | SDIOD_CCCR_BRCM_CARDCAP_CMD14_EXT));
  832. #endif /* USE_CMD14 */
  833. if (CHIPID(bus->sih->chip) == BCM43430_CHIP_ID ||
  834. CHIPID(bus->sih->chip) == BCM43018_CHIP_ID ||
  835. CHIPID(bus->sih->chip) == BCM4339_CHIP_ID ||
  836. CHIPID(bus->sih->chip) == BCM43012_CHIP_ID ||
  837. CHIPID(bus->sih->chip) == BCM4362_CHIP_ID ||
  838. CHIPID(bus->sih->chip) == BCM43014_CHIP_ID ||
  839. CHIPID(bus->sih->chip) == BCM43751_CHIP_ID)
  840. dhdsdio_devcap_set(bus, SDIOD_CCCR_BRCM_CARDCAP_CMD_NODEC);
  841. if (bus->sih->chip == BCM43012_CHIP_ID) {
  842. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
  843. SBSDIO_FUNC1_CHIPCLKCSR, SBSDIO_HT_AVAIL_REQ, &err);
  844. } else {
  845. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
  846. SBSDIO_FUNC1_CHIPCLKCSR, SBSDIO_FORCE_HT, &err);
  847. }
  848. bus->_slpauto = dhd_slpauto ? TRUE : FALSE;
  849. bus->_srenab = TRUE;
  850. return 0;
  851. }
  852. #endif /* BCMSPI */
  853. /*
  854. * FIX: Be sure KSO bit is enabled
  855. * Currently, it's defaulting to 0 which should be 1.
  856. */
  857. static int
  858. dhdsdio_clk_kso_init(dhd_bus_t *bus)
  859. {
  860. uint8 val;
  861. int err = 0;
  862. /* set flag */
  863. bus->kso = TRUE;
  864. /*
  865. * Enable KeepSdioOn (KSO) bit for normal operation
  866. * Default is 0 (4334A0) so set it. Fixed in B0.
  867. */
  868. val = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SLEEPCSR, NULL);
  869. if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
  870. val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
  871. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SLEEPCSR, val, &err);
  872. if (err)
  873. DHD_ERROR(("%s: SBSDIO_FUNC1_SLEEPCSR err: 0x%x\n", __FUNCTION__, err));
  874. }
  875. return 0;
  876. }
  877. #define KSO_DBG(x)
  878. #define KSO_WAIT_US 50
  879. #define KSO_WAIT_MS 1
  880. #define KSO_SLEEP_RETRY_COUNT 20
  881. #define KSO_WAKE_RETRY_COUNT 100
  882. #define ERROR_BCME_NODEVICE_MAX 1
  883. #define DEFAULT_MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
  884. #ifndef CUSTOM_MAX_KSO_ATTEMPTS
  885. #define CUSTOM_MAX_KSO_ATTEMPTS DEFAULT_MAX_KSO_ATTEMPTS
  886. #endif // endif
  887. static int
  888. dhdsdio_clk_kso_enab(dhd_bus_t *bus, bool on)
  889. {
  890. uint8 wr_val = 0, rd_val, cmp_val, bmask;
  891. int err = 0;
  892. int try_cnt = 0;
  893. KSO_DBG(("%s> op:%s\n", __FUNCTION__, (on ? "KSO_SET" : "KSO_CLR")));
  894. wr_val |= (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
  895. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SLEEPCSR, wr_val, &err);
  896. /* In case of 43012 chip, the chip could go down immediately after KSO bit is cleared.
  897. * So the further reads of KSO register could fail. Thereby just bailing out immediately
  898. * after clearing KSO bit, to avoid polling of KSO bit.
  899. */
  900. if ((!on) && (bus->sih->chip == BCM43012_CHIP_ID)) {
  901. return err;
  902. }
  903. if (on) {
  904. cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK | SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
  905. bmask = cmp_val;
  906. OSL_SLEEP(3);
  907. } else {
  908. /* Put device to sleep, turn off KSO */
  909. cmp_val = 0;
  910. bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
  911. }
  912. do {
  913. rd_val = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SLEEPCSR, &err);
  914. if (((rd_val & bmask) == cmp_val) && !err)
  915. break;
  916. KSO_DBG(("%s> KSO wr/rd retry:%d, ERR:%x \n", __FUNCTION__, try_cnt, err));
  917. if (((try_cnt + 1) % KSO_SLEEP_RETRY_COUNT) == 0) {
  918. OSL_SLEEP(KSO_WAIT_MS);
  919. } else
  920. OSL_DELAY(KSO_WAIT_US);
  921. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SLEEPCSR, wr_val, &err);
  922. } while (try_cnt++ < CUSTOM_MAX_KSO_ATTEMPTS);
  923. if (try_cnt > 2)
  924. KSO_DBG(("%s> op:%s, try_cnt:%d, rd_val:%x, ERR:%x \n",
  925. __FUNCTION__, (on ? "KSO_SET" : "KSO_CLR"), try_cnt, rd_val, err));
  926. if (try_cnt > CUSTOM_MAX_KSO_ATTEMPTS) {
  927. DHD_ERROR(("%s> op:%s, ERROR: try_cnt:%d, rd_val:%x, ERR:%x \n",
  928. __FUNCTION__, (on ? "KSO_SET" : "KSO_CLR"), try_cnt, rd_val, err));
  929. }
  930. return err;
  931. }
  932. static int
  933. dhdsdio_clk_kso_iovar(dhd_bus_t *bus, bool on)
  934. {
  935. int err = 0;
  936. if (on == FALSE) {
  937. BUS_WAKE(bus);
  938. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  939. DHD_ERROR(("%s: KSO disable clk: 0x%x\n", __FUNCTION__,
  940. bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
  941. SBSDIO_FUNC1_CHIPCLKCSR, &err)));
  942. dhdsdio_clk_kso_enab(bus, FALSE);
  943. } else {
  944. DHD_ERROR(("%s: KSO enable\n", __FUNCTION__));
  945. /* Make sure we have SD bus access */
  946. if (bus->clkstate == CLK_NONE) {
  947. DHD_ERROR(("%s: Request SD clk\n", __FUNCTION__));
  948. dhdsdio_clkctl(bus, CLK_SDONLY, FALSE);
  949. }
  950. dhdsdio_clk_kso_enab(bus, TRUE);
  951. DHD_ERROR(("%s: sleepcsr: 0x%x\n", __FUNCTION__,
  952. dhdsdio_sleepcsr_get(bus)));
  953. }
  954. bus->kso = on;
  955. BCM_REFERENCE(err);
  956. return 0;
  957. }
  958. static uint8
  959. dhdsdio_sleepcsr_get(dhd_bus_t *bus)
  960. {
  961. int err = 0;
  962. uint8 val = 0;
  963. val = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SLEEPCSR, &err);
  964. if (err)
  965. DHD_TRACE(("Failed to read SLEEPCSR: %d\n", err));
  966. return val;
  967. }
  968. uint8
  969. dhdsdio_devcap_get(dhd_bus_t *bus)
  970. {
  971. return bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_BRCM_CARDCAP, NULL);
  972. }
  973. static int
  974. dhdsdio_devcap_set(dhd_bus_t *bus, uint8 cap)
  975. {
  976. int err = 0;
  977. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_BRCM_CARDCAP, cap, &err);
  978. if (err)
  979. DHD_ERROR(("%s: devcap set err: 0x%x\n", __FUNCTION__, err));
  980. return 0;
  981. }
  982. static int
  983. dhdsdio_clk_devsleep_iovar(dhd_bus_t *bus, bool on)
  984. {
  985. int err = 0, retry;
  986. uint8 val;
  987. retry = 0;
  988. if (on == TRUE) {
  989. /* Enter Sleep */
  990. /* Be sure we request clk before going to sleep
  991. * so we can wake-up with clk request already set
  992. * else device can go back to sleep immediately
  993. */
  994. if (!SLPAUTO_ENAB(bus))
  995. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  996. else {
  997. val = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, &err);
  998. if ((val & SBSDIO_CSR_MASK) == 0) {
  999. DHD_ERROR(("%s: No clock before enter sleep:0x%x\n",
  1000. __FUNCTION__, val));
  1001. /* Reset clock request */
  1002. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
  1003. SBSDIO_ALP_AVAIL_REQ, &err);
  1004. DHD_ERROR(("%s: clock before sleep:0x%x\n", __FUNCTION__,
  1005. bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
  1006. SBSDIO_FUNC1_CHIPCLKCSR, &err)));
  1007. }
  1008. }
  1009. DHD_TRACE(("%s: clk before sleep: 0x%x\n", __FUNCTION__,
  1010. bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
  1011. SBSDIO_FUNC1_CHIPCLKCSR, &err)));
  1012. #ifdef USE_CMD14
  1013. err = bcmsdh_sleep(bus->sdh, TRUE);
  1014. #else
  1015. if ((SLPAUTO_ENAB(bus)) && (bus->idleclock == DHD_IDLE_STOP)) {
  1016. if (sd1idle) {
  1017. /* Change to SD1 mode */
  1018. dhdsdio_set_sdmode(bus, 1);
  1019. }
  1020. }
  1021. err = dhdsdio_clk_kso_enab(bus, FALSE);
  1022. if (OOB_WAKEUP_ENAB(bus))
  1023. {
  1024. err = bcmsdh_gpioout(bus->sdh, GPIO_DEV_WAKEUP, FALSE); /* GPIO_1 is off */
  1025. }
  1026. #endif /* USE_CMD14 */
  1027. if ((SLPAUTO_ENAB(bus)) && (bus->idleclock != DHD_IDLE_ACTIVE)) {
  1028. DHD_TRACE(("%s: Turnoff SD clk\n", __FUNCTION__));
  1029. /* Now remove the SD clock */
  1030. err = dhdsdio_sdclk(bus, FALSE);
  1031. }
  1032. } else {
  1033. /* Exit Sleep */
  1034. /* Make sure we have SD bus access */
  1035. if (bus->clkstate == CLK_NONE) {
  1036. DHD_TRACE(("%s: Request SD clk\n", __FUNCTION__));
  1037. dhdsdio_clkctl(bus, CLK_SDONLY, FALSE);
  1038. }
  1039. #ifdef USE_CMD14
  1040. err = bcmsdh_sleep(bus->sdh, FALSE);
  1041. if (SLPAUTO_ENAB(bus) && (err != 0)) {
  1042. OSL_DELAY(10000);
  1043. DHD_TRACE(("%s: Resync device sleep\n", __FUNCTION__));
  1044. /* Toggle sleep to resync with host and device */
  1045. err = bcmsdh_sleep(bus->sdh, TRUE);
  1046. OSL_DELAY(10000);
  1047. err = bcmsdh_sleep(bus->sdh, FALSE);
  1048. if (err) {
  1049. OSL_DELAY(10000);
  1050. DHD_ERROR(("%s: CMD14 exit failed again!\n", __FUNCTION__));
  1051. /* Toggle sleep to resync with host and device */
  1052. err = bcmsdh_sleep(bus->sdh, TRUE);
  1053. OSL_DELAY(10000);
  1054. err = bcmsdh_sleep(bus->sdh, FALSE);
  1055. if (err) {
  1056. DHD_ERROR(("%s: CMD14 exit failed twice!\n", __FUNCTION__));
  1057. DHD_ERROR(("%s: FATAL: Device non-response!\n",
  1058. __FUNCTION__));
  1059. err = 0;
  1060. }
  1061. }
  1062. }
  1063. #else
  1064. if (OOB_WAKEUP_ENAB(bus))
  1065. {
  1066. err = bcmsdh_gpioout(bus->sdh, GPIO_DEV_WAKEUP, TRUE); /* GPIO_1 is on */
  1067. }
  1068. do {
  1069. err = dhdsdio_clk_kso_enab(bus, TRUE);
  1070. if (err)
  1071. OSL_SLEEP(10);
  1072. } while ((err != 0) && (++retry < 3));
  1073. if (err != 0) {
  1074. DHD_ERROR(("ERROR: kso set failed retry: %d\n", retry));
  1075. #ifndef BT_OVER_SDIO
  1076. err = 0; /* continue anyway */
  1077. #endif /* BT_OVER_SDIO */
  1078. }
  1079. if ((SLPAUTO_ENAB(bus)) && (bus->idleclock == DHD_IDLE_STOP)) {
  1080. dhdsdio_set_sdmode(bus, bus->sd_mode);
  1081. }
  1082. #endif /* !USE_CMD14 */
  1083. if (err == 0) {
  1084. uint8 csr;
  1085. /* Wait for device ready during transition to wake-up */
  1086. SPINWAIT_SLEEP(sdioh_spinwait_sleep,
  1087. (((csr = dhdsdio_sleepcsr_get(bus)) &
  1088. SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK) !=
  1089. (SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK)), (20000));
  1090. DHD_TRACE(("%s: ExitSleep sleepcsr: 0x%x\n", __FUNCTION__, csr));
  1091. if (!(csr & SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK)) {
  1092. DHD_ERROR(("%s:ERROR: ExitSleep device NOT Ready! 0x%x\n",
  1093. __FUNCTION__, csr));
  1094. err = BCME_NODEVICE;
  1095. }
  1096. SPINWAIT_SLEEP(sdioh_spinwait_sleep,
  1097. (((csr = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
  1098. SBSDIO_FUNC1_CHIPCLKCSR, &err)) & SBSDIO_HT_AVAIL) !=
  1099. (SBSDIO_HT_AVAIL)), (DHD_WAIT_HTAVAIL));
  1100. DHD_TRACE(("%s: SBSDIO_FUNC1_CHIPCLKCSR : 0x%x\n", __FUNCTION__, csr));
  1101. if (!err && ((csr & SBSDIO_HT_AVAIL) != SBSDIO_HT_AVAIL)) {
  1102. DHD_ERROR(("%s:ERROR: device NOT Ready! 0x%x\n",
  1103. __FUNCTION__, csr));
  1104. err = BCME_NODEVICE;
  1105. }
  1106. }
  1107. }
  1108. /* Update if successful */
  1109. if (err == 0)
  1110. bus->kso = on ? FALSE : TRUE;
  1111. else {
  1112. DHD_ERROR(("%s: Sleep request failed: kso:%d on:%d err:%d\n",
  1113. __FUNCTION__, bus->kso, on, err));
  1114. if (!on && retry > 2)
  1115. bus->kso = FALSE;
  1116. }
  1117. return err;
  1118. }
  1119. /* Turn backplane clock on or off */
  1120. static int
  1121. dhdsdio_htclk(dhd_bus_t *bus, bool on, bool pendok)
  1122. {
  1123. #define HT_AVAIL_ERROR_MAX 10
  1124. static int ht_avail_error = 0;
  1125. int err;
  1126. uint8 clkctl, clkreq, devctl;
  1127. bcmsdh_info_t *sdh;
  1128. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  1129. clkctl = 0;
  1130. sdh = bus->sdh;
  1131. if (!KSO_ENAB(bus))
  1132. return BCME_OK;
  1133. if (SLPAUTO_ENAB(bus)) {
  1134. bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
  1135. return BCME_OK;
  1136. }
  1137. if (on) {
  1138. /* Request HT Avail */
  1139. clkreq = bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
  1140. #ifdef BCMSPI
  1141. dhdsdio_wkwlan(bus, TRUE);
  1142. #endif /* BCMSPI */
  1143. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err);
  1144. if (err) {
  1145. ht_avail_error++;
  1146. if (ht_avail_error < HT_AVAIL_ERROR_MAX) {
  1147. DHD_ERROR(("%s: HT Avail request error: %d\n", __FUNCTION__, err));
  1148. }
  1149. #ifdef OEM_ANDROID
  1150. else if (ht_avail_error == HT_AVAIL_ERROR_MAX) {
  1151. bus->dhd->hang_reason = HANG_REASON_HT_AVAIL_ERROR;
  1152. dhd_os_send_hang_message(bus->dhd);
  1153. }
  1154. #endif /* OEM_ANDROID */
  1155. return BCME_ERROR;
  1156. } else {
  1157. ht_avail_error = 0;
  1158. }
  1159. /* Check current status */
  1160. clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, &err);
  1161. if (err) {
  1162. DHD_ERROR(("%s: HT Avail read error: %d\n", __FUNCTION__, err));
  1163. return BCME_ERROR;
  1164. }
  1165. #if !defined(OOB_INTR_ONLY)
  1166. /* Go to pending and await interrupt if appropriate */
  1167. if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
  1168. /* Allow only clock-available interrupt */
  1169. devctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
  1170. if (err) {
  1171. DHD_ERROR(("%s: Devctl access error setting CA: %d\n",
  1172. __FUNCTION__, err));
  1173. return BCME_ERROR;
  1174. }
  1175. devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
  1176. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, devctl, &err);
  1177. DHD_INFO(("CLKCTL: set PENDING\n"));
  1178. bus->clkstate = CLK_PENDING;
  1179. return BCME_OK;
  1180. } else
  1181. #endif /* !defined (OOB_INTR_ONLY) */
  1182. {
  1183. if (bus->clkstate == CLK_PENDING) {
  1184. /* Cancel CA-only interrupt filter */
  1185. devctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
  1186. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  1187. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, devctl, &err);
  1188. }
  1189. }
  1190. #ifndef BCMSDIOLITE
  1191. /* Otherwise, wait here (polling) for HT Avail */
  1192. if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
  1193. SPINWAIT_SLEEP(sdioh_spinwait_sleep,
  1194. ((clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
  1195. SBSDIO_FUNC1_CHIPCLKCSR, &err)),
  1196. !SBSDIO_CLKAV(clkctl, bus->alp_only)), PMU_MAX_TRANSITION_DLY);
  1197. }
  1198. if (err) {
  1199. DHD_ERROR(("%s: HT Avail request error: %d\n", __FUNCTION__, err));
  1200. return BCME_ERROR;
  1201. }
  1202. if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
  1203. DHD_ERROR(("%s: HT Avail timeout (%d): clkctl 0x%02x\n",
  1204. __FUNCTION__, PMU_MAX_TRANSITION_DLY, clkctl));
  1205. return BCME_ERROR;
  1206. }
  1207. #endif /* BCMSDIOLITE */
  1208. /* Mark clock available */
  1209. bus->clkstate = CLK_AVAIL;
  1210. DHD_INFO(("CLKCTL: turned ON\n"));
  1211. #if defined(DHD_DEBUG)
  1212. if (bus->alp_only == TRUE) {
  1213. #if !defined(BCMLXSDMMC)
  1214. if (!SBSDIO_ALPONLY(clkctl)) {
  1215. DHD_ERROR(("%s: HT Clock, when ALP Only\n", __FUNCTION__));
  1216. }
  1217. #endif /* !defined(BCMLXSDMMC) */
  1218. } else {
  1219. if (SBSDIO_ALPONLY(clkctl)) {
  1220. DHD_ERROR(("%s: HT Clock should be on.\n", __FUNCTION__));
  1221. }
  1222. }
  1223. #endif /* defined (DHD_DEBUG) */
  1224. bus->activity = TRUE;
  1225. #ifdef DHD_USE_IDLECOUNT
  1226. bus->idlecount = 0;
  1227. #endif /* DHD_USE_IDLECOUNT */
  1228. } else {
  1229. clkreq = 0;
  1230. if (bus->clkstate == CLK_PENDING) {
  1231. /* Cancel CA-only interrupt filter */
  1232. devctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
  1233. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  1234. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, devctl, &err);
  1235. }
  1236. bus->clkstate = CLK_SDONLY;
  1237. if (!SR_ENAB(bus)) {
  1238. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err);
  1239. DHD_INFO(("CLKCTL: turned OFF\n"));
  1240. if (err) {
  1241. DHD_ERROR(("%s: Failed access turning clock off: %d\n",
  1242. __FUNCTION__, err));
  1243. return BCME_ERROR;
  1244. }
  1245. }
  1246. #ifdef BCMSPI
  1247. dhdsdio_wkwlan(bus, FALSE);
  1248. #endif /* BCMSPI */
  1249. }
  1250. return BCME_OK;
  1251. }
  1252. /* Change SD1/SD4 bus mode */
  1253. static int
  1254. dhdsdio_set_sdmode(dhd_bus_t *bus, int32 sd_mode)
  1255. {
  1256. int err;
  1257. err = bcmsdh_iovar_op(bus->sdh, "sd_mode", NULL, 0,
  1258. &sd_mode, sizeof(sd_mode), TRUE);
  1259. if (err) {
  1260. DHD_ERROR(("%s: error changing sd_mode: %d\n",
  1261. __FUNCTION__, err));
  1262. return BCME_ERROR;
  1263. }
  1264. return BCME_OK;
  1265. }
  1266. /* Change idle/active SD state */
  1267. static int
  1268. dhdsdio_sdclk(dhd_bus_t *bus, bool on)
  1269. {
  1270. #ifndef BCMSPI
  1271. int err;
  1272. int32 iovalue;
  1273. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  1274. if (on) {
  1275. if (bus->idleclock == DHD_IDLE_STOP) {
  1276. /* Turn on clock and restore mode */
  1277. iovalue = 1;
  1278. err = bcmsdh_iovar_op(bus->sdh, "sd_clock", NULL, 0,
  1279. &iovalue, sizeof(iovalue), TRUE);
  1280. if (err) {
  1281. DHD_ERROR(("%s: error enabling sd_clock: %d\n",
  1282. __FUNCTION__, err));
  1283. return BCME_ERROR;
  1284. }
  1285. } else if (bus->idleclock != DHD_IDLE_ACTIVE) {
  1286. /* Restore clock speed */
  1287. iovalue = bus->sd_divisor;
  1288. err = bcmsdh_iovar_op(bus->sdh, "sd_divisor", NULL, 0,
  1289. &iovalue, sizeof(iovalue), TRUE);
  1290. if (err) {
  1291. DHD_ERROR(("%s: error restoring sd_divisor: %d\n",
  1292. __FUNCTION__, err));
  1293. return BCME_ERROR;
  1294. }
  1295. }
  1296. bus->clkstate = CLK_SDONLY;
  1297. } else {
  1298. /* Stop or slow the SD clock itself */
  1299. if ((bus->sd_divisor == -1) || (bus->sd_mode == -1)) {
  1300. DHD_TRACE(("%s: can't idle clock, divisor %d mode %d\n",
  1301. __FUNCTION__, bus->sd_divisor, bus->sd_mode));
  1302. return BCME_ERROR;
  1303. }
  1304. if (bus->idleclock == DHD_IDLE_STOP) {
  1305. iovalue = 0;
  1306. err = bcmsdh_iovar_op(bus->sdh, "sd_clock", NULL, 0,
  1307. &iovalue, sizeof(iovalue), TRUE);
  1308. if (err) {
  1309. DHD_ERROR(("%s: error disabling sd_clock: %d\n",
  1310. __FUNCTION__, err));
  1311. return BCME_ERROR;
  1312. }
  1313. } else if (bus->idleclock != DHD_IDLE_ACTIVE) {
  1314. /* Set divisor to idle value */
  1315. iovalue = bus->idleclock;
  1316. err = bcmsdh_iovar_op(bus->sdh, "sd_divisor", NULL, 0,
  1317. &iovalue, sizeof(iovalue), TRUE);
  1318. if (err) {
  1319. DHD_ERROR(("%s: error changing sd_divisor: %d\n",
  1320. __FUNCTION__, err));
  1321. return BCME_ERROR;
  1322. }
  1323. }
  1324. bus->clkstate = CLK_NONE;
  1325. }
  1326. #endif /* BCMSPI */
  1327. return BCME_OK;
  1328. }
  1329. /* Transition SD and backplane clock readiness */
  1330. static int
  1331. dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok)
  1332. {
  1333. int ret = BCME_OK;
  1334. #ifdef DHD_DEBUG
  1335. uint oldstate = bus->clkstate;
  1336. #endif /* DHD_DEBUG */
  1337. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  1338. /* Early exit if we're already there */
  1339. if (bus->clkstate == target) {
  1340. if (target == CLK_AVAIL) {
  1341. dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
  1342. bus->activity = TRUE;
  1343. #ifdef DHD_USE_IDLECOUNT
  1344. bus->idlecount = 0;
  1345. #endif /* DHD_USE_IDLECOUNT */
  1346. }
  1347. return ret;
  1348. }
  1349. switch (target) {
  1350. case CLK_AVAIL:
  1351. /* Make sure SD clock is available */
  1352. if (bus->clkstate == CLK_NONE)
  1353. dhdsdio_sdclk(bus, TRUE);
  1354. /* Now request HT Avail on the backplane */
  1355. ret = dhdsdio_htclk(bus, TRUE, pendok);
  1356. if (ret == BCME_OK) {
  1357. dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
  1358. bus->activity = TRUE;
  1359. #ifdef DHD_USE_IDLECOUNT
  1360. bus->idlecount = 0;
  1361. #endif /* DHD_USE_IDLECOUNT */
  1362. }
  1363. break;
  1364. case CLK_SDONLY:
  1365. #ifdef BT_OVER_SDIO
  1366. /*
  1367. * If the request is to switch off Back plane clock,
  1368. * confirm that BT is inactive before doing so.
  1369. * If this call had come from Non Watchdog context any way
  1370. * the Watchdog would switch off the clock again when
  1371. * nothing is to be done & Bt has finished using the bus.
  1372. */
  1373. if (bus->bt_use_count != 0) {
  1374. DHD_INFO(("%s(): Req CLK_SDONLY, BT is active %d not switching off \r\n",
  1375. __FUNCTION__, bus->bt_use_count));
  1376. ret = BCME_OK;
  1377. dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
  1378. break;
  1379. }
  1380. DHD_INFO(("%s(): Request CLK_NONE BT is NOT active switching off \r\n",
  1381. __FUNCTION__));
  1382. #endif /* BT_OVER_SDIO */
  1383. /* Remove HT request, or bring up SD clock */
  1384. if (bus->clkstate == CLK_NONE)
  1385. ret = dhdsdio_sdclk(bus, TRUE);
  1386. else if (bus->clkstate == CLK_AVAIL)
  1387. ret = dhdsdio_htclk(bus, FALSE, FALSE);
  1388. else
  1389. DHD_ERROR(("dhdsdio_clkctl: request for %d -> %d\n",
  1390. bus->clkstate, target));
  1391. if (ret == BCME_OK) {
  1392. dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
  1393. }
  1394. break;
  1395. case CLK_NONE:
  1396. #ifdef BT_OVER_SDIO
  1397. /*
  1398. * If the request is to switch off Back plane clock,
  1399. * confirm that BT is inactive before doing so.
  1400. * If this call had come from Non Watchdog context any way
  1401. * the Watchdog would switch off the clock again when
  1402. * nothing is to be done & Bt has finished using the bus.
  1403. */
  1404. if (bus->bt_use_count != 0) {
  1405. DHD_INFO(("%s(): Request CLK_NONE BT is active %d not switching off \r\n",
  1406. __FUNCTION__, bus->bt_use_count));
  1407. ret = BCME_OK;
  1408. break;
  1409. }
  1410. DHD_INFO(("%s(): Request CLK_NONE BT is NOT active switching off \r\n",
  1411. __FUNCTION__));
  1412. #endif /* BT_OVER_SDIO */
  1413. /* Make sure to remove HT request */
  1414. if (bus->clkstate == CLK_AVAIL)
  1415. ret = dhdsdio_htclk(bus, FALSE, FALSE);
  1416. /* Now remove the SD clock */
  1417. ret = dhdsdio_sdclk(bus, FALSE);
  1418. #ifdef DHD_DEBUG
  1419. if (bus->dhd->dhd_console_ms == 0)
  1420. #endif /* DHD_DEBUG */
  1421. if (bus->poll == 0)
  1422. dhd_os_wd_timer(bus->dhd, 0);
  1423. break;
  1424. }
  1425. #ifdef DHD_DEBUG
  1426. DHD_INFO(("dhdsdio_clkctl: %d -> %d\n", oldstate, bus->clkstate));
  1427. #endif /* DHD_DEBUG */
  1428. return ret;
  1429. }
  1430. static int
  1431. dhdsdio_bussleep(dhd_bus_t *bus, bool sleep)
  1432. {
  1433. int err = 0;
  1434. bcmsdh_info_t *sdh = bus->sdh;
  1435. sdpcmd_regs_t *regs = bus->regs;
  1436. uint retries = 0;
  1437. #if defined(BCMSDIOH_STD)
  1438. uint32 sd3_tuning_disable = FALSE;
  1439. #endif /* BCMSDIOH_STD */
  1440. DHD_INFO(("dhdsdio_bussleep: request %s (currently %s)\n",
  1441. (sleep ? "SLEEP" : "WAKE"),
  1442. (bus->sleeping ? "SLEEP" : "WAKE")));
  1443. if (bus->dhd->hang_was_sent)
  1444. return BCME_ERROR;
  1445. /* Done if we're already in the requested state */
  1446. if (sleep == bus->sleeping)
  1447. return BCME_OK;
  1448. /* Going to sleep: set the alarm and turn off the lights... */
  1449. if (sleep) {
  1450. /* Don't sleep if something is pending */
  1451. #ifdef DHD_USE_IDLECOUNT
  1452. if (bus->dpc_sched || bus->rxskip || pktq_n_pkts_tot(&bus->txq) ||
  1453. bus->readframes || bus->ctrl_frame_stat)
  1454. #else
  1455. if (bus->dpc_sched || bus->rxskip || pktq_n_pkts_tot(&bus->txq))
  1456. #endif /* DHD_USE_IDLECOUNT */
  1457. return BCME_BUSY;
  1458. #ifdef BT_OVER_SDIO
  1459. /*
  1460. * The following is the assumption based on which the hook is placed.
  1461. * From WLAN driver, either from the active contexts OR from the Watchdog contexts
  1462. * we will be attempting to Go to Sleep. AT that moment if we see that BT is still
  1463. * actively using the bus, we will return BCME_BUSY from here, but the bus->sleeping
  1464. * state would not have changed. So the caller can then schedule the Watchdog again
  1465. * which will come and attempt to sleep at a later point.
  1466. *
  1467. * In case if BT is the only one and is the last user, we don't switch off the clock
  1468. * immediately, we allow the WLAN to decide when to sleep i.e from the watchdog.
  1469. * Now if the watchdog becomes active and attempts to switch off the clock and if
  1470. * another WLAN context is active they are any way serialized with sdlock.
  1471. */
  1472. if (bus->bt_use_count != 0) {
  1473. DHD_INFO(("%s(): Cannot sleep BT is active \r\n", __FUNCTION__));
  1474. return BCME_BUSY;
  1475. }
  1476. #endif /* !BT_OVER_SDIO */
  1477. if (!SLPAUTO_ENAB(bus)) {
  1478. /* Disable SDIO interrupts (no longer interested) */
  1479. bcmsdh_intr_disable(bus->sdh);
  1480. /* Make sure the controller has the bus up */
  1481. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  1482. /* Tell device to start using OOB wakeup */
  1483. W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
  1484. if (retries > retry_limit)
  1485. DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
  1486. /* Turn off our contribution to the HT clock request */
  1487. dhdsdio_clkctl(bus, CLK_SDONLY, FALSE);
  1488. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
  1489. SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
  1490. /* Isolate the bus */
  1491. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
  1492. SBSDIO_DEVCTL_PADS_ISO, NULL);
  1493. } else {
  1494. /* Leave interrupts enabled since device can exit sleep and
  1495. * interrupt host
  1496. */
  1497. err = dhdsdio_clk_devsleep_iovar(bus, TRUE /* sleep */);
  1498. }
  1499. /* Change state */
  1500. bus->sleeping = TRUE;
  1501. #if defined(BCMSDIOH_STD)
  1502. sd3_tuning_disable = TRUE;
  1503. err = bcmsdh_iovar_op(bus->sdh, "sd3_tuning_disable", NULL, 0,
  1504. &sd3_tuning_disable, sizeof(sd3_tuning_disable), TRUE);
  1505. #endif /* BCMSDIOH_STD */
  1506. #if defined(SUPPORT_P2P_GO_PS)
  1507. wake_up(&bus->bus_sleep);
  1508. #endif /* LINUX && SUPPORT_P2P_GO_PS */
  1509. } else {
  1510. /* Waking up: bus power up is ok, set local state */
  1511. if (!SLPAUTO_ENAB(bus)) {
  1512. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, &err);
  1513. /* Force pad isolation off if possible (in case power never toggled) */
  1514. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, 0, NULL);
  1515. /* Make sure the controller has the bus up */
  1516. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  1517. /* Send misc interrupt to indicate OOB not needed */
  1518. W_SDREG(0, &regs->tosbmailboxdata, retries);
  1519. if (retries <= retry_limit)
  1520. W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
  1521. if (retries > retry_limit)
  1522. DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
  1523. /* Make sure we have SD bus access */
  1524. dhdsdio_clkctl(bus, CLK_SDONLY, FALSE);
  1525. /* Enable interrupts again */
  1526. if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
  1527. bus->intdis = FALSE;
  1528. bcmsdh_intr_enable(bus->sdh);
  1529. }
  1530. } else {
  1531. err = dhdsdio_clk_devsleep_iovar(bus, FALSE /* wake */);
  1532. #ifdef BT_OVER_SDIO
  1533. if (err < 0) {
  1534. struct net_device *net = NULL;
  1535. dhd_pub_t *dhd = bus->dhd;
  1536. net = dhd_idx2net(dhd, 0);
  1537. if (net != NULL) {
  1538. DHD_ERROR(("<< WIFI HANG by KSO Enabled failure\n"));
  1539. dhd_os_sdunlock(dhd);
  1540. net_os_send_hang_message(net);
  1541. dhd_os_sdlock(dhd);
  1542. } else {
  1543. DHD_ERROR(("<< WIFI HANG Fail because net is NULL\n"));
  1544. }
  1545. }
  1546. #endif /* BT_OVER_SDIO */
  1547. }
  1548. if (err == 0) {
  1549. /* Change state */
  1550. bus->sleeping = FALSE;
  1551. #if defined(BCMSDIOH_STD)
  1552. sd3_tuning_disable = FALSE;
  1553. err = bcmsdh_iovar_op(bus->sdh, "sd3_tuning_disable", NULL, 0,
  1554. &sd3_tuning_disable, sizeof(sd3_tuning_disable), TRUE);
  1555. #endif /* BCMSDIOH_STD */
  1556. }
  1557. }
  1558. return err;
  1559. }
  1560. #ifdef BT_OVER_SDIO
  1561. /*
  1562. * Call this function to Get the Clock running.
  1563. * Assumes that the caller holds the sdlock.
  1564. * bus - Pointer to the dhd_bus handle
  1565. * can_wait - TRUE if the caller can wait until the clock becomes ready
  1566. * FALSE if the caller cannot wait
  1567. */
  1568. int __dhdsdio_clk_enable(struct dhd_bus *bus, bus_owner_t owner, int can_wait)
  1569. {
  1570. int ret = BCME_ERROR;
  1571. BCM_REFERENCE(owner);
  1572. bus->bt_use_count++;
  1573. /*
  1574. * We can call BUS_WAKE, clkctl multiple times, both of the items
  1575. * have states and if its already ON, no new configuration is done
  1576. */
  1577. /* Wake up the Dongle FW from SR */
  1578. BUS_WAKE(bus);
  1579. /*
  1580. * Make sure back plane ht clk is on
  1581. * CLK_AVAIL - Turn On both SD & HT clock
  1582. */
  1583. ret = dhdsdio_clkctl(bus, CLK_AVAIL, can_wait);
  1584. DHD_INFO(("%s():bt_use_count %d \r\n", __FUNCTION__,
  1585. bus->bt_use_count));
  1586. return ret;
  1587. }
  1588. /*
  1589. * Call this function to relinquish the Clock.
  1590. * Assumes that the caller holds the sdlock.
  1591. * bus - Pointer to the dhd_bus handle
  1592. * can_wait - TRUE if the caller can wait until the clock becomes ready
  1593. * FALSE if the caller cannot wait
  1594. */
  1595. int __dhdsdio_clk_disable(struct dhd_bus *bus, bus_owner_t owner, int can_wait)
  1596. {
  1597. int ret = BCME_ERROR;
  1598. BCM_REFERENCE(owner);
  1599. BCM_REFERENCE(can_wait);
  1600. if (bus->bt_use_count == 0) {
  1601. DHD_ERROR(("%s(): Clocks are already turned off \r\n",
  1602. __FUNCTION__));
  1603. return ret;
  1604. }
  1605. bus->bt_use_count--;
  1606. /*
  1607. * When the SDIO Bus is shared between BT & WLAN, we turn Off the clock
  1608. * once the last user has relinqushed the same. But there are two schemes
  1609. * in that too. We consider WLAN as the bus master (even if its not
  1610. * active). Even when the WLAN is OFF the DHD Watchdog is active.
  1611. * So this Bus Watchdog is the context whill put the Bus to sleep.
  1612. * Refer dhd_bus_watchdog function
  1613. */
  1614. ret = BCME_OK;
  1615. DHD_INFO(("%s():bt_use_count %d \r\n", __FUNCTION__,
  1616. bus->bt_use_count));
  1617. return ret;
  1618. }
  1619. void dhdsdio_reset_bt_use_count(struct dhd_bus *bus)
  1620. {
  1621. /* reset bt use count */
  1622. bus->bt_use_count = 0;
  1623. }
  1624. #endif /* BT_OVER_SDIO */
  1625. #ifdef USE_DYNAMIC_F2_BLKSIZE
  1626. int dhdsdio_func_blocksize(dhd_pub_t *dhd, int function_num, int block_size)
  1627. {
  1628. int func_blk_size = function_num;
  1629. int bcmerr = 0;
  1630. int result;
  1631. bcmerr = dhd_bus_iovar_op(dhd, "sd_blocksize", &func_blk_size,
  1632. sizeof(int), &result, sizeof(int), IOV_GET);
  1633. if (bcmerr != BCME_OK) {
  1634. DHD_ERROR(("%s: Get F%d Block size error\n", __FUNCTION__, function_num));
  1635. return BCME_ERROR;
  1636. }
  1637. if (result != block_size) {
  1638. DHD_TRACE_HW4(("%s: F%d Block size set from %d to %d\n",
  1639. __FUNCTION__, function_num, result, block_size));
  1640. func_blk_size = function_num << 16 | block_size;
  1641. bcmerr = dhd_bus_iovar_op(dhd, "sd_blocksize", NULL,
  1642. 0, &func_blk_size, sizeof(int32), IOV_SET);
  1643. if (bcmerr != BCME_OK) {
  1644. DHD_ERROR(("%s: Set F%d Block size error\n", __FUNCTION__, function_num));
  1645. return BCME_ERROR;
  1646. }
  1647. }
  1648. return BCME_OK;
  1649. }
  1650. #endif /* USE_DYNAMIC_F2_BLKSIZE */
  1651. #if defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID)
  1652. void
  1653. dhd_enable_oob_intr(struct dhd_bus *bus, bool enable)
  1654. {
  1655. #if defined(BCMSPI_ANDROID)
  1656. bcmsdh_intr_enable(bus->sdh);
  1657. #elif defined(HW_OOB)
  1658. bcmsdh_enable_hw_oob_intr(bus->sdh, enable);
  1659. #else
  1660. sdpcmd_regs_t *regs = bus->regs;
  1661. uint retries = 0;
  1662. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  1663. if (enable == TRUE) {
  1664. /* Tell device to start using OOB wakeup */
  1665. W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
  1666. if (retries > retry_limit)
  1667. DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
  1668. } else {
  1669. /* Send misc interrupt to indicate OOB not needed */
  1670. W_SDREG(0, &regs->tosbmailboxdata, retries);
  1671. if (retries <= retry_limit)
  1672. W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
  1673. }
  1674. /* Turn off our contribution to the HT clock request */
  1675. dhdsdio_clkctl(bus, CLK_SDONLY, FALSE);
  1676. #endif /* !defined(HW_OOB) */
  1677. }
  1678. #endif /* defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID) */
  1679. int
  1680. dhd_bus_txdata(struct dhd_bus *bus, void *pkt)
  1681. {
  1682. int ret = BCME_ERROR;
  1683. osl_t *osh;
  1684. uint datalen, prec, prio;
  1685. #if defined(DHD_TX_DUMP)
  1686. uint8 *dump_data;
  1687. #endif /* DHD_TX_DUMP */
  1688. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  1689. osh = bus->dhd->osh;
  1690. datalen = PKTLEN(osh, pkt);
  1691. #ifdef SDTEST
  1692. /* Push the test header if doing loopback */
  1693. if (bus->ext_loop) {
  1694. uint8* data;
  1695. PKTPUSH(osh, pkt, SDPCM_TEST_HDRLEN);
  1696. data = PKTDATA(osh, pkt);
  1697. *data++ = SDPCM_TEST_ECHOREQ;
  1698. *data++ = (uint8)bus->loopid++;
  1699. *data++ = (datalen >> 0);
  1700. *data++ = (datalen >> 8);
  1701. datalen += SDPCM_TEST_HDRLEN;
  1702. }
  1703. #else /* SDTEST */
  1704. BCM_REFERENCE(datalen);
  1705. #endif /* SDTEST */
  1706. #ifdef DHD_ULP
  1707. dhd_ulp_set_path(bus->dhd, DHD_ULP_TX_DATA);
  1708. #endif /* DHD_ULP */
  1709. #if defined(DHD_TX_DUMP) && defined(DHD_TX_FULL_DUMP)
  1710. dump_data = PKTDATA(osh, pkt);
  1711. dump_data += 4; /* skip 4 bytes header */
  1712. {
  1713. int i;
  1714. DHD_ERROR(("TX DUMP\n"));
  1715. for (i = 0; i < (datalen - 4); i++) {
  1716. DHD_ERROR(("%02X ", dump_data[i]));
  1717. if ((i & 15) == 15)
  1718. printk("\n");
  1719. }
  1720. DHD_ERROR(("\n"));
  1721. }
  1722. #endif /* DHD_TX_DUMP && DHD_TX_FULL_DUMP */
  1723. prec = PRIO2PREC((PKTPRIO(pkt) & PRIOMASK));
  1724. #ifdef REVERSE_AIFSN
  1725. /* Updating the precedence value if aifsn are reverse than 802.11 */
  1726. if (bus->dhd->aifsn_reverse) {
  1727. prio = PKTPRIO(pkt) & PRIOMASK;
  1728. if (prio == PRIO_8021D_BE) {
  1729. prec = PRIO2PREC((PRIO_8021D_VI & PRIOMASK));
  1730. } else if (prio == PRIO_8021D_VI) {
  1731. prec = PRIO2PREC((PRIO_8021D_BE & PRIOMASK));
  1732. }
  1733. }
  1734. #endif /* REVERSE_AIFSN */
  1735. /* Check for existing queue, current flow-control, pending event, or pending clock */
  1736. if (dhd_deferred_tx || bus->fcstate || pktq_n_pkts_tot(&bus->txq) || bus->dpc_sched ||
  1737. (!DATAOK(bus)) || (bus->flowcontrol & NBITVAL(prec)) ||
  1738. (bus->clkstate != CLK_AVAIL)) {
  1739. bool deq_ret;
  1740. int pkq_len;
  1741. DHD_TRACE(("%s: deferring pktq len %d\n", __FUNCTION__,
  1742. pktq_n_pkts_tot(&bus->txq)));
  1743. bus->fcqueued++;
  1744. /* Priority based enq */
  1745. dhd_os_sdlock_txq(bus->dhd);
  1746. deq_ret = dhd_prec_enq(bus->dhd, &bus->txq, pkt, prec);
  1747. dhd_os_sdunlock_txq(bus->dhd);
  1748. if (!deq_ret) {
  1749. #ifdef PROP_TXSTATUS
  1750. if (DHD_PKTTAG_WLFCPKT(PKTTAG(pkt)) == 0)
  1751. #endif /* PROP_TXSTATUS */
  1752. {
  1753. #ifdef DHDTCPACK_SUPPRESS
  1754. if (dhd_tcpack_check_xmit(bus->dhd, pkt) == BCME_ERROR) {
  1755. DHD_ERROR(("%s %d: tcpack_suppress ERROR!!! Stop using\n",
  1756. __FUNCTION__, __LINE__));
  1757. dhd_tcpack_suppress_set(bus->dhd, TCPACK_SUP_OFF);
  1758. }
  1759. #endif /* DHDTCPACK_SUPPRESS */
  1760. dhd_txcomplete(bus->dhd, pkt, FALSE);
  1761. PKTFREE(osh, pkt, TRUE);
  1762. }
  1763. ret = BCME_NORESOURCE;
  1764. } else
  1765. ret = BCME_OK;
  1766. dhd_os_sdlock_txq(bus->dhd);
  1767. pkq_len = pktq_n_pkts_tot(&bus->txq);
  1768. dhd_os_sdunlock_txq(bus->dhd);
  1769. if (pkq_len >= FCHI) {
  1770. bool wlfc_enabled = FALSE;
  1771. #ifdef PROP_TXSTATUS
  1772. wlfc_enabled = (dhd_wlfc_flowcontrol(bus->dhd, ON, FALSE) !=
  1773. WLFC_UNSUPPORTED);
  1774. #endif // endif
  1775. if (!wlfc_enabled && dhd_doflow) {
  1776. dhd_txflowcontrol(bus->dhd, ALL_INTERFACES, ON);
  1777. }
  1778. }
  1779. #ifdef DHD_DEBUG
  1780. dhd_os_sdlock_txq(bus->dhd);
  1781. if (pktqprec_n_pkts(&bus->txq, prec) > qcount[prec])
  1782. qcount[prec] = pktqprec_n_pkts(&bus->txq, prec);
  1783. dhd_os_sdunlock_txq(bus->dhd);
  1784. #endif // endif
  1785. /* Schedule DPC if needed to send queued packet(s) */
  1786. if (dhd_deferred_tx && !bus->dpc_sched) {
  1787. bus->dpc_sched = TRUE;
  1788. dhd_sched_dpc(bus->dhd);
  1789. }
  1790. } else {
  1791. int chan = SDPCM_DATA_CHANNEL;
  1792. #ifdef SDTEST
  1793. chan = (bus->ext_loop ? SDPCM_TEST_CHANNEL : SDPCM_DATA_CHANNEL);
  1794. #endif // endif
  1795. /* Lock: we're about to use shared data/code (and SDIO) */
  1796. dhd_os_sdlock(bus->dhd);
  1797. /* Otherwise, send it now */
  1798. BUS_WAKE(bus);
  1799. /* Make sure back plane ht clk is on, no pending allowed */
  1800. dhdsdio_clkctl(bus, CLK_AVAIL, TRUE);
  1801. ret = dhdsdio_txpkt(bus, chan, &pkt, 1, TRUE);
  1802. if (ret != BCME_OK)
  1803. bus->dhd->tx_errors++;
  1804. else
  1805. bus->dhd->dstats.tx_bytes += datalen;
  1806. if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched &&
  1807. NO_OTHER_ACTIVE_BUS_USER(bus)) {
  1808. bus->activity = FALSE;
  1809. dhdsdio_bussleep(bus, TRUE);
  1810. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  1811. }
  1812. dhd_os_sdunlock(bus->dhd);
  1813. }
  1814. return ret;
  1815. }
  1816. /* align packet data pointer and packet length to n-byte boundary, process packet headers,
  1817. * a new packet may be allocated if there is not enough head and/or tail from for padding.
  1818. * the caller is responsible for updating the glom size in the head packet (when glom is
  1819. * used)
  1820. *
  1821. * pad_pkt_len: returns the length of extra padding needed from the padding packet, this parameter
  1822. * is taken in tx glom mode only
  1823. *
  1824. * new_pkt: out, pointer of the new packet allocated due to insufficient head room for alignment
  1825. * padding, NULL if not needed, the caller is responsible for freeing the new packet
  1826. *
  1827. * return: positive value - length of the packet, including head and tail padding
  1828. * negative value - errors
  1829. */
  1830. static int dhdsdio_txpkt_preprocess(dhd_bus_t *bus, void *pkt, int chan, int txseq,
  1831. int prev_chain_total_len, bool last_chained_pkt,
  1832. int *pad_pkt_len, void **new_pkt)
  1833. {
  1834. osl_t *osh;
  1835. uint8 *frame;
  1836. int pkt_len;
  1837. int modulo;
  1838. int head_padding;
  1839. int tail_padding = 0;
  1840. uint32 swheader;
  1841. uint32 swhdr_offset;
  1842. bool alloc_new_pkt = FALSE;
  1843. uint8 sdpcm_hdrlen = bus->txglom_enable ? SDPCM_HDRLEN_TXGLOM : SDPCM_HDRLEN;
  1844. *new_pkt = NULL;
  1845. osh = bus->dhd->osh;
  1846. #ifdef DHDTCPACK_SUPPRESS
  1847. if (dhd_tcpack_check_xmit(bus->dhd, pkt) == BCME_ERROR) {
  1848. DHD_ERROR(("%s %d: tcpack_suppress ERROR!!! Stop using it\n",
  1849. __FUNCTION__, __LINE__));
  1850. dhd_tcpack_suppress_set(bus->dhd, TCPACK_SUP_OFF);
  1851. }
  1852. #endif /* DHDTCPACK_SUPPRESS */
  1853. /* Add space for the SDPCM hardware/software headers */
  1854. PKTPUSH(osh, pkt, sdpcm_hdrlen);
  1855. ASSERT(ISALIGNED((uintptr)PKTDATA(osh, pkt), 2));
  1856. frame = (uint8*)PKTDATA(osh, pkt);
  1857. pkt_len = (uint16)PKTLEN(osh, pkt);
  1858. #ifdef DHD_DEBUG
  1859. if (PKTPRIO(pkt) < ARRAYSIZE(tx_packets))
  1860. tx_packets[PKTPRIO(pkt)]++;
  1861. #endif /* DHD_DEBUG */
  1862. /* align the data pointer, allocate a new packet if there is not enough space (new
  1863. * packet data pointer will be aligned thus no padding will be needed)
  1864. */
  1865. head_padding = (uintptr)frame % DHD_SDALIGN;
  1866. if (PKTHEADROOM(osh, pkt) < head_padding) {
  1867. head_padding = 0;
  1868. alloc_new_pkt = TRUE;
  1869. } else {
  1870. uint cur_chain_total_len;
  1871. int chain_tail_padding = 0;
  1872. /* All packets need to be aligned by DHD_SDALIGN */
  1873. modulo = (pkt_len + head_padding) % DHD_SDALIGN;
  1874. tail_padding = modulo > 0 ? (DHD_SDALIGN - modulo) : 0;
  1875. /* Total pkt chain length needs to be aligned by block size,
  1876. * unless it is a single pkt chain with total length less than one block size,
  1877. * which we prefer sending by byte mode.
  1878. *
  1879. * Do the chain alignment here if
  1880. * 1. This is the last pkt of the chain of multiple pkts or a single pkt.
  1881. * 2-1. This chain is of multiple pkts, or
  1882. * 2-2. This is a single pkt whose size is longer than one block size.
  1883. */
  1884. cur_chain_total_len = prev_chain_total_len +
  1885. (head_padding + pkt_len + tail_padding);
  1886. if (last_chained_pkt && bus->blocksize != 0 &&
  1887. (cur_chain_total_len > (int)bus->blocksize || prev_chain_total_len > 0)) {
  1888. modulo = cur_chain_total_len % bus->blocksize;
  1889. chain_tail_padding = modulo > 0 ? (bus->blocksize - modulo) : 0;
  1890. }
  1891. #ifdef DHDENABLE_TAILPAD
  1892. if (PKTTAILROOM(osh, pkt) < tail_padding) {
  1893. /* We don't have tail room to align by DHD_SDALIGN */
  1894. alloc_new_pkt = TRUE;
  1895. bus->tx_tailpad_pktget++;
  1896. } else if (PKTTAILROOM(osh, pkt) < tail_padding + chain_tail_padding) {
  1897. /* We have tail room for tail_padding of this pkt itself, but not for
  1898. * total pkt chain alignment by block size.
  1899. * Use the padding packet to avoid memory copy if applicable,
  1900. * otherwise, just allocate a new pkt.
  1901. */
  1902. if (bus->pad_pkt) {
  1903. *pad_pkt_len = chain_tail_padding;
  1904. bus->tx_tailpad_chain++;
  1905. } else {
  1906. alloc_new_pkt = TRUE;
  1907. bus->tx_tailpad_pktget++;
  1908. }
  1909. } else
  1910. /* This last pkt's tailroom is sufficient to hold both tail_padding
  1911. * of the pkt itself and chain_tail_padding of total pkt chain
  1912. */
  1913. #endif /* DHDENABLE_TAILPAD */
  1914. tail_padding += chain_tail_padding;
  1915. }
  1916. DHD_INFO(("%s sdhdr len + orig_pkt_len %d h_pad %d t_pad %d pad_pkt_len %d\n",
  1917. __FUNCTION__, pkt_len, head_padding, tail_padding, *pad_pkt_len));
  1918. if (alloc_new_pkt) {
  1919. void *tmp_pkt;
  1920. int newpkt_size;
  1921. int cur_total_len;
  1922. ASSERT(*pad_pkt_len == 0);
  1923. DHD_INFO(("%s allocating new packet for padding\n", __FUNCTION__));
  1924. /* head pointer is aligned now, no padding needed */
  1925. head_padding = 0;
  1926. /* update the tail padding as it depends on the head padding, since a new packet is
  1927. * allocated, the head padding is non longer needed and packet length is chagned
  1928. */
  1929. cur_total_len = prev_chain_total_len + pkt_len;
  1930. if (last_chained_pkt && bus->blocksize != 0 &&
  1931. (cur_total_len > (int)bus->blocksize || prev_chain_total_len > 0)) {
  1932. modulo = cur_total_len % bus->blocksize;
  1933. tail_padding = modulo > 0 ? (bus->blocksize - modulo) : 0;
  1934. } else {
  1935. modulo = pkt_len % DHD_SDALIGN;
  1936. tail_padding = modulo > 0 ? (DHD_SDALIGN - modulo) : 0;
  1937. }
  1938. newpkt_size = PKTLEN(osh, pkt) + bus->blocksize + DHD_SDALIGN;
  1939. bus->dhd->tx_realloc++;
  1940. tmp_pkt = PKTGET(osh, newpkt_size, TRUE);
  1941. if (tmp_pkt == NULL) {
  1942. DHD_ERROR(("failed to alloc new %d byte packet\n", newpkt_size));
  1943. return BCME_NOMEM;
  1944. }
  1945. PKTALIGN(osh, tmp_pkt, PKTLEN(osh, pkt), DHD_SDALIGN);
  1946. bcopy(PKTDATA(osh, pkt), PKTDATA(osh, tmp_pkt), PKTLEN(osh, pkt));
  1947. *new_pkt = tmp_pkt;
  1948. pkt = tmp_pkt;
  1949. }
  1950. if (head_padding)
  1951. PKTPUSH(osh, pkt, head_padding);
  1952. frame = (uint8*)PKTDATA(osh, pkt);
  1953. bzero(frame, head_padding + sdpcm_hdrlen);
  1954. pkt_len = (uint16)PKTLEN(osh, pkt);
  1955. /* the header has the followming format
  1956. * 4-byte HW frame tag: length, ~length (for glom this is the total length)
  1957. *
  1958. * 8-byte HW extesion flags (glom mode only) as the following:
  1959. * 2-byte packet length, excluding HW tag and padding
  1960. * 2-byte frame channel and frame flags (e.g. next frame following)
  1961. * 2-byte header length
  1962. * 2-byte tail padding size
  1963. *
  1964. * 8-byte SW frame tags as the following
  1965. * 4-byte flags: host tx seq, channel, data offset
  1966. * 4-byte flags: TBD
  1967. */
  1968. swhdr_offset = SDPCM_FRAMETAG_LEN;
  1969. /* hardware frame tag:
  1970. *
  1971. * in tx-glom mode, dongle only checks the hardware frame tag in the first
  1972. * packet and sees it as the total lenght of the glom (including tail padding),
  1973. * for each packet in the glom, the packet length needs to be updated, (see
  1974. * below PKTSETLEN)
  1975. *
  1976. * in non tx-glom mode, PKTLEN still need to include tail padding as to be
  1977. * referred to in sdioh_request_buffer(). The tail length will be excluded in
  1978. * dhdsdio_txpkt_postprocess().
  1979. */
  1980. *(uint16*)frame = (uint16)htol16(pkt_len);
  1981. *(((uint16*)frame) + 1) = (uint16)htol16(~pkt_len);
  1982. pkt_len += tail_padding;
  1983. /* hardware extesion flags */
  1984. if (bus->txglom_enable) {
  1985. uint32 hwheader1;
  1986. uint32 hwheader2;
  1987. swhdr_offset += SDPCM_HWEXT_LEN;
  1988. hwheader1 = (pkt_len - SDPCM_FRAMETAG_LEN - tail_padding) |
  1989. (last_chained_pkt << 24);
  1990. hwheader2 = (tail_padding) << 16;
  1991. htol32_ua_store(hwheader1, frame + SDPCM_FRAMETAG_LEN);
  1992. htol32_ua_store(hwheader2, frame + SDPCM_FRAMETAG_LEN + 4);
  1993. }
  1994. PKTSETLEN((osh), (pkt), (pkt_len));
  1995. /* software frame tags */
  1996. swheader = ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK)
  1997. | (txseq % SDPCM_SEQUENCE_WRAP) |
  1998. (((head_padding + sdpcm_hdrlen) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
  1999. htol32_ua_store(swheader, frame + swhdr_offset);
  2000. htol32_ua_store(0, frame + swhdr_offset + sizeof(swheader));
  2001. return pkt_len;
  2002. }
  2003. static int dhdsdio_txpkt_postprocess(dhd_bus_t *bus, void *pkt)
  2004. {
  2005. osl_t *osh;
  2006. uint8 *frame;
  2007. int data_offset;
  2008. int tail_padding;
  2009. int swhdr_offset = SDPCM_FRAMETAG_LEN + (bus->txglom_enable ? SDPCM_HWEXT_LEN : 0);
  2010. (void)osh;
  2011. osh = bus->dhd->osh;
  2012. /* restore pkt buffer pointer, but keeps the header pushed by dhd_prot_hdrpush */
  2013. frame = (uint8*)PKTDATA(osh, pkt);
  2014. DHD_INFO(("%s PKTLEN before postprocess %d",
  2015. __FUNCTION__, PKTLEN(osh, pkt)));
  2016. /* PKTLEN still includes tail_padding, so exclude it.
  2017. * We shall have head_padding + original pkt_len for PKTLEN afterwards.
  2018. */
  2019. if (bus->txglom_enable) {
  2020. /* txglom pkts have tail_padding length in HW ext header */
  2021. tail_padding = ltoh32_ua(frame + SDPCM_FRAMETAG_LEN + 4) >> 16;
  2022. PKTSETLEN(osh, pkt, PKTLEN(osh, pkt) - tail_padding);
  2023. DHD_INFO((" txglom pkt: tail_padding %d PKTLEN %d\n",
  2024. tail_padding, PKTLEN(osh, pkt)));
  2025. } else {
  2026. /* non-txglom pkts have head_padding + original pkt length in HW frame tag.
  2027. * We cannot refer to this field for txglom pkts as the first pkt of the chain will
  2028. * have the field for the total length of the chain.
  2029. */
  2030. PKTSETLEN(osh, pkt, *(uint16*)frame);
  2031. DHD_INFO((" non-txglom pkt: HW frame tag len %d after PKTLEN %d\n",
  2032. *(uint16*)frame, PKTLEN(osh, pkt)));
  2033. }
  2034. data_offset = ltoh32_ua(frame + swhdr_offset);
  2035. data_offset = (data_offset & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT;
  2036. /* Get rid of sdpcm header + head_padding */
  2037. PKTPULL(osh, pkt, data_offset);
  2038. DHD_INFO(("%s data_offset %d, PKTLEN %d\n",
  2039. __FUNCTION__, data_offset, PKTLEN(osh, pkt)));
  2040. return BCME_OK;
  2041. }
  2042. static int dhdsdio_txpkt(dhd_bus_t *bus, uint chan, void** pkts, int num_pkt, bool free_pkt)
  2043. {
  2044. int i;
  2045. int ret = 0;
  2046. osl_t *osh;
  2047. bcmsdh_info_t *sdh;
  2048. void *pkt = NULL;
  2049. void *pkt_chain;
  2050. int total_len = 0;
  2051. void *head_pkt = NULL;
  2052. void *prev_pkt = NULL;
  2053. int pad_pkt_len = 0;
  2054. int new_pkt_num = 0;
  2055. void *new_pkts[MAX_TX_PKTCHAIN_CNT];
  2056. bool wlfc_enabled = FALSE;
  2057. if (bus->dhd->dongle_reset)
  2058. return BCME_NOTREADY;
  2059. if (num_pkt <= 0)
  2060. return BCME_BADARG;
  2061. sdh = bus->sdh;
  2062. osh = bus->dhd->osh;
  2063. /* init new_pkts[0] to make some compiler happy, not necessary as we check new_pkt_num */
  2064. new_pkts[0] = NULL;
  2065. for (i = 0; i < num_pkt; i++) {
  2066. int pkt_len;
  2067. bool last_pkt;
  2068. void *new_pkt = NULL;
  2069. pkt = pkts[i];
  2070. ASSERT(pkt);
  2071. last_pkt = (i == num_pkt - 1);
  2072. pkt_len = dhdsdio_txpkt_preprocess(bus, pkt, chan, bus->tx_seq + i,
  2073. total_len, last_pkt, &pad_pkt_len, &new_pkt);
  2074. if (pkt_len <= 0)
  2075. goto done;
  2076. if (new_pkt) {
  2077. pkt = new_pkt;
  2078. new_pkts[new_pkt_num++] = new_pkt;
  2079. }
  2080. total_len += pkt_len;
  2081. PKTSETNEXT(osh, pkt, NULL);
  2082. /* insert the packet into the list */
  2083. head_pkt ? PKTSETNEXT(osh, prev_pkt, pkt) : (head_pkt = pkt);
  2084. prev_pkt = pkt;
  2085. }
  2086. /* Update the HW frame tag (total length) in the first pkt of the glom */
  2087. if (bus->txglom_enable) {
  2088. uint8 *frame;
  2089. total_len += pad_pkt_len;
  2090. frame = (uint8*)PKTDATA(osh, head_pkt);
  2091. *(uint16*)frame = (uint16)htol16(total_len);
  2092. *(((uint16*)frame) + 1) = (uint16)htol16(~total_len);
  2093. }
  2094. #ifdef DHDENABLE_TAILPAD
  2095. /* if a padding packet if needed, insert it to the end of the link list */
  2096. if (pad_pkt_len) {
  2097. PKTSETLEN(osh, bus->pad_pkt, pad_pkt_len);
  2098. PKTSETNEXT(osh, pkt, bus->pad_pkt);
  2099. }
  2100. #endif /* DHDENABLE_TAILPAD */
  2101. /* dhd_bcmsdh_send_buf ignores the buffer pointer if he packet
  2102. * parameter is not NULL, for non packet chian we pass NULL pkt pointer
  2103. * so it will take the aligned length and buffer pointer.
  2104. */
  2105. pkt_chain = PKTNEXT(osh, head_pkt) ? head_pkt : NULL;
  2106. ret = dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2, F2SYNC,
  2107. PKTDATA(osh, head_pkt), total_len, pkt_chain, NULL, NULL, TXRETRIES);
  2108. if (ret == BCME_OK)
  2109. bus->tx_seq = (bus->tx_seq + num_pkt) % SDPCM_SEQUENCE_WRAP;
  2110. /* if a padding packet was needed, remove it from the link list as it not a data pkt */
  2111. if (pad_pkt_len && pkt)
  2112. PKTSETNEXT(osh, pkt, NULL);
  2113. done:
  2114. pkt = head_pkt;
  2115. while (pkt) {
  2116. void *pkt_next = PKTNEXT(osh, pkt);
  2117. PKTSETNEXT(osh, pkt, NULL);
  2118. dhdsdio_txpkt_postprocess(bus, pkt);
  2119. pkt = pkt_next;
  2120. }
  2121. /* new packets might be allocated due to insufficient room for padding, but we
  2122. * still have to indicate the original packets to upper layer
  2123. */
  2124. for (i = 0; i < num_pkt; i++) {
  2125. pkt = pkts[i];
  2126. wlfc_enabled = FALSE;
  2127. #ifdef PROP_TXSTATUS
  2128. if (DHD_PKTTAG_WLFCPKT(PKTTAG(pkt))) {
  2129. wlfc_enabled = (dhd_wlfc_txcomplete(bus->dhd, pkt, ret == 0) !=
  2130. WLFC_UNSUPPORTED);
  2131. }
  2132. #endif /* PROP_TXSTATUS */
  2133. if (!wlfc_enabled) {
  2134. PKTSETNEXT(osh, pkt, NULL);
  2135. dhd_txcomplete(bus->dhd, pkt, ret != 0);
  2136. if (free_pkt)
  2137. PKTFREE(osh, pkt, TRUE);
  2138. }
  2139. }
  2140. for (i = 0; i < new_pkt_num; i++)
  2141. PKTFREE(osh, new_pkts[i], TRUE);
  2142. return ret;
  2143. }
  2144. static uint
  2145. dhdsdio_sendfromq(dhd_bus_t *bus, uint maxframes)
  2146. {
  2147. uint cnt = 0;
  2148. uint8 tx_prec_map;
  2149. uint16 txpktqlen = 0;
  2150. uint32 intstatus = 0;
  2151. uint retries = 0;
  2152. osl_t *osh;
  2153. uint datalen = 0;
  2154. dhd_pub_t *dhd = bus->dhd;
  2155. sdpcmd_regs_t *regs = bus->regs;
  2156. #ifdef DHD_LOSSLESS_ROAMING
  2157. uint8 *pktdata;
  2158. struct ether_header *eh;
  2159. #ifdef BDC
  2160. struct bdc_header *bdc_header;
  2161. uint8 data_offset;
  2162. #endif // endif
  2163. #endif /* DHD_LOSSLESS_ROAMING */
  2164. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  2165. if (!KSO_ENAB(bus)) {
  2166. DHD_ERROR(("%s: Device asleep\n", __FUNCTION__));
  2167. return BCME_NODEVICE;
  2168. }
  2169. osh = dhd->osh;
  2170. tx_prec_map = ~bus->flowcontrol;
  2171. #ifdef DHD_LOSSLESS_ROAMING
  2172. tx_prec_map &= dhd->dequeue_prec_map;
  2173. #endif /* DHD_LOSSLESS_ROAMING */
  2174. for (cnt = 0; (cnt < maxframes) && DATAOK(bus);) {
  2175. int i;
  2176. int num_pkt = 1;
  2177. void *pkts[MAX_TX_PKTCHAIN_CNT];
  2178. int prec_out;
  2179. dhd_os_sdlock_txq(bus->dhd);
  2180. if (bus->txglom_enable) {
  2181. uint32 glomlimit = (uint32)bus->txglomsize;
  2182. #if defined(BCMSDIOH_STD)
  2183. if (bus->blocksize == 64) {
  2184. glomlimit = MIN((uint32)bus->txglomsize, BLK_64_MAXTXGLOM);
  2185. }
  2186. #endif /* BCMSDIOH_STD */
  2187. num_pkt = MIN((uint32)DATABUFCNT(bus), glomlimit);
  2188. num_pkt = MIN(num_pkt, ARRAYSIZE(pkts));
  2189. }
  2190. num_pkt = MIN(num_pkt, pktq_mlen(&bus->txq, tx_prec_map));
  2191. for (i = 0; i < num_pkt; i++) {
  2192. pkts[i] = pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
  2193. if (!pkts[i]) {
  2194. DHD_ERROR(("%s: pktq_mlen non-zero when no pkt\n",
  2195. __FUNCTION__));
  2196. ASSERT(0);
  2197. break;
  2198. }
  2199. #ifdef DHD_LOSSLESS_ROAMING
  2200. pktdata = (uint8 *)PKTDATA(osh, pkts[i]);
  2201. #ifdef BDC
  2202. /* Skip BDC header */
  2203. bdc_header = (struct bdc_header *)pktdata;
  2204. data_offset = bdc_header->dataOffset;
  2205. pktdata += BDC_HEADER_LEN + (data_offset << 2);
  2206. #endif // endif
  2207. eh = (struct ether_header *)pktdata;
  2208. if (eh->ether_type == hton16(ETHER_TYPE_802_1X)) {
  2209. uint8 prio = (uint8)PKTPRIO(pkts[i]);
  2210. /* Restore to original priority for 802.1X packet */
  2211. if (prio == PRIO_8021D_NC) {
  2212. PKTSETPRIO(pkts[i], dhd->prio_8021x);
  2213. #ifdef BDC
  2214. /* Restore to original priority in BDC header */
  2215. bdc_header->priority =
  2216. (dhd->prio_8021x & BDC_PRIORITY_MASK);
  2217. #endif // endif
  2218. }
  2219. }
  2220. #endif /* DHD_LOSSLESS_ROAMING */
  2221. PKTORPHAN(pkts[i]);
  2222. datalen += PKTLEN(osh, pkts[i]);
  2223. }
  2224. dhd_os_sdunlock_txq(bus->dhd);
  2225. if (i == 0)
  2226. break;
  2227. if (dhdsdio_txpkt(bus, SDPCM_DATA_CHANNEL, pkts, i, TRUE) != BCME_OK)
  2228. dhd->tx_errors++;
  2229. else
  2230. dhd->dstats.tx_bytes += datalen;
  2231. cnt += i;
  2232. /* In poll mode, need to check for other events */
  2233. if (!bus->intr && cnt)
  2234. {
  2235. /* Check device status, signal pending interrupt */
  2236. R_SDREG(intstatus, &regs->intstatus, retries);
  2237. bus->f2txdata++;
  2238. if (bcmsdh_regfail(bus->sdh))
  2239. break;
  2240. if (intstatus & bus->hostintmask)
  2241. bus->ipend = TRUE;
  2242. }
  2243. }
  2244. dhd_os_sdlock_txq(bus->dhd);
  2245. txpktqlen = pktq_n_pkts_tot(&bus->txq);
  2246. dhd_os_sdunlock_txq(bus->dhd);
  2247. /* Do flow-control if needed */
  2248. if (dhd->up && (dhd->busstate == DHD_BUS_DATA) && (txpktqlen < FCLOW)) {
  2249. bool wlfc_enabled = FALSE;
  2250. #ifdef PROP_TXSTATUS
  2251. wlfc_enabled = (dhd_wlfc_flowcontrol(dhd, OFF, TRUE) != WLFC_UNSUPPORTED);
  2252. #endif // endif
  2253. if (!wlfc_enabled && dhd_doflow && dhd->txoff) {
  2254. dhd_txflowcontrol(dhd, ALL_INTERFACES, OFF);
  2255. }
  2256. }
  2257. return cnt;
  2258. }
  2259. static void
  2260. dhdsdio_sendpendctl(dhd_bus_t *bus)
  2261. {
  2262. bcmsdh_info_t *sdh = bus->sdh;
  2263. int ret;
  2264. uint8* frame_seq = bus->ctrl_frame_buf + SDPCM_FRAMETAG_LEN;
  2265. if (bus->txglom_enable)
  2266. frame_seq += SDPCM_HWEXT_LEN;
  2267. if (*frame_seq != bus->tx_seq) {
  2268. DHD_INFO(("%s IOCTL frame seq lag detected!"
  2269. " frm_seq:%d != bus->tx_seq:%d, corrected\n",
  2270. __FUNCTION__, *frame_seq, bus->tx_seq));
  2271. *frame_seq = bus->tx_seq;
  2272. }
  2273. ret = dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2, F2SYNC,
  2274. (uint8 *)bus->ctrl_frame_buf, (uint32)bus->ctrl_frame_len,
  2275. NULL, NULL, NULL, 1);
  2276. if (ret == BCME_OK)
  2277. bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
  2278. bus->ctrl_frame_stat = FALSE;
  2279. dhd_wait_event_wakeup(bus->dhd);
  2280. }
  2281. int
  2282. dhd_bus_txctl(struct dhd_bus *bus, uchar *msg, uint msglen)
  2283. {
  2284. static int err_nodevice = 0;
  2285. uint8 *frame;
  2286. uint16 len;
  2287. uint32 swheader;
  2288. uint8 doff = 0;
  2289. int ret = -1;
  2290. uint8 sdpcm_hdrlen = bus->txglom_enable ? SDPCM_HDRLEN_TXGLOM : SDPCM_HDRLEN;
  2291. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  2292. if (bus->dhd->dongle_reset)
  2293. return -EIO;
  2294. /* Back the pointer to make a room for bus header */
  2295. frame = msg - sdpcm_hdrlen;
  2296. len = (msglen += sdpcm_hdrlen);
  2297. /* Add alignment padding (optional for ctl frames) */
  2298. if (dhd_alignctl) {
  2299. if ((doff = ((uintptr)frame % DHD_SDALIGN))) {
  2300. frame -= doff;
  2301. len += doff;
  2302. msglen += doff;
  2303. bzero(frame, doff + sdpcm_hdrlen);
  2304. }
  2305. ASSERT(doff < DHD_SDALIGN);
  2306. }
  2307. doff += sdpcm_hdrlen;
  2308. #ifndef BCMSPI
  2309. /* Round send length to next SDIO block */
  2310. if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
  2311. uint16 pad = bus->blocksize - (len % bus->blocksize);
  2312. if ((pad <= bus->roundup) && (pad < bus->blocksize))
  2313. len += pad;
  2314. } else if (len % DHD_SDALIGN) {
  2315. len += DHD_SDALIGN - (len % DHD_SDALIGN);
  2316. }
  2317. #endif /* BCMSPI */
  2318. /* Satisfy length-alignment requirements */
  2319. if (forcealign && (len & (ALIGNMENT - 1)))
  2320. len = ROUNDUP(len, ALIGNMENT);
  2321. ASSERT(ISALIGNED((uintptr)frame, 2));
  2322. /* Need to lock here to protect txseq and SDIO tx calls */
  2323. dhd_os_sdlock(bus->dhd);
  2324. BUS_WAKE(bus);
  2325. /* Make sure backplane clock is on */
  2326. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  2327. /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
  2328. *(uint16*)frame = htol16((uint16)msglen);
  2329. *(((uint16*)frame) + 1) = htol16(~msglen);
  2330. if (bus->txglom_enable) {
  2331. uint32 hwheader1, hwheader2;
  2332. /* Software tag: channel, sequence number, data offset */
  2333. swheader = ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK)
  2334. | bus->tx_seq
  2335. | ((doff << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
  2336. htol32_ua_store(swheader, frame + SDPCM_FRAMETAG_LEN + SDPCM_HWEXT_LEN);
  2337. htol32_ua_store(0, frame + SDPCM_FRAMETAG_LEN
  2338. + SDPCM_HWEXT_LEN + sizeof(swheader));
  2339. hwheader1 = (msglen - SDPCM_FRAMETAG_LEN) | (1 << 24);
  2340. hwheader2 = (len - (msglen)) << 16;
  2341. htol32_ua_store(hwheader1, frame + SDPCM_FRAMETAG_LEN);
  2342. htol32_ua_store(hwheader2, frame + SDPCM_FRAMETAG_LEN + 4);
  2343. *(uint16*)frame = htol16(len);
  2344. *(((uint16*)frame) + 1) = htol16(~(len));
  2345. } else {
  2346. /* Software tag: channel, sequence number, data offset */
  2347. swheader = ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK)
  2348. | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
  2349. htol32_ua_store(swheader, frame + SDPCM_FRAMETAG_LEN);
  2350. htol32_ua_store(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
  2351. }
  2352. #ifdef DHD_ULP
  2353. dhd_ulp_set_path(bus->dhd, DHD_ULP_TX_CTRL);
  2354. if (!TXCTLOK(bus) || !dhd_ulp_f2_ready(bus->dhd, bus->sdh))
  2355. #else
  2356. if (!TXCTLOK(bus))
  2357. #endif // endif
  2358. {
  2359. DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
  2360. __FUNCTION__, bus->tx_max, bus->tx_seq));
  2361. bus->ctrl_frame_stat = TRUE;
  2362. /* Send from dpc */
  2363. bus->ctrl_frame_buf = frame;
  2364. bus->ctrl_frame_len = len;
  2365. if (!bus->dpc_sched) {
  2366. bus->dpc_sched = TRUE;
  2367. dhd_sched_dpc(bus->dhd);
  2368. }
  2369. if (bus->ctrl_frame_stat) {
  2370. dhd_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
  2371. }
  2372. if (bus->ctrl_frame_stat == FALSE) {
  2373. DHD_INFO(("%s: ctrl_frame_stat == FALSE\n", __FUNCTION__));
  2374. ret = 0;
  2375. } else {
  2376. bus->dhd->txcnt_timeout++;
  2377. if (!bus->dhd->hang_was_sent) {
  2378. #ifdef CUSTOMER_HW4_DEBUG
  2379. uint32 status, retry = 0;
  2380. R_SDREG(status, &bus->regs->intstatus, retry);
  2381. DHD_TRACE_HW4(("%s: txcnt_timeout, INT status=0x%08X\n",
  2382. __FUNCTION__, status));
  2383. DHD_TRACE_HW4(("%s : tx_max : %d, tx_seq : %d, clkstate : %d \n",
  2384. __FUNCTION__, bus->tx_max, bus->tx_seq, bus->clkstate));
  2385. #endif /* CUSTOMER_HW4_DEBUG */
  2386. DHD_ERROR(("%s: ctrl_frame_stat == TRUE txcnt_timeout=%d\n",
  2387. __FUNCTION__, bus->dhd->txcnt_timeout));
  2388. }
  2389. #ifdef DHD_FW_COREDUMP
  2390. /* Collect socram dump */
  2391. if ((bus->dhd->memdump_enabled) &&
  2392. (bus->dhd->txcnt_timeout >= MAX_CNTL_TX_TIMEOUT)) {
  2393. /* collect core dump */
  2394. bus->dhd->memdump_type = DUMP_TYPE_RESUMED_ON_TIMEOUT_TX;
  2395. dhd_os_sdunlock(bus->dhd);
  2396. dhd_bus_mem_dump(bus->dhd);
  2397. dhd_os_sdlock(bus->dhd);
  2398. }
  2399. #endif /* DHD_FW_COREDUMP */
  2400. ret = -1;
  2401. bus->ctrl_frame_stat = FALSE;
  2402. goto done;
  2403. }
  2404. }
  2405. bus->dhd->txcnt_timeout = 0;
  2406. bus->ctrl_frame_stat = TRUE;
  2407. if (ret == -1) {
  2408. #ifdef DHD_DEBUG
  2409. if (DHD_BYTES_ON() && DHD_CTL_ON()) {
  2410. prhex("Tx Frame", frame, len);
  2411. } else if (DHD_HDRS_ON()) {
  2412. prhex("TxHdr", frame, MIN(len, 16));
  2413. }
  2414. #endif // endif
  2415. ret = dhd_bcmsdh_send_buffer(bus, frame, len);
  2416. }
  2417. bus->ctrl_frame_stat = FALSE;
  2418. #ifdef DHD_ULP
  2419. dhd_ulp_enable_cached_sbwad(bus->dhd, bus->sdh);
  2420. #endif /* DHD_ULP */
  2421. done:
  2422. if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched &&
  2423. NO_OTHER_ACTIVE_BUS_USER(bus)) {
  2424. bus->activity = FALSE;
  2425. dhdsdio_bussleep(bus, TRUE);
  2426. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  2427. }
  2428. dhd_os_sdunlock(bus->dhd);
  2429. if (ret)
  2430. bus->dhd->tx_ctlerrs++;
  2431. else
  2432. bus->dhd->tx_ctlpkts++;
  2433. if (bus->dhd->txcnt_timeout >= MAX_CNTL_TX_TIMEOUT) {
  2434. #ifdef DHD_PM_CONTROL_FROM_FILE
  2435. if (g_pm_control == TRUE) {
  2436. return -BCME_ERROR;
  2437. } else {
  2438. return -ETIMEDOUT;
  2439. }
  2440. #else
  2441. return -ETIMEDOUT;
  2442. #endif /* DHD_PM_CONTROL_FROM_FILE */
  2443. }
  2444. if (ret == BCME_NODEVICE)
  2445. err_nodevice++;
  2446. else
  2447. err_nodevice = 0;
  2448. return ret ? err_nodevice >= ERROR_BCME_NODEVICE_MAX ? -ETIMEDOUT : -EIO : 0;
  2449. }
  2450. int
  2451. dhd_bus_rxctl(struct dhd_bus *bus, uchar *msg, uint msglen)
  2452. {
  2453. int timeleft;
  2454. uint rxlen = 0;
  2455. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  2456. if (bus->dhd->dongle_reset)
  2457. return -EIO;
  2458. /* Wait until control frame is available */
  2459. timeleft = dhd_os_ioctl_resp_wait(bus->dhd, &bus->rxlen);
  2460. dhd_os_sdlock(bus->dhd);
  2461. rxlen = bus->rxlen;
  2462. bcopy(bus->rxctl, msg, MIN(msglen, rxlen));
  2463. bus->rxlen = 0;
  2464. dhd_os_sdunlock(bus->dhd);
  2465. if (rxlen) {
  2466. DHD_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
  2467. __FUNCTION__, rxlen, msglen));
  2468. } else {
  2469. if (timeleft == 0) {
  2470. #ifdef DHD_DEBUG
  2471. uint32 status, retry = 0;
  2472. R_SDREG(status, &bus->regs->intstatus, retry);
  2473. DHD_ERROR(("%s: resumed on timeout, INT status=0x%08X\n",
  2474. __FUNCTION__, status));
  2475. #else
  2476. DHD_ERROR(("%s: resumed on timeout\n", __FUNCTION__));
  2477. #endif /* DHD_DEBUG */
  2478. if (!bus->dhd->dongle_trap_occured) {
  2479. #ifdef DHD_FW_COREDUMP
  2480. bus->dhd->memdump_type = DUMP_TYPE_RESUMED_ON_TIMEOUT;
  2481. #endif /* DHD_FW_COREDUMP */
  2482. dhd_os_sdlock(bus->dhd);
  2483. dhdsdio_checkdied(bus, NULL, 0);
  2484. dhd_os_sdunlock(bus->dhd);
  2485. }
  2486. } else {
  2487. DHD_CTL(("%s: resumed for unknown reason?\n", __FUNCTION__));
  2488. if (!bus->dhd->dongle_trap_occured) {
  2489. #ifdef DHD_FW_COREDUMP
  2490. bus->dhd->memdump_type = DUMP_TYPE_RESUMED_UNKNOWN;
  2491. #endif /* DHD_FW_COREDUMP */
  2492. dhd_os_sdlock(bus->dhd);
  2493. dhdsdio_checkdied(bus, NULL, 0);
  2494. dhd_os_sdunlock(bus->dhd);
  2495. }
  2496. }
  2497. #ifdef DHD_FW_COREDUMP
  2498. /* Dump the ram image */
  2499. if (bus->dhd->memdump_enabled && !bus->dhd->dongle_trap_occured)
  2500. dhdsdio_mem_dump(bus);
  2501. #endif /* DHD_FW_COREDUMP */
  2502. }
  2503. if (timeleft == 0) {
  2504. if (rxlen == 0)
  2505. bus->dhd->rxcnt_timeout++;
  2506. DHD_ERROR(("%s: rxcnt_timeout=%d, rxlen=%d\n", __FUNCTION__,
  2507. bus->dhd->rxcnt_timeout, rxlen));
  2508. #ifdef DHD_FW_COREDUMP
  2509. /* collect socram dump */
  2510. if (bus->dhd->memdump_enabled) {
  2511. bus->dhd->memdump_type = DUMP_TYPE_RESUMED_ON_TIMEOUT_RX;
  2512. dhd_bus_mem_dump(bus->dhd);
  2513. }
  2514. #endif /* DHD_FW_COREDUMP */
  2515. } else {
  2516. bus->dhd->rxcnt_timeout = 0;
  2517. }
  2518. if (rxlen)
  2519. bus->dhd->rx_ctlpkts++;
  2520. else
  2521. bus->dhd->rx_ctlerrs++;
  2522. if (bus->dhd->rxcnt_timeout >= MAX_CNTL_RX_TIMEOUT) {
  2523. #ifdef DHD_PM_CONTROL_FROM_FILE
  2524. if (g_pm_control == TRUE) {
  2525. return -BCME_ERROR;
  2526. } else {
  2527. return -ETIMEDOUT;
  2528. }
  2529. #else
  2530. return -ETIMEDOUT;
  2531. #endif /* DHD_PM_CONTROL_FROM_FILE */
  2532. }
  2533. if (bus->dhd->dongle_trap_occured)
  2534. return -EREMOTEIO;
  2535. return rxlen ? (int)rxlen : -EIO;
  2536. }
  2537. /* IOVar table */
  2538. enum {
  2539. IOV_INTR = 1,
  2540. IOV_POLLRATE,
  2541. IOV_SDREG,
  2542. IOV_SBREG,
  2543. IOV_SDCIS,
  2544. #ifdef DHD_BUS_MEM_ACCESS
  2545. IOV_MEMBYTES,
  2546. #endif /* DHD_BUS_MEM_ACCESS */
  2547. IOV_RAMSIZE,
  2548. IOV_RAMSTART,
  2549. #ifdef DHD_DEBUG
  2550. IOV_CHECKDIED,
  2551. IOV_SERIALCONS,
  2552. #endif /* DHD_DEBUG */
  2553. IOV_SET_DOWNLOAD_STATE,
  2554. IOV_SOCRAM_STATE,
  2555. IOV_FORCEEVEN,
  2556. IOV_SDIOD_DRIVE,
  2557. IOV_READAHEAD,
  2558. IOV_SDRXCHAIN,
  2559. IOV_ALIGNCTL,
  2560. IOV_SDALIGN,
  2561. IOV_DEVRESET,
  2562. IOV_CPU,
  2563. #if defined(USE_SDIOFIFO_IOVAR)
  2564. IOV_WATERMARK,
  2565. IOV_MESBUSYCTRL,
  2566. #endif /* USE_SDIOFIFO_IOVAR */
  2567. #ifdef SDTEST
  2568. IOV_PKTGEN,
  2569. IOV_EXTLOOP,
  2570. #endif /* SDTEST */
  2571. IOV_SPROM,
  2572. IOV_TXBOUND,
  2573. IOV_RXBOUND,
  2574. IOV_TXMINMAX,
  2575. IOV_IDLETIME,
  2576. IOV_IDLECLOCK,
  2577. IOV_SD1IDLE,
  2578. IOV_SLEEP,
  2579. IOV_DONGLEISOLATION,
  2580. IOV_KSO,
  2581. IOV_DEVSLEEP,
  2582. IOV_DEVCAP,
  2583. IOV_VARS,
  2584. #ifdef SOFTAP
  2585. IOV_FWPATH,
  2586. #endif // endif
  2587. IOV_TXGLOMSIZE,
  2588. IOV_TXGLOMMODE,
  2589. IOV_HANGREPORT,
  2590. IOV_TXINRX_THRES,
  2591. IOV_SDIO_SUSPEND
  2592. #if defined(DEBUGGER) || defined(DHD_DSCOPE)
  2593. IOV_GDB_SERVER, /**< starts gdb server on given interface */
  2594. #endif /* DEBUGGER || DHD_DSCOPE */
  2595. };
  2596. const bcm_iovar_t dhdsdio_iovars[] = {
  2597. {"intr", IOV_INTR, 0, 0, IOVT_BOOL, 0 },
  2598. {"sleep", IOV_SLEEP, 0, 0, IOVT_BOOL, 0 },
  2599. {"pollrate", IOV_POLLRATE, 0, 0, IOVT_UINT32, 0 },
  2600. {"idletime", IOV_IDLETIME, 0, 0, IOVT_INT32, 0 },
  2601. {"idleclock", IOV_IDLECLOCK, 0, 0, IOVT_INT32, 0 },
  2602. {"sd1idle", IOV_SD1IDLE, 0, 0, IOVT_BOOL, 0 },
  2603. #ifdef DHD_BUS_MEM_ACCESS
  2604. {"membytes", IOV_MEMBYTES, 0, 0, IOVT_BUFFER, 2 * sizeof(int) },
  2605. #endif /* DHD_BUS_MEM_ACCESS */
  2606. {"ramsize", IOV_RAMSIZE, 0, 0, IOVT_UINT32, 0 },
  2607. {"ramstart", IOV_RAMSTART, 0, 0, IOVT_UINT32, 0 },
  2608. {"dwnldstate", IOV_SET_DOWNLOAD_STATE, 0, 0, IOVT_BOOL, 0 },
  2609. {"socram_state", IOV_SOCRAM_STATE, 0, 0, IOVT_BOOL, 0 },
  2610. {"vars", IOV_VARS, 0, 0, IOVT_BUFFER, 0 },
  2611. {"sdiod_drive", IOV_SDIOD_DRIVE, 0, 0, IOVT_UINT32, 0 },
  2612. {"readahead", IOV_READAHEAD, 0, 0, IOVT_BOOL, 0 },
  2613. {"sdrxchain", IOV_SDRXCHAIN, 0, 0, IOVT_BOOL, 0 },
  2614. {"alignctl", IOV_ALIGNCTL, 0, 0, IOVT_BOOL, 0 },
  2615. {"sdalign", IOV_SDALIGN, 0, 0, IOVT_BOOL, 0 },
  2616. {"devreset", IOV_DEVRESET, 0, 0, IOVT_BOOL, 0 },
  2617. #ifdef DHD_DEBUG
  2618. {"sdreg", IOV_SDREG, 0, 0, IOVT_BUFFER, sizeof(sdreg_t) },
  2619. {"sbreg", IOV_SBREG, 0, 0, IOVT_BUFFER, sizeof(sdreg_t) },
  2620. {"sd_cis", IOV_SDCIS, 0, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN },
  2621. {"forcealign", IOV_FORCEEVEN, 0, 0, IOVT_BOOL, 0 },
  2622. {"txbound", IOV_TXBOUND, 0, 0, IOVT_UINT32, 0 },
  2623. {"rxbound", IOV_RXBOUND, 0, 0, IOVT_UINT32, 0 },
  2624. {"txminmax", IOV_TXMINMAX, 0, 0, IOVT_UINT32, 0 },
  2625. {"cpu", IOV_CPU, 0, 0, IOVT_BOOL, 0 },
  2626. #ifdef DHD_DEBUG
  2627. {"checkdied", IOV_CHECKDIED, 0, 0, IOVT_BUFFER, 0 },
  2628. {"serial", IOV_SERIALCONS, 0, 0, IOVT_UINT32, 0 },
  2629. #endif /* DHD_DEBUG */
  2630. #endif /* DHD_DEBUG */
  2631. #ifdef SDTEST
  2632. {"extloop", IOV_EXTLOOP, 0, 0, IOVT_BOOL, 0 },
  2633. {"pktgen", IOV_PKTGEN, 0, 0, IOVT_BUFFER, sizeof(dhd_pktgen_t) },
  2634. #endif /* SDTEST */
  2635. #if defined(USE_SDIOFIFO_IOVAR)
  2636. {"watermark", IOV_WATERMARK, 0, 0, IOVT_UINT32, 0 },
  2637. {"mesbusyctrl", IOV_MESBUSYCTRL, 0, 0, IOVT_UINT32, 0 },
  2638. #endif /* USE_SDIOFIFO_IOVAR */
  2639. {"devcap", IOV_DEVCAP, 0, 0, IOVT_UINT32, 0 },
  2640. {"dngl_isolation", IOV_DONGLEISOLATION, 0, 0, IOVT_UINT32, 0 },
  2641. {"kso", IOV_KSO, 0, 0, IOVT_UINT32, 0 },
  2642. {"devsleep", IOV_DEVSLEEP, 0, 0, IOVT_UINT32, 0 },
  2643. #ifdef SOFTAP
  2644. {"fwpath", IOV_FWPATH, 0, 0, IOVT_BUFFER, 0 },
  2645. #endif // endif
  2646. {"txglomsize", IOV_TXGLOMSIZE, 0, 0, IOVT_UINT32, 0 },
  2647. {"fw_hang_report", IOV_HANGREPORT, 0, 0, IOVT_BOOL, 0 },
  2648. {"txinrx_thres", IOV_TXINRX_THRES, 0, 0, IOVT_INT32, 0 },
  2649. {"sdio_suspend", IOV_SDIO_SUSPEND, 0, 0, IOVT_UINT32, 0 },
  2650. #if defined(DEBUGGER) || defined(DHD_DSCOPE)
  2651. {"gdb_server", IOV_GDB_SERVER, 0, 0, IOVT_UINT32, 0 },
  2652. #endif /* DEBUGGER || DHD_DSCOPE */
  2653. {NULL, 0, 0, 0, 0, 0 }
  2654. };
  2655. static void
  2656. dhd_dump_pct(struct bcmstrbuf *strbuf, char *desc, uint num, uint div)
  2657. {
  2658. uint q1, q2;
  2659. if (!div) {
  2660. bcm_bprintf(strbuf, "%s N/A", desc);
  2661. } else {
  2662. q1 = num / div;
  2663. q2 = (100 * (num - (q1 * div))) / div;
  2664. bcm_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
  2665. }
  2666. }
  2667. void
  2668. dhd_bus_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
  2669. {
  2670. dhd_bus_t *bus = dhdp->bus;
  2671. #if defined(DHD_WAKE_STATUS) && defined(DHD_WAKE_EVENT_STATUS)
  2672. int i;
  2673. #endif // endif
  2674. bcm_bprintf(strbuf, "Bus SDIO structure:\n");
  2675. bcm_bprintf(strbuf, "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
  2676. bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
  2677. bcm_bprintf(strbuf, "fcstate %d qlen %u tx_seq %d, max %d, rxskip %d rxlen %u rx_seq %d\n",
  2678. bus->fcstate, pktq_n_pkts_tot(&bus->txq), bus->tx_seq, bus->tx_max, bus->rxskip,
  2679. bus->rxlen, bus->rx_seq);
  2680. bcm_bprintf(strbuf, "intr %d intrcount %u lastintrs %u spurious %u\n",
  2681. bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
  2682. #ifdef DHD_WAKE_STATUS
  2683. bcm_bprintf(strbuf, "wake %u rxwake %u readctrlwake %u\n",
  2684. bcmsdh_get_total_wake(bus->sdh), bus->wake_counts.rxwake,
  2685. bus->wake_counts.rcwake);
  2686. #ifdef DHD_WAKE_RX_STATUS
  2687. bcm_bprintf(strbuf, " unicast %u multicast %u broadcast %u arp %u\n",
  2688. bus->wake_counts.rx_ucast, bus->wake_counts.rx_mcast,
  2689. bus->wake_counts.rx_bcast, bus->wake_counts.rx_arp);
  2690. bcm_bprintf(strbuf, " multi4 %u multi6 %u icmp6 %u multiother %u\n",
  2691. bus->wake_counts.rx_multi_ipv4, bus->wake_counts.rx_multi_ipv6,
  2692. bus->wake_counts.rx_icmpv6, bus->wake_counts.rx_multi_other);
  2693. bcm_bprintf(strbuf, " icmp6_ra %u, icmp6_na %u, icmp6_ns %u\n",
  2694. bus->wake_counts.rx_icmpv6_ra, bus->wake_counts.rx_icmpv6_na,
  2695. bus->wake_counts.rx_icmpv6_ns);
  2696. #endif /* DHD_WAKE_RX_STATUS */
  2697. #ifdef DHD_WAKE_EVENT_STATUS
  2698. for (i = 0; i < WLC_E_LAST; i++)
  2699. if (bus->wake_counts.rc_event[i] != 0)
  2700. bcm_bprintf(strbuf, " %s = %u\n", bcmevent_get_name(i),
  2701. bus->wake_counts.rc_event[i]);
  2702. bcm_bprintf(strbuf, "\n");
  2703. #endif /* DHD_WAKE_EVENT_STATUS */
  2704. #endif /* DHD_WAKE_STATUS */
  2705. bcm_bprintf(strbuf, "pollrate %u pollcnt %u regfails %u\n",
  2706. bus->pollrate, bus->pollcnt, bus->regfails);
  2707. bcm_bprintf(strbuf, "\nAdditional counters:\n");
  2708. #ifdef DHDENABLE_TAILPAD
  2709. bcm_bprintf(strbuf, "tx_tailpad_chain %u tx_tailpad_pktget %u\n",
  2710. bus->tx_tailpad_chain, bus->tx_tailpad_pktget);
  2711. #endif /* DHDENABLE_TAILPAD */
  2712. bcm_bprintf(strbuf, "tx_sderrs %u fcqueued %u rxrtx %u rx_toolong %u rxc_errors %u\n",
  2713. bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
  2714. bus->rxc_errors);
  2715. bcm_bprintf(strbuf, "rx_hdrfail %u badhdr %u badseq %u\n",
  2716. bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
  2717. bcm_bprintf(strbuf, "fc_rcvd %u, fc_xoff %u, fc_xon %u\n",
  2718. bus->fc_rcvd, bus->fc_xoff, bus->fc_xon);
  2719. bcm_bprintf(strbuf, "rxglomfail %u, rxglomframes %u, rxglompkts %u\n",
  2720. bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
  2721. bcm_bprintf(strbuf, "f2rx (hdrs/data) %u (%u/%u), f2tx %u f1regs %u\n",
  2722. (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs, bus->f2rxdata,
  2723. bus->f2txdata, bus->f1regdata);
  2724. {
  2725. dhd_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->dhd->rx_packets,
  2726. (bus->f2rxhdrs + bus->f2rxdata));
  2727. dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->rx_packets, bus->f1regdata);
  2728. dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->rx_packets,
  2729. (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
  2730. dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->rx_packets, bus->intrcount);
  2731. bcm_bprintf(strbuf, "\n");
  2732. dhd_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
  2733. bus->dhd->rx_packets);
  2734. dhd_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts, bus->rxglomframes);
  2735. bcm_bprintf(strbuf, "\n");
  2736. dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets, bus->f2txdata);
  2737. dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets, bus->f1regdata);
  2738. dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->tx_packets,
  2739. (bus->f2txdata + bus->f1regdata));
  2740. dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->tx_packets, bus->intrcount);
  2741. bcm_bprintf(strbuf, "\n");
  2742. dhd_dump_pct(strbuf, "Total: pkts/f2rw",
  2743. (bus->dhd->tx_packets + bus->dhd->rx_packets),
  2744. (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
  2745. dhd_dump_pct(strbuf, ", pkts/f1sd",
  2746. (bus->dhd->tx_packets + bus->dhd->rx_packets), bus->f1regdata);
  2747. dhd_dump_pct(strbuf, ", pkts/sd",
  2748. (bus->dhd->tx_packets + bus->dhd->rx_packets),
  2749. (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
  2750. dhd_dump_pct(strbuf, ", pkts/int",
  2751. (bus->dhd->tx_packets + bus->dhd->rx_packets), bus->intrcount);
  2752. bcm_bprintf(strbuf, "\n\n");
  2753. }
  2754. #ifdef SDTEST
  2755. if (bus->pktgen_count) {
  2756. bcm_bprintf(strbuf, "pktgen config and count:\n");
  2757. bcm_bprintf(strbuf, "freq %u count %u print %u total %u min %u len %u\n",
  2758. bus->pktgen_freq, bus->pktgen_count, bus->pktgen_print,
  2759. bus->pktgen_total, bus->pktgen_minlen, bus->pktgen_maxlen);
  2760. bcm_bprintf(strbuf, "send attempts %u rcvd %u fail %u\n",
  2761. bus->pktgen_sent, bus->pktgen_rcvd, bus->pktgen_fail);
  2762. }
  2763. #endif /* SDTEST */
  2764. #ifdef DHD_DEBUG
  2765. bcm_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
  2766. bus->dpc_sched, (bcmsdh_intr_pending(bus->sdh) ? " " : " not "));
  2767. bcm_bprintf(strbuf, "blocksize %u roundup %u\n", bus->blocksize, bus->roundup);
  2768. #endif /* DHD_DEBUG */
  2769. bcm_bprintf(strbuf, "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
  2770. bus->clkstate, bus->activity, bus->idletime, bus->idlecount, bus->sleeping);
  2771. }
  2772. void
  2773. dhd_bus_clearcounts(dhd_pub_t *dhdp)
  2774. {
  2775. dhd_bus_t *bus = (dhd_bus_t *)dhdp->bus;
  2776. bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
  2777. bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
  2778. bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
  2779. #ifdef DHDENABLE_TAILPAD
  2780. bus->tx_tailpad_chain = bus->tx_tailpad_pktget = 0;
  2781. #endif /* DHDENABLE_TAILPAD */
  2782. bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
  2783. bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
  2784. bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
  2785. }
  2786. #ifdef SDTEST
  2787. static int
  2788. dhdsdio_pktgen_get(dhd_bus_t *bus, uint8 *arg)
  2789. {
  2790. dhd_pktgen_t pktgen;
  2791. pktgen.version = DHD_PKTGEN_VERSION;
  2792. pktgen.freq = bus->pktgen_freq;
  2793. pktgen.count = bus->pktgen_count;
  2794. pktgen.print = bus->pktgen_print;
  2795. pktgen.total = bus->pktgen_total;
  2796. pktgen.minlen = bus->pktgen_minlen;
  2797. pktgen.maxlen = bus->pktgen_maxlen;
  2798. pktgen.numsent = bus->pktgen_sent;
  2799. pktgen.numrcvd = bus->pktgen_rcvd;
  2800. pktgen.numfail = bus->pktgen_fail;
  2801. pktgen.mode = bus->pktgen_mode;
  2802. pktgen.stop = bus->pktgen_stop;
  2803. bcopy(&pktgen, arg, sizeof(pktgen));
  2804. return 0;
  2805. }
  2806. static int
  2807. dhdsdio_pktgen_set(dhd_bus_t *bus, uint8 *arg)
  2808. {
  2809. dhd_pktgen_t pktgen;
  2810. uint oldcnt, oldmode;
  2811. bcopy(arg, &pktgen, sizeof(pktgen));
  2812. if (pktgen.version != DHD_PKTGEN_VERSION)
  2813. return BCME_BADARG;
  2814. oldcnt = bus->pktgen_count;
  2815. oldmode = bus->pktgen_mode;
  2816. bus->pktgen_freq = pktgen.freq;
  2817. bus->pktgen_count = pktgen.count;
  2818. bus->pktgen_print = pktgen.print;
  2819. bus->pktgen_total = pktgen.total;
  2820. bus->pktgen_minlen = pktgen.minlen;
  2821. bus->pktgen_maxlen = pktgen.maxlen;
  2822. bus->pktgen_mode = pktgen.mode;
  2823. bus->pktgen_stop = pktgen.stop;
  2824. bus->pktgen_tick = bus->pktgen_ptick = 0;
  2825. bus->pktgen_prev_time = jiffies;
  2826. bus->pktgen_len = MAX(bus->pktgen_len, bus->pktgen_minlen);
  2827. bus->pktgen_len = MIN(bus->pktgen_len, bus->pktgen_maxlen);
  2828. /* Clear counts for a new pktgen (mode change, or was stopped) */
  2829. if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode)) {
  2830. bus->pktgen_sent = bus->pktgen_prev_sent = bus->pktgen_rcvd = 0;
  2831. bus->pktgen_prev_rcvd = bus->pktgen_fail = 0;
  2832. }
  2833. return 0;
  2834. }
  2835. #endif /* SDTEST */
  2836. static void
  2837. dhdsdio_devram_remap(dhd_bus_t *bus, bool val)
  2838. {
  2839. uint8 enable, protect, remap;
  2840. si_socdevram(bus->sih, FALSE, &enable, &protect, &remap);
  2841. remap = val ? TRUE : FALSE;
  2842. si_socdevram(bus->sih, TRUE, &enable, &protect, &remap);
  2843. }
  2844. static int
  2845. dhdsdio_membytes(dhd_bus_t *bus, bool write, uint32 address, uint8 *data, uint size)
  2846. {
  2847. int bcmerror = 0;
  2848. uint32 sdaddr;
  2849. uint dsize;
  2850. /* In remap mode, adjust address beyond socram and redirect
  2851. * to devram at SOCDEVRAM_BP_ADDR since remap address > orig_ramsize
  2852. * is not backplane accessible
  2853. */
  2854. if (REMAP_ENAB(bus) && REMAP_ISADDR(bus, address)) {
  2855. address -= bus->orig_ramsize;
  2856. address += SOCDEVRAM_BP_ADDR;
  2857. }
  2858. /* Determine initial transfer parameters */
  2859. sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
  2860. if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
  2861. dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
  2862. else
  2863. dsize = size;
  2864. /* Set the backplane window to include the start address */
  2865. if ((bcmerror = dhdsdio_set_siaddr_window(bus, address))) {
  2866. DHD_ERROR(("%s: window change failed\n", __FUNCTION__));
  2867. goto xfer_done;
  2868. }
  2869. /* Do the transfer(s) */
  2870. while (size) {
  2871. DHD_INFO(("%s: %s %d bytes at offset 0x%08x in window 0x%08x\n",
  2872. __FUNCTION__, (write ? "write" : "read"), dsize, sdaddr,
  2873. (address & SBSDIO_SBWINDOW_MASK)));
  2874. if ((bcmerror = bcmsdh_rwdata(bus->sdh, write, sdaddr, data, dsize))) {
  2875. DHD_ERROR(("%s: membytes transfer failed\n", __FUNCTION__));
  2876. break;
  2877. }
  2878. /* Adjust for next transfer (if any) */
  2879. if ((size -= dsize)) {
  2880. data += dsize;
  2881. address += dsize;
  2882. if ((bcmerror = dhdsdio_set_siaddr_window(bus, address))) {
  2883. DHD_ERROR(("%s: window change failed\n", __FUNCTION__));
  2884. break;
  2885. }
  2886. sdaddr = 0;
  2887. dsize = MIN(SBSDIO_SB_OFT_ADDR_LIMIT, size);
  2888. }
  2889. }
  2890. xfer_done:
  2891. /* Return the window to backplane enumeration space for core access */
  2892. if (dhdsdio_set_siaddr_window(bus, bcmsdh_cur_sbwad(bus->sdh))) {
  2893. DHD_ERROR(("%s: FAILED to set window back to 0x%x\n", __FUNCTION__,
  2894. bcmsdh_cur_sbwad(bus->sdh)));
  2895. }
  2896. return bcmerror;
  2897. }
  2898. static int
  2899. dhdsdio_readshared(dhd_bus_t *bus, sdpcm_shared_t *sh)
  2900. {
  2901. uint32 addr;
  2902. int rv, i;
  2903. uint32 shaddr = 0;
  2904. if (bus->sih == NULL) {
  2905. if (bus->dhd && bus->dhd->dongle_reset) {
  2906. DHD_ERROR(("%s: Dongle is in reset state\n", __FUNCTION__));
  2907. return BCME_NOTREADY;
  2908. } else {
  2909. ASSERT(bus->dhd);
  2910. ASSERT(bus->sih);
  2911. DHD_ERROR(("%s: The address of sih is invalid\n", __FUNCTION__));
  2912. return BCME_ERROR;
  2913. }
  2914. }
  2915. if ((CHIPID(bus->sih->chip) == BCM43430_CHIP_ID ||
  2916. CHIPID(bus->sih->chip) == BCM43018_CHIP_ID) && !dhdsdio_sr_cap(bus))
  2917. bus->srmemsize = 0;
  2918. shaddr = bus->dongle_ram_base + bus->ramsize - 4;
  2919. i = 0;
  2920. do {
  2921. /* Read last word in memory to determine address of sdpcm_shared structure */
  2922. if ((rv = dhdsdio_membytes(bus, FALSE, shaddr, (uint8 *)&addr, 4)) < 0)
  2923. return rv;
  2924. addr = ltoh32(addr);
  2925. DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
  2926. /*
  2927. * Check if addr is valid.
  2928. * NVRAM length at the end of memory should have been overwritten.
  2929. */
  2930. if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
  2931. if ((bus->srmemsize > 0) && (i++ == 0)) {
  2932. shaddr -= bus->srmemsize;
  2933. } else {
  2934. DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
  2935. __FUNCTION__, addr));
  2936. return BCME_ERROR;
  2937. }
  2938. } else
  2939. break;
  2940. } while (i < 2);
  2941. /* Read hndrte_shared structure */
  2942. if ((rv = dhdsdio_membytes(bus, FALSE, addr, (uint8 *)sh, sizeof(sdpcm_shared_t))) < 0)
  2943. return rv;
  2944. /* Endianness */
  2945. sh->flags = ltoh32(sh->flags);
  2946. sh->trap_addr = ltoh32(sh->trap_addr);
  2947. sh->assert_exp_addr = ltoh32(sh->assert_exp_addr);
  2948. sh->assert_file_addr = ltoh32(sh->assert_file_addr);
  2949. sh->assert_line = ltoh32(sh->assert_line);
  2950. sh->console_addr = ltoh32(sh->console_addr);
  2951. sh->msgtrace_addr = ltoh32(sh->msgtrace_addr);
  2952. if ((sh->flags & SDPCM_SHARED_VERSION_MASK) == 3 && SDPCM_SHARED_VERSION == 1)
  2953. return BCME_OK;
  2954. if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
  2955. DHD_ERROR(("%s: sdpcm_shared version %d in dhd "
  2956. "is different than sdpcm_shared version %d in dongle\n",
  2957. __FUNCTION__, SDPCM_SHARED_VERSION,
  2958. sh->flags & SDPCM_SHARED_VERSION_MASK));
  2959. return BCME_ERROR;
  2960. }
  2961. return BCME_OK;
  2962. }
  2963. #define CONSOLE_LINE_MAX 192
  2964. #ifdef DHD_DEBUG
  2965. static int
  2966. dhdsdio_readconsole(dhd_bus_t *bus)
  2967. {
  2968. dhd_console_t *c = &bus->console;
  2969. uint8 line[CONSOLE_LINE_MAX], ch;
  2970. uint32 n, idx, addr;
  2971. int rv;
  2972. /* Don't do anything until FWREADY updates console address */
  2973. if (bus->console_addr == 0)
  2974. return 0;
  2975. if (!KSO_ENAB(bus))
  2976. return 0;
  2977. /* Read console log struct */
  2978. addr = bus->console_addr + OFFSETOF(hnd_cons_t, log);
  2979. if ((rv = dhdsdio_membytes(bus, FALSE, addr, (uint8 *)&c->log, sizeof(c->log))) < 0)
  2980. return rv;
  2981. /* Allocate console buffer (one time only) */
  2982. if (c->buf == NULL) {
  2983. c->bufsize = ltoh32(c->log.buf_size);
  2984. if ((c->buf = MALLOC(bus->dhd->osh, c->bufsize)) == NULL)
  2985. return BCME_NOMEM;
  2986. }
  2987. idx = ltoh32(c->log.idx);
  2988. /* Protect against corrupt value */
  2989. if (idx > c->bufsize)
  2990. return BCME_ERROR;
  2991. /* Skip reading the console buffer if the index pointer has not moved */
  2992. if (idx == c->last)
  2993. return BCME_OK;
  2994. /* Read the console buffer */
  2995. addr = ltoh32(c->log.buf);
  2996. if ((rv = dhdsdio_membytes(bus, FALSE, addr, c->buf, c->bufsize)) < 0)
  2997. return rv;
  2998. while (c->last != idx) {
  2999. for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
  3000. if (c->last == idx) {
  3001. /* This would output a partial line. Instead, back up
  3002. * the buffer pointer and output this line next time around.
  3003. */
  3004. if (c->last >= n)
  3005. c->last -= n;
  3006. else
  3007. c->last = c->bufsize - n;
  3008. goto break2;
  3009. }
  3010. ch = c->buf[c->last];
  3011. c->last = (c->last + 1) % c->bufsize;
  3012. if (ch == '\n')
  3013. break;
  3014. line[n] = ch;
  3015. }
  3016. if (n > 0) {
  3017. if (line[n - 1] == '\r')
  3018. n--;
  3019. line[n] = 0;
  3020. printf("CONSOLE: %s\n", line);
  3021. #ifdef LOG_INTO_TCPDUMP
  3022. dhd_sendup_log(bus->dhd, line, n);
  3023. #endif /* LOG_INTO_TCPDUMP */
  3024. }
  3025. }
  3026. break2:
  3027. return BCME_OK;
  3028. }
  3029. #endif /* DHD_DEBUG */
  3030. static int
  3031. dhdsdio_checkdied(dhd_bus_t *bus, char *data, uint size)
  3032. {
  3033. int bcmerror = 0;
  3034. uint msize = 512;
  3035. char *mbuffer = NULL;
  3036. char *console_buffer = NULL;
  3037. uint maxstrlen = 256;
  3038. char *str = NULL;
  3039. sdpcm_shared_t l_sdpcm_shared;
  3040. struct bcmstrbuf strbuf;
  3041. uint32 console_ptr, console_size, console_index;
  3042. uint8 line[CONSOLE_LINE_MAX], ch;
  3043. uint32 n, i, addr;
  3044. int rv;
  3045. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  3046. if (DHD_NOCHECKDIED_ON())
  3047. return 0;
  3048. if (data == NULL) {
  3049. /*
  3050. * Called after a rx ctrl timeout. "data" is NULL.
  3051. * allocate memory to trace the trap or assert.
  3052. */
  3053. size = msize;
  3054. mbuffer = data = MALLOC(bus->dhd->osh, msize);
  3055. if (mbuffer == NULL) {
  3056. DHD_ERROR(("%s: MALLOC(%d) failed \n", __FUNCTION__, msize));
  3057. bcmerror = BCME_NOMEM;
  3058. goto done;
  3059. }
  3060. }
  3061. if ((str = MALLOC(bus->dhd->osh, maxstrlen)) == NULL) {
  3062. DHD_ERROR(("%s: MALLOC(%d) failed \n", __FUNCTION__, maxstrlen));
  3063. bcmerror = BCME_NOMEM;
  3064. goto done;
  3065. }
  3066. if ((bcmerror = dhdsdio_readshared(bus, &l_sdpcm_shared)) < 0)
  3067. goto done;
  3068. bcm_binit(&strbuf, data, size);
  3069. bcm_bprintf(&strbuf, "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
  3070. l_sdpcm_shared.msgtrace_addr, l_sdpcm_shared.console_addr);
  3071. if ((l_sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
  3072. /* NOTE: Misspelled assert is intentional - DO NOT FIX.
  3073. * (Avoids conflict with real asserts for programmatic parsing of output.)
  3074. */
  3075. bcm_bprintf(&strbuf, "Assrt not built in dongle\n");
  3076. }
  3077. if ((l_sdpcm_shared.flags & (SDPCM_SHARED_ASSERT|SDPCM_SHARED_TRAP)) == 0) {
  3078. /* NOTE: Misspelled assert is intentional - DO NOT FIX.
  3079. * (Avoids conflict with real asserts for programmatic parsing of output.)
  3080. */
  3081. bcm_bprintf(&strbuf, "No trap%s in dongle",
  3082. (l_sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
  3083. ?"/assrt" :"");
  3084. } else {
  3085. if (l_sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
  3086. /* Download assert */
  3087. bcm_bprintf(&strbuf, "Dongle assert");
  3088. if (l_sdpcm_shared.assert_exp_addr != 0) {
  3089. str[0] = '\0';
  3090. if ((bcmerror = dhdsdio_membytes(bus, FALSE,
  3091. l_sdpcm_shared.assert_exp_addr,
  3092. (uint8 *)str, maxstrlen)) < 0)
  3093. goto done;
  3094. str[maxstrlen - 1] = '\0';
  3095. bcm_bprintf(&strbuf, " expr \"%s\"", str);
  3096. }
  3097. if (l_sdpcm_shared.assert_file_addr != 0) {
  3098. str[0] = '\0';
  3099. if ((bcmerror = dhdsdio_membytes(bus, FALSE,
  3100. l_sdpcm_shared.assert_file_addr,
  3101. (uint8 *)str, maxstrlen)) < 0)
  3102. goto done;
  3103. str[maxstrlen - 1] = '\0';
  3104. bcm_bprintf(&strbuf, " file \"%s\"", str);
  3105. }
  3106. bcm_bprintf(&strbuf, " line %d ", l_sdpcm_shared.assert_line);
  3107. }
  3108. if (l_sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
  3109. trap_t *tr = &bus->dhd->last_trap_info;
  3110. bus->dhd->dongle_trap_occured = TRUE;
  3111. if ((bcmerror = dhdsdio_membytes(bus, FALSE,
  3112. l_sdpcm_shared.trap_addr,
  3113. (uint8*)tr, sizeof(trap_t))) < 0)
  3114. goto done;
  3115. bus->dongle_trap_addr = ltoh32(l_sdpcm_shared.trap_addr);
  3116. dhd_bus_dump_trap_info(bus, &strbuf);
  3117. addr = l_sdpcm_shared.console_addr + OFFSETOF(hnd_cons_t, log);
  3118. if ((rv = dhdsdio_membytes(bus, FALSE, addr,
  3119. (uint8 *)&console_ptr, sizeof(console_ptr))) < 0)
  3120. goto printbuf;
  3121. addr = l_sdpcm_shared.console_addr + OFFSETOF(hnd_cons_t, log.buf_size);
  3122. if ((rv = dhdsdio_membytes(bus, FALSE, addr,
  3123. (uint8 *)&console_size, sizeof(console_size))) < 0)
  3124. goto printbuf;
  3125. addr = l_sdpcm_shared.console_addr + OFFSETOF(hnd_cons_t, log.idx);
  3126. if ((rv = dhdsdio_membytes(bus, FALSE, addr,
  3127. (uint8 *)&console_index, sizeof(console_index))) < 0)
  3128. goto printbuf;
  3129. console_ptr = ltoh32(console_ptr);
  3130. console_size = ltoh32(console_size);
  3131. console_index = ltoh32(console_index);
  3132. if (console_size > CONSOLE_BUFFER_MAX ||
  3133. !(console_buffer = MALLOC(bus->dhd->osh, console_size)))
  3134. goto printbuf;
  3135. if ((rv = dhdsdio_membytes(bus, FALSE, console_ptr,
  3136. (uint8 *)console_buffer, console_size)) < 0)
  3137. goto printbuf;
  3138. for (i = 0, n = 0; i < console_size; i += n + 1) {
  3139. for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
  3140. ch = console_buffer[(console_index + i + n) % console_size];
  3141. if (ch == '\n')
  3142. break;
  3143. line[n] = ch;
  3144. }
  3145. if (n > 0) {
  3146. if (line[n - 1] == '\r')
  3147. n--;
  3148. line[n] = 0;
  3149. /* Don't use DHD_ERROR macro since we print
  3150. * a lot of information quickly. The macro
  3151. * will truncate a lot of the printfs
  3152. */
  3153. if (dhd_msg_level & DHD_ERROR_VAL)
  3154. printf("CONSOLE: %s\n", line);
  3155. }
  3156. }
  3157. }
  3158. }
  3159. printbuf:
  3160. if (l_sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) {
  3161. DHD_ERROR(("%s: %s\n", __FUNCTION__, strbuf.origbuf));
  3162. }
  3163. #if defined(DHD_FW_COREDUMP)
  3164. if (bus->dhd->memdump_enabled && (l_sdpcm_shared.flags & SDPCM_SHARED_TRAP)) {
  3165. /* Mem dump to a file on device */
  3166. bus->dhd->memdump_type = DUMP_TYPE_DONGLE_TRAP;
  3167. dhd_os_sdunlock(bus->dhd);
  3168. dhdsdio_mem_dump(bus);
  3169. dhd_os_sdlock(bus->dhd);
  3170. }
  3171. #endif /* #if defined(DHD_FW_COREDUMP) */
  3172. done:
  3173. if (mbuffer)
  3174. MFREE(bus->dhd->osh, mbuffer, msize);
  3175. if (str)
  3176. MFREE(bus->dhd->osh, str, maxstrlen);
  3177. if (console_buffer)
  3178. MFREE(bus->dhd->osh, console_buffer, console_size);
  3179. return bcmerror;
  3180. }
  3181. #if defined(DHD_FW_COREDUMP)
  3182. int
  3183. dhd_bus_mem_dump(dhd_pub_t *dhdp)
  3184. {
  3185. dhd_bus_t *bus = dhdp->bus;
  3186. if (dhdp->busstate == DHD_BUS_SUSPEND) {
  3187. DHD_ERROR(("%s: Bus is suspend so skip\n", __FUNCTION__));
  3188. return 0;
  3189. }
  3190. return dhdsdio_mem_dump(bus);
  3191. }
  3192. int
  3193. dhd_bus_get_mem_dump(dhd_pub_t *dhdp)
  3194. {
  3195. if (!dhdp) {
  3196. DHD_ERROR(("%s: dhdp is NULL\n", __FUNCTION__));
  3197. return BCME_ERROR;
  3198. }
  3199. return dhdsdio_get_mem_dump(dhdp->bus);
  3200. }
  3201. static int
  3202. dhdsdio_get_mem_dump(dhd_bus_t *bus)
  3203. {
  3204. int ret = BCME_ERROR;
  3205. int size = bus->ramsize; /* Full mem size */
  3206. uint32 start = bus->dongle_ram_base; /* Start address */
  3207. uint read_size = 0; /* Read size of each iteration */
  3208. uint8 *p_buf = NULL, *databuf = NULL;
  3209. /* Get full mem size */
  3210. p_buf = dhd_get_fwdump_buf(bus->dhd, size);
  3211. if (!p_buf) {
  3212. DHD_ERROR(("%s: Out of memory (%d bytes)\n",
  3213. __FUNCTION__, size));
  3214. return BCME_ERROR;
  3215. }
  3216. dhd_os_sdlock(bus->dhd);
  3217. BUS_WAKE(bus);
  3218. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  3219. /* Read mem content */
  3220. DHD_ERROR(("Dump dongle memory\n"));
  3221. databuf = p_buf;
  3222. while (size) {
  3223. read_size = MIN(MEMBLOCK, size);
  3224. ret = dhdsdio_membytes(bus, FALSE, start, databuf, read_size);
  3225. if (ret) {
  3226. DHD_ERROR(("%s: Error membytes %d\n", __FUNCTION__, ret));
  3227. ret = BCME_ERROR;
  3228. break;
  3229. }
  3230. /* Decrement size and increment start address */
  3231. size -= read_size;
  3232. start += read_size;
  3233. databuf += read_size;
  3234. }
  3235. if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched &&
  3236. NO_OTHER_ACTIVE_BUS_USER(bus)) {
  3237. bus->activity = FALSE;
  3238. dhdsdio_clkctl(bus, CLK_NONE, TRUE);
  3239. }
  3240. dhd_os_sdunlock(bus->dhd);
  3241. return ret;
  3242. }
  3243. static int
  3244. dhdsdio_mem_dump(dhd_bus_t *bus)
  3245. {
  3246. dhd_pub_t *dhdp;
  3247. int ret = BCME_ERROR;
  3248. dhdp = bus->dhd;
  3249. if (!dhdp) {
  3250. DHD_ERROR(("%s: dhdp is NULL\n", __FUNCTION__));
  3251. return ret;
  3252. }
  3253. ret = dhdsdio_get_mem_dump(bus);
  3254. if (ret) {
  3255. DHD_ERROR(("%s: failed to get mem dump, err=%d\n",
  3256. __FUNCTION__, ret));
  3257. } else {
  3258. /* schedule a work queue to perform actual memdump.
  3259. * dhd_mem_dump() performs the job
  3260. */
  3261. dhd_schedule_memdump(dhdp, dhdp->soc_ram, dhdp->soc_ram_length);
  3262. /* soc_ram free handled in dhd_{free,clear} */
  3263. }
  3264. return ret;
  3265. }
  3266. #endif /* DHD_FW_COREDUMP */
  3267. int
  3268. dhd_socram_dump(dhd_bus_t * bus)
  3269. {
  3270. #if defined(DHD_FW_COREDUMP)
  3271. return (dhdsdio_mem_dump(bus));
  3272. #else
  3273. return -1;
  3274. #endif // endif
  3275. }
  3276. int
  3277. dhdsdio_downloadvars(dhd_bus_t *bus, void *arg, int len)
  3278. {
  3279. int bcmerror = BCME_OK;
  3280. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  3281. if (bus->dhd->up &&
  3282. #ifdef DHD_ULP
  3283. (DHD_ULP_DISABLED == dhd_ulp_get_ulp_state(bus->dhd)) &&
  3284. #endif /* DHD_ULP */
  3285. 1) {
  3286. bcmerror = BCME_NOTDOWN;
  3287. goto err;
  3288. }
  3289. if (!len) {
  3290. bcmerror = BCME_BUFTOOSHORT;
  3291. goto err;
  3292. }
  3293. /* Free the old ones and replace with passed variables */
  3294. if (bus->vars)
  3295. MFREE(bus->dhd->osh, bus->vars, bus->varsz);
  3296. bus->vars = MALLOC(bus->dhd->osh, len);
  3297. bus->varsz = bus->vars ? len : 0;
  3298. if (bus->vars == NULL) {
  3299. bcmerror = BCME_NOMEM;
  3300. goto err;
  3301. }
  3302. /* Copy the passed variables, which should include the terminating double-null */
  3303. bcopy(arg, bus->vars, bus->varsz);
  3304. err:
  3305. return bcmerror;
  3306. }
  3307. #ifdef DHD_DEBUG
  3308. static int
  3309. dhd_serialconsole(dhd_bus_t *bus, bool set, bool enable, int *bcmerror)
  3310. {
  3311. int int_val;
  3312. uint32 addr, data, uart_enab = 0;
  3313. addr = SI_ENUM_BASE(bus->sih) + OFFSETOF(chipcregs_t, chipcontrol_addr);
  3314. data = SI_ENUM_BASE(bus->sih) + OFFSETOF(chipcregs_t, chipcontrol_data);
  3315. *bcmerror = 0;
  3316. bcmsdh_reg_write(bus->sdh, addr, 4, 1);
  3317. if (bcmsdh_regfail(bus->sdh)) {
  3318. *bcmerror = BCME_SDIO_ERROR;
  3319. return -1;
  3320. }
  3321. int_val = bcmsdh_reg_read(bus->sdh, data, 4);
  3322. if (bcmsdh_regfail(bus->sdh)) {
  3323. *bcmerror = BCME_SDIO_ERROR;
  3324. return -1;
  3325. }
  3326. if (!set)
  3327. return (int_val & uart_enab);
  3328. if (enable)
  3329. int_val |= uart_enab;
  3330. else
  3331. int_val &= ~uart_enab;
  3332. bcmsdh_reg_write(bus->sdh, data, 4, int_val);
  3333. if (bcmsdh_regfail(bus->sdh)) {
  3334. *bcmerror = BCME_SDIO_ERROR;
  3335. return -1;
  3336. }
  3337. return (int_val & uart_enab);
  3338. }
  3339. #endif // endif
  3340. static int
  3341. dhdsdio_doiovar(dhd_bus_t *bus, const bcm_iovar_t *vi, uint32 actionid, const char *name,
  3342. void *params, int plen, void *arg, int len, int val_size)
  3343. {
  3344. int bcmerror = 0;
  3345. int32 int_val = 0;
  3346. bool bool_val = 0;
  3347. DHD_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p len %d val_size %d\n",
  3348. __FUNCTION__, actionid, name, params, plen, arg, len, val_size));
  3349. if ((bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid))) != 0)
  3350. goto exit;
  3351. if (plen >= (int)sizeof(int_val))
  3352. bcopy(params, &int_val, sizeof(int_val));
  3353. bool_val = (int_val != 0) ? TRUE : FALSE;
  3354. /* Some ioctls use the bus */
  3355. dhd_os_sdlock(bus->dhd);
  3356. /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
  3357. if (bus->dhd->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
  3358. actionid == IOV_GVAL(IOV_DEVRESET))) {
  3359. bcmerror = BCME_NOTREADY;
  3360. goto exit;
  3361. }
  3362. /*
  3363. * Special handling for keepSdioOn: New SDIO Wake-up Mechanism
  3364. */
  3365. if ((vi->varid == IOV_KSO) && (IOV_ISSET(actionid))) {
  3366. dhdsdio_clk_kso_iovar(bus, bool_val);
  3367. goto exit;
  3368. } else if ((vi->varid == IOV_DEVSLEEP) && (IOV_ISSET(actionid))) {
  3369. {
  3370. dhdsdio_clk_devsleep_iovar(bus, bool_val);
  3371. if (!SLPAUTO_ENAB(bus) && (bool_val == FALSE) && (bus->ipend)) {
  3372. DHD_ERROR(("INT pending in devsleep 1, dpc_sched: %d\n",
  3373. bus->dpc_sched));
  3374. if (!bus->dpc_sched) {
  3375. bus->dpc_sched = TRUE;
  3376. dhd_sched_dpc(bus->dhd);
  3377. }
  3378. }
  3379. }
  3380. goto exit;
  3381. }
  3382. /* Handle sleep stuff before any clock mucking */
  3383. if (vi->varid == IOV_SLEEP) {
  3384. if (IOV_ISSET(actionid)) {
  3385. bcmerror = dhdsdio_bussleep(bus, bool_val);
  3386. } else {
  3387. int_val = (int32)bus->sleeping;
  3388. bcopy(&int_val, arg, val_size);
  3389. }
  3390. goto exit;
  3391. }
  3392. /* Request clock to allow SDIO accesses */
  3393. if (!bus->dhd->dongle_reset) {
  3394. BUS_WAKE(bus);
  3395. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  3396. }
  3397. switch (actionid) {
  3398. case IOV_GVAL(IOV_INTR):
  3399. int_val = (int32)bus->intr;
  3400. bcopy(&int_val, arg, val_size);
  3401. break;
  3402. case IOV_SVAL(IOV_INTR):
  3403. bus->intr = bool_val;
  3404. bus->intdis = FALSE;
  3405. if (bus->dhd->up) {
  3406. if (bus->intr) {
  3407. DHD_INTR(("%s: enable SDIO device interrupts\n", __FUNCTION__));
  3408. bcmsdh_intr_enable(bus->sdh);
  3409. } else {
  3410. DHD_INTR(("%s: disable SDIO interrupts\n", __FUNCTION__));
  3411. bcmsdh_intr_disable(bus->sdh);
  3412. }
  3413. }
  3414. break;
  3415. case IOV_GVAL(IOV_POLLRATE):
  3416. int_val = (int32)bus->pollrate;
  3417. bcopy(&int_val, arg, val_size);
  3418. break;
  3419. case IOV_SVAL(IOV_POLLRATE):
  3420. bus->pollrate = (uint)int_val;
  3421. bus->poll = (bus->pollrate != 0);
  3422. break;
  3423. case IOV_GVAL(IOV_IDLETIME):
  3424. int_val = bus->idletime;
  3425. bcopy(&int_val, arg, val_size);
  3426. break;
  3427. case IOV_SVAL(IOV_IDLETIME):
  3428. if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE)) {
  3429. bcmerror = BCME_BADARG;
  3430. } else {
  3431. bus->idletime = int_val;
  3432. }
  3433. break;
  3434. case IOV_GVAL(IOV_IDLECLOCK):
  3435. int_val = (int32)bus->idleclock;
  3436. bcopy(&int_val, arg, val_size);
  3437. break;
  3438. case IOV_SVAL(IOV_IDLECLOCK):
  3439. bus->idleclock = int_val;
  3440. break;
  3441. case IOV_GVAL(IOV_SD1IDLE):
  3442. int_val = (int32)sd1idle;
  3443. bcopy(&int_val, arg, val_size);
  3444. break;
  3445. case IOV_SVAL(IOV_SD1IDLE):
  3446. sd1idle = bool_val;
  3447. break;
  3448. #ifdef DHD_DEBUG
  3449. case IOV_GVAL(IOV_CHECKDIED):
  3450. bcmerror = dhdsdio_checkdied(bus, arg, len);
  3451. break;
  3452. #endif /* DHD_DEBUG */
  3453. #ifdef DHD_BUS_MEM_ACCESS
  3454. case IOV_SVAL(IOV_MEMBYTES):
  3455. case IOV_GVAL(IOV_MEMBYTES):
  3456. {
  3457. uint32 address;
  3458. uint size, dsize;
  3459. uint8 *data;
  3460. bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
  3461. ASSERT(plen >= 2*sizeof(int));
  3462. address = (uint32)int_val;
  3463. bcopy((char *)params + sizeof(int_val), &int_val, sizeof(int_val));
  3464. size = (uint)int_val;
  3465. /* Do some validation */
  3466. dsize = set ? plen - (2 * sizeof(int)) : len;
  3467. if (dsize < size) {
  3468. DHD_ERROR(("%s: error on %s membytes, addr 0x%08x size %d dsize %d\n",
  3469. __FUNCTION__, (set ? "set" : "get"), address, size, dsize));
  3470. bcmerror = BCME_BADARG;
  3471. break;
  3472. }
  3473. DHD_INFO(("%s: Request to %s %d bytes at address 0x%08x\n", __FUNCTION__,
  3474. (set ? "write" : "read"), size, address));
  3475. /* check if CR4 */
  3476. if (si_setcore(bus->sih, ARMCR4_CORE_ID, 0)) {
  3477. /*
  3478. * If address is start of RAM (i.e. a downloaded image),
  3479. * store the reset instruction to be written in 0
  3480. */
  3481. if (set && address == bus->dongle_ram_base) {
  3482. bus->resetinstr = *(((uint32*)params) + 2);
  3483. }
  3484. } else {
  3485. /* If we know about SOCRAM, check for a fit */
  3486. if ((bus->orig_ramsize) &&
  3487. ((address > bus->orig_ramsize) || (address + size > bus->orig_ramsize)))
  3488. {
  3489. uint8 enable, protect, remap;
  3490. si_socdevram(bus->sih, FALSE, &enable, &protect, &remap);
  3491. if (!enable || protect) {
  3492. DHD_ERROR(("%s: ramsize 0x%08x doesn't have %d bytes at 0x%08x\n",
  3493. __FUNCTION__, bus->orig_ramsize, size, address));
  3494. DHD_ERROR(("%s: socram enable %d, protect %d\n",
  3495. __FUNCTION__, enable, protect));
  3496. bcmerror = BCME_BADARG;
  3497. break;
  3498. }
  3499. if (!REMAP_ENAB(bus) && (address >= SOCDEVRAM_ARM_ADDR)) {
  3500. uint32 devramsize = si_socdevram_size(bus->sih);
  3501. if ((address < SOCDEVRAM_ARM_ADDR) ||
  3502. (address + size > (SOCDEVRAM_ARM_ADDR + devramsize))) {
  3503. DHD_ERROR(("%s: bad address 0x%08x, size 0x%08x\n",
  3504. __FUNCTION__, address, size));
  3505. DHD_ERROR(("%s: socram range 0x%08x,size 0x%08x\n",
  3506. __FUNCTION__, SOCDEVRAM_ARM_ADDR, devramsize));
  3507. bcmerror = BCME_BADARG;
  3508. break;
  3509. }
  3510. /* move it such that address is real now */
  3511. address -= SOCDEVRAM_ARM_ADDR;
  3512. address += SOCDEVRAM_BP_ADDR;
  3513. DHD_INFO(("%s: Request to %s %d bytes @ Mapped address 0x%08x\n",
  3514. __FUNCTION__, (set ? "write" : "read"), size, address));
  3515. } else if (REMAP_ENAB(bus) && REMAP_ISADDR(bus, address) && remap) {
  3516. /* Can not access remap region while devram remap bit is set
  3517. * ROM content would be returned in this case
  3518. */
  3519. DHD_ERROR(("%s: Need to disable remap for address 0x%08x\n",
  3520. __FUNCTION__, address));
  3521. bcmerror = BCME_ERROR;
  3522. break;
  3523. }
  3524. }
  3525. }
  3526. /* Generate the actual data pointer */
  3527. data = set ? (uint8*)params + 2 * sizeof(int): (uint8*)arg;
  3528. /* Call to do the transfer */
  3529. bcmerror = dhdsdio_membytes(bus, set, address, data, size);
  3530. break;
  3531. }
  3532. #endif /* DHD_BUS_MEM_ACCESS */
  3533. case IOV_GVAL(IOV_RAMSIZE):
  3534. int_val = (int32)bus->ramsize;
  3535. bcopy(&int_val, arg, val_size);
  3536. break;
  3537. case IOV_GVAL(IOV_RAMSTART):
  3538. int_val = (int32)bus->dongle_ram_base;
  3539. bcopy(&int_val, arg, val_size);
  3540. break;
  3541. case IOV_GVAL(IOV_SDIOD_DRIVE):
  3542. int_val = (int32)dhd_sdiod_drive_strength;
  3543. bcopy(&int_val, arg, val_size);
  3544. break;
  3545. case IOV_SVAL(IOV_SDIOD_DRIVE):
  3546. dhd_sdiod_drive_strength = int_val;
  3547. si_sdiod_drive_strength_init(bus->sih, bus->dhd->osh, dhd_sdiod_drive_strength);
  3548. break;
  3549. case IOV_SVAL(IOV_SET_DOWNLOAD_STATE):
  3550. bcmerror = dhdsdio_download_state(bus, bool_val);
  3551. break;
  3552. case IOV_SVAL(IOV_SOCRAM_STATE):
  3553. bcmerror = dhdsdio_download_state(bus, bool_val);
  3554. break;
  3555. case IOV_SVAL(IOV_VARS):
  3556. bcmerror = dhdsdio_downloadvars(bus, arg, len);
  3557. break;
  3558. case IOV_GVAL(IOV_READAHEAD):
  3559. int_val = (int32)dhd_readahead;
  3560. bcopy(&int_val, arg, val_size);
  3561. break;
  3562. case IOV_SVAL(IOV_READAHEAD):
  3563. if (bool_val && !dhd_readahead)
  3564. bus->nextlen = 0;
  3565. dhd_readahead = bool_val;
  3566. break;
  3567. case IOV_GVAL(IOV_SDRXCHAIN):
  3568. int_val = (int32)bus->use_rxchain;
  3569. bcopy(&int_val, arg, val_size);
  3570. break;
  3571. case IOV_SVAL(IOV_SDRXCHAIN):
  3572. if (bool_val && !bus->sd_rxchain)
  3573. bcmerror = BCME_UNSUPPORTED;
  3574. else
  3575. bus->use_rxchain = bool_val;
  3576. break;
  3577. #ifndef BCMSPI
  3578. case IOV_GVAL(IOV_ALIGNCTL):
  3579. int_val = (int32)dhd_alignctl;
  3580. bcopy(&int_val, arg, val_size);
  3581. break;
  3582. case IOV_SVAL(IOV_ALIGNCTL):
  3583. dhd_alignctl = bool_val;
  3584. break;
  3585. #endif /* BCMSPI */
  3586. case IOV_GVAL(IOV_SDALIGN):
  3587. int_val = DHD_SDALIGN;
  3588. bcopy(&int_val, arg, val_size);
  3589. break;
  3590. #ifdef DHD_DEBUG
  3591. case IOV_GVAL(IOV_VARS):
  3592. if (bus->varsz < (uint)len)
  3593. bcopy(bus->vars, arg, bus->varsz);
  3594. else
  3595. bcmerror = BCME_BUFTOOSHORT;
  3596. break;
  3597. #endif /* DHD_DEBUG */
  3598. #ifdef DHD_DEBUG
  3599. case IOV_GVAL(IOV_SDREG):
  3600. {
  3601. sdreg_t *sd_ptr;
  3602. uintptr addr;
  3603. uint size;
  3604. sd_ptr = (sdreg_t *)params;
  3605. addr = ((uintptr)bus->regs + sd_ptr->offset);
  3606. size = sd_ptr->func;
  3607. int_val = (int32)bcmsdh_reg_read(bus->sdh, addr, size);
  3608. if (bcmsdh_regfail(bus->sdh))
  3609. bcmerror = BCME_SDIO_ERROR;
  3610. bcopy(&int_val, arg, sizeof(int32));
  3611. break;
  3612. }
  3613. case IOV_SVAL(IOV_SDREG):
  3614. {
  3615. sdreg_t *sd_ptr;
  3616. uintptr addr;
  3617. uint size;
  3618. sd_ptr = (sdreg_t *)params;
  3619. addr = ((uintptr)bus->regs + sd_ptr->offset);
  3620. size = sd_ptr->func;
  3621. bcmsdh_reg_write(bus->sdh, addr, size, sd_ptr->value);
  3622. if (bcmsdh_regfail(bus->sdh))
  3623. bcmerror = BCME_SDIO_ERROR;
  3624. break;
  3625. }
  3626. /* Same as above, but offset is not backplane (not SDIO core) */
  3627. case IOV_GVAL(IOV_SBREG):
  3628. {
  3629. sdreg_t sdreg;
  3630. uint32 addr, size;
  3631. bcopy(params, &sdreg, sizeof(sdreg));
  3632. addr = SI_ENUM_BASE(bus->sih) + sdreg.offset;
  3633. size = sdreg.func;
  3634. int_val = (int32)bcmsdh_reg_read(bus->sdh, addr, size);
  3635. if (bcmsdh_regfail(bus->sdh))
  3636. bcmerror = BCME_SDIO_ERROR;
  3637. bcopy(&int_val, arg, sizeof(int32));
  3638. break;
  3639. }
  3640. case IOV_SVAL(IOV_SBREG):
  3641. {
  3642. sdreg_t sdreg;
  3643. uint32 addr, size;
  3644. bcopy(params, &sdreg, sizeof(sdreg));
  3645. addr = SI_ENUM_BASE(bus->sih) + sdreg.offset;
  3646. size = sdreg.func;
  3647. bcmsdh_reg_write(bus->sdh, addr, size, sdreg.value);
  3648. if (bcmsdh_regfail(bus->sdh))
  3649. bcmerror = BCME_SDIO_ERROR;
  3650. break;
  3651. }
  3652. case IOV_GVAL(IOV_SDCIS):
  3653. {
  3654. *(char *)arg = 0;
  3655. bcmstrcat(arg, "\nFunc 0\n");
  3656. bcmsdh_cis_read(bus->sdh, 0x10, (uint8 *)arg + strlen(arg), SBSDIO_CIS_SIZE_LIMIT);
  3657. bcmstrcat(arg, "\nFunc 1\n");
  3658. bcmsdh_cis_read(bus->sdh, 0x11, (uint8 *)arg + strlen(arg), SBSDIO_CIS_SIZE_LIMIT);
  3659. bcmstrcat(arg, "\nFunc 2\n");
  3660. bcmsdh_cis_read(bus->sdh, 0x12, (uint8 *)arg + strlen(arg), SBSDIO_CIS_SIZE_LIMIT);
  3661. break;
  3662. }
  3663. case IOV_GVAL(IOV_FORCEEVEN):
  3664. int_val = (int32)forcealign;
  3665. bcopy(&int_val, arg, val_size);
  3666. break;
  3667. case IOV_SVAL(IOV_FORCEEVEN):
  3668. forcealign = bool_val;
  3669. break;
  3670. case IOV_GVAL(IOV_TXBOUND):
  3671. int_val = (int32)dhd_txbound;
  3672. bcopy(&int_val, arg, val_size);
  3673. break;
  3674. case IOV_SVAL(IOV_TXBOUND):
  3675. dhd_txbound = (uint)int_val;
  3676. break;
  3677. case IOV_GVAL(IOV_RXBOUND):
  3678. int_val = (int32)dhd_rxbound;
  3679. bcopy(&int_val, arg, val_size);
  3680. break;
  3681. case IOV_SVAL(IOV_RXBOUND):
  3682. dhd_rxbound = (uint)int_val;
  3683. break;
  3684. case IOV_GVAL(IOV_TXMINMAX):
  3685. int_val = (int32)dhd_txminmax;
  3686. bcopy(&int_val, arg, val_size);
  3687. break;
  3688. case IOV_SVAL(IOV_TXMINMAX):
  3689. dhd_txminmax = (uint)int_val;
  3690. break;
  3691. #ifdef DHD_DEBUG
  3692. case IOV_GVAL(IOV_SERIALCONS):
  3693. int_val = dhd_serialconsole(bus, FALSE, 0, &bcmerror);
  3694. if (bcmerror != 0)
  3695. break;
  3696. bcopy(&int_val, arg, val_size);
  3697. break;
  3698. case IOV_SVAL(IOV_SERIALCONS):
  3699. dhd_serialconsole(bus, TRUE, bool_val, &bcmerror);
  3700. break;
  3701. #endif /* DHD_DEBUG */
  3702. #endif /* DHD_DEBUG */
  3703. #ifdef SDTEST
  3704. case IOV_GVAL(IOV_EXTLOOP):
  3705. int_val = (int32)bus->ext_loop;
  3706. bcopy(&int_val, arg, val_size);
  3707. break;
  3708. case IOV_SVAL(IOV_EXTLOOP):
  3709. bus->ext_loop = bool_val;
  3710. break;
  3711. case IOV_GVAL(IOV_PKTGEN):
  3712. bcmerror = dhdsdio_pktgen_get(bus, arg);
  3713. break;
  3714. case IOV_SVAL(IOV_PKTGEN):
  3715. bcmerror = dhdsdio_pktgen_set(bus, arg);
  3716. break;
  3717. #endif /* SDTEST */
  3718. #if defined(USE_SDIOFIFO_IOVAR)
  3719. case IOV_GVAL(IOV_WATERMARK):
  3720. int_val = (int32)watermark;
  3721. bcopy(&int_val, arg, val_size);
  3722. break;
  3723. case IOV_SVAL(IOV_WATERMARK):
  3724. watermark = (uint)int_val;
  3725. watermark = (watermark > SBSDIO_WATERMARK_MASK) ? SBSDIO_WATERMARK_MASK : watermark;
  3726. DHD_ERROR(("Setting watermark as 0x%x.\n", watermark));
  3727. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK, (uint8)watermark, NULL);
  3728. break;
  3729. case IOV_GVAL(IOV_MESBUSYCTRL):
  3730. int_val = (int32)mesbusyctrl;
  3731. bcopy(&int_val, arg, val_size);
  3732. break;
  3733. case IOV_SVAL(IOV_MESBUSYCTRL):
  3734. mesbusyctrl = (uint)int_val;
  3735. mesbusyctrl = (mesbusyctrl > SBSDIO_MESBUSYCTRL_MASK)
  3736. ? SBSDIO_MESBUSYCTRL_MASK : mesbusyctrl;
  3737. DHD_ERROR(("Setting mesbusyctrl as 0x%x.\n", mesbusyctrl));
  3738. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_MESBUSYCTRL,
  3739. ((uint8)mesbusyctrl | 0x80), NULL);
  3740. break;
  3741. #endif // endif
  3742. case IOV_GVAL(IOV_DONGLEISOLATION):
  3743. int_val = bus->dhd->dongle_isolation;
  3744. bcopy(&int_val, arg, val_size);
  3745. break;
  3746. case IOV_SVAL(IOV_DONGLEISOLATION):
  3747. bus->dhd->dongle_isolation = bool_val;
  3748. break;
  3749. case IOV_SVAL(IOV_DEVRESET):
  3750. DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d busstate=%d\n",
  3751. __FUNCTION__, bool_val, bus->dhd->dongle_reset,
  3752. bus->dhd->busstate));
  3753. ASSERT(bus->dhd->osh);
  3754. /* ASSERT(bus->cl_devid); */
  3755. /* must release sdlock, since devreset also acquires it */
  3756. dhd_os_sdunlock(bus->dhd);
  3757. dhd_bus_devreset(bus->dhd, (uint8)bool_val);
  3758. dhd_os_sdlock(bus->dhd);
  3759. break;
  3760. /*
  3761. * softap firmware is updated through module parameter or android private command
  3762. */
  3763. case IOV_GVAL(IOV_DEVRESET):
  3764. DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __FUNCTION__));
  3765. /* Get its status */
  3766. int_val = (bool) bus->dhd->dongle_reset;
  3767. bcopy(&int_val, arg, val_size);
  3768. break;
  3769. case IOV_GVAL(IOV_KSO):
  3770. int_val = dhdsdio_sleepcsr_get(bus);
  3771. bcopy(&int_val, arg, val_size);
  3772. break;
  3773. case IOV_GVAL(IOV_DEVCAP):
  3774. int_val = dhdsdio_devcap_get(bus);
  3775. bcopy(&int_val, arg, val_size);
  3776. break;
  3777. case IOV_SVAL(IOV_DEVCAP):
  3778. dhdsdio_devcap_set(bus, (uint8) int_val);
  3779. break;
  3780. case IOV_GVAL(IOV_TXGLOMSIZE):
  3781. int_val = (int32)bus->txglomsize;
  3782. bcopy(&int_val, arg, val_size);
  3783. break;
  3784. case IOV_SVAL(IOV_TXGLOMSIZE):
  3785. if (int_val > SDPCM_MAXGLOM_SIZE) {
  3786. bcmerror = BCME_ERROR;
  3787. } else {
  3788. bus->txglomsize = (uint)int_val;
  3789. }
  3790. break;
  3791. case IOV_SVAL(IOV_HANGREPORT):
  3792. bus->dhd->hang_report = bool_val;
  3793. DHD_ERROR(("%s: Set hang_report as %d\n", __FUNCTION__, bus->dhd->hang_report));
  3794. break;
  3795. case IOV_GVAL(IOV_HANGREPORT):
  3796. int_val = (int32)bus->dhd->hang_report;
  3797. bcopy(&int_val, arg, val_size);
  3798. break;
  3799. case IOV_GVAL(IOV_TXINRX_THRES):
  3800. int_val = bus->txinrx_thres;
  3801. bcopy(&int_val, arg, val_size);
  3802. break;
  3803. case IOV_SVAL(IOV_TXINRX_THRES):
  3804. if (int_val < 0) {
  3805. bcmerror = BCME_BADARG;
  3806. } else {
  3807. bus->txinrx_thres = int_val;
  3808. }
  3809. break;
  3810. case IOV_GVAL(IOV_SDIO_SUSPEND):
  3811. int_val = (bus->dhd->busstate == DHD_BUS_SUSPEND) ? 1 : 0;
  3812. bcopy(&int_val, arg, val_size);
  3813. break;
  3814. case IOV_SVAL(IOV_SDIO_SUSPEND):
  3815. if (bool_val) { /* Suspend */
  3816. dhdsdio_suspend(bus);
  3817. }
  3818. else { /* Resume */
  3819. dhdsdio_resume(bus);
  3820. }
  3821. break;
  3822. #if defined(DEBUGGER) || defined(DHD_DSCOPE)
  3823. case IOV_SVAL(IOV_GDB_SERVER):
  3824. if (bool_val == TRUE) {
  3825. debugger_init((void *) bus, &bus_ops, int_val, SI_ENUM_BASE(bus->sih));
  3826. } else {
  3827. debugger_close();
  3828. }
  3829. break;
  3830. #endif /* DEBUGGER || DHD_DSCOPE */
  3831. default:
  3832. bcmerror = BCME_UNSUPPORTED;
  3833. break;
  3834. }
  3835. exit:
  3836. if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched &&
  3837. NO_OTHER_ACTIVE_BUS_USER(bus)) {
  3838. bus->activity = FALSE;
  3839. dhdsdio_bussleep(bus, TRUE);
  3840. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  3841. }
  3842. dhd_os_sdunlock(bus->dhd);
  3843. return bcmerror;
  3844. }
  3845. static int
  3846. dhdsdio_write_vars(dhd_bus_t *bus)
  3847. {
  3848. int bcmerror = 0;
  3849. uint32 varsize, phys_size;
  3850. uint32 varaddr;
  3851. uint8 *vbuffer;
  3852. uint32 varsizew;
  3853. #ifdef DHD_DEBUG
  3854. uint8 *nvram_ularray;
  3855. #endif /* DHD_DEBUG */
  3856. /* Even if there are no vars are to be written, we still need to set the ramsize. */
  3857. varsize = bus->varsz ? ROUNDUP(bus->varsz, 4) : 0;
  3858. varaddr = (bus->ramsize - 4) - varsize;
  3859. varaddr += bus->dongle_ram_base;
  3860. if (bus->vars) {
  3861. if ((bus->sih->buscoretype == SDIOD_CORE_ID) && (bus->sdpcmrev == 7)) {
  3862. if (((varaddr & 0x3C) == 0x3C) && (varsize > 4)) {
  3863. DHD_ERROR(("PR85623WAR in place\n"));
  3864. varsize += 4;
  3865. varaddr -= 4;
  3866. }
  3867. }
  3868. vbuffer = (uint8 *)MALLOC(bus->dhd->osh, varsize);
  3869. if (!vbuffer)
  3870. return BCME_NOMEM;
  3871. bzero(vbuffer, varsize);
  3872. bcopy(bus->vars, vbuffer, bus->varsz);
  3873. /* Write the vars list */
  3874. bcmerror = dhdsdio_membytes(bus, TRUE, varaddr, vbuffer, varsize);
  3875. if (bcmerror) {
  3876. DHD_ERROR(("%s: error %d on writing %d membytes at 0x%08x\n",
  3877. __FUNCTION__, bcmerror, varsize, varaddr));
  3878. return bcmerror;
  3879. }
  3880. #ifdef DHD_DEBUG
  3881. /* Verify NVRAM bytes */
  3882. DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
  3883. nvram_ularray = (uint8*)MALLOC(bus->dhd->osh, varsize);
  3884. if (!nvram_ularray) {
  3885. MFREE(bus->dhd->osh, vbuffer, varsize);
  3886. return BCME_NOMEM;
  3887. }
  3888. /* Upload image to verify downloaded contents. */
  3889. memset(nvram_ularray, 0xaa, varsize);
  3890. /* Read the vars list to temp buffer for comparison */
  3891. bcmerror = dhdsdio_membytes(bus, FALSE, varaddr, nvram_ularray, varsize);
  3892. if (bcmerror) {
  3893. DHD_ERROR(("%s: error %d on reading %d nvram bytes at 0x%08x\n",
  3894. __FUNCTION__, bcmerror, varsize, varaddr));
  3895. }
  3896. /* Compare the org NVRAM with the one read from RAM */
  3897. if (memcmp(vbuffer, nvram_ularray, varsize)) {
  3898. DHD_ERROR(("%s: Downloaded NVRAM image is corrupted.\n", __FUNCTION__));
  3899. } else
  3900. DHD_ERROR(("%s: Download, Upload and compare of NVRAM succeeded.\n",
  3901. __FUNCTION__));
  3902. MFREE(bus->dhd->osh, nvram_ularray, varsize);
  3903. #endif /* DHD_DEBUG */
  3904. MFREE(bus->dhd->osh, vbuffer, varsize);
  3905. }
  3906. phys_size = REMAP_ENAB(bus) ? bus->ramsize : bus->orig_ramsize;
  3907. phys_size += bus->dongle_ram_base;
  3908. /* adjust to the user specified RAM */
  3909. DHD_INFO(("Physical memory size: %d, usable memory size: %d\n",
  3910. phys_size, bus->ramsize));
  3911. DHD_INFO(("Vars are at %d, orig varsize is %d\n",
  3912. varaddr, varsize));
  3913. varsize = ((phys_size - 4) - varaddr);
  3914. /*
  3915. * Determine the length token:
  3916. * Varsize, converted to words, in lower 16-bits, checksum in upper 16-bits.
  3917. */
  3918. #ifdef DHD_DEBUG
  3919. if (bcmerror) {
  3920. varsizew = 0;
  3921. } else
  3922. #endif /* DHD_DEBUG */
  3923. {
  3924. varsizew = varsize / 4;
  3925. varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
  3926. varsizew = htol32(varsizew);
  3927. }
  3928. DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize, varsizew));
  3929. /* Write the length token to the last word */
  3930. bcmerror = dhdsdio_membytes(bus, TRUE, (phys_size - 4),
  3931. (uint8*)&varsizew, 4);
  3932. return bcmerror;
  3933. }
  3934. bool
  3935. dhd_bus_is_multibp_capable(struct dhd_bus *bus)
  3936. {
  3937. return MULTIBP_CAP(bus->sih);
  3938. }
  3939. static int
  3940. dhdsdio_download_state(dhd_bus_t *bus, bool enter)
  3941. {
  3942. uint retries;
  3943. int bcmerror = 0;
  3944. int foundcr4 = 0;
  3945. if (!bus->sih)
  3946. return BCME_ERROR;
  3947. /* To enter download state, disable ARM and reset SOCRAM.
  3948. * To exit download state, simply reset ARM (default is RAM boot).
  3949. */
  3950. if (enter) {
  3951. bus->alp_only = TRUE;
  3952. if (!(si_setcore(bus->sih, ARM7S_CORE_ID, 0)) &&
  3953. !(si_setcore(bus->sih, ARMCM3_CORE_ID, 0))) {
  3954. if (si_setcore(bus->sih, ARMCR4_CORE_ID, 0)) {
  3955. foundcr4 = 1;
  3956. } else {
  3957. DHD_ERROR(("%s: Failed to find ARM core!\n", __FUNCTION__));
  3958. bcmerror = BCME_ERROR;
  3959. goto fail;
  3960. }
  3961. }
  3962. if (!foundcr4) {
  3963. si_core_disable(bus->sih, 0);
  3964. if (bcmsdh_regfail(bus->sdh)) {
  3965. bcmerror = BCME_SDIO_ERROR;
  3966. goto fail;
  3967. }
  3968. if (!(si_setcore(bus->sih, SOCRAM_CORE_ID, 0))) {
  3969. DHD_ERROR(("%s: Failed to find SOCRAM core!\n", __FUNCTION__));
  3970. bcmerror = BCME_ERROR;
  3971. goto fail;
  3972. }
  3973. si_core_reset(bus->sih, 0, 0);
  3974. if (bcmsdh_regfail(bus->sdh)) {
  3975. DHD_ERROR(("%s: Failure trying reset SOCRAM core?\n",
  3976. __FUNCTION__));
  3977. bcmerror = BCME_SDIO_ERROR;
  3978. goto fail;
  3979. }
  3980. /* Disable remap for download */
  3981. if (REMAP_ENAB(bus) && si_socdevram_remap_isenb(bus->sih))
  3982. dhdsdio_devram_remap(bus, FALSE);
  3983. if (CHIPID(bus->sih->chip) == BCM43430_CHIP_ID ||
  3984. CHIPID(bus->sih->chip) == BCM43018_CHIP_ID) {
  3985. /* Disabling Remap for SRAM_3 */
  3986. si_socram_set_bankpda(bus->sih, 0x3, 0x0);
  3987. }
  3988. /* Clear the top bit of memory */
  3989. if (bus->ramsize) {
  3990. uint32 zeros = 0;
  3991. if (dhdsdio_membytes(bus, TRUE, bus->ramsize - 4,
  3992. (uint8*)&zeros, 4) < 0) {
  3993. bcmerror = BCME_SDIO_ERROR;
  3994. goto fail;
  3995. }
  3996. }
  3997. } else {
  3998. /* For CR4,
  3999. * Halt ARM
  4000. * Remove ARM reset
  4001. * Read RAM base address [0x18_0000]
  4002. * [next] Download firmware
  4003. * [done at else] Populate the reset vector
  4004. * [done at else] Remove ARM halt
  4005. */
  4006. /* Halt ARM & remove reset */
  4007. si_core_reset(bus->sih, SICF_CPUHALT, SICF_CPUHALT);
  4008. }
  4009. } else {
  4010. if (!si_setcore(bus->sih, ARMCR4_CORE_ID, 0)) {
  4011. if (!(si_setcore(bus->sih, SOCRAM_CORE_ID, 0))) {
  4012. DHD_ERROR(("%s: Failed to find SOCRAM core!\n", __FUNCTION__));
  4013. bcmerror = BCME_ERROR;
  4014. goto fail;
  4015. }
  4016. if (!si_iscoreup(bus->sih)) {
  4017. DHD_ERROR(("%s: SOCRAM core is down after reset?\n", __FUNCTION__));
  4018. bcmerror = BCME_ERROR;
  4019. goto fail;
  4020. }
  4021. if ((bcmerror = dhdsdio_write_vars(bus))) {
  4022. DHD_ERROR(("%s: could not write vars to RAM\n", __FUNCTION__));
  4023. goto fail;
  4024. }
  4025. /* Enable remap before ARM reset but after vars.
  4026. * No backplane access in remap mode
  4027. */
  4028. if (REMAP_ENAB(bus) && !si_socdevram_remap_isenb(bus->sih))
  4029. dhdsdio_devram_remap(bus, TRUE);
  4030. #ifdef BCMSDIOLITE
  4031. if (!si_setcore(bus->sih, CC_CORE_ID, 0)) {
  4032. DHD_ERROR(("%s: Can't set to Chip Common core?\n", __FUNCTION__));
  4033. bcmerror = BCME_ERROR;
  4034. goto fail;
  4035. }
  4036. #else
  4037. if (!si_setcore(bus->sih, PCMCIA_CORE_ID, 0) &&
  4038. !si_setcore(bus->sih, SDIOD_CORE_ID, 0)) {
  4039. DHD_ERROR(("%s: Can't change back to SDIO core?\n", __FUNCTION__));
  4040. bcmerror = BCME_ERROR;
  4041. goto fail;
  4042. }
  4043. #endif // endif
  4044. W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
  4045. if (!(si_setcore(bus->sih, ARM7S_CORE_ID, 0)) &&
  4046. !(si_setcore(bus->sih, ARMCM3_CORE_ID, 0))) {
  4047. DHD_ERROR(("%s: Failed to find ARM core!\n", __FUNCTION__));
  4048. bcmerror = BCME_ERROR;
  4049. goto fail;
  4050. }
  4051. } else {
  4052. /* cr4 has no socram, but tcm's */
  4053. /* write vars */
  4054. if ((bcmerror = dhdsdio_write_vars(bus))) {
  4055. DHD_ERROR(("%s: could not write vars to RAM\n", __FUNCTION__));
  4056. goto fail;
  4057. }
  4058. #ifdef BCMSDIOLITE
  4059. if (!si_setcore(bus->sih, CC_CORE_ID, 0)) {
  4060. DHD_ERROR(("%s: Can't set to Chip Common core?\n", __FUNCTION__));
  4061. bcmerror = BCME_ERROR;
  4062. goto fail;
  4063. }
  4064. #else
  4065. if (!si_setcore(bus->sih, PCMCIA_CORE_ID, 0) &&
  4066. !si_setcore(bus->sih, SDIOD_CORE_ID, 0)) {
  4067. DHD_ERROR(("%s: Can't change back to SDIO core?\n", __FUNCTION__));
  4068. bcmerror = BCME_ERROR;
  4069. goto fail;
  4070. }
  4071. #endif // endif
  4072. W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
  4073. /* switch back to arm core again */
  4074. if (!(si_setcore(bus->sih, ARMCR4_CORE_ID, 0))) {
  4075. DHD_ERROR(("%s: Failed to find ARM CR4 core!\n", __FUNCTION__));
  4076. bcmerror = BCME_ERROR;
  4077. goto fail;
  4078. }
  4079. /* write address 0 with reset instruction */
  4080. bcmerror = dhdsdio_membytes(bus, TRUE, 0,
  4081. (uint8 *)&bus->resetinstr, sizeof(bus->resetinstr));
  4082. if (bcmerror == BCME_OK) {
  4083. uint32 tmp;
  4084. /* verify write */
  4085. bcmerror = dhdsdio_membytes(bus, FALSE, 0,
  4086. (uint8 *)&tmp, sizeof(tmp));
  4087. if (bcmerror == BCME_OK && tmp != bus->resetinstr) {
  4088. DHD_ERROR(("%s: Failed to write 0x%08x to addr 0\n",
  4089. __FUNCTION__, bus->resetinstr));
  4090. DHD_ERROR(("%s: contents of addr 0 is 0x%08x\n",
  4091. __FUNCTION__, tmp));
  4092. bcmerror = BCME_SDIO_ERROR;
  4093. goto fail;
  4094. }
  4095. }
  4096. /* now remove reset and halt and continue to run CR4 */
  4097. }
  4098. si_core_reset(bus->sih, 0, 0);
  4099. if (bcmsdh_regfail(bus->sdh)) {
  4100. DHD_ERROR(("%s: Failure trying to reset ARM core?\n", __FUNCTION__));
  4101. bcmerror = BCME_SDIO_ERROR;
  4102. goto fail;
  4103. }
  4104. /* Allow HT Clock now that the ARM is running. */
  4105. bus->alp_only = FALSE;
  4106. bus->dhd->busstate = DHD_BUS_LOAD;
  4107. }
  4108. fail:
  4109. /* Always return to SDIOD core */
  4110. if (!si_setcore(bus->sih, PCMCIA_CORE_ID, 0))
  4111. si_setcore(bus->sih, SDIOD_CORE_ID, 0);
  4112. return bcmerror;
  4113. }
  4114. int
  4115. dhd_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
  4116. void *params, int plen, void *arg, int len, bool set)
  4117. {
  4118. dhd_bus_t *bus = dhdp->bus;
  4119. const bcm_iovar_t *vi = NULL;
  4120. int bcmerror = 0;
  4121. int val_size;
  4122. uint32 actionid;
  4123. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  4124. ASSERT(name);
  4125. ASSERT(len >= 0);
  4126. /* Get MUST have return space */
  4127. ASSERT(set || (arg && len));
  4128. /* Set does NOT take qualifiers */
  4129. ASSERT(!set || (!params && !plen));
  4130. /* Look up var locally; if not found pass to host driver */
  4131. if ((vi = bcm_iovar_lookup(dhdsdio_iovars, name)) == NULL) {
  4132. dhd_os_sdlock(bus->dhd);
  4133. BUS_WAKE(bus);
  4134. /* Turn on clock in case SD command needs backplane */
  4135. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  4136. bcmerror = bcmsdh_iovar_op(bus->sdh, name, params, plen, arg, len, set);
  4137. /* Check for bus configuration changes of interest */
  4138. /* If it was divisor change, read the new one */
  4139. if (set && strcmp(name, "sd_divisor") == 0) {
  4140. if (bcmsdh_iovar_op(bus->sdh, "sd_divisor", NULL, 0,
  4141. &bus->sd_divisor, sizeof(int32), FALSE) != BCME_OK) {
  4142. bus->sd_divisor = -1;
  4143. DHD_ERROR(("%s: fail on %s get\n", __FUNCTION__, name));
  4144. } else {
  4145. DHD_INFO(("%s: noted %s update, value now %d\n",
  4146. __FUNCTION__, name, bus->sd_divisor));
  4147. }
  4148. }
  4149. /* If it was a mode change, read the new one */
  4150. if (set && strcmp(name, "sd_mode") == 0) {
  4151. if (bcmsdh_iovar_op(bus->sdh, "sd_mode", NULL, 0,
  4152. &bus->sd_mode, sizeof(int32), FALSE) != BCME_OK) {
  4153. bus->sd_mode = -1;
  4154. DHD_ERROR(("%s: fail on %s get\n", __FUNCTION__, name));
  4155. } else {
  4156. DHD_INFO(("%s: noted %s update, value now %d\n",
  4157. __FUNCTION__, name, bus->sd_mode));
  4158. }
  4159. }
  4160. /* Similar check for blocksize change */
  4161. if (set && strcmp(name, "sd_blocksize") == 0) {
  4162. int32 fnum = 2;
  4163. if (bcmsdh_iovar_op(bus->sdh, "sd_blocksize", &fnum, sizeof(int32),
  4164. &bus->blocksize, sizeof(int32), FALSE) != BCME_OK) {
  4165. bus->blocksize = 0;
  4166. DHD_ERROR(("%s: fail on fn %d %s get\n",
  4167. __FUNCTION__, fnum, "sd_blocksize"));
  4168. } else {
  4169. DHD_INFO(("%s: noted fn %d %s update, value now %d\n",
  4170. __FUNCTION__, fnum, "sd_blocksize", bus->blocksize));
  4171. dhdsdio_tune_fifoparam(bus);
  4172. }
  4173. }
  4174. bus->roundup = MIN(max_roundup, bus->blocksize);
  4175. if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched &&
  4176. NO_OTHER_ACTIVE_BUS_USER(bus)) {
  4177. bus->activity = FALSE;
  4178. dhdsdio_bussleep(bus, TRUE);
  4179. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  4180. }
  4181. dhd_os_sdunlock(bus->dhd);
  4182. goto exit;
  4183. }
  4184. DHD_CTL(("%s: %s %s, len %d plen %d\n", __FUNCTION__,
  4185. name, (set ? "set" : "get"), len, plen));
  4186. /* set up 'params' pointer in case this is a set command so that
  4187. * the convenience int and bool code can be common to set and get
  4188. */
  4189. if (params == NULL) {
  4190. params = arg;
  4191. plen = len;
  4192. }
  4193. if (vi->type == IOVT_VOID)
  4194. val_size = 0;
  4195. else if (vi->type == IOVT_BUFFER)
  4196. val_size = len;
  4197. else
  4198. /* all other types are integer sized */
  4199. val_size = sizeof(int);
  4200. actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
  4201. bcmerror = dhdsdio_doiovar(bus, vi, actionid, name, params, plen, arg, len, val_size);
  4202. exit:
  4203. return bcmerror;
  4204. }
  4205. void
  4206. dhd_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
  4207. {
  4208. osl_t *osh;
  4209. uint32 local_hostintmask;
  4210. uint8 saveclk;
  4211. uint retries;
  4212. int err;
  4213. bool wlfc_enabled = FALSE;
  4214. unsigned long flags;
  4215. if (!bus->dhd)
  4216. return;
  4217. osh = bus->dhd->osh;
  4218. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  4219. bcmsdh_waitlockfree(bus->sdh);
  4220. if (enforce_mutex)
  4221. dhd_os_sdlock(bus->dhd);
  4222. if ((bus->dhd->busstate == DHD_BUS_DOWN) || bus->dhd->hang_was_sent) {
  4223. /* if Firmware already hangs disbale any interrupt */
  4224. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  4225. bus->dhd->busstate = DHD_BUS_DOWN;
  4226. bus->hostintmask = 0;
  4227. bcmsdh_intr_disable(bus->sdh);
  4228. } else {
  4229. BUS_WAKE(bus);
  4230. if (KSO_ENAB(bus)) {
  4231. /* Enable clock for device interrupts */
  4232. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  4233. /* Disable and clear interrupts at the chip level also */
  4234. W_SDREG(0, &bus->regs->hostintmask, retries);
  4235. local_hostintmask = bus->hostintmask;
  4236. bus->hostintmask = 0;
  4237. /* Change our idea of bus state */
  4238. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  4239. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  4240. bus->dhd->busstate = DHD_BUS_DOWN;
  4241. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  4242. /* Force clocks on backplane to be sure F2 interrupt propagates */
  4243. saveclk = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, &err);
  4244. if (!err) {
  4245. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
  4246. (saveclk | SBSDIO_FORCE_HT), &err);
  4247. }
  4248. if (err) {
  4249. DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
  4250. __FUNCTION__, err));
  4251. }
  4252. /* Turn off the bus (F2), free any pending packets */
  4253. DHD_INTR(("%s: disable SDIO interrupts\n", __FUNCTION__));
  4254. bcmsdh_intr_disable(bus->sdh);
  4255. #ifndef BCMSPI
  4256. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, SDIO_FUNC_ENABLE_1, NULL);
  4257. #endif /* !BCMSPI */
  4258. /* Clear any pending interrupts now that F2 is disabled */
  4259. W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
  4260. }
  4261. /* Turn off the backplane clock (only) */
  4262. dhdsdio_clkctl(bus, CLK_SDONLY, FALSE);
  4263. }
  4264. #ifdef PROP_TXSTATUS
  4265. wlfc_enabled = (dhd_wlfc_cleanup_txq(bus->dhd, NULL, 0) != WLFC_UNSUPPORTED);
  4266. #endif // endif
  4267. if (!wlfc_enabled) {
  4268. #ifdef DHDTCPACK_SUPPRESS
  4269. /* Clean tcp_ack_info_tbl in order to prevent access to flushed pkt,
  4270. * when there is a newly coming packet from network stack.
  4271. */
  4272. dhd_tcpack_info_tbl_clean(bus->dhd);
  4273. #endif /* DHDTCPACK_SUPPRESS */
  4274. dhd_os_sdlock_txq(bus->dhd);
  4275. /* Clear the data packet queues */
  4276. pktq_flush(osh, &bus->txq, TRUE);
  4277. dhd_os_sdunlock_txq(bus->dhd);
  4278. }
  4279. /* Clear any held glomming stuff */
  4280. if (bus->glomd)
  4281. PKTFREE(osh, bus->glomd, FALSE);
  4282. if (bus->glom)
  4283. PKTFREE(osh, bus->glom, FALSE);
  4284. bus->glom = bus->glomd = NULL;
  4285. /* Clear rx control and wake any waiters */
  4286. bus->rxlen = 0;
  4287. dhd_os_ioctl_resp_wake(bus->dhd);
  4288. /* Reset some F2 state stuff */
  4289. bus->rxskip = FALSE;
  4290. bus->tx_seq = bus->rx_seq = 0;
  4291. bus->tx_max = 4;
  4292. if (enforce_mutex)
  4293. dhd_os_sdunlock(bus->dhd);
  4294. }
  4295. #if defined(BCMSDIOH_TXGLOM) && defined(BCMSDIOH_STD)
  4296. extern uint sd_txglom;
  4297. #endif // endif
  4298. void
  4299. dhd_txglom_enable(dhd_pub_t *dhdp, bool enable)
  4300. {
  4301. /* can't enable host txglom by default, some platforms have no
  4302. * (or crappy) ADMA support and txglom will cause kernel assertions (e.g.
  4303. * panda board)
  4304. */
  4305. dhd_bus_t *bus = dhdp->bus;
  4306. #ifdef BCMSDIOH_TXGLOM
  4307. uint32 rxglom;
  4308. int32 ret;
  4309. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  4310. #ifdef BCMSDIOH_STD
  4311. if (enable)
  4312. enable = sd_txglom;
  4313. #endif /* BCMSDIOH_STD */
  4314. if (enable) {
  4315. rxglom = 1;
  4316. ret = dhd_iovar(dhdp, 0, "bus:rxglom", (char *)&rxglom, sizeof(rxglom), NULL, 0,
  4317. TRUE);
  4318. if (ret >= 0)
  4319. bus->txglom_enable = TRUE;
  4320. else {
  4321. #ifdef BCMSDIOH_STD
  4322. sd_txglom = 0;
  4323. #endif /* BCMSDIOH_STD */
  4324. bus->txglom_enable = FALSE;
  4325. }
  4326. } else
  4327. #endif /* BCMSDIOH_TXGLOM */
  4328. bus->txglom_enable = FALSE;
  4329. }
  4330. int
  4331. dhd_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
  4332. {
  4333. dhd_bus_t *bus = dhdp->bus;
  4334. dhd_timeout_t tmo;
  4335. uint retries = 0;
  4336. uint8 ready, enable;
  4337. int err, ret = 0;
  4338. #ifdef BCMSPI
  4339. uint32 dstatus = 0; /* gSPI device-status bits */
  4340. #else /* BCMSPI */
  4341. uint8 saveclk;
  4342. #endif /* BCMSPI */
  4343. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  4344. ASSERT(bus->dhd);
  4345. if (!bus->dhd)
  4346. return 0;
  4347. if (enforce_mutex)
  4348. dhd_os_sdlock(bus->dhd);
  4349. /* Make sure backplane clock is on, needed to generate F2 interrupt */
  4350. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  4351. if (bus->clkstate != CLK_AVAIL) {
  4352. DHD_ERROR(("%s: clock state is wrong. state = %d\n", __FUNCTION__, bus->clkstate));
  4353. ret = -1;
  4354. goto exit;
  4355. }
  4356. #ifdef BCMSPI
  4357. /* fake "ready" for spi, wake-wlan would have already enabled F1 and F2 */
  4358. ready = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
  4359. enable = 0;
  4360. /* Give the dongle some time to do its thing and set IOR2 */
  4361. dhd_timeout_start(&tmo, WAIT_F2RXFIFORDY * WAIT_F2RXFIFORDY_DELAY * 1000);
  4362. while (!enable && !dhd_timeout_expired(&tmo)) {
  4363. dstatus = bcmsdh_cfg_read_word(bus->sdh, SDIO_FUNC_0, SPID_STATUS_REG, NULL);
  4364. if (dstatus & STATUS_F2_RX_READY)
  4365. enable = TRUE;
  4366. }
  4367. if (enable) {
  4368. DHD_ERROR(("Took %u usec before dongle is ready\n", tmo.elapsed));
  4369. enable = ready;
  4370. } else {
  4371. DHD_ERROR(("dstatus when timed out on f2-fifo not ready = 0x%x\n", dstatus));
  4372. DHD_ERROR(("Waited %u usec, dongle is not ready\n", tmo.elapsed));
  4373. ret = -1;
  4374. goto exit;
  4375. }
  4376. #else /* !BCMSPI */
  4377. /* Force clocks on backplane to be sure F2 interrupt propagates */
  4378. saveclk = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, &err);
  4379. if (!err) {
  4380. if (bus->sih->chip == BCM43012_CHIP_ID) {
  4381. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
  4382. (saveclk | SBSDIO_HT_AVAIL_REQ), &err);
  4383. } else {
  4384. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
  4385. (saveclk | SBSDIO_FORCE_HT), &err);
  4386. }
  4387. }
  4388. if (err) {
  4389. DHD_ERROR(("%s: Failed to force clock for F2: err %d\n", __FUNCTION__, err));
  4390. ret = -1;
  4391. goto exit;
  4392. }
  4393. /* Enable function 2 (frame transfers) */
  4394. W_SDREG((SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT),
  4395. &bus->regs->tosbmailboxdata, retries);
  4396. enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
  4397. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable, NULL);
  4398. /* Give the dongle some time to do its thing and set IOR2 */
  4399. dhd_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
  4400. ready = 0;
  4401. while (ready != enable && !dhd_timeout_expired(&tmo))
  4402. ready = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IORDY, NULL);
  4403. #endif /* !BCMSPI */
  4404. DHD_ERROR(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
  4405. __FUNCTION__, enable, ready, tmo.elapsed));
  4406. /* If F2 successfully enabled, set core and enable interrupts */
  4407. if (ready == enable) {
  4408. /* Make sure we're talking to the core. */
  4409. #ifdef BCMSDIOLITE
  4410. bus->regs = si_setcore(bus->sih, CC_CORE_ID, 0);
  4411. ASSERT(bus->regs != NULL);
  4412. #else
  4413. if (!(bus->regs = si_setcore(bus->sih, PCMCIA_CORE_ID, 0)))
  4414. bus->regs = si_setcore(bus->sih, SDIOD_CORE_ID, 0);
  4415. ASSERT(bus->regs != NULL);
  4416. #endif // endif
  4417. /* Set up the interrupt mask and enable interrupts */
  4418. bus->hostintmask = HOSTINTMASK;
  4419. /* corerev 4 could use the newer interrupt logic to detect the frames */
  4420. #ifndef BCMSPI
  4421. if ((bus->sih->buscoretype == SDIOD_CORE_ID) && (bus->sdpcmrev == 4) &&
  4422. (bus->rxint_mode != SDIO_DEVICE_HMB_RXINT)) {
  4423. bus->hostintmask &= ~I_HMB_FRAME_IND;
  4424. bus->hostintmask |= I_XMTDATA_AVAIL;
  4425. }
  4426. #endif /* BCMSPI */
  4427. W_SDREG(bus->hostintmask, &bus->regs->hostintmask, retries);
  4428. if (bus->sih->buscorerev < 15) {
  4429. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
  4430. (uint8)watermark, &err);
  4431. }
  4432. /* Set bus state according to enable result */
  4433. dhdp->busstate = DHD_BUS_DATA;
  4434. /* Need to set fn2 block size to match fn1 block size.
  4435. * Requests to fn2 go thru fn1. *
  4436. * faltwig has this code contitioned with #if !BCMSPI_ANDROID.
  4437. * It would be cleaner to use the ->sdh->block_sz[fno] instead of
  4438. * 64, but this layer has no access to sdh types.
  4439. */
  4440. /* bcmsdh_intr_unmask(bus->sdh); */
  4441. bus->intdis = FALSE;
  4442. if (bus->intr) {
  4443. DHD_INTR(("%s: enable SDIO device interrupts\n", __FUNCTION__));
  4444. #ifndef BCMSPI_ANDROID
  4445. bcmsdh_intr_enable(bus->sdh);
  4446. #endif /* !BCMSPI_ANDROID */
  4447. } else {
  4448. DHD_INTR(("%s: disable SDIO interrupts\n", __FUNCTION__));
  4449. bcmsdh_intr_disable(bus->sdh);
  4450. }
  4451. }
  4452. #ifndef BCMSPI
  4453. else {
  4454. /* Disable F2 again */
  4455. enable = SDIO_FUNC_ENABLE_1;
  4456. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable, NULL);
  4457. }
  4458. if (dhdsdio_sr_cap(bus)) {
  4459. dhdsdio_sr_init(bus);
  4460. /* Masking the chip active interrupt permanantly */
  4461. bus->hostintmask &= ~I_CHIPACTIVE;
  4462. W_SDREG(bus->hostintmask, &bus->regs->hostintmask, retries);
  4463. DHD_INFO(("%s: disable I_CHIPACTIVE in hostintmask[0x%08x]\n",
  4464. __FUNCTION__, bus->hostintmask));
  4465. } else {
  4466. bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
  4467. SBSDIO_FUNC1_CHIPCLKCSR, saveclk, &err);
  4468. }
  4469. #endif /* !BCMSPI */
  4470. /* If we didn't come up, turn off backplane clock */
  4471. if (dhdp->busstate != DHD_BUS_DATA)
  4472. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  4473. exit:
  4474. if (enforce_mutex)
  4475. dhd_os_sdunlock(bus->dhd);
  4476. return ret;
  4477. }
  4478. static void
  4479. dhdsdio_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
  4480. {
  4481. bcmsdh_info_t *sdh = bus->sdh;
  4482. sdpcmd_regs_t *regs = bus->regs;
  4483. uint retries = 0;
  4484. uint16 lastrbc;
  4485. uint8 hi, lo;
  4486. int err;
  4487. DHD_ERROR(("%s: %sterminate frame%s\n", __FUNCTION__,
  4488. (abort ? "abort command, " : ""), (rtx ? ", send NAK" : "")));
  4489. if (!KSO_ENAB(bus)) {
  4490. DHD_ERROR(("%s: Device asleep\n", __FUNCTION__));
  4491. return;
  4492. }
  4493. if (abort) {
  4494. bcmsdh_abort(sdh, SDIO_FUNC_2);
  4495. }
  4496. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM, &err);
  4497. if (err) {
  4498. DHD_ERROR(("%s: SBSDIO_FUNC1_FRAMECTRL cmd err\n", __FUNCTION__));
  4499. goto fail;
  4500. }
  4501. bus->f1regdata++;
  4502. /* Wait until the packet has been flushed (device/FIFO stable) */
  4503. for (lastrbc = retries = 0xffff; retries > 0; retries--) {
  4504. hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCHI, NULL);
  4505. lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCLO, &err);
  4506. if (err) {
  4507. DHD_ERROR(("%s: SBSDIO_FUNC1_RFAMEBCLO cmd err\n", __FUNCTION__));
  4508. goto fail;
  4509. }
  4510. bus->f1regdata += 2;
  4511. if ((hi == 0) && (lo == 0))
  4512. break;
  4513. if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
  4514. DHD_ERROR(("%s: count growing: last 0x%04x now 0x%04x\n",
  4515. __FUNCTION__, lastrbc, ((hi << 8) + lo)));
  4516. }
  4517. lastrbc = (hi << 8) + lo;
  4518. }
  4519. if (!retries) {
  4520. DHD_ERROR(("%s: count never zeroed: last 0x%04x\n", __FUNCTION__, lastrbc));
  4521. } else {
  4522. DHD_INFO(("%s: flush took %d iterations\n", __FUNCTION__, (0xffff - retries)));
  4523. }
  4524. if (rtx) {
  4525. bus->rxrtx++;
  4526. W_SDREG(SMB_NAK, &regs->tosbmailbox, retries);
  4527. bus->f1regdata++;
  4528. if (retries <= retry_limit) {
  4529. bus->rxskip = TRUE;
  4530. }
  4531. }
  4532. /* Clear partial in any case */
  4533. bus->nextlen = 0;
  4534. fail:
  4535. /* If we can't reach the device, signal failure */
  4536. if (err || bcmsdh_regfail(sdh)) {
  4537. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  4538. bus->dhd->busstate = DHD_BUS_DOWN;
  4539. }
  4540. }
  4541. static void
  4542. dhdsdio_read_control(dhd_bus_t *bus, uint8 *hdr, uint len, uint doff)
  4543. {
  4544. bcmsdh_info_t *sdh = bus->sdh;
  4545. uint rdlen, pad;
  4546. int sdret;
  4547. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  4548. /* Control data already received in aligned rxctl */
  4549. if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
  4550. goto gotpkt;
  4551. ASSERT(bus->rxbuf);
  4552. /* Set rxctl for frame (w/optional alignment) */
  4553. bus->rxctl = bus->rxbuf;
  4554. if (dhd_alignctl) {
  4555. bus->rxctl += firstread;
  4556. if ((pad = ((uintptr)bus->rxctl % DHD_SDALIGN)))
  4557. bus->rxctl += (DHD_SDALIGN - pad);
  4558. bus->rxctl -= firstread;
  4559. }
  4560. ASSERT(bus->rxctl >= bus->rxbuf);
  4561. /* Copy the already-read portion over */
  4562. bcopy(hdr, bus->rxctl, firstread);
  4563. if (len <= firstread)
  4564. goto gotpkt;
  4565. /* Copy the full data pkt in gSPI case and process ioctl. */
  4566. if (bus->bus == SPI_BUS) {
  4567. bcopy(hdr, bus->rxctl, len);
  4568. goto gotpkt;
  4569. }
  4570. /* Raise rdlen to next SDIO block to avoid tail command */
  4571. rdlen = len - firstread;
  4572. if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
  4573. pad = bus->blocksize - (rdlen % bus->blocksize);
  4574. if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
  4575. ((len + pad) < bus->dhd->maxctl))
  4576. rdlen += pad;
  4577. } else if (rdlen % DHD_SDALIGN) {
  4578. rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
  4579. }
  4580. /* Satisfy length-alignment requirements */
  4581. if (forcealign && (rdlen & (ALIGNMENT - 1)))
  4582. rdlen = ROUNDUP(rdlen, ALIGNMENT);
  4583. /* Drop if the read is too big or it exceeds our maximum */
  4584. if ((rdlen + firstread) > bus->dhd->maxctl) {
  4585. DHD_ERROR(("%s: %d-byte control read exceeds %d-byte buffer\n",
  4586. __FUNCTION__, rdlen, bus->dhd->maxctl));
  4587. bus->dhd->rx_errors++;
  4588. dhdsdio_rxfail(bus, FALSE, FALSE);
  4589. goto done;
  4590. }
  4591. if ((len - doff) > bus->dhd->maxctl) {
  4592. DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
  4593. __FUNCTION__, len, (len - doff), bus->dhd->maxctl));
  4594. bus->dhd->rx_errors++; bus->rx_toolong++;
  4595. dhdsdio_rxfail(bus, FALSE, FALSE);
  4596. goto done;
  4597. }
  4598. /* Read remainder of frame body into the rxctl buffer */
  4599. sdret = dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2, F2SYNC,
  4600. (bus->rxctl + firstread), rdlen, NULL, NULL, NULL);
  4601. bus->f2rxdata++;
  4602. ASSERT(sdret != BCME_PENDING);
  4603. /* Control frame failures need retransmission */
  4604. if (sdret < 0) {
  4605. DHD_ERROR(("%s: read %d control bytes failed: %d\n", __FUNCTION__, rdlen, sdret));
  4606. bus->rxc_errors++; /* dhd.rx_ctlerrs is higher level */
  4607. dhdsdio_rxfail(bus, TRUE, TRUE);
  4608. goto done;
  4609. }
  4610. gotpkt:
  4611. #ifdef DHD_DEBUG
  4612. if (DHD_BYTES_ON() && DHD_CTL_ON()) {
  4613. prhex("RxCtrl", bus->rxctl, len);
  4614. }
  4615. #endif // endif
  4616. /* Point to valid data and indicate its length */
  4617. bus->rxctl += doff;
  4618. bus->rxlen = len - doff;
  4619. done:
  4620. /* Awake any waiters */
  4621. dhd_os_ioctl_resp_wake(bus->dhd);
  4622. }
  4623. int
  4624. dhd_process_pkt_reorder_info(dhd_pub_t *dhd, uchar *reorder_info_buf, uint reorder_info_len,
  4625. void **pkt, uint32 *pkt_count);
  4626. static uint8
  4627. dhdsdio_rxglom(dhd_bus_t *bus, uint8 rxseq)
  4628. {
  4629. uint16 dlen, totlen;
  4630. uint8 *dptr, num = 0;
  4631. uint16 sublen, check;
  4632. void *pfirst, *plast, *pnext;
  4633. void * list_tail[DHD_MAX_IFS] = { NULL };
  4634. void * list_head[DHD_MAX_IFS] = { NULL };
  4635. uint8 idx;
  4636. osl_t *osh = bus->dhd->osh;
  4637. int errcode;
  4638. uint8 chan, seq, doff, sfdoff;
  4639. uint8 txmax;
  4640. uchar reorder_info_buf[WLHOST_REORDERDATA_TOTLEN];
  4641. uint reorder_info_len;
  4642. int ifidx = 0;
  4643. bool usechain = bus->use_rxchain;
  4644. /* If packets, issue read(s) and send up packet chain */
  4645. /* Return sequence numbers consumed? */
  4646. DHD_TRACE(("dhdsdio_rxglom: start: glomd %p glom %p\n", bus->glomd, bus->glom));
  4647. /* If there's a descriptor, generate the packet chain */
  4648. if (bus->glomd) {
  4649. dhd_os_sdlock_rxq(bus->dhd);
  4650. pfirst = plast = pnext = NULL;
  4651. dlen = (uint16)PKTLEN(osh, bus->glomd);
  4652. dptr = PKTDATA(osh, bus->glomd);
  4653. if (!dlen || (dlen & 1)) {
  4654. DHD_ERROR(("%s: bad glomd len (%d), ignore descriptor\n",
  4655. __FUNCTION__, dlen));
  4656. dlen = 0;
  4657. }
  4658. for (totlen = num = 0; dlen; num++) {
  4659. /* Get (and move past) next length */
  4660. sublen = ltoh16_ua(dptr);
  4661. dlen -= sizeof(uint16);
  4662. dptr += sizeof(uint16);
  4663. if ((sublen < SDPCM_HDRLEN) ||
  4664. ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
  4665. DHD_ERROR(("%s: descriptor len %d bad: %d\n",
  4666. __FUNCTION__, num, sublen));
  4667. pnext = NULL;
  4668. break;
  4669. }
  4670. if (sublen % DHD_SDALIGN) {
  4671. DHD_ERROR(("%s: sublen %d not a multiple of %d\n",
  4672. __FUNCTION__, sublen, DHD_SDALIGN));
  4673. usechain = FALSE;
  4674. }
  4675. totlen += sublen;
  4676. /* For last frame, adjust read len so total is a block multiple */
  4677. if (!dlen) {
  4678. sublen += (ROUNDUP(totlen, bus->blocksize) - totlen);
  4679. totlen = ROUNDUP(totlen, bus->blocksize);
  4680. }
  4681. /* Allocate/chain packet for next subframe */
  4682. if ((pnext = PKTGET(osh, sublen + DHD_SDALIGN, FALSE)) == NULL) {
  4683. DHD_ERROR(("%s: PKTGET failed, num %d len %d\n",
  4684. __FUNCTION__, num, sublen));
  4685. break;
  4686. }
  4687. ASSERT(!PKTLINK(pnext));
  4688. if (!pfirst) {
  4689. ASSERT(!plast);
  4690. pfirst = plast = pnext;
  4691. } else {
  4692. ASSERT(plast);
  4693. PKTSETNEXT(osh, plast, pnext);
  4694. plast = pnext;
  4695. }
  4696. /* Adhere to start alignment requirements */
  4697. PKTALIGN(osh, pnext, sublen, DHD_SDALIGN);
  4698. }
  4699. /* If all allocations succeeded, save packet chain in bus structure */
  4700. if (pnext) {
  4701. DHD_GLOM(("%s: allocated %d-byte packet chain for %d subframes\n",
  4702. __FUNCTION__, totlen, num));
  4703. if (DHD_GLOM_ON() && bus->nextlen) {
  4704. if (totlen != bus->nextlen) {
  4705. DHD_GLOM(("%s: glomdesc mismatch: nextlen %d glomdesc %d "
  4706. "rxseq %d\n", __FUNCTION__, bus->nextlen,
  4707. totlen, rxseq));
  4708. }
  4709. }
  4710. bus->glom = pfirst;
  4711. pfirst = pnext = NULL;
  4712. } else {
  4713. if (pfirst)
  4714. PKTFREE(osh, pfirst, FALSE);
  4715. bus->glom = NULL;
  4716. num = 0;
  4717. }
  4718. /* Done with descriptor packet */
  4719. PKTFREE(osh, bus->glomd, FALSE);
  4720. bus->glomd = NULL;
  4721. bus->nextlen = 0;
  4722. dhd_os_sdunlock_rxq(bus->dhd);
  4723. }
  4724. /* Ok -- either we just generated a packet chain, or had one from before */
  4725. if (bus->glom) {
  4726. if (DHD_GLOM_ON()) {
  4727. DHD_GLOM(("%s: attempt superframe read, packet chain:\n", __FUNCTION__));
  4728. for (pnext = bus->glom; pnext; pnext = PKTNEXT(osh, pnext)) {
  4729. DHD_GLOM((" %p: %p len 0x%04x (%d)\n",
  4730. pnext, (uint8*)PKTDATA(osh, pnext),
  4731. PKTLEN(osh, pnext), PKTLEN(osh, pnext)));
  4732. }
  4733. }
  4734. pfirst = bus->glom;
  4735. dlen = (uint16)pkttotlen(osh, pfirst);
  4736. /* Do an SDIO read for the superframe. Configurable iovar to
  4737. * read directly into the chained packet, or allocate a large
  4738. * packet and and copy into the chain.
  4739. */
  4740. if (usechain) {
  4741. errcode = dhd_bcmsdh_recv_buf(bus,
  4742. bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
  4743. F2SYNC, (uint8*)PKTDATA(osh, pfirst),
  4744. dlen, pfirst, NULL, NULL);
  4745. } else if (bus->dataptr) {
  4746. errcode = dhd_bcmsdh_recv_buf(bus,
  4747. bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
  4748. F2SYNC, bus->dataptr,
  4749. dlen, NULL, NULL, NULL);
  4750. sublen = (uint16)pktfrombuf(osh, pfirst, 0, dlen, bus->dataptr);
  4751. if (sublen != dlen) {
  4752. DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
  4753. __FUNCTION__, dlen, sublen));
  4754. errcode = -1;
  4755. }
  4756. pnext = NULL;
  4757. BCM_REFERENCE(pnext);
  4758. } else {
  4759. DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n", dlen));
  4760. errcode = -1;
  4761. }
  4762. bus->f2rxdata++;
  4763. ASSERT(errcode != BCME_PENDING);
  4764. /* On failure, kill the superframe, allow a couple retries */
  4765. if (errcode < 0) {
  4766. DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
  4767. __FUNCTION__, dlen, errcode));
  4768. bus->dhd->rx_errors++;
  4769. if (bus->glomerr++ < 3) {
  4770. dhdsdio_rxfail(bus, TRUE, TRUE);
  4771. } else {
  4772. bus->glomerr = 0;
  4773. dhdsdio_rxfail(bus, TRUE, FALSE);
  4774. dhd_os_sdlock_rxq(bus->dhd);
  4775. PKTFREE(osh, bus->glom, FALSE);
  4776. dhd_os_sdunlock_rxq(bus->dhd);
  4777. bus->rxglomfail++;
  4778. bus->glom = NULL;
  4779. }
  4780. return 0;
  4781. }
  4782. #ifdef DHD_DEBUG
  4783. if (DHD_GLOM_ON()) {
  4784. prhex("SUPERFRAME", PKTDATA(osh, pfirst),
  4785. MIN(PKTLEN(osh, pfirst), 48));
  4786. }
  4787. #endif // endif
  4788. /* Validate the superframe header */
  4789. dptr = (uint8 *)PKTDATA(osh, pfirst);
  4790. sublen = ltoh16_ua(dptr);
  4791. check = ltoh16_ua(dptr + sizeof(uint16));
  4792. chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
  4793. seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
  4794. bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
  4795. if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
  4796. DHD_INFO(("%s: got frame w/nextlen too large (%d) seq %d\n",
  4797. __FUNCTION__, bus->nextlen, seq));
  4798. bus->nextlen = 0;
  4799. }
  4800. doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
  4801. txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
  4802. errcode = 0;
  4803. if ((uint16)~(sublen^check)) {
  4804. DHD_ERROR(("%s (superframe): HW hdr error: len/check 0x%04x/0x%04x\n",
  4805. __FUNCTION__, sublen, check));
  4806. errcode = -1;
  4807. } else if (ROUNDUP(sublen, bus->blocksize) != dlen) {
  4808. DHD_ERROR(("%s (superframe): len 0x%04x, rounded 0x%04x, expect 0x%04x\n",
  4809. __FUNCTION__, sublen, ROUNDUP(sublen, bus->blocksize), dlen));
  4810. errcode = -1;
  4811. } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) != SDPCM_GLOM_CHANNEL) {
  4812. DHD_ERROR(("%s (superframe): bad channel %d\n", __FUNCTION__,
  4813. SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN])));
  4814. errcode = -1;
  4815. } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
  4816. DHD_ERROR(("%s (superframe): got second descriptor?\n", __FUNCTION__));
  4817. errcode = -1;
  4818. } else if ((doff < SDPCM_HDRLEN) ||
  4819. (doff > (PKTLEN(osh, pfirst) - SDPCM_HDRLEN))) {
  4820. DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d pkt %d min %d\n",
  4821. __FUNCTION__, doff, sublen, PKTLEN(osh, pfirst),
  4822. SDPCM_HDRLEN));
  4823. errcode = -1;
  4824. }
  4825. /* Check sequence number of superframe SW header */
  4826. if (rxseq != seq) {
  4827. DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
  4828. __FUNCTION__, seq, rxseq));
  4829. bus->rx_badseq++;
  4830. rxseq = seq;
  4831. }
  4832. /* Check window for sanity */
  4833. if ((uint8)(txmax - bus->tx_seq) > 0x70) {
  4834. DHD_ERROR(("%s: got unlikely tx max %d with tx_seq %d\n",
  4835. __FUNCTION__, txmax, bus->tx_seq));
  4836. txmax = bus->tx_max;
  4837. }
  4838. bus->tx_max = txmax;
  4839. /* Remove superframe header, remember offset */
  4840. PKTPULL(osh, pfirst, doff);
  4841. sfdoff = doff;
  4842. /* Validate all the subframe headers */
  4843. for (num = 0, pnext = pfirst; pnext && !errcode;
  4844. num++, pnext = PKTNEXT(osh, pnext)) {
  4845. dptr = (uint8 *)PKTDATA(osh, pnext);
  4846. dlen = (uint16)PKTLEN(osh, pnext);
  4847. sublen = ltoh16_ua(dptr);
  4848. check = ltoh16_ua(dptr + sizeof(uint16));
  4849. chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
  4850. doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
  4851. #ifdef DHD_DEBUG
  4852. if (DHD_GLOM_ON()) {
  4853. prhex("subframe", dptr, 32);
  4854. }
  4855. #endif // endif
  4856. if ((uint16)~(sublen^check)) {
  4857. DHD_ERROR(("%s (subframe %d): HW hdr error: "
  4858. "len/check 0x%04x/0x%04x\n",
  4859. __FUNCTION__, num, sublen, check));
  4860. errcode = -1;
  4861. } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
  4862. DHD_ERROR(("%s (subframe %d): length mismatch: "
  4863. "len 0x%04x, expect 0x%04x\n",
  4864. __FUNCTION__, num, sublen, dlen));
  4865. errcode = -1;
  4866. } else if ((chan != SDPCM_DATA_CHANNEL) &&
  4867. (chan != SDPCM_EVENT_CHANNEL)) {
  4868. DHD_ERROR(("%s (subframe %d): bad channel %d\n",
  4869. __FUNCTION__, num, chan));
  4870. errcode = -1;
  4871. } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
  4872. DHD_ERROR(("%s (subframe %d): Bad data offset %d: HW %d min %d\n",
  4873. __FUNCTION__, num, doff, sublen, SDPCM_HDRLEN));
  4874. errcode = -1;
  4875. }
  4876. }
  4877. if (errcode) {
  4878. /* Terminate frame on error, request a couple retries */
  4879. if (bus->glomerr++ < 3) {
  4880. /* Restore superframe header space */
  4881. PKTPUSH(osh, pfirst, sfdoff);
  4882. dhdsdio_rxfail(bus, TRUE, TRUE);
  4883. } else {
  4884. bus->glomerr = 0;
  4885. dhdsdio_rxfail(bus, TRUE, FALSE);
  4886. dhd_os_sdlock_rxq(bus->dhd);
  4887. PKTFREE(osh, bus->glom, FALSE);
  4888. dhd_os_sdunlock_rxq(bus->dhd);
  4889. bus->rxglomfail++;
  4890. bus->glom = NULL;
  4891. }
  4892. bus->nextlen = 0;
  4893. return 0;
  4894. }
  4895. /* Basic SD framing looks ok - process each packet (header) */
  4896. bus->glom = NULL;
  4897. plast = NULL;
  4898. dhd_os_sdlock_rxq(bus->dhd);
  4899. for (num = 0; pfirst; rxseq++, pfirst = pnext) {
  4900. pnext = PKTNEXT(osh, pfirst);
  4901. PKTSETNEXT(osh, pfirst, NULL);
  4902. dptr = (uint8 *)PKTDATA(osh, pfirst);
  4903. sublen = ltoh16_ua(dptr);
  4904. chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
  4905. seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
  4906. doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
  4907. DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d chan %d seq %d\n",
  4908. __FUNCTION__, num, pfirst, PKTDATA(osh, pfirst),
  4909. PKTLEN(osh, pfirst), sublen, chan, seq));
  4910. ASSERT((chan == SDPCM_DATA_CHANNEL) || (chan == SDPCM_EVENT_CHANNEL));
  4911. if (rxseq != seq) {
  4912. DHD_GLOM(("%s: rx_seq %d, expected %d\n",
  4913. __FUNCTION__, seq, rxseq));
  4914. bus->rx_badseq++;
  4915. rxseq = seq;
  4916. }
  4917. #ifdef DHD_DEBUG
  4918. if (DHD_BYTES_ON() && DHD_DATA_ON()) {
  4919. prhex("Rx Subframe Data", dptr, dlen);
  4920. }
  4921. #endif // endif
  4922. PKTSETLEN(osh, pfirst, sublen);
  4923. PKTPULL(osh, pfirst, doff);
  4924. reorder_info_len = sizeof(reorder_info_buf);
  4925. if (PKTLEN(osh, pfirst) == 0) {
  4926. PKTFREE(bus->dhd->osh, pfirst, FALSE);
  4927. continue;
  4928. } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pfirst, reorder_info_buf,
  4929. &reorder_info_len) != 0) {
  4930. DHD_ERROR(("%s: rx protocol error\n", __FUNCTION__));
  4931. bus->dhd->rx_errors++;
  4932. PKTFREE(osh, pfirst, FALSE);
  4933. continue;
  4934. }
  4935. if (reorder_info_len) {
  4936. uint32 free_buf_count;
  4937. void *ppfirst;
  4938. ppfirst = pfirst;
  4939. /* Reordering info from the firmware */
  4940. dhd_process_pkt_reorder_info(bus->dhd, reorder_info_buf,
  4941. reorder_info_len, &ppfirst, &free_buf_count);
  4942. if (free_buf_count == 0) {
  4943. continue;
  4944. } else {
  4945. void *temp;
  4946. /* go to the end of the chain and attach the pnext there */
  4947. temp = ppfirst;
  4948. while (PKTNEXT(osh, temp) != NULL) {
  4949. temp = PKTNEXT(osh, temp);
  4950. }
  4951. pfirst = temp;
  4952. if (list_tail[ifidx] == NULL)
  4953. list_head[ifidx] = ppfirst;
  4954. else
  4955. PKTSETNEXT(osh, list_tail[ifidx], ppfirst);
  4956. list_tail[ifidx] = pfirst;
  4957. }
  4958. num += (uint8)free_buf_count;
  4959. } else {
  4960. /* this packet will go up, link back into chain and count it */
  4961. if (list_tail[ifidx] == NULL) {
  4962. list_head[ifidx] = list_tail[ifidx] = pfirst;
  4963. } else {
  4964. PKTSETNEXT(osh, list_tail[ifidx], pfirst);
  4965. list_tail[ifidx] = pfirst;
  4966. }
  4967. num++;
  4968. }
  4969. #ifdef DHD_DEBUG
  4970. if (DHD_GLOM_ON()) {
  4971. DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) nxt/lnk %p/%p\n",
  4972. __FUNCTION__, num, pfirst,
  4973. PKTDATA(osh, pfirst), PKTLEN(osh, pfirst),
  4974. PKTNEXT(osh, pfirst), PKTLINK(pfirst)));
  4975. prhex("", (uint8 *)PKTDATA(osh, pfirst),
  4976. MIN(PKTLEN(osh, pfirst), 32));
  4977. }
  4978. #endif /* DHD_DEBUG */
  4979. }
  4980. dhd_os_sdunlock_rxq(bus->dhd);
  4981. for (idx = 0; idx < DHD_MAX_IFS; idx++) {
  4982. if (list_head[idx]) {
  4983. void *temp;
  4984. uint8 cnt = 0;
  4985. temp = list_head[idx];
  4986. do {
  4987. temp = PKTNEXT(osh, temp);
  4988. cnt++;
  4989. } while (temp);
  4990. if (cnt) {
  4991. dhd_os_sdunlock(bus->dhd);
  4992. dhd_rx_frame(bus->dhd, idx, list_head[idx], cnt, 0);
  4993. dhd_os_sdlock(bus->dhd);
  4994. }
  4995. }
  4996. }
  4997. bus->rxglomframes++;
  4998. bus->rxglompkts += num;
  4999. }
  5000. return num;
  5001. }
  5002. /* Return TRUE if there may be more frames to read */
  5003. static uint
  5004. dhdsdio_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
  5005. {
  5006. osl_t *osh = bus->dhd->osh;
  5007. bcmsdh_info_t *sdh = bus->sdh;
  5008. uint16 len, check; /* Extracted hardware header fields */
  5009. uint8 chan, seq, doff; /* Extracted software header fields */
  5010. uint8 fcbits; /* Extracted fcbits from software header */
  5011. uint8 delta;
  5012. void *pkt; /* Packet for event or data frames */
  5013. uint16 pad; /* Number of pad bytes to read */
  5014. uint16 rdlen; /* Total number of bytes to read */
  5015. uint8 rxseq; /* Next sequence number to expect */
  5016. uint rxleft = 0; /* Remaining number of frames allowed */
  5017. int sdret; /* Return code from bcmsdh calls */
  5018. uint8 txmax; /* Maximum tx sequence offered */
  5019. #ifdef BCMSPI
  5020. uint32 dstatus = 0; /* gSPI device status bits of */
  5021. #endif /* BCMSPI */
  5022. bool len_consistent; /* Result of comparing readahead len and len from hw-hdr */
  5023. uint8 *rxbuf;
  5024. int ifidx = 0;
  5025. uint rxcount = 0; /* Total frames read */
  5026. uchar reorder_info_buf[WLHOST_REORDERDATA_TOTLEN];
  5027. uint reorder_info_len;
  5028. uint pkt_count;
  5029. #if defined(DHD_DEBUG) || defined(SDTEST)
  5030. bool sdtest = FALSE; /* To limit message spew from test mode */
  5031. #endif // endif
  5032. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  5033. bus->readframes = TRUE;
  5034. if (!KSO_ENAB(bus)) {
  5035. DHD_ERROR(("%s: KSO off\n", __FUNCTION__));
  5036. bus->readframes = FALSE;
  5037. return 0;
  5038. }
  5039. ASSERT(maxframes);
  5040. #ifdef SDTEST
  5041. /* Allow pktgen to override maxframes */
  5042. if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
  5043. maxframes = bus->pktgen_count;
  5044. sdtest = TRUE;
  5045. }
  5046. #endif // endif
  5047. /* Not finished unless we encounter no more frames indication */
  5048. *finished = FALSE;
  5049. #ifdef BCMSPI
  5050. /* Get pktlen from gSPI device F0 reg. */
  5051. if (bus->bus == SPI_BUS) {
  5052. /* Peek in dstatus bits and find out size to do rx-read. */
  5053. dstatus = bcmsdh_get_dstatus(bus->sdh);
  5054. if (dstatus == 0)
  5055. DHD_ERROR(("%s:ZERO spi dstatus, a case observed in PR61352 hit !!!\n",
  5056. __FUNCTION__));
  5057. DHD_TRACE(("Device status from regread = 0x%x\n", dstatus));
  5058. DHD_TRACE(("Device status from bit-reconstruction = 0x%x\n",
  5059. bcmsdh_get_dstatus((void *)bus->sdh)));
  5060. if ((dstatus & STATUS_F2_PKT_AVAILABLE) && (((dstatus & STATUS_UNDERFLOW)) == 0)) {
  5061. bus->nextlen = ((dstatus & STATUS_F2_PKT_LEN_MASK) >>
  5062. STATUS_F2_PKT_LEN_SHIFT);
  5063. /* '0' size with pkt-available interrupt is eqvt to 2048 bytes */
  5064. bus->nextlen = (bus->nextlen == 0) ? SPI_MAX_PKT_LEN : bus->nextlen;
  5065. if (bus->dwordmode)
  5066. bus->nextlen = bus->nextlen << 2;
  5067. DHD_TRACE(("Entering %s: length to be read from gSPI = %d\n",
  5068. __FUNCTION__, bus->nextlen));
  5069. } else {
  5070. if (dstatus & STATUS_F2_PKT_AVAILABLE)
  5071. DHD_ERROR(("Underflow during %s.\n", __FUNCTION__));
  5072. else
  5073. DHD_ERROR(("False pkt-available intr.\n"));
  5074. *finished = TRUE;
  5075. return (maxframes - rxleft);
  5076. }
  5077. }
  5078. #endif /* BCMSPI */
  5079. for (rxseq = bus->rx_seq, rxleft = maxframes;
  5080. !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
  5081. rxseq++, rxleft--) {
  5082. #ifdef DHDTCPACK_SUP_DBG
  5083. if (bus->dhd->tcpack_sup_mode != TCPACK_SUP_DELAYTX) {
  5084. if (bus->dotxinrx == FALSE)
  5085. DHD_ERROR(("%s %d: dotxinrx FALSE with tcpack_sub_mode %d\n",
  5086. __FUNCTION__, __LINE__, bus->dhd->tcpack_sup_mode));
  5087. }
  5088. #ifdef DEBUG_COUNTER
  5089. else if (pktq_mlen(&bus->txq, ~bus->flowcontrol) > 0) {
  5090. tack_tbl.cnt[bus->dotxinrx ? 6 : 7]++;
  5091. }
  5092. #endif /* DEBUG_COUNTER */
  5093. #endif /* DHDTCPACK_SUP_DBG */
  5094. /* tx more to improve rx performance */
  5095. if (TXCTLOK(bus) && bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL)) {
  5096. dhdsdio_sendpendctl(bus);
  5097. } else if (bus->dotxinrx && (bus->clkstate == CLK_AVAIL) &&
  5098. !bus->fcstate && DATAOK(bus) &&
  5099. (pktq_mlen(&bus->txq, ~bus->flowcontrol) > bus->txinrx_thres)) {
  5100. dhdsdio_sendfromq(bus, dhd_txbound);
  5101. #ifdef DHDTCPACK_SUPPRESS
  5102. /* In TCPACK_SUP_DELAYTX mode, do txinrx only if
  5103. * 1. Any DATA packet to TX
  5104. * 2. TCPACK to TCPDATA PSH packets.
  5105. * in bus txq.
  5106. */
  5107. bus->dotxinrx = (bus->dhd->tcpack_sup_mode == TCPACK_SUP_DELAYTX) ?
  5108. FALSE : TRUE;
  5109. #endif // endif
  5110. }
  5111. /* Handle glomming separately */
  5112. if (bus->glom || bus->glomd) {
  5113. uint8 cnt;
  5114. DHD_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
  5115. __FUNCTION__, bus->glomd, bus->glom));
  5116. cnt = dhdsdio_rxglom(bus, rxseq);
  5117. DHD_GLOM(("%s: rxglom returned %d\n", __FUNCTION__, cnt));
  5118. rxseq += cnt - 1;
  5119. rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
  5120. continue;
  5121. }
  5122. /* Try doing single read if we can */
  5123. if (dhd_readahead && bus->nextlen) {
  5124. uint16 nextlen = bus->nextlen;
  5125. bus->nextlen = 0;
  5126. if (bus->bus == SPI_BUS) {
  5127. rdlen = len = nextlen;
  5128. } else {
  5129. rdlen = len = nextlen << 4;
  5130. /* Pad read to blocksize for efficiency */
  5131. if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
  5132. pad = bus->blocksize - (rdlen % bus->blocksize);
  5133. if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
  5134. ((rdlen + pad + firstread) < MAX_RX_DATASZ))
  5135. rdlen += pad;
  5136. } else if (rdlen % DHD_SDALIGN) {
  5137. rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
  5138. }
  5139. }
  5140. /* We use bus->rxctl buffer in WinXP for initial control pkt receives.
  5141. * Later we use buffer-poll for data as well as control packets.
  5142. * This is required because dhd receives full frame in gSPI unlike SDIO.
  5143. * After the frame is received we have to distinguish whether it is data
  5144. * or non-data frame.
  5145. */
  5146. /* Allocate a packet buffer */
  5147. dhd_os_sdlock_rxq(bus->dhd);
  5148. if (!(pkt = PKTGET(osh, rdlen + DHD_SDALIGN, FALSE))) {
  5149. if (bus->bus == SPI_BUS) {
  5150. bus->usebufpool = FALSE;
  5151. bus->rxctl = bus->rxbuf;
  5152. if (dhd_alignctl) {
  5153. bus->rxctl += firstread;
  5154. if ((pad = ((uintptr)bus->rxctl % DHD_SDALIGN)))
  5155. bus->rxctl += (DHD_SDALIGN - pad);
  5156. bus->rxctl -= firstread;
  5157. }
  5158. ASSERT(bus->rxctl >= bus->rxbuf);
  5159. rxbuf = bus->rxctl;
  5160. /* Read the entire frame */
  5161. sdret = dhd_bcmsdh_recv_buf(bus,
  5162. bcmsdh_cur_sbwad(sdh),
  5163. SDIO_FUNC_2,
  5164. F2SYNC, rxbuf, rdlen,
  5165. NULL, NULL, NULL);
  5166. bus->f2rxdata++;
  5167. ASSERT(sdret != BCME_PENDING);
  5168. #ifdef BCMSPI
  5169. if (bcmsdh_get_dstatus((void *)bus->sdh) &
  5170. STATUS_UNDERFLOW) {
  5171. bus->nextlen = 0;
  5172. *finished = TRUE;
  5173. DHD_ERROR(("%s: read %d control bytes failed "
  5174. "due to spi underflow\n",
  5175. __FUNCTION__, rdlen));
  5176. /* dhd.rx_ctlerrs is higher level */
  5177. bus->rxc_errors++;
  5178. dhd_os_sdunlock_rxq(bus->dhd);
  5179. continue;
  5180. }
  5181. #endif /* BCMSPI */
  5182. /* Control frame failures need retransmission */
  5183. if (sdret < 0) {
  5184. DHD_ERROR(("%s: read %d control bytes failed: %d\n",
  5185. __FUNCTION__, rdlen, sdret));
  5186. /* dhd.rx_ctlerrs is higher level */
  5187. bus->rxc_errors++;
  5188. dhd_os_sdunlock_rxq(bus->dhd);
  5189. dhdsdio_rxfail(bus, TRUE,
  5190. (bus->bus == SPI_BUS) ? FALSE : TRUE);
  5191. continue;
  5192. }
  5193. } else {
  5194. /* Give up on data, request rtx of events */
  5195. DHD_ERROR(("%s (nextlen): PKTGET failed: len %d rdlen %d "
  5196. "expected rxseq %d\n",
  5197. __FUNCTION__, len, rdlen, rxseq));
  5198. /* Just go try again w/normal header read */
  5199. dhd_os_sdunlock_rxq(bus->dhd);
  5200. continue;
  5201. }
  5202. } else {
  5203. if (bus->bus == SPI_BUS)
  5204. bus->usebufpool = TRUE;
  5205. ASSERT(!PKTLINK(pkt));
  5206. PKTALIGN(osh, pkt, rdlen, DHD_SDALIGN);
  5207. rxbuf = (uint8 *)PKTDATA(osh, pkt);
  5208. /* Read the entire frame */
  5209. sdret = dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh),
  5210. SDIO_FUNC_2,
  5211. F2SYNC, rxbuf, rdlen,
  5212. pkt, NULL, NULL);
  5213. bus->f2rxdata++;
  5214. ASSERT(sdret != BCME_PENDING);
  5215. #ifdef BCMSPI
  5216. if (bcmsdh_get_dstatus((void *)bus->sdh) & STATUS_UNDERFLOW) {
  5217. bus->nextlen = 0;
  5218. *finished = TRUE;
  5219. DHD_ERROR(("%s (nextlen): read %d bytes failed due "
  5220. "to spi underflow\n",
  5221. __FUNCTION__, rdlen));
  5222. PKTFREE(bus->dhd->osh, pkt, FALSE);
  5223. bus->dhd->rx_errors++;
  5224. dhd_os_sdunlock_rxq(bus->dhd);
  5225. continue;
  5226. }
  5227. #endif /* BCMSPI */
  5228. if (sdret < 0) {
  5229. DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
  5230. __FUNCTION__, rdlen, sdret));
  5231. PKTFREE(bus->dhd->osh, pkt, FALSE);
  5232. bus->dhd->rx_errors++;
  5233. dhd_os_sdunlock_rxq(bus->dhd);
  5234. /* Force retry w/normal header read. Don't attempt NAK for
  5235. * gSPI
  5236. */
  5237. dhdsdio_rxfail(bus, TRUE,
  5238. (bus->bus == SPI_BUS) ? FALSE : TRUE);
  5239. continue;
  5240. }
  5241. }
  5242. dhd_os_sdunlock_rxq(bus->dhd);
  5243. /* Now check the header */
  5244. bcopy(rxbuf, bus->rxhdr, SDPCM_HDRLEN);
  5245. /* Extract hardware header fields */
  5246. len = ltoh16_ua(bus->rxhdr);
  5247. check = ltoh16_ua(bus->rxhdr + sizeof(uint16));
  5248. /* All zeros means readahead info was bad */
  5249. if (!(len|check)) {
  5250. DHD_INFO(("%s (nextlen): read zeros in HW header???\n",
  5251. __FUNCTION__));
  5252. dhd_os_sdlock_rxq(bus->dhd);
  5253. PKTFREE2();
  5254. dhd_os_sdunlock_rxq(bus->dhd);
  5255. GSPI_PR55150_BAILOUT;
  5256. continue;
  5257. }
  5258. /* Validate check bytes */
  5259. if ((uint16)~(len^check)) {
  5260. DHD_ERROR(("%s (nextlen): HW hdr error: nextlen/len/check"
  5261. " 0x%04x/0x%04x/0x%04x\n", __FUNCTION__, nextlen,
  5262. len, check));
  5263. dhd_os_sdlock_rxq(bus->dhd);
  5264. PKTFREE2();
  5265. dhd_os_sdunlock_rxq(bus->dhd);
  5266. bus->rx_badhdr++;
  5267. dhdsdio_rxfail(bus, FALSE, FALSE);
  5268. GSPI_PR55150_BAILOUT;
  5269. continue;
  5270. }
  5271. /* Validate frame length */
  5272. if (len < SDPCM_HDRLEN) {
  5273. DHD_ERROR(("%s (nextlen): HW hdr length invalid: %d\n",
  5274. __FUNCTION__, len));
  5275. dhd_os_sdlock_rxq(bus->dhd);
  5276. PKTFREE2();
  5277. dhd_os_sdunlock_rxq(bus->dhd);
  5278. GSPI_PR55150_BAILOUT;
  5279. continue;
  5280. }
  5281. /* Check for consistency with readahead info */
  5282. #ifdef BCMSPI
  5283. if (bus->bus == SPI_BUS) {
  5284. if (bus->dwordmode) {
  5285. uint16 spilen;
  5286. spilen = ROUNDUP(len, 4);
  5287. len_consistent = (nextlen != spilen);
  5288. } else
  5289. len_consistent = (nextlen != len);
  5290. } else
  5291. #endif /* BCMSPI */
  5292. len_consistent = (nextlen != (ROUNDUP(len, 16) >> 4));
  5293. if (len_consistent) {
  5294. /* Mismatch, force retry w/normal header (may be >4K) */
  5295. DHD_ERROR(("%s (nextlen): mismatch, nextlen %d len %d rnd %d; "
  5296. "expected rxseq %d\n",
  5297. __FUNCTION__, nextlen, len, ROUNDUP(len, 16), rxseq));
  5298. dhd_os_sdlock_rxq(bus->dhd);
  5299. PKTFREE2();
  5300. dhd_os_sdunlock_rxq(bus->dhd);
  5301. dhdsdio_rxfail(bus, TRUE, (bus->bus == SPI_BUS) ? FALSE : TRUE);
  5302. GSPI_PR55150_BAILOUT;
  5303. continue;
  5304. }
  5305. /* Extract software header fields */
  5306. chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5307. seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5308. doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5309. txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5310. #ifdef BCMSPI
  5311. /* Save the readahead length if there is one */
  5312. if (bus->bus == SPI_BUS) {
  5313. /* Use reconstructed dstatus bits and find out readahead size */
  5314. dstatus = bcmsdh_get_dstatus((void *)bus->sdh);
  5315. DHD_INFO(("Device status from bit-reconstruction = 0x%x\n",
  5316. bcmsdh_get_dstatus((void *)bus->sdh)));
  5317. if (dstatus & STATUS_F2_PKT_AVAILABLE) {
  5318. bus->nextlen = ((dstatus & STATUS_F2_PKT_LEN_MASK) >>
  5319. STATUS_F2_PKT_LEN_SHIFT);
  5320. bus->nextlen = (bus->nextlen == 0) ?
  5321. SPI_MAX_PKT_LEN : bus->nextlen;
  5322. if (bus->dwordmode)
  5323. bus->nextlen = bus->nextlen << 2;
  5324. DHD_INFO(("readahead len from gSPI = %d \n",
  5325. bus->nextlen));
  5326. bus->dhd->rx_readahead_cnt ++;
  5327. } else {
  5328. bus->nextlen = 0;
  5329. *finished = TRUE;
  5330. }
  5331. } else {
  5332. #endif /* BCMSPI */
  5333. bus->nextlen =
  5334. bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
  5335. if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
  5336. DHD_INFO(("%s (nextlen): got frame w/nextlen too large"
  5337. " (%d), seq %d\n", __FUNCTION__, bus->nextlen,
  5338. seq));
  5339. bus->nextlen = 0;
  5340. }
  5341. bus->dhd->rx_readahead_cnt ++;
  5342. #ifdef BCMSPI
  5343. }
  5344. #endif /* BCMSPI */
  5345. /* Handle Flow Control */
  5346. fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5347. delta = 0;
  5348. if (~bus->flowcontrol & fcbits) {
  5349. bus->fc_xoff++;
  5350. delta = 1;
  5351. }
  5352. if (bus->flowcontrol & ~fcbits) {
  5353. bus->fc_xon++;
  5354. delta = 1;
  5355. }
  5356. if (delta) {
  5357. bus->fc_rcvd++;
  5358. bus->flowcontrol = fcbits;
  5359. }
  5360. /* Check and update sequence number */
  5361. if (rxseq != seq) {
  5362. DHD_INFO(("%s (nextlen): rx_seq %d, expected %d\n",
  5363. __FUNCTION__, seq, rxseq));
  5364. bus->rx_badseq++;
  5365. rxseq = seq;
  5366. }
  5367. /* Check window for sanity */
  5368. if ((uint8)(txmax - bus->tx_seq) > 0x70) {
  5369. #ifdef BCMSPI
  5370. if ((bus->bus == SPI_BUS) && !(dstatus & STATUS_F2_RX_READY)) {
  5371. DHD_ERROR(("%s: got unlikely tx max %d with tx_seq %d\n",
  5372. __FUNCTION__, txmax, bus->tx_seq));
  5373. txmax = bus->tx_seq + 2;
  5374. } else {
  5375. #endif /* BCMSPI */
  5376. DHD_ERROR(("%s: got unlikely tx max %d with tx_seq %d\n",
  5377. __FUNCTION__, txmax, bus->tx_seq));
  5378. txmax = bus->tx_max;
  5379. #ifdef BCMSPI
  5380. }
  5381. #endif /* BCMSPI */
  5382. }
  5383. bus->tx_max = txmax;
  5384. #ifdef DHD_DEBUG
  5385. if (DHD_BYTES_ON() && DHD_DATA_ON()) {
  5386. prhex("Rx Data", rxbuf, len);
  5387. } else if (DHD_HDRS_ON()) {
  5388. prhex("RxHdr", bus->rxhdr, SDPCM_HDRLEN);
  5389. }
  5390. #endif // endif
  5391. if (chan == SDPCM_CONTROL_CHANNEL) {
  5392. if (bus->bus == SPI_BUS) {
  5393. dhdsdio_read_control(bus, rxbuf, len, doff);
  5394. if (bus->usebufpool) {
  5395. dhd_os_sdlock_rxq(bus->dhd);
  5396. PKTFREE(bus->dhd->osh, pkt, FALSE);
  5397. dhd_os_sdunlock_rxq(bus->dhd);
  5398. }
  5399. continue;
  5400. } else {
  5401. DHD_ERROR(("%s (nextlen): readahead on control"
  5402. " packet %d?\n", __FUNCTION__, seq));
  5403. /* Force retry w/normal header read */
  5404. bus->nextlen = 0;
  5405. dhdsdio_rxfail(bus, FALSE, TRUE);
  5406. dhd_os_sdlock_rxq(bus->dhd);
  5407. PKTFREE2();
  5408. dhd_os_sdunlock_rxq(bus->dhd);
  5409. continue;
  5410. }
  5411. }
  5412. if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
  5413. DHD_ERROR(("Received %d bytes on %d channel. Running out of "
  5414. "rx pktbuf's or not yet malloced.\n", len, chan));
  5415. continue;
  5416. }
  5417. /* Validate data offset */
  5418. if ((doff < SDPCM_HDRLEN) || (doff > len)) {
  5419. DHD_ERROR(("%s (nextlen): bad data offset %d: HW len %d min %d\n",
  5420. __FUNCTION__, doff, len, SDPCM_HDRLEN));
  5421. dhd_os_sdlock_rxq(bus->dhd);
  5422. PKTFREE2();
  5423. dhd_os_sdunlock_rxq(bus->dhd);
  5424. ASSERT(0);
  5425. dhdsdio_rxfail(bus, FALSE, FALSE);
  5426. continue;
  5427. }
  5428. /* All done with this one -- now deliver the packet */
  5429. goto deliver;
  5430. }
  5431. /* gSPI frames should not be handled in fractions */
  5432. if (bus->bus == SPI_BUS) {
  5433. break;
  5434. }
  5435. /* Read frame header (hardware and software) */
  5436. sdret = dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2, F2SYNC,
  5437. bus->rxhdr, firstread, NULL, NULL, NULL);
  5438. bus->f2rxhdrs++;
  5439. ASSERT(sdret != BCME_PENDING);
  5440. if (sdret < 0) {
  5441. DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __FUNCTION__, sdret));
  5442. bus->rx_hdrfail++;
  5443. dhdsdio_rxfail(bus, TRUE, TRUE);
  5444. continue;
  5445. }
  5446. #ifdef DHD_DEBUG
  5447. if (DHD_BYTES_ON() || DHD_HDRS_ON()) {
  5448. prhex("RxHdr", bus->rxhdr, SDPCM_HDRLEN);
  5449. }
  5450. #endif // endif
  5451. /* Extract hardware header fields */
  5452. len = ltoh16_ua(bus->rxhdr);
  5453. check = ltoh16_ua(bus->rxhdr + sizeof(uint16));
  5454. /* All zeros means no more frames */
  5455. if (!(len|check)) {
  5456. *finished = TRUE;
  5457. break;
  5458. }
  5459. /* Validate check bytes */
  5460. if ((uint16)~(len^check)) {
  5461. DHD_ERROR(("%s: HW hdr error: len/check 0x%04x/0x%04x\n",
  5462. __FUNCTION__, len, check));
  5463. bus->rx_badhdr++;
  5464. dhdsdio_rxfail(bus, FALSE, FALSE);
  5465. continue;
  5466. }
  5467. /* Validate frame length */
  5468. if (len < SDPCM_HDRLEN) {
  5469. DHD_ERROR(("%s: HW hdr length invalid: %d\n", __FUNCTION__, len));
  5470. continue;
  5471. }
  5472. /* Extract software header fields */
  5473. chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5474. seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5475. doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5476. txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5477. /* Validate data offset */
  5478. if ((doff < SDPCM_HDRLEN) || (doff > len)) {
  5479. DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d seq %d\n",
  5480. __FUNCTION__, doff, len, SDPCM_HDRLEN, seq));
  5481. bus->rx_badhdr++;
  5482. ASSERT(0);
  5483. dhdsdio_rxfail(bus, FALSE, FALSE);
  5484. continue;
  5485. }
  5486. /* Save the readahead length if there is one */
  5487. bus->nextlen = bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
  5488. if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
  5489. DHD_INFO(("%s (nextlen): got frame w/nextlen too large (%d), seq %d\n",
  5490. __FUNCTION__, bus->nextlen, seq));
  5491. bus->nextlen = 0;
  5492. }
  5493. /* Handle Flow Control */
  5494. fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
  5495. delta = 0;
  5496. if (~bus->flowcontrol & fcbits) {
  5497. bus->fc_xoff++;
  5498. delta = 1;
  5499. }
  5500. if (bus->flowcontrol & ~fcbits) {
  5501. bus->fc_xon++;
  5502. delta = 1;
  5503. }
  5504. if (delta) {
  5505. bus->fc_rcvd++;
  5506. bus->flowcontrol = fcbits;
  5507. }
  5508. /* Check and update sequence number */
  5509. if (rxseq != seq) {
  5510. DHD_INFO(("%s: rx_seq %d, expected %d\n", __FUNCTION__, seq, rxseq));
  5511. bus->rx_badseq++;
  5512. rxseq = seq;
  5513. }
  5514. /* Check window for sanity */
  5515. if ((uint8)(txmax - bus->tx_seq) > 0x70) {
  5516. DHD_ERROR(("%s: got unlikely tx max %d with tx_seq %d\n",
  5517. __FUNCTION__, txmax, bus->tx_seq));
  5518. txmax = bus->tx_max;
  5519. }
  5520. bus->tx_max = txmax;
  5521. /* Call a separate function for control frames */
  5522. if (chan == SDPCM_CONTROL_CHANNEL) {
  5523. dhdsdio_read_control(bus, bus->rxhdr, len, doff);
  5524. continue;
  5525. }
  5526. ASSERT((chan == SDPCM_DATA_CHANNEL) || (chan == SDPCM_EVENT_CHANNEL) ||
  5527. (chan == SDPCM_TEST_CHANNEL) || (chan == SDPCM_GLOM_CHANNEL));
  5528. /* Length to read */
  5529. rdlen = (len > firstread) ? (len - firstread) : 0;
  5530. /* May pad read to blocksize for efficiency */
  5531. if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
  5532. pad = bus->blocksize - (rdlen % bus->blocksize);
  5533. if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
  5534. ((rdlen + pad + firstread) < MAX_RX_DATASZ))
  5535. rdlen += pad;
  5536. } else if (rdlen % DHD_SDALIGN) {
  5537. rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
  5538. }
  5539. /* Satisfy length-alignment requirements */
  5540. if (forcealign && (rdlen & (ALIGNMENT - 1)))
  5541. rdlen = ROUNDUP(rdlen, ALIGNMENT);
  5542. if ((rdlen + firstread) > MAX_RX_DATASZ) {
  5543. /* Too long -- skip this frame */
  5544. DHD_ERROR(("%s: too long: len %d rdlen %d\n", __FUNCTION__, len, rdlen));
  5545. bus->dhd->rx_errors++; bus->rx_toolong++;
  5546. dhdsdio_rxfail(bus, FALSE, FALSE);
  5547. continue;
  5548. }
  5549. dhd_os_sdlock_rxq(bus->dhd);
  5550. if (!(pkt = PKTGET(osh, (rdlen + firstread + DHD_SDALIGN), FALSE))) {
  5551. /* Give up on data, request rtx of events */
  5552. DHD_ERROR(("%s: PKTGET failed: rdlen %d chan %d\n",
  5553. __FUNCTION__, rdlen, chan));
  5554. bus->dhd->rx_dropped++;
  5555. dhd_os_sdunlock_rxq(bus->dhd);
  5556. dhdsdio_rxfail(bus, FALSE, RETRYCHAN(chan));
  5557. continue;
  5558. }
  5559. dhd_os_sdunlock_rxq(bus->dhd);
  5560. ASSERT(!PKTLINK(pkt));
  5561. /* Leave room for what we already read, and align remainder */
  5562. ASSERT(firstread < (PKTLEN(osh, pkt)));
  5563. PKTPULL(osh, pkt, firstread);
  5564. PKTALIGN(osh, pkt, rdlen, DHD_SDALIGN);
  5565. /* Read the remaining frame data */
  5566. sdret = dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2, F2SYNC,
  5567. ((uint8 *)PKTDATA(osh, pkt)), rdlen, pkt, NULL, NULL);
  5568. bus->f2rxdata++;
  5569. ASSERT(sdret != BCME_PENDING);
  5570. if (sdret < 0) {
  5571. DHD_ERROR(("%s: read %d %s bytes failed: %d\n", __FUNCTION__, rdlen,
  5572. ((chan == SDPCM_EVENT_CHANNEL) ? "event" :
  5573. ((chan == SDPCM_DATA_CHANNEL) ? "data" : "test")), sdret));
  5574. dhd_os_sdlock_rxq(bus->dhd);
  5575. PKTFREE(bus->dhd->osh, pkt, FALSE);
  5576. dhd_os_sdunlock_rxq(bus->dhd);
  5577. bus->dhd->rx_errors++;
  5578. dhdsdio_rxfail(bus, TRUE, RETRYCHAN(chan));
  5579. continue;
  5580. }
  5581. /* Copy the already-read portion */
  5582. PKTPUSH(osh, pkt, firstread);
  5583. bcopy(bus->rxhdr, PKTDATA(osh, pkt), firstread);
  5584. #ifdef DHD_DEBUG
  5585. if (DHD_BYTES_ON() && DHD_DATA_ON()) {
  5586. prhex("Rx Data", PKTDATA(osh, pkt), len);
  5587. }
  5588. #endif // endif
  5589. deliver:
  5590. /* Save superframe descriptor and allocate packet frame */
  5591. if (chan == SDPCM_GLOM_CHANNEL) {
  5592. if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
  5593. DHD_GLOM(("%s: got glom descriptor, %d bytes:\n",
  5594. __FUNCTION__, len));
  5595. #ifdef DHD_DEBUG
  5596. if (DHD_GLOM_ON()) {
  5597. prhex("Glom Data", PKTDATA(osh, pkt), len);
  5598. }
  5599. #endif // endif
  5600. PKTSETLEN(osh, pkt, len);
  5601. ASSERT(doff == SDPCM_HDRLEN);
  5602. PKTPULL(osh, pkt, SDPCM_HDRLEN);
  5603. bus->glomd = pkt;
  5604. } else {
  5605. DHD_ERROR(("%s: glom superframe w/o descriptor!\n", __FUNCTION__));
  5606. dhdsdio_rxfail(bus, FALSE, FALSE);
  5607. }
  5608. continue;
  5609. }
  5610. /* Fill in packet len and prio, deliver upward */
  5611. PKTSETLEN(osh, pkt, len);
  5612. PKTPULL(osh, pkt, doff);
  5613. #ifdef SDTEST
  5614. /* Test channel packets are processed separately */
  5615. if (chan == SDPCM_TEST_CHANNEL) {
  5616. dhdsdio_testrcv(bus, pkt, seq);
  5617. continue;
  5618. }
  5619. #endif /* SDTEST */
  5620. if (PKTLEN(osh, pkt) == 0) {
  5621. dhd_os_sdlock_rxq(bus->dhd);
  5622. PKTFREE(bus->dhd->osh, pkt, FALSE);
  5623. dhd_os_sdunlock_rxq(bus->dhd);
  5624. continue;
  5625. } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pkt, reorder_info_buf,
  5626. &reorder_info_len) != 0) {
  5627. DHD_ERROR(("%s: rx protocol error\n", __FUNCTION__));
  5628. dhd_os_sdlock_rxq(bus->dhd);
  5629. PKTFREE(bus->dhd->osh, pkt, FALSE);
  5630. dhd_os_sdunlock_rxq(bus->dhd);
  5631. bus->dhd->rx_errors++;
  5632. continue;
  5633. }
  5634. if (reorder_info_len) {
  5635. /* Reordering info from the firmware */
  5636. dhd_process_pkt_reorder_info(bus->dhd, reorder_info_buf, reorder_info_len,
  5637. &pkt, &pkt_count);
  5638. if (pkt_count == 0)
  5639. continue;
  5640. } else {
  5641. pkt_count = 1;
  5642. }
  5643. /* Unlock during rx call */
  5644. dhd_os_sdunlock(bus->dhd);
  5645. dhd_rx_frame(bus->dhd, ifidx, pkt, pkt_count, chan);
  5646. dhd_os_sdlock(bus->dhd);
  5647. }
  5648. rxcount = maxframes - rxleft;
  5649. #ifdef DHD_DEBUG
  5650. /* Message if we hit the limit */
  5651. if (!rxleft && !sdtest)
  5652. DHD_DATA(("%s: hit rx limit of %d frames\n", __FUNCTION__, maxframes));
  5653. else
  5654. #endif /* DHD_DEBUG */
  5655. DHD_DATA(("%s: processed %d frames\n", __FUNCTION__, rxcount));
  5656. /* Back off rxseq if awaiting rtx, update rx_seq */
  5657. if (bus->rxskip)
  5658. rxseq--;
  5659. bus->rx_seq = rxseq;
  5660. if (bus->reqbussleep)
  5661. {
  5662. dhdsdio_bussleep(bus, TRUE);
  5663. bus->reqbussleep = FALSE;
  5664. }
  5665. bus->readframes = FALSE;
  5666. return rxcount;
  5667. }
  5668. static uint32
  5669. dhdsdio_hostmail(dhd_bus_t *bus, uint32 *hmbd)
  5670. {
  5671. sdpcmd_regs_t *regs = bus->regs;
  5672. uint32 intstatus = 0;
  5673. uint32 hmb_data;
  5674. uint8 fcbits;
  5675. uint retries = 0;
  5676. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  5677. /* Read mailbox data and ack that we did so */
  5678. R_SDREG(hmb_data, &regs->tohostmailboxdata, retries);
  5679. if (retries <= retry_limit)
  5680. W_SDREG(SMB_INT_ACK, &regs->tosbmailbox, retries);
  5681. bus->f1regdata += 2;
  5682. /* Dongle recomposed rx frames, accept them again */
  5683. if (hmb_data & HMB_DATA_NAKHANDLED) {
  5684. DHD_INFO(("Dongle reports NAK handled, expect rtx of %d\n", bus->rx_seq));
  5685. if (!bus->rxskip) {
  5686. DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __FUNCTION__));
  5687. }
  5688. bus->rxskip = FALSE;
  5689. intstatus |= FRAME_AVAIL_MASK(bus);
  5690. }
  5691. /*
  5692. * DEVREADY does not occur with gSPI.
  5693. */
  5694. if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
  5695. bus->sdpcm_ver = (hmb_data & HMB_DATA_VERSION_MASK) >> HMB_DATA_VERSION_SHIFT;
  5696. if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
  5697. DHD_ERROR(("Version mismatch, dongle reports %d, expecting %d\n",
  5698. bus->sdpcm_ver, SDPCM_PROT_VERSION));
  5699. else
  5700. DHD_INFO(("Dongle ready, protocol version %d\n", bus->sdpcm_ver));
  5701. #ifndef BCMSPI
  5702. /* make sure for the SDIO_DEVICE_RXDATAINT_MODE_1 corecontrol is proper */
  5703. if ((bus->sih->buscoretype == SDIOD_CORE_ID) && (bus->sdpcmrev >= 4) &&
  5704. (bus->rxint_mode == SDIO_DEVICE_RXDATAINT_MODE_1)) {
  5705. uint32 val;
  5706. val = R_REG(bus->dhd->osh, &bus->regs->corecontrol);
  5707. val &= ~CC_XMTDATAAVAIL_MODE;
  5708. val |= CC_XMTDATAAVAIL_CTRL;
  5709. W_REG(bus->dhd->osh, &bus->regs->corecontrol, val);
  5710. val = R_REG(bus->dhd->osh, &bus->regs->corecontrol);
  5711. }
  5712. #endif /* BCMSPI */
  5713. #ifdef DHD_DEBUG
  5714. /* Retrieve console state address now that firmware should have updated it */
  5715. {
  5716. sdpcm_shared_t shared;
  5717. if (dhdsdio_readshared(bus, &shared) == 0)
  5718. bus->console_addr = shared.console_addr;
  5719. }
  5720. #endif /* DHD_DEBUG */
  5721. }
  5722. /*
  5723. * Flow Control has been moved into the RX headers and this out of band
  5724. * method isn't used any more. Leave this here for possibly remaining backward
  5725. * compatible with older dongles
  5726. */
  5727. if (hmb_data & HMB_DATA_FC) {
  5728. fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >> HMB_DATA_FCDATA_SHIFT;
  5729. if (fcbits & ~bus->flowcontrol)
  5730. bus->fc_xoff++;
  5731. if (bus->flowcontrol & ~fcbits)
  5732. bus->fc_xon++;
  5733. bus->fc_rcvd++;
  5734. bus->flowcontrol = fcbits;
  5735. }
  5736. /* At least print a message if FW halted */
  5737. if (hmb_data & HMB_DATA_FWHALT) {
  5738. DHD_ERROR(("INTERNAL ERROR: FIRMWARE HALTED : set BUS DOWN\n"));
  5739. dhdsdio_checkdied(bus, NULL, 0);
  5740. DHD_ERROR(("Not doing bus down untill memdump done \n"));
  5741. }
  5742. /* Shouldn't be any others */
  5743. if (hmb_data & ~(HMB_DATA_DEVREADY |
  5744. HMB_DATA_FWHALT |
  5745. HMB_DATA_NAKHANDLED |
  5746. HMB_DATA_FC |
  5747. HMB_DATA_FWREADY |
  5748. HMB_DATA_FCDATA_MASK |
  5749. HMB_DATA_VERSION_MASK)) {
  5750. DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
  5751. }
  5752. if (hmbd) {
  5753. *hmbd = hmb_data;
  5754. }
  5755. return intstatus;
  5756. }
  5757. static bool
  5758. dhdsdio_dpc(dhd_bus_t *bus)
  5759. {
  5760. bcmsdh_info_t *sdh = bus->sdh;
  5761. sdpcmd_regs_t *regs = bus->regs;
  5762. uint32 intstatus, newstatus = 0;
  5763. uint retries = 0;
  5764. uint rxlimit = dhd_rxbound; /* Rx frames to read before resched */
  5765. uint txlimit = dhd_txbound; /* Tx frames to send before resched */
  5766. uint framecnt = 0; /* Temporary counter of tx/rx frames */
  5767. bool rxdone = TRUE; /* Flag for no more read data */
  5768. bool resched = FALSE; /* Flag indicating resched wanted */
  5769. unsigned long flags;
  5770. #ifdef DEBUG_DPC_THREAD_WATCHDOG
  5771. bool is_resched_by_readframe = FALSE;
  5772. #endif /* DEBUG_DPC_THREAD_WATCHDOG */
  5773. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  5774. dhd_os_sdlock(bus->dhd);
  5775. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  5776. if (bus->dhd->busstate == DHD_BUS_DOWN) {
  5777. DHD_ERROR(("%s: Bus down, ret\n", __FUNCTION__));
  5778. bus->intstatus = 0;
  5779. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  5780. dhd_os_sdunlock(bus->dhd);
  5781. return 0;
  5782. }
  5783. DHD_BUS_BUSY_SET_IN_DPC(bus->dhd);
  5784. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  5785. /* Start with leftover status bits */
  5786. intstatus = bus->intstatus;
  5787. if (!SLPAUTO_ENAB(bus) && !KSO_ENAB(bus)) {
  5788. DHD_ERROR(("%s: Device asleep\n", __FUNCTION__));
  5789. goto exit;
  5790. }
  5791. /* If waiting for HTAVAIL, check status */
  5792. if (!SLPAUTO_ENAB(bus) && (bus->clkstate == CLK_PENDING)) {
  5793. int err;
  5794. uint8 clkctl, devctl = 0;
  5795. #ifdef DHD_DEBUG
  5796. /* Check for inconsistent device control */
  5797. devctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
  5798. if (err) {
  5799. DHD_ERROR(("%s: error reading DEVCTL: %d\n", __FUNCTION__, err));
  5800. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  5801. bus->dhd->busstate = DHD_BUS_DOWN;
  5802. } else {
  5803. ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
  5804. }
  5805. #endif /* DHD_DEBUG */
  5806. /* Read CSR, if clock on switch to AVAIL, else ignore */
  5807. clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, &err);
  5808. if (err) {
  5809. DHD_ERROR(("%s: error reading CSR: %d\n", __FUNCTION__, err));
  5810. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  5811. bus->dhd->busstate = DHD_BUS_DOWN;
  5812. }
  5813. DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl, clkctl));
  5814. if (SBSDIO_HTAV(clkctl)) {
  5815. devctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
  5816. if (err) {
  5817. DHD_ERROR(("%s: error reading DEVCTL: %d\n",
  5818. __FUNCTION__, err));
  5819. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  5820. bus->dhd->busstate = DHD_BUS_DOWN;
  5821. }
  5822. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  5823. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, devctl, &err);
  5824. if (err) {
  5825. DHD_ERROR(("%s: error writing DEVCTL: %d\n",
  5826. __FUNCTION__, err));
  5827. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  5828. bus->dhd->busstate = DHD_BUS_DOWN;
  5829. }
  5830. bus->clkstate = CLK_AVAIL;
  5831. } else {
  5832. goto clkwait;
  5833. }
  5834. }
  5835. BUS_WAKE(bus);
  5836. /* Make sure backplane clock is on */
  5837. dhdsdio_clkctl(bus, CLK_AVAIL, TRUE);
  5838. if (bus->clkstate != CLK_AVAIL)
  5839. goto clkwait;
  5840. /* Pending interrupt indicates new device status */
  5841. if (bus->ipend) {
  5842. bus->ipend = FALSE;
  5843. #if defined(BT_OVER_SDIO)
  5844. bcmsdh_btsdio_process_f3_intr();
  5845. #endif /* defined (BT_OVER_SDIO) */
  5846. R_SDREG(newstatus, &regs->intstatus, retries);
  5847. bus->f1regdata++;
  5848. if (bcmsdh_regfail(bus->sdh))
  5849. newstatus = 0;
  5850. newstatus &= bus->hostintmask;
  5851. bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
  5852. if (newstatus) {
  5853. bus->f1regdata++;
  5854. #ifndef BCMSPI
  5855. if ((bus->rxint_mode == SDIO_DEVICE_RXDATAINT_MODE_0) &&
  5856. (newstatus == I_XMTDATA_AVAIL)) {
  5857. } else
  5858. #endif /* BCMSPI */
  5859. W_SDREG(newstatus, &regs->intstatus, retries);
  5860. }
  5861. }
  5862. /* Merge new bits with previous */
  5863. intstatus |= newstatus;
  5864. bus->intstatus = 0;
  5865. /* Handle flow-control change: read new state in case our ack
  5866. * crossed another change interrupt. If change still set, assume
  5867. * FC ON for safety, let next loop through do the debounce.
  5868. */
  5869. if (intstatus & I_HMB_FC_CHANGE) {
  5870. intstatus &= ~I_HMB_FC_CHANGE;
  5871. W_SDREG(I_HMB_FC_CHANGE, &regs->intstatus, retries);
  5872. R_SDREG(newstatus, &regs->intstatus, retries);
  5873. bus->f1regdata += 2;
  5874. bus->fcstate = !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
  5875. intstatus |= (newstatus & bus->hostintmask);
  5876. }
  5877. /* Handle host mailbox indication */
  5878. if (intstatus & I_HMB_HOST_INT) {
  5879. uint32 hmbdata = 0;
  5880. intstatus &= ~I_HMB_HOST_INT;
  5881. intstatus |= dhdsdio_hostmail(bus, &hmbdata);
  5882. #ifdef DHD_ULP
  5883. /* ULP prototyping. Redowload fw on oob interupt */
  5884. /* all the writes after this point CAN use cached sbwad value */
  5885. bcmsdh_force_sbwad_calc(bus->sdh, FALSE);
  5886. if (dhd_ulp_pre_redownload_check(bus->dhd, bus->sdh, hmbdata)) {
  5887. if (dhd_bus_ulp_reinit_fw(bus) < 0) {
  5888. DHD_ERROR(("%s:%d FW redownload failed\n",
  5889. __FUNCTION__, __LINE__));
  5890. goto exit;
  5891. }
  5892. }
  5893. #endif // endif
  5894. }
  5895. #ifdef DHD_UCODE_DOWNLOAD
  5896. exit_ucode:
  5897. #endif /* DHD_UCODE_DOWNLOAD */
  5898. /* Just being here means nothing more to do for chipactive */
  5899. if (intstatus & I_CHIPACTIVE) {
  5900. /* ASSERT(bus->clkstate == CLK_AVAIL); */
  5901. intstatus &= ~I_CHIPACTIVE;
  5902. }
  5903. /* Handle host mailbox indication */
  5904. if (intstatus & I_HMB_HOST_INT) {
  5905. intstatus &= ~I_HMB_HOST_INT;
  5906. intstatus |= dhdsdio_hostmail(bus, NULL);
  5907. }
  5908. /* Generally don't ask for these, can get CRC errors... */
  5909. if (intstatus & I_WR_OOSYNC) {
  5910. DHD_ERROR(("Dongle reports WR_OOSYNC\n"));
  5911. intstatus &= ~I_WR_OOSYNC;
  5912. }
  5913. if (intstatus & I_RD_OOSYNC) {
  5914. DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
  5915. intstatus &= ~I_RD_OOSYNC;
  5916. }
  5917. if (intstatus & I_SBINT) {
  5918. DHD_ERROR(("Dongle reports SBINT\n"));
  5919. intstatus &= ~I_SBINT;
  5920. }
  5921. /* Would be active due to wake-wlan in gSPI */
  5922. if (intstatus & I_CHIPACTIVE) {
  5923. DHD_INFO(("Dongle reports CHIPACTIVE\n"));
  5924. intstatus &= ~I_CHIPACTIVE;
  5925. }
  5926. if (intstatus & I_HMB_FC_STATE) {
  5927. DHD_INFO(("Dongle reports HMB_FC_STATE\n"));
  5928. intstatus &= ~I_HMB_FC_STATE;
  5929. }
  5930. /* Ignore frame indications if rxskip is set */
  5931. if (bus->rxskip) {
  5932. intstatus &= ~FRAME_AVAIL_MASK(bus);
  5933. }
  5934. /* On frame indication, read available frames */
  5935. if (PKT_AVAILABLE(bus, intstatus)) {
  5936. framecnt = dhdsdio_readframes(bus, rxlimit, &rxdone);
  5937. if (rxdone || bus->rxskip)
  5938. intstatus &= ~FRAME_AVAIL_MASK(bus);
  5939. rxlimit -= MIN(framecnt, rxlimit);
  5940. }
  5941. /* Keep still-pending events for next scheduling */
  5942. bus->intstatus = intstatus;
  5943. clkwait:
  5944. /* Re-enable interrupts to detect new device events (mailbox, rx frame)
  5945. * or clock availability. (Allows tx loop to check ipend if desired.)
  5946. * (Unless register access seems hosed, as we may not be able to ACK...)
  5947. */
  5948. if (bus->intr && bus->intdis && !bcmsdh_regfail(sdh)) {
  5949. DHD_INTR(("%s: enable SDIO interrupts, rxdone %d framecnt %d\n",
  5950. __FUNCTION__, rxdone, framecnt));
  5951. bus->intdis = FALSE;
  5952. #if defined(OOB_INTR_ONLY)
  5953. bcmsdh_oob_intr_set(bus->sdh, TRUE);
  5954. #endif /* defined(OOB_INTR_ONLY) */
  5955. bcmsdh_intr_enable(sdh);
  5956. #ifdef BCMSPI_ANDROID
  5957. if (*dhd_spi_lockcount == 0)
  5958. bcmsdh_oob_intr_set(bus->sdh, TRUE);
  5959. #endif /* BCMSPI_ANDROID */
  5960. }
  5961. #if defined(OOB_INTR_ONLY) && !defined(HW_OOB)
  5962. /* In case of SW-OOB(using edge trigger),
  5963. * Check interrupt status in the dongle again after enable irq on the host.
  5964. * and rechedule dpc if interrupt is pended in the dongle.
  5965. * There is a chance to miss OOB interrupt while irq is disabled on the host.
  5966. * No need to do this with HW-OOB(level trigger)
  5967. */
  5968. R_SDREG(newstatus, &regs->intstatus, retries);
  5969. if (bcmsdh_regfail(bus->sdh))
  5970. newstatus = 0;
  5971. if (newstatus & bus->hostintmask) {
  5972. bus->ipend = TRUE;
  5973. resched = TRUE;
  5974. }
  5975. #endif /* defined(OOB_INTR_ONLY) && !defined(HW_OOB) */
  5976. #ifdef PROP_TXSTATUS
  5977. dhd_wlfc_commit_packets(bus->dhd, (f_commitpkt_t)dhd_bus_txdata, (void *)bus, NULL, FALSE);
  5978. #endif // endif
  5979. if (TXCTLOK(bus) && bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL))
  5980. dhdsdio_sendpendctl(bus);
  5981. /* Send queued frames (limit 1 if rx may still be pending) */
  5982. else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
  5983. pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit && DATAOK(bus)) {
  5984. #ifdef DHD_ULP
  5985. if (dhd_ulp_f2_ready(bus->dhd, bus->sdh)) {
  5986. #endif /* DHD_ULP */
  5987. framecnt = rxdone ? txlimit : MIN(txlimit, dhd_txminmax);
  5988. framecnt = dhdsdio_sendfromq(bus, framecnt);
  5989. txlimit -= framecnt;
  5990. #ifdef DHD_ULP
  5991. } else {
  5992. /* In other transient states like DHD_ULP_, after the states are
  5993. * DHD_ULP_F2ENAB_CLEARING and DHD_ULP_F2ENAB_SETTING,
  5994. * dpc is scheduled after steady-state and dhdsdio_sendfromq() will
  5995. * execute again
  5996. */
  5997. }
  5998. #endif /* DHD_ULP */
  5999. }
  6000. /* Resched the DPC if ctrl cmd is pending on bus credit */
  6001. if (bus->ctrl_frame_stat)
  6002. resched = TRUE;
  6003. /* Resched if events or tx frames are pending, else await next interrupt */
  6004. /* On failed register access, all bets are off: no resched or interrupts */
  6005. if ((bus->dhd->busstate == DHD_BUS_DOWN) || bcmsdh_regfail(sdh)) {
  6006. if ((bus->sih && bus->sih->buscorerev >= 12) && !(dhdsdio_sleepcsr_get(bus) &
  6007. SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
  6008. /* Bus failed because of KSO */
  6009. DHD_ERROR(("%s: Bus failed due to KSO\n", __FUNCTION__));
  6010. bus->kso = FALSE;
  6011. } else {
  6012. DHD_ERROR(("%s: failed backplane access over SDIO, halting operation\n",
  6013. __FUNCTION__));
  6014. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  6015. bus->dhd->busstate = DHD_BUS_DOWN;
  6016. bus->intstatus = 0;
  6017. }
  6018. } else if (bus->clkstate == CLK_PENDING) {
  6019. /* Awaiting I_CHIPACTIVE; don't resched */
  6020. } else if (bus->intstatus || bus->ipend ||
  6021. (!bus->fcstate && pktq_mlen(&bus->txq, ~bus->flowcontrol) && DATAOK(bus)) ||
  6022. PKT_AVAILABLE(bus, bus->intstatus)) { /* Read multiple frames */
  6023. resched = TRUE;
  6024. }
  6025. bus->dpc_sched = resched;
  6026. /* If we're done for now, turn off clock request. */
  6027. if ((bus->idletime == DHD_IDLE_IMMEDIATE) && (bus->clkstate != CLK_PENDING) &&
  6028. NO_OTHER_ACTIVE_BUS_USER(bus)) {
  6029. bus->activity = FALSE;
  6030. dhdsdio_bussleep(bus, TRUE);
  6031. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  6032. }
  6033. exit:
  6034. if (!resched && dhd_dpcpoll) {
  6035. if (dhdsdio_readframes(bus, dhd_rxbound, &rxdone) != 0) {
  6036. resched = TRUE;
  6037. #ifdef DEBUG_DPC_THREAD_WATCHDOG
  6038. is_resched_by_readframe = TRUE;
  6039. #endif /* DEBUG_DPC_THREAD_WATCHDOG */
  6040. }
  6041. }
  6042. dhd_os_sdunlock(bus->dhd);
  6043. #ifdef DEBUG_DPC_THREAD_WATCHDOG
  6044. if (bus->dhd->dhd_bug_on) {
  6045. DHD_INFO(("%s: resched = %d ctrl_frame_stat = %d intstatus 0x%08x"
  6046. " ipend = %d pktq_mlen = %d is_resched_by_readframe = %d \n",
  6047. __FUNCTION__, resched, bus->ctrl_frame_stat,
  6048. bus->intstatus, bus->ipend,
  6049. pktq_mlen(&bus->txq, ~bus->flowcontrol), is_resched_by_readframe));
  6050. bus->dhd->dhd_bug_on = FALSE;
  6051. }
  6052. #endif /* DEBUG_DPC_THREAD_WATCHDOG */
  6053. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  6054. DHD_BUS_BUSY_CLEAR_IN_DPC(bus->dhd);
  6055. dhd_os_busbusy_wake(bus->dhd);
  6056. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  6057. return resched;
  6058. }
  6059. bool
  6060. dhd_bus_dpc(struct dhd_bus *bus)
  6061. {
  6062. bool resched;
  6063. /* Call the DPC directly. */
  6064. DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __FUNCTION__));
  6065. resched = dhdsdio_dpc(bus);
  6066. return resched;
  6067. }
  6068. void
  6069. dhdsdio_isr(void *arg)
  6070. {
  6071. dhd_bus_t *bus = (dhd_bus_t*)arg;
  6072. bcmsdh_info_t *sdh;
  6073. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  6074. if (!bus) {
  6075. DHD_ERROR(("%s : bus is null pointer , exit \n", __FUNCTION__));
  6076. return;
  6077. }
  6078. sdh = bus->sdh;
  6079. if (bus->dhd->busstate == DHD_BUS_DOWN) {
  6080. DHD_ERROR(("%s : bus is down. we have nothing to do\n", __FUNCTION__));
  6081. return;
  6082. }
  6083. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  6084. /* Count the interrupt call */
  6085. bus->intrcount++;
  6086. bus->ipend = TRUE;
  6087. /* Shouldn't get this interrupt if we're sleeping? */
  6088. if (!SLPAUTO_ENAB(bus)) {
  6089. if (bus->sleeping) {
  6090. DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
  6091. return;
  6092. } else if (!KSO_ENAB(bus)) {
  6093. DHD_ERROR(("ISR in devsleep 1\n"));
  6094. }
  6095. }
  6096. /* Disable additional interrupts (is this needed now)? */
  6097. if (bus->intr) {
  6098. DHD_INTR(("%s: disable SDIO interrupts\n", __FUNCTION__));
  6099. } else {
  6100. DHD_ERROR(("dhdsdio_isr() w/o interrupt configured!\n"));
  6101. }
  6102. #ifdef BCMSPI_ANDROID
  6103. bcmsdh_oob_intr_set(bus->sdh, FALSE);
  6104. #endif /* BCMSPI_ANDROID */
  6105. bcmsdh_intr_disable(sdh);
  6106. bus->intdis = TRUE;
  6107. #if defined(SDIO_ISR_THREAD)
  6108. DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __FUNCTION__));
  6109. DHD_OS_WAKE_LOCK(bus->dhd);
  6110. dhdsdio_dpc(bus);
  6111. DHD_OS_WAKE_UNLOCK(bus->dhd);
  6112. #else
  6113. bus->dpc_sched = TRUE;
  6114. dhd_sched_dpc(bus->dhd);
  6115. #endif /* defined(SDIO_ISR_THREAD) */
  6116. }
  6117. #ifdef SDTEST
  6118. static void
  6119. dhdsdio_pktgen_init(dhd_bus_t *bus)
  6120. {
  6121. /* Default to specified length, or full range */
  6122. if (dhd_pktgen_len) {
  6123. bus->pktgen_maxlen = MIN(dhd_pktgen_len, MAX_PKTGEN_LEN);
  6124. bus->pktgen_minlen = bus->pktgen_maxlen;
  6125. } else {
  6126. bus->pktgen_maxlen = MAX_PKTGEN_LEN;
  6127. bus->pktgen_minlen = 0;
  6128. }
  6129. bus->pktgen_len = (uint16)bus->pktgen_minlen;
  6130. /* Default to per-watchdog burst with 10s print time */
  6131. bus->pktgen_freq = 1;
  6132. bus->pktgen_print = dhd_watchdog_ms ? (10000 / dhd_watchdog_ms) : 0;
  6133. bus->pktgen_count = (dhd_pktgen * dhd_watchdog_ms + 999) / 1000;
  6134. /* Default to echo mode */
  6135. bus->pktgen_mode = DHD_PKTGEN_ECHO;
  6136. bus->pktgen_stop = 1;
  6137. }
  6138. static void
  6139. dhdsdio_pktgen(dhd_bus_t *bus)
  6140. {
  6141. void *pkt;
  6142. uint8 *data;
  6143. uint pktcount;
  6144. uint fillbyte;
  6145. osl_t *osh = bus->dhd->osh;
  6146. uint16 len;
  6147. ulong time_lapse;
  6148. uint sent_pkts;
  6149. uint rcvd_pkts;
  6150. /* Display current count if appropriate */
  6151. if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
  6152. bus->pktgen_ptick = 0;
  6153. printf("%s: send attempts %d, rcvd %d, errors %d\n",
  6154. __FUNCTION__, bus->pktgen_sent, bus->pktgen_rcvd, bus->pktgen_fail);
  6155. /* Print throughput stats only for constant length packet runs */
  6156. if (bus->pktgen_minlen == bus->pktgen_maxlen) {
  6157. time_lapse = jiffies - bus->pktgen_prev_time;
  6158. bus->pktgen_prev_time = jiffies;
  6159. sent_pkts = bus->pktgen_sent - bus->pktgen_prev_sent;
  6160. bus->pktgen_prev_sent = bus->pktgen_sent;
  6161. rcvd_pkts = bus->pktgen_rcvd - bus->pktgen_prev_rcvd;
  6162. bus->pktgen_prev_rcvd = bus->pktgen_rcvd;
  6163. printf("%s: Tx Throughput %d kbps, Rx Throughput %d kbps\n",
  6164. __FUNCTION__,
  6165. (sent_pkts * bus->pktgen_len / jiffies_to_msecs(time_lapse)) * 8,
  6166. (rcvd_pkts * bus->pktgen_len / jiffies_to_msecs(time_lapse)) * 8);
  6167. }
  6168. }
  6169. /* For recv mode, just make sure dongle has started sending */
  6170. if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
  6171. if (bus->pktgen_rcv_state == PKTGEN_RCV_IDLE) {
  6172. bus->pktgen_rcv_state = PKTGEN_RCV_ONGOING;
  6173. dhdsdio_sdtest_set(bus, bus->pktgen_total);
  6174. }
  6175. return;
  6176. }
  6177. /* Otherwise, generate or request the specified number of packets */
  6178. for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
  6179. /* Stop if total has been reached */
  6180. if (bus->pktgen_total && (bus->pktgen_sent >= bus->pktgen_total)) {
  6181. bus->pktgen_count = 0;
  6182. break;
  6183. }
  6184. /* Allocate an appropriate-sized packet */
  6185. if (bus->pktgen_mode == DHD_PKTGEN_RXBURST) {
  6186. len = SDPCM_TEST_PKT_CNT_FLD_LEN;
  6187. } else {
  6188. len = bus->pktgen_len;
  6189. }
  6190. if (!(pkt = PKTGET(osh, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN),
  6191. TRUE))) {;
  6192. DHD_ERROR(("%s: PKTGET failed!\n", __FUNCTION__));
  6193. break;
  6194. }
  6195. PKTALIGN(osh, pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), DHD_SDALIGN);
  6196. data = (uint8*)PKTDATA(osh, pkt) + SDPCM_HDRLEN;
  6197. /* Write test header cmd and extra based on mode */
  6198. switch (bus->pktgen_mode) {
  6199. case DHD_PKTGEN_ECHO:
  6200. *data++ = SDPCM_TEST_ECHOREQ;
  6201. *data++ = (uint8)bus->pktgen_sent;
  6202. break;
  6203. case DHD_PKTGEN_SEND:
  6204. *data++ = SDPCM_TEST_DISCARD;
  6205. *data++ = (uint8)bus->pktgen_sent;
  6206. break;
  6207. case DHD_PKTGEN_RXBURST:
  6208. *data++ = SDPCM_TEST_BURST;
  6209. *data++ = (uint8)bus->pktgen_count; /* Just for backward compatability */
  6210. break;
  6211. default:
  6212. DHD_ERROR(("Unrecognized pktgen mode %d\n", bus->pktgen_mode));
  6213. PKTFREE(osh, pkt, TRUE);
  6214. bus->pktgen_count = 0;
  6215. return;
  6216. }
  6217. /* Write test header length field */
  6218. *data++ = (bus->pktgen_len >> 0);
  6219. *data++ = (bus->pktgen_len >> 8);
  6220. /* Write frame count in a 4 byte field adjucent to SDPCM test header for
  6221. * burst mode
  6222. */
  6223. if (bus->pktgen_mode == DHD_PKTGEN_RXBURST) {
  6224. *data++ = (uint8)(bus->pktgen_count >> 0);
  6225. *data++ = (uint8)(bus->pktgen_count >> 8);
  6226. *data++ = (uint8)(bus->pktgen_count >> 16);
  6227. *data++ = (uint8)(bus->pktgen_count >> 24);
  6228. } else {
  6229. /* Then fill in the remainder -- N/A for burst */
  6230. for (fillbyte = 0; fillbyte < len; fillbyte++)
  6231. *data++ = SDPCM_TEST_FILL(fillbyte, (uint8)bus->pktgen_sent);
  6232. }
  6233. #ifdef DHD_DEBUG
  6234. if (DHD_BYTES_ON() && DHD_DATA_ON()) {
  6235. data = (uint8*)PKTDATA(osh, pkt) + SDPCM_HDRLEN;
  6236. prhex("dhdsdio_pktgen: Tx Data", data, PKTLEN(osh, pkt) - SDPCM_HDRLEN);
  6237. }
  6238. #endif // endif
  6239. /* Send it */
  6240. if (dhdsdio_txpkt(bus, SDPCM_TEST_CHANNEL, &pkt, 1, TRUE) != BCME_OK) {
  6241. bus->pktgen_fail++;
  6242. if (bus->pktgen_stop && bus->pktgen_stop == bus->pktgen_fail)
  6243. bus->pktgen_count = 0;
  6244. }
  6245. bus->pktgen_sent++;
  6246. /* Bump length if not fixed, wrap at max */
  6247. if (++bus->pktgen_len > bus->pktgen_maxlen)
  6248. bus->pktgen_len = (uint16)bus->pktgen_minlen;
  6249. /* Special case for burst mode: just send one request! */
  6250. if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
  6251. break;
  6252. }
  6253. }
  6254. static void
  6255. dhdsdio_sdtest_set(dhd_bus_t *bus, uint count)
  6256. {
  6257. void *pkt;
  6258. uint8 *data;
  6259. osl_t *osh = bus->dhd->osh;
  6260. /* Allocate the packet */
  6261. if (!(pkt = PKTGET(osh, SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
  6262. SDPCM_TEST_PKT_CNT_FLD_LEN + DHD_SDALIGN, TRUE))) {
  6263. DHD_ERROR(("%s: PKTGET failed!\n", __FUNCTION__));
  6264. return;
  6265. }
  6266. PKTALIGN(osh, pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
  6267. SDPCM_TEST_PKT_CNT_FLD_LEN), DHD_SDALIGN);
  6268. data = (uint8*)PKTDATA(osh, pkt) + SDPCM_HDRLEN;
  6269. /* Fill in the test header */
  6270. *data++ = SDPCM_TEST_SEND;
  6271. *data++ = (count > 0)?TRUE:FALSE;
  6272. *data++ = (bus->pktgen_maxlen >> 0);
  6273. *data++ = (bus->pktgen_maxlen >> 8);
  6274. *data++ = (uint8)(count >> 0);
  6275. *data++ = (uint8)(count >> 8);
  6276. *data++ = (uint8)(count >> 16);
  6277. *data++ = (uint8)(count >> 24);
  6278. /* Send it */
  6279. if (dhdsdio_txpkt(bus, SDPCM_TEST_CHANNEL, &pkt, 1, TRUE) != BCME_OK)
  6280. bus->pktgen_fail++;
  6281. }
  6282. static void
  6283. dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq)
  6284. {
  6285. osl_t *osh = bus->dhd->osh;
  6286. uint8 *data;
  6287. uint pktlen;
  6288. uint8 cmd;
  6289. uint8 extra;
  6290. uint16 len;
  6291. uint16 offset;
  6292. /* Check for min length */
  6293. if ((pktlen = PKTLEN(osh, pkt)) < SDPCM_TEST_HDRLEN) {
  6294. DHD_ERROR(("dhdsdio_restrcv: toss runt frame, pktlen %d\n", pktlen));
  6295. PKTFREE(osh, pkt, FALSE);
  6296. return;
  6297. }
  6298. /* Extract header fields */
  6299. data = PKTDATA(osh, pkt);
  6300. cmd = *data++;
  6301. extra = *data++;
  6302. len = *data++; len += *data++ << 8;
  6303. DHD_TRACE(("%s:cmd:%d, xtra:%d,len:%d\n", __FUNCTION__, cmd, extra, len));
  6304. /* Check length for relevant commands */
  6305. if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ || cmd == SDPCM_TEST_ECHORSP) {
  6306. if (pktlen != len + SDPCM_TEST_HDRLEN) {
  6307. DHD_ERROR(("dhdsdio_testrcv: frame length mismatch, pktlen %d seq %d"
  6308. " cmd %d extra %d len %d\n", pktlen, seq, cmd, extra, len));
  6309. PKTFREE(osh, pkt, FALSE);
  6310. return;
  6311. }
  6312. }
  6313. /* Process as per command */
  6314. switch (cmd) {
  6315. case SDPCM_TEST_ECHOREQ:
  6316. /* Rx->Tx turnaround ok (even on NDIS w/current implementation) */
  6317. *(uint8 *)(PKTDATA(osh, pkt)) = SDPCM_TEST_ECHORSP;
  6318. if (dhdsdio_txpkt(bus, SDPCM_TEST_CHANNEL, &pkt, 1, TRUE) == BCME_OK) {
  6319. bus->pktgen_sent++;
  6320. } else {
  6321. bus->pktgen_fail++;
  6322. PKTFREE(osh, pkt, FALSE);
  6323. }
  6324. bus->pktgen_rcvd++;
  6325. break;
  6326. case SDPCM_TEST_ECHORSP:
  6327. if (bus->ext_loop) {
  6328. PKTFREE(osh, pkt, FALSE);
  6329. bus->pktgen_rcvd++;
  6330. break;
  6331. }
  6332. for (offset = 0; offset < len; offset++, data++) {
  6333. if (*data != SDPCM_TEST_FILL(offset, extra)) {
  6334. DHD_ERROR(("dhdsdio_testrcv: echo data mismatch: "
  6335. "offset %d (len %d) expect 0x%02x rcvd 0x%02x\n",
  6336. offset, len, SDPCM_TEST_FILL(offset, extra), *data));
  6337. break;
  6338. }
  6339. }
  6340. PKTFREE(osh, pkt, FALSE);
  6341. bus->pktgen_rcvd++;
  6342. break;
  6343. case SDPCM_TEST_DISCARD:
  6344. {
  6345. int i = 0;
  6346. uint8 *prn = data;
  6347. uint8 testval = extra;
  6348. for (i = 0; i < len; i++) {
  6349. if (*prn != testval) {
  6350. DHD_ERROR(("DIErr@Pkt#:%d,Ix:%d, expected:0x%x, got:0x%x\n",
  6351. i, bus->pktgen_rcvd_rcvsession, testval, *prn));
  6352. prn++; testval++;
  6353. }
  6354. }
  6355. }
  6356. PKTFREE(osh, pkt, FALSE);
  6357. bus->pktgen_rcvd++;
  6358. break;
  6359. case SDPCM_TEST_BURST:
  6360. case SDPCM_TEST_SEND:
  6361. default:
  6362. DHD_INFO(("dhdsdio_testrcv: unsupported or unknown command, pktlen %d seq %d"
  6363. " cmd %d extra %d len %d\n", pktlen, seq, cmd, extra, len));
  6364. PKTFREE(osh, pkt, FALSE);
  6365. break;
  6366. }
  6367. /* For recv mode, stop at limit (and tell dongle to stop sending) */
  6368. if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
  6369. if (bus->pktgen_rcv_state != PKTGEN_RCV_IDLE) {
  6370. bus->pktgen_rcvd_rcvsession++;
  6371. if (bus->pktgen_total &&
  6372. (bus->pktgen_rcvd_rcvsession >= bus->pktgen_total)) {
  6373. bus->pktgen_count = 0;
  6374. DHD_ERROR(("Pktgen:rcv test complete!\n"));
  6375. bus->pktgen_rcv_state = PKTGEN_RCV_IDLE;
  6376. dhdsdio_sdtest_set(bus, FALSE);
  6377. bus->pktgen_rcvd_rcvsession = 0;
  6378. }
  6379. }
  6380. }
  6381. }
  6382. #endif /* SDTEST */
  6383. int dhd_bus_oob_intr_register(dhd_pub_t *dhdp)
  6384. {
  6385. int err = 0;
  6386. #if defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID)
  6387. err = bcmsdh_oob_intr_register(dhdp->bus->sdh, dhdsdio_isr, dhdp->bus);
  6388. #endif // endif
  6389. return err;
  6390. }
  6391. void dhd_bus_oob_intr_unregister(dhd_pub_t *dhdp)
  6392. {
  6393. #if defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID)
  6394. bcmsdh_oob_intr_unregister(dhdp->bus->sdh);
  6395. #endif // endif
  6396. }
  6397. void dhd_bus_oob_intr_set(dhd_pub_t *dhdp, bool enable)
  6398. {
  6399. #if defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID)
  6400. bcmsdh_oob_intr_set(dhdp->bus->sdh, enable);
  6401. #endif // endif
  6402. }
  6403. void dhd_bus_dev_pm_stay_awake(dhd_pub_t *dhdpub)
  6404. {
  6405. bcmsdh_dev_pm_stay_awake(dhdpub->bus->sdh);
  6406. }
  6407. void dhd_bus_dev_pm_relax(dhd_pub_t *dhdpub)
  6408. {
  6409. bcmsdh_dev_relax(dhdpub->bus->sdh);
  6410. }
  6411. bool dhd_bus_dev_pm_enabled(dhd_pub_t *dhdpub)
  6412. {
  6413. bool enabled = FALSE;
  6414. enabled = bcmsdh_dev_pm_enabled(dhdpub->bus->sdh);
  6415. return enabled;
  6416. }
  6417. extern bool
  6418. dhd_bus_watchdog(dhd_pub_t *dhdp)
  6419. {
  6420. dhd_bus_t *bus;
  6421. unsigned long flags;
  6422. DHD_TIMER(("%s: Enter\n", __FUNCTION__));
  6423. bus = dhdp->bus;
  6424. if (bus->dhd->dongle_reset)
  6425. return FALSE;
  6426. if (bus->dhd->hang_was_sent) {
  6427. dhd_os_wd_timer(bus->dhd, 0);
  6428. return FALSE;
  6429. }
  6430. /* Ignore the timer if simulating bus down */
  6431. if (!SLPAUTO_ENAB(bus) && bus->sleeping)
  6432. return FALSE;
  6433. DHD_LINUX_GENERAL_LOCK(dhdp, flags);
  6434. if (DHD_BUS_CHECK_DOWN_OR_DOWN_IN_PROGRESS(dhdp) ||
  6435. DHD_BUS_CHECK_SUSPEND_OR_SUSPEND_IN_PROGRESS(dhdp)) {
  6436. DHD_LINUX_GENERAL_UNLOCK(dhdp, flags);
  6437. return FALSE;
  6438. }
  6439. DHD_BUS_BUSY_SET_IN_WD(dhdp);
  6440. DHD_LINUX_GENERAL_UNLOCK(dhdp, flags);
  6441. dhd_os_sdlock(bus->dhd);
  6442. /* Poll period: check device if appropriate. */
  6443. if (!SLPAUTO_ENAB(bus) && (bus->poll && (++bus->polltick >= bus->pollrate))) {
  6444. uint32 intstatus = 0;
  6445. /* Reset poll tick */
  6446. bus->polltick = 0;
  6447. /* Check device if no interrupts */
  6448. if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
  6449. #ifndef BCMSPI
  6450. if (!bus->dpc_sched) {
  6451. uint8 devpend;
  6452. devpend = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0,
  6453. SDIOD_CCCR_INTPEND, NULL);
  6454. intstatus = devpend & (INTR_STATUS_FUNC1 | INTR_STATUS_FUNC2);
  6455. }
  6456. #else
  6457. if (!bus->dpc_sched) {
  6458. uint32 devpend;
  6459. devpend = bcmsdh_cfg_read_word(bus->sdh, SDIO_FUNC_0,
  6460. SPID_STATUS_REG, NULL);
  6461. intstatus = devpend & STATUS_F2_PKT_AVAILABLE;
  6462. }
  6463. #endif /* !BCMSPI */
  6464. /* If there is something, make like the ISR and schedule the DPC */
  6465. if (intstatus) {
  6466. bus->pollcnt++;
  6467. bus->ipend = TRUE;
  6468. if (bus->intr) {
  6469. bcmsdh_intr_disable(bus->sdh);
  6470. }
  6471. bus->dpc_sched = TRUE;
  6472. dhd_sched_dpc(bus->dhd);
  6473. }
  6474. }
  6475. /* Update interrupt tracking */
  6476. bus->lastintrs = bus->intrcount;
  6477. }
  6478. #ifdef DHD_DEBUG
  6479. /* Poll for console output periodically */
  6480. if (dhdp->busstate == DHD_BUS_DATA && dhdp->dhd_console_ms != 0) {
  6481. bus->console.count += dhd_watchdog_ms;
  6482. if (bus->console.count >= dhdp->dhd_console_ms) {
  6483. bus->console.count -= dhdp->dhd_console_ms;
  6484. /* Make sure backplane clock is on */
  6485. if (SLPAUTO_ENAB(bus))
  6486. dhdsdio_bussleep(bus, FALSE);
  6487. else
  6488. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  6489. if (dhdsdio_readconsole(bus) < 0)
  6490. dhdp->dhd_console_ms = 0; /* On error, stop trying */
  6491. }
  6492. }
  6493. #endif /* DHD_DEBUG */
  6494. #ifdef SDTEST
  6495. /* Generate packets if configured */
  6496. if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
  6497. /* Make sure backplane clock is on */
  6498. if (SLPAUTO_ENAB(bus))
  6499. dhdsdio_bussleep(bus, FALSE);
  6500. else
  6501. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  6502. bus->pktgen_tick = 0;
  6503. dhdsdio_pktgen(bus);
  6504. }
  6505. #endif // endif
  6506. /* On idle timeout clear activity flag and/or turn off clock */
  6507. #ifdef DHD_USE_IDLECOUNT
  6508. if (bus->activity)
  6509. bus->activity = FALSE;
  6510. else {
  6511. bus->idlecount++;
  6512. /*
  6513. * If the condition to switch off the clock is reached And if
  6514. * BT is inactive (in case of BT_OVER_SDIO build) turn off clk.
  6515. *
  6516. * Consider the following case, DHD is configured with
  6517. * 1) idletime == DHD_IDLE_IMMEDIATE
  6518. * 2) BT is the last user of the clock
  6519. * We cannot disable the clock from __dhdsdio_clk_disable
  6520. * since WLAN might be using it. If WLAN is active then
  6521. * from the respective function/context after doing the job
  6522. * the clk is turned off.
  6523. * But if WLAN is actually inactive then the watchdog should
  6524. * disable the clock. So the condition check below should be
  6525. * bus->idletime != 0 instead of idletime == 0
  6526. */
  6527. if ((bus->idletime != 0) && (bus->idlecount >= bus->idletime) &&
  6528. NO_OTHER_ACTIVE_BUS_USER(bus)) {
  6529. DHD_TIMER(("%s: DHD Idle state!!\n", __FUNCTION__));
  6530. if (SLPAUTO_ENAB(bus)) {
  6531. if (dhdsdio_bussleep(bus, TRUE) != BCME_BUSY)
  6532. dhd_os_wd_timer(bus->dhd, 0);
  6533. } else
  6534. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  6535. bus->idlecount = 0;
  6536. }
  6537. }
  6538. #else
  6539. if ((bus->idletime != 0) && (bus->clkstate == CLK_AVAIL) &&
  6540. NO_OTHER_ACTIVE_BUS_USER(bus)) {
  6541. if (++bus->idlecount >= bus->idletime) {
  6542. bus->idlecount = 0;
  6543. if (bus->activity) {
  6544. bus->activity = FALSE;
  6545. #if !defined(OEM_ANDROID)
  6546. } else {
  6547. #endif // endif
  6548. if (SLPAUTO_ENAB(bus)) {
  6549. if (!bus->readframes)
  6550. dhdsdio_bussleep(bus, TRUE);
  6551. else
  6552. bus->reqbussleep = TRUE;
  6553. } else {
  6554. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  6555. }
  6556. }
  6557. }
  6558. }
  6559. #endif /* DHD_USE_IDLECOUNT */
  6560. dhd_os_sdunlock(bus->dhd);
  6561. DHD_LINUX_GENERAL_LOCK(dhdp, flags);
  6562. DHD_BUS_BUSY_CLEAR_IN_WD(dhdp);
  6563. dhd_os_busbusy_wake(dhdp);
  6564. DHD_LINUX_GENERAL_UNLOCK(dhdp, flags);
  6565. return bus->ipend;
  6566. }
  6567. extern int
  6568. dhd_bus_console_in(dhd_pub_t *dhdp, uchar *msg, uint msglen)
  6569. {
  6570. dhd_bus_t *bus = dhdp->bus;
  6571. uint32 addr, val;
  6572. int rv;
  6573. void *pkt;
  6574. /* Address could be zero if CONSOLE := 0 in dongle Makefile */
  6575. if (bus->console_addr == 0)
  6576. return BCME_UNSUPPORTED;
  6577. /* Exclusive bus access */
  6578. dhd_os_sdlock(bus->dhd);
  6579. /* Don't allow input if dongle is in reset */
  6580. if (bus->dhd->dongle_reset) {
  6581. dhd_os_sdunlock(bus->dhd);
  6582. return BCME_NOTREADY;
  6583. }
  6584. /* Request clock to allow SDIO accesses */
  6585. BUS_WAKE(bus);
  6586. /* No pend allowed since txpkt is called later, ht clk has to be on */
  6587. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  6588. /* Zero cbuf_index */
  6589. addr = bus->console_addr + OFFSETOF(hnd_cons_t, cbuf_idx);
  6590. val = htol32(0);
  6591. if ((rv = dhdsdio_membytes(bus, TRUE, addr, (uint8 *)&val, sizeof(val))) < 0)
  6592. goto done;
  6593. /* Write message into cbuf */
  6594. addr = bus->console_addr + OFFSETOF(hnd_cons_t, cbuf);
  6595. if ((rv = dhdsdio_membytes(bus, TRUE, addr, (uint8 *)msg, msglen)) < 0)
  6596. goto done;
  6597. /* Write length into vcons_in */
  6598. addr = bus->console_addr + OFFSETOF(hnd_cons_t, vcons_in);
  6599. val = htol32(msglen);
  6600. if ((rv = dhdsdio_membytes(bus, TRUE, addr, (uint8 *)&val, sizeof(val))) < 0)
  6601. goto done;
  6602. /* Bump dongle by sending an empty packet on the event channel.
  6603. * sdpcm_sendup (RX) checks for virtual console input.
  6604. */
  6605. if ((pkt = PKTGET(bus->dhd->osh, 4 + SDPCM_RESERVE, TRUE)) != NULL)
  6606. rv = dhdsdio_txpkt(bus, SDPCM_EVENT_CHANNEL, &pkt, 1, TRUE);
  6607. done:
  6608. if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched &&
  6609. NO_OTHER_ACTIVE_BUS_USER(bus)) {
  6610. bus->activity = FALSE;
  6611. dhdsdio_bussleep(bus, TRUE);
  6612. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  6613. }
  6614. dhd_os_sdunlock(bus->dhd);
  6615. return rv;
  6616. }
  6617. #if defined(DHD_DEBUG) && !defined(BCMSDIOLITE)
  6618. static void
  6619. dhd_dump_cis(uint fn, uint8 *cis)
  6620. {
  6621. uint byte, tag, tdata;
  6622. DHD_INFO(("Function %d CIS:\n", fn));
  6623. for (tdata = byte = 0; byte < SBSDIO_CIS_SIZE_LIMIT; byte++) {
  6624. if ((byte % 16) == 0)
  6625. DHD_INFO((" "));
  6626. DHD_INFO(("%02x ", cis[byte]));
  6627. if ((byte % 16) == 15)
  6628. DHD_INFO(("\n"));
  6629. if (!tdata--) {
  6630. tag = cis[byte];
  6631. if (tag == 0xff)
  6632. break;
  6633. else if (!tag)
  6634. tdata = 0;
  6635. else if ((byte + 1) < SBSDIO_CIS_SIZE_LIMIT)
  6636. tdata = cis[byte + 1] + 1;
  6637. else
  6638. DHD_INFO(("]"));
  6639. }
  6640. }
  6641. if ((byte % 16) != 15)
  6642. DHD_INFO(("\n"));
  6643. }
  6644. #endif /* DHD_DEBUG */
  6645. static bool
  6646. dhdsdio_chipmatch(uint16 chipid)
  6647. {
  6648. if (chipid == BCM4335_CHIP_ID)
  6649. return TRUE;
  6650. if (chipid == BCM4339_CHIP_ID)
  6651. return TRUE;
  6652. if (BCM4345_CHIP(chipid))
  6653. return TRUE;
  6654. if (chipid == BCM4350_CHIP_ID)
  6655. return TRUE;
  6656. if (chipid == BCM4354_CHIP_ID)
  6657. return TRUE;
  6658. if (chipid == BCM4358_CHIP_ID)
  6659. return TRUE;
  6660. if (chipid == BCM43430_CHIP_ID)
  6661. return TRUE;
  6662. if (chipid == BCM43018_CHIP_ID)
  6663. return TRUE;
  6664. if (BCM4349_CHIP(chipid))
  6665. return TRUE;
  6666. if (chipid == BCM4373_CHIP_ID)
  6667. return TRUE;
  6668. if (chipid == BCM4364_CHIP_ID)
  6669. return TRUE;
  6670. if (chipid == BCM43012_CHIP_ID)
  6671. return TRUE;
  6672. if (chipid == BCM43014_CHIP_ID)
  6673. return TRUE;
  6674. if (chipid == BCM4369_CHIP_ID)
  6675. return TRUE;
  6676. #ifdef CHIPS_CUSTOMER_HW6
  6677. if (BCM4378_CHIP(chipid)) {
  6678. return TRUE;
  6679. }
  6680. #endif /* CHIPS_CUSTOMER_HW6 */
  6681. if (chipid == BCM4362_CHIP_ID)
  6682. return TRUE;
  6683. if (chipid == BCM43751_CHIP_ID)
  6684. return TRUE;
  6685. return FALSE;
  6686. }
  6687. static void *
  6688. dhdsdio_probe(uint16 venid, uint16 devid, uint16 bus_no, uint16 slot,
  6689. uint16 func, uint bustype, void *regsva, osl_t * osh, void *sdh)
  6690. {
  6691. int ret;
  6692. dhd_bus_t *bus;
  6693. #if defined(MULTIPLE_SUPPLICANT)
  6694. if (mutex_is_locked(&_dhd_sdio_mutex_lock_) == 0) {
  6695. DHD_ERROR(("%s : no mutex held. set lock\n", __FUNCTION__));
  6696. } else {
  6697. DHD_ERROR(("%s : mutex is locked!. wait for unlocking\n", __FUNCTION__));
  6698. }
  6699. mutex_lock(&_dhd_sdio_mutex_lock_);
  6700. #endif // endif
  6701. /* Init global variables at run-time, not as part of the declaration.
  6702. * This is required to support init/de-init of the driver. Initialization
  6703. * of globals as part of the declaration results in non-deterministic
  6704. * behavior since the value of the globals may be different on the
  6705. * first time that the driver is initialized vs subsequent initializations.
  6706. */
  6707. dhd_txbound = DHD_TXBOUND;
  6708. dhd_rxbound = DHD_RXBOUND;
  6709. #ifdef BCMSPI
  6710. dhd_alignctl = FALSE;
  6711. #else
  6712. dhd_alignctl = TRUE;
  6713. #endif /* BCMSPI */
  6714. sd1idle = TRUE;
  6715. dhd_readahead = TRUE;
  6716. retrydata = FALSE;
  6717. #ifdef DISABLE_FLOW_CONTROL
  6718. dhd_doflow = FALSE;
  6719. #endif /* DISABLE_FLOW_CONTROL */
  6720. dhd_dongle_ramsize = 0;
  6721. dhd_txminmax = DHD_TXMINMAX;
  6722. #ifdef BCMSPI
  6723. forcealign = FALSE;
  6724. #else
  6725. forcealign = TRUE;
  6726. #endif /* !BCMSPI */
  6727. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  6728. DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __FUNCTION__, venid, devid));
  6729. /* We make assumptions about address window mappings */
  6730. ASSERT((uintptr)regsva == si_enum_base(devid));
  6731. /* BCMSDH passes venid and devid based on CIS parsing -- but low-power start
  6732. * means early parse could fail, so here we should get either an ID
  6733. * we recognize OR (-1) indicating we must request power first.
  6734. */
  6735. /* Check the Vendor ID */
  6736. switch (venid) {
  6737. case 0x0000:
  6738. case VENDOR_BROADCOM:
  6739. break;
  6740. default:
  6741. DHD_ERROR(("%s: unknown vendor: 0x%04x\n",
  6742. __FUNCTION__, venid));
  6743. goto forcereturn;
  6744. }
  6745. /* Check the Device ID and make sure it's one that we support */
  6746. switch (devid) {
  6747. case 0:
  6748. DHD_INFO(("%s: allow device id 0, will check chip internals\n",
  6749. __FUNCTION__));
  6750. break;
  6751. default:
  6752. DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
  6753. __FUNCTION__, venid, devid));
  6754. goto forcereturn;
  6755. }
  6756. if (osh == NULL) {
  6757. DHD_ERROR(("%s: osh is NULL!\n", __FUNCTION__));
  6758. goto forcereturn;
  6759. }
  6760. /* Allocate private bus interface state */
  6761. if (!(bus = MALLOC(osh, sizeof(dhd_bus_t)))) {
  6762. DHD_ERROR(("%s: MALLOC of dhd_bus_t failed\n", __FUNCTION__));
  6763. goto fail;
  6764. }
  6765. bzero(bus, sizeof(dhd_bus_t));
  6766. bus->sdh = sdh;
  6767. bus->cl_devid = (uint16)devid;
  6768. bus->bus = DHD_BUS;
  6769. bus->bus_num = bus_no;
  6770. bus->slot_num = slot;
  6771. bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
  6772. bus->usebufpool = FALSE; /* Use bufpool if allocated, else use locally malloced rxbuf */
  6773. #ifdef BT_OVER_SDIO
  6774. bus->bt_use_count = 0;
  6775. #endif // endif
  6776. #if defined(SUPPORT_P2P_GO_PS)
  6777. init_waitqueue_head(&bus->bus_sleep);
  6778. #endif /* LINUX && SUPPORT_P2P_GO_PS */
  6779. /* attempt to attach to the dongle */
  6780. if (!(dhdsdio_probe_attach(bus, osh, sdh, regsva, devid))) {
  6781. DHD_ERROR(("%s: dhdsdio_probe_attach failed\n", __FUNCTION__));
  6782. goto fail;
  6783. }
  6784. /* Attach to the dhd/OS/network interface */
  6785. if (!(bus->dhd = dhd_attach(osh, bus, SDPCM_RESERVE))) {
  6786. DHD_ERROR(("%s: dhd_attach failed\n", __FUNCTION__));
  6787. goto fail;
  6788. }
  6789. /* Allocate buffers */
  6790. if (!(dhdsdio_probe_malloc(bus, osh, sdh))) {
  6791. DHD_ERROR(("%s: dhdsdio_probe_malloc failed\n", __FUNCTION__));
  6792. goto fail;
  6793. }
  6794. if (!(dhdsdio_probe_init(bus, osh, sdh))) {
  6795. DHD_ERROR(("%s: dhdsdio_probe_init failed\n", __FUNCTION__));
  6796. goto fail;
  6797. }
  6798. if (bus->intr) {
  6799. /* Register interrupt callback, but mask it (not operational yet). */
  6800. DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n", __FUNCTION__));
  6801. bcmsdh_intr_disable(sdh);
  6802. if ((ret = bcmsdh_intr_reg(sdh, dhdsdio_isr, bus)) != 0) {
  6803. DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
  6804. __FUNCTION__, ret));
  6805. goto fail;
  6806. }
  6807. DHD_INTR(("%s: registered SDIO interrupt function ok\n", __FUNCTION__));
  6808. } else {
  6809. DHD_INFO(("%s: SDIO interrupt function is NOT registered due to polling mode\n",
  6810. __FUNCTION__));
  6811. }
  6812. DHD_INFO(("%s: completed!!\n", __FUNCTION__));
  6813. /* if firmware path present try to download and bring up bus */
  6814. bus->dhd->hang_report = TRUE;
  6815. if (dhd_download_fw_on_driverload) {
  6816. if ((ret = dhd_bus_start(bus->dhd)) != 0) {
  6817. DHD_ERROR(("%s: dhd_bus_start failed\n", __FUNCTION__));
  6818. #if !defined(OEM_ANDROID)
  6819. if (ret == BCME_NOTUP)
  6820. #endif /* !OEM_ANDROID */
  6821. goto fail;
  6822. }
  6823. }
  6824. else {
  6825. /* Set random MAC address during boot time */
  6826. get_random_bytes(&bus->dhd->mac.octet[3], 3);
  6827. /* Adding BRCM OUI */
  6828. bus->dhd->mac.octet[0] = 0;
  6829. bus->dhd->mac.octet[1] = 0x90;
  6830. bus->dhd->mac.octet[2] = 0x4C;
  6831. }
  6832. #if defined(BT_OVER_SDIO)
  6833. /* At this point Regulators are turned on and iconditionaly sdio bus is started
  6834. * based upon dhd_download_fw_on_driverload check, so
  6835. * increase the bus user count, this count will only be disabled inside
  6836. * dhd_register_if() function if flag dhd_download_fw_on_driverload is set to false,
  6837. * i.e FW download during insmod is not needed, otherwise it will not be decremented
  6838. * so that WALN will always hold the bus untill rmmod is done.
  6839. */
  6840. dhdsdio_bus_usr_cnt_inc(bus->dhd);
  6841. #endif /* BT_OVER_SDIO */
  6842. /* Ok, have the per-port tell the stack we're open for business */
  6843. if (dhd_attach_net(bus->dhd, TRUE) != 0)
  6844. {
  6845. DHD_ERROR(("%s: Net attach failed!!\n", __FUNCTION__));
  6846. goto fail;
  6847. }
  6848. #ifdef BCMHOST_XTAL_PU_TIME_MOD
  6849. bcmsdh_reg_write(bus->sdh, 0x18000620, 2, 11);
  6850. bcmsdh_reg_write(bus->sdh, 0x18000628, 4, 0x00F80001);
  6851. #endif /* BCMHOST_XTAL_PU_TIME_MOD */
  6852. #if defined(MULTIPLE_SUPPLICANT)
  6853. mutex_unlock(&_dhd_sdio_mutex_lock_);
  6854. DHD_ERROR(("%s : the lock is released.\n", __FUNCTION__));
  6855. #endif // endif
  6856. return bus;
  6857. fail:
  6858. dhdsdio_release(bus, osh);
  6859. forcereturn:
  6860. #if defined(MULTIPLE_SUPPLICANT)
  6861. mutex_unlock(&_dhd_sdio_mutex_lock_);
  6862. DHD_ERROR(("%s : the lock is released.\n", __FUNCTION__));
  6863. #endif // endif
  6864. return NULL;
  6865. }
  6866. static bool
  6867. dhdsdio_probe_attach(struct dhd_bus *bus, osl_t *osh, void *sdh, void *regsva,
  6868. uint16 devid)
  6869. {
  6870. #ifndef BCMSPI
  6871. uint8 clkctl = 0;
  6872. #endif /* !BCMSPI */
  6873. uint fn, numfn;
  6874. uint8 *cis[SDIOD_MAX_IOFUNCS];
  6875. int32 value;
  6876. int32 size;
  6877. int err = 0;
  6878. BCM_REFERENCE(value);
  6879. bus->alp_only = TRUE;
  6880. bus->sih = NULL;
  6881. /* Return the window to backplane enumeration space for core access */
  6882. if (dhdsdio_set_siaddr_window(bus, si_enum_base(devid))) {
  6883. DHD_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n", __FUNCTION__));
  6884. }
  6885. #if defined(DHD_DEBUG) && !defined(CUSTOMER_HW4_DEBUG)
  6886. DHD_ERROR(("F1 signature read @0x18000000=0x%4x\n",
  6887. bcmsdh_reg_read(bus->sdh, si_enum_base(devid), 4)));
  6888. #endif /* DHD_DEBUG && !CUSTOMER_HW4_DEBUG */
  6889. #ifndef BCMSPI /* wake-wlan in gSPI will bring up the htavail/alpavail clocks. */
  6890. /* Force PLL off until si_attach() programs PLL control regs */
  6891. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, DHD_INIT_CLKCTL1, &err);
  6892. if (!err)
  6893. clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, &err);
  6894. if (err || ((clkctl & ~SBSDIO_AVBITS) != DHD_INIT_CLKCTL1)) {
  6895. DHD_ERROR(("dhdsdio_probe: ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
  6896. err, DHD_INIT_CLKCTL1, clkctl));
  6897. goto fail;
  6898. }
  6899. #endif /* !BCMSPI */
  6900. #ifndef BCMSPI
  6901. numfn = bcmsdh_query_iofnum(sdh);
  6902. ASSERT(numfn <= SDIOD_MAX_IOFUNCS);
  6903. /* Make sure ALP is available before trying to read CIS */
  6904. SPINWAIT(((clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
  6905. SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
  6906. !SBSDIO_ALPAV(clkctl)), PMU_MAX_TRANSITION_DLY);
  6907. /* Now request ALP be put on the bus */
  6908. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
  6909. DHD_INIT_CLKCTL2, &err);
  6910. OSL_DELAY(65);
  6911. #else
  6912. numfn = 0; /* internally func is hardcoded to 1 as gSPI has cis on F1 only */
  6913. #endif /* !BCMSPI */
  6914. #ifndef BCMSDIOLITE
  6915. for (fn = 0; fn <= numfn; fn++) {
  6916. if (!(cis[fn] = MALLOC(osh, SBSDIO_CIS_SIZE_LIMIT))) {
  6917. DHD_INFO(("dhdsdio_probe: fn %d cis malloc failed\n", fn));
  6918. break;
  6919. }
  6920. bzero(cis[fn], SBSDIO_CIS_SIZE_LIMIT);
  6921. if ((err = bcmsdh_cis_read(sdh, fn, cis[fn],
  6922. SBSDIO_CIS_SIZE_LIMIT))) {
  6923. DHD_INFO(("dhdsdio_probe: fn %d cis read err %d\n", fn, err));
  6924. MFREE(osh, cis[fn], SBSDIO_CIS_SIZE_LIMIT);
  6925. break;
  6926. }
  6927. /* Reading the F1, F2 and F3 max blocksize values from CIS
  6928. * and writing into the F1, F2 and F3 block size registers.
  6929. * There is no max block size register value available for F0 in CIS register.
  6930. * So, setting default value for F0 block size as 32 (which was set earlier
  6931. * in iovar). IOVAR takes only one arguement.
  6932. * So, we are passing the function number alongwith the value (fn<<16)
  6933. */
  6934. if (!fn)
  6935. value = F0_BLOCK_SIZE;
  6936. else
  6937. value = (cis[fn][25]<<8) | cis[fn][24] | (fn<<16);
  6938. /* Get block size from sd */
  6939. if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fn, sizeof(int32),
  6940. &size, sizeof(int32), FALSE) != BCME_OK) {
  6941. size = 0;
  6942. DHD_ERROR(("%s: fail on fn %d %s get\n",
  6943. __FUNCTION__, fn, "sd_blocksize"));
  6944. } else {
  6945. DHD_INFO(("%s: Initial value for fn %d %s is %d\n",
  6946. __FUNCTION__, fn, "sd_blocksize", size));
  6947. }
  6948. if (size != 0 && size < value) {
  6949. value = size;
  6950. }
  6951. value = fn << 16 | value;
  6952. if (bcmsdh_iovar_op(sdh, "sd_blocksize", NULL, 0, &value,
  6953. sizeof(value), TRUE) != BCME_OK) {
  6954. bus->blocksize = 0;
  6955. DHD_ERROR(("%s: fail on fn %d %s set\n", __FUNCTION__,
  6956. fn, "sd_blocksize"));
  6957. }
  6958. #ifdef DHD_DEBUG
  6959. if (DHD_INFO_ON()) {
  6960. dhd_dump_cis(fn, cis[fn]);
  6961. }
  6962. #endif /* DHD_DEBUG */
  6963. }
  6964. while (fn-- > 0) {
  6965. ASSERT(cis[fn]);
  6966. MFREE(osh, cis[fn], SBSDIO_CIS_SIZE_LIMIT);
  6967. }
  6968. #else
  6969. BCM_REFERENCE(cis);
  6970. BCM_REFERENCE(fn);
  6971. #endif /* DHD_DEBUG */
  6972. if (err) {
  6973. DHD_ERROR(("dhdsdio_probe: failure reading or parsing CIS\n"));
  6974. goto fail;
  6975. }
  6976. /* si_attach() will provide an SI handle and scan the backplane */
  6977. if (!(bus->sih = si_attach((uint)devid, osh, regsva, DHD_BUS, sdh,
  6978. &bus->vars, &bus->varsz))) {
  6979. DHD_ERROR(("%s: si_attach failed!\n", __FUNCTION__));
  6980. goto fail;
  6981. }
  6982. #ifdef DHD_DEBUG
  6983. DHD_ERROR(("F1 signature OK, socitype:0x%x chip:0x%4x rev:0x%x pkg:0x%x\n",
  6984. bus->sih->socitype, bus->sih->chip, bus->sih->chiprev, bus->sih->chippkg));
  6985. #endif /* DHD_DEBUG */
  6986. bcmsdh_chipinfo(sdh, bus->sih->chip, bus->sih->chiprev);
  6987. if (!dhdsdio_chipmatch((uint16)bus->sih->chip)) {
  6988. DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
  6989. __FUNCTION__, bus->sih->chip));
  6990. goto fail;
  6991. }
  6992. if (bus->sih->buscorerev >= 12)
  6993. dhdsdio_clk_kso_init(bus);
  6994. else
  6995. bus->kso = TRUE;
  6996. si_sdiod_drive_strength_init(bus->sih, osh, dhd_sdiod_drive_strength);
  6997. /* Get info on the ARM and SOCRAM cores... */
  6998. if (!DHD_NOPMU(bus)) {
  6999. if ((si_setcore(bus->sih, ARM7S_CORE_ID, 0)) ||
  7000. (si_setcore(bus->sih, ARMCM3_CORE_ID, 0)) ||
  7001. (si_setcore(bus->sih, ARMCR4_CORE_ID, 0))) {
  7002. bus->armrev = si_corerev(bus->sih);
  7003. } else {
  7004. DHD_ERROR(("%s: failed to find ARM core!\n", __FUNCTION__));
  7005. goto fail;
  7006. }
  7007. if (!si_setcore(bus->sih, ARMCR4_CORE_ID, 0)) {
  7008. if (!(bus->orig_ramsize = si_socram_size(bus->sih))) {
  7009. DHD_ERROR(("%s: failed to find SOCRAM memory!\n", __FUNCTION__));
  7010. goto fail;
  7011. }
  7012. } else {
  7013. /* cr4 has a different way to find the RAM size from TCM's */
  7014. if (!(bus->orig_ramsize = si_tcm_size(bus->sih))) {
  7015. DHD_ERROR(("%s: failed to find CR4-TCM memory!\n", __FUNCTION__));
  7016. goto fail;
  7017. }
  7018. /* also populate base address */
  7019. switch ((uint16)bus->sih->chip) {
  7020. case BCM4335_CHIP_ID:
  7021. case BCM4339_CHIP_ID:
  7022. bus->dongle_ram_base = CR4_4335_RAM_BASE;
  7023. break;
  7024. case BCM4350_CHIP_ID:
  7025. case BCM4354_CHIP_ID:
  7026. case BCM4358_CHIP_ID:
  7027. bus->dongle_ram_base = CR4_4350_RAM_BASE;
  7028. break;
  7029. case BCM4360_CHIP_ID:
  7030. bus->dongle_ram_base = CR4_4360_RAM_BASE;
  7031. break;
  7032. CASE_BCM4345_CHIP:
  7033. bus->dongle_ram_base = (bus->sih->chiprev < 6) /* from 4345C0 */
  7034. ? CR4_4345_LT_C0_RAM_BASE : CR4_4345_GE_C0_RAM_BASE;
  7035. break;
  7036. case BCM4349_CHIP_GRPID:
  7037. /* RAM based changed from 4349c0(revid=9) onwards */
  7038. bus->dongle_ram_base = ((bus->sih->chiprev < 9) ?
  7039. CR4_4349_RAM_BASE: CR4_4349_RAM_BASE_FROM_REV_9);
  7040. break;
  7041. case BCM4373_CHIP_ID:
  7042. bus->dongle_ram_base = CR4_4373_RAM_BASE;
  7043. /* Updating F2 Block size to 256 for 4373 to fix TX Transmit
  7044. * Underflow issue during Bi-Directional Traffic
  7045. */
  7046. {
  7047. uint fn = 2;
  7048. fn = fn << SDIO_FUNC_BLOCK_SIZE_SHIFT | F2_BLOCK_SIZE_256;
  7049. if (bcmsdh_iovar_op(sdh, "sd_blocksize",
  7050. NULL, 0, &fn, sizeof(fn), TRUE) != BCME_OK) {
  7051. DHD_ERROR(("%s: Set F2 Block size error\n",
  7052. __FUNCTION__));
  7053. goto fail;
  7054. }
  7055. }
  7056. break;
  7057. case BCM4364_CHIP_ID:
  7058. bus->dongle_ram_base = CR4_4364_RAM_BASE;
  7059. break;
  7060. case BCM4362_CHIP_ID:
  7061. bus->dongle_ram_base = CR4_4362_RAM_BASE;
  7062. break;
  7063. case BCM43751_CHIP_ID:
  7064. bus->dongle_ram_base = CR4_43751_RAM_BASE;
  7065. break;
  7066. case BCM4369_CHIP_ID:
  7067. bus->dongle_ram_base = CR4_4369_RAM_BASE;
  7068. break;
  7069. #ifdef CHIPS_CUSTOMER_HW6
  7070. case BCM4378_CHIP_GRPID:
  7071. bus->dongle_ram_base = CR4_4378_RAM_BASE;
  7072. break;
  7073. #endif /* CHIPS_CUSTOMER_HW6 */
  7074. default:
  7075. bus->dongle_ram_base = 0;
  7076. DHD_ERROR(("%s: WARNING: Using default ram base at 0x%x\n",
  7077. __FUNCTION__, bus->dongle_ram_base));
  7078. }
  7079. }
  7080. bus->ramsize = bus->orig_ramsize;
  7081. if (dhd_dongle_ramsize)
  7082. dhd_dongle_setramsize(bus, dhd_dongle_ramsize);
  7083. DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d) at 0x%x\n",
  7084. bus->ramsize, bus->orig_ramsize, bus->dongle_ram_base));
  7085. bus->srmemsize = si_socram_srmem_size(bus->sih);
  7086. }
  7087. /* ...but normally deal with the SDPCMDEV core */
  7088. #ifdef BCMSDIOLITE
  7089. if (!(bus->regs = si_setcore(bus->sih, CC_CORE_ID, 0))) {
  7090. DHD_ERROR(("%s: failed to find Chip Common core!\n", __FUNCTION__));
  7091. goto fail;
  7092. }
  7093. #else
  7094. if (!(bus->regs = si_setcore(bus->sih, PCMCIA_CORE_ID, 0)) &&
  7095. !(bus->regs = si_setcore(bus->sih, SDIOD_CORE_ID, 0))) {
  7096. DHD_ERROR(("%s: failed to find SDIODEV core!\n", __FUNCTION__));
  7097. goto fail;
  7098. }
  7099. #endif // endif
  7100. bus->sdpcmrev = si_corerev(bus->sih);
  7101. /* Set core control so an SDIO reset does a backplane reset */
  7102. OR_REG(osh, &bus->regs->corecontrol, CC_BPRESEN);
  7103. #ifndef BCMSPI
  7104. bus->rxint_mode = SDIO_DEVICE_HMB_RXINT;
  7105. if ((bus->sih->buscoretype == SDIOD_CORE_ID) && (bus->sdpcmrev >= 4) &&
  7106. (bus->rxint_mode == SDIO_DEVICE_RXDATAINT_MODE_1))
  7107. {
  7108. uint32 val;
  7109. val = R_REG(osh, &bus->regs->corecontrol);
  7110. val &= ~CC_XMTDATAAVAIL_MODE;
  7111. val |= CC_XMTDATAAVAIL_CTRL;
  7112. W_REG(osh, &bus->regs->corecontrol, val);
  7113. }
  7114. #endif /* BCMSPI */
  7115. pktq_init(&bus->txq, (PRIOMASK + 1), QLEN);
  7116. /* Locate an appropriately-aligned portion of hdrbuf */
  7117. bus->rxhdr = (uint8 *)ROUNDUP((uintptr)&bus->hdrbuf[0], DHD_SDALIGN);
  7118. /* Set the poll and/or interrupt flags */
  7119. bus->intr = (bool)dhd_intr;
  7120. if ((bus->poll = (bool)dhd_poll))
  7121. bus->pollrate = 1;
  7122. /* Setting default Glom size */
  7123. bus->txglomsize = SDPCM_DEFGLOM_SIZE;
  7124. return TRUE;
  7125. fail:
  7126. if (bus->sih != NULL) {
  7127. si_detach(bus->sih);
  7128. bus->sih = NULL;
  7129. }
  7130. return FALSE;
  7131. }
  7132. static bool
  7133. dhdsdio_probe_malloc(dhd_bus_t *bus, osl_t *osh, void *sdh)
  7134. {
  7135. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  7136. if (bus->dhd->maxctl) {
  7137. bus->rxblen = ROUNDUP((bus->dhd->maxctl+SDPCM_HDRLEN), ALIGNMENT) + DHD_SDALIGN;
  7138. if (!(bus->rxbuf = DHD_OS_PREALLOC(bus->dhd, DHD_PREALLOC_RXBUF, bus->rxblen))) {
  7139. DHD_ERROR(("%s: MALLOC of %d-byte rxbuf failed\n",
  7140. __FUNCTION__, bus->rxblen));
  7141. goto fail;
  7142. }
  7143. }
  7144. /* Allocate buffer to receive glomed packet */
  7145. if (!(bus->databuf = DHD_OS_PREALLOC(bus->dhd, DHD_PREALLOC_DATABUF, MAX_DATA_BUF))) {
  7146. DHD_ERROR(("%s: MALLOC of %d-byte databuf failed\n",
  7147. __FUNCTION__, MAX_DATA_BUF));
  7148. /* release rxbuf which was already located as above */
  7149. if (!bus->rxblen)
  7150. DHD_OS_PREFREE(bus->dhd, bus->rxbuf, bus->rxblen);
  7151. goto fail;
  7152. }
  7153. /* Align the buffer */
  7154. if ((uintptr)bus->databuf % DHD_SDALIGN)
  7155. bus->dataptr = bus->databuf + (DHD_SDALIGN - ((uintptr)bus->databuf % DHD_SDALIGN));
  7156. else
  7157. bus->dataptr = bus->databuf;
  7158. return TRUE;
  7159. fail:
  7160. return FALSE;
  7161. }
  7162. static bool
  7163. dhdsdio_probe_init(dhd_bus_t *bus, osl_t *osh, void *sdh)
  7164. {
  7165. int32 fnum;
  7166. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  7167. bus->_srenab = FALSE;
  7168. #ifdef SDTEST
  7169. dhdsdio_pktgen_init(bus);
  7170. #endif /* SDTEST */
  7171. #ifndef BCMSPI
  7172. /* Disable F2 to clear any intermediate frame state on the dongle */
  7173. bcmsdh_cfg_write(sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, SDIO_FUNC_ENABLE_1, NULL);
  7174. #endif /* !BCMSPI */
  7175. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  7176. bus->dhd->busstate = DHD_BUS_DOWN;
  7177. bus->sleeping = FALSE;
  7178. bus->rxflow = FALSE;
  7179. bus->prev_rxlim_hit = 0;
  7180. #ifndef BCMSPI
  7181. /* Done with backplane-dependent accesses, can drop clock... */
  7182. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
  7183. #endif /* !BCMSPI */
  7184. /* ...and initialize clock/power states */
  7185. bus->clkstate = CLK_SDONLY;
  7186. bus->idletime = (int32)dhd_idletime;
  7187. bus->idleclock = DHD_IDLE_ACTIVE;
  7188. /* Query the SD clock speed */
  7189. if (bcmsdh_iovar_op(sdh, "sd_divisor", NULL, 0,
  7190. &bus->sd_divisor, sizeof(int32), FALSE) != BCME_OK) {
  7191. DHD_ERROR(("%s: fail on %s get\n", __FUNCTION__, "sd_divisor"));
  7192. bus->sd_divisor = -1;
  7193. } else {
  7194. DHD_INFO(("%s: Initial value for %s is %d\n",
  7195. __FUNCTION__, "sd_divisor", bus->sd_divisor));
  7196. }
  7197. /* Query the SD bus mode */
  7198. if (bcmsdh_iovar_op(sdh, "sd_mode", NULL, 0,
  7199. &bus->sd_mode, sizeof(int32), FALSE) != BCME_OK) {
  7200. DHD_ERROR(("%s: fail on %s get\n", __FUNCTION__, "sd_mode"));
  7201. bus->sd_mode = -1;
  7202. } else {
  7203. DHD_INFO(("%s: Initial value for %s is %d\n",
  7204. __FUNCTION__, "sd_mode", bus->sd_mode));
  7205. }
  7206. /* Query the F2 block size, set roundup accordingly */
  7207. fnum = 2;
  7208. if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(int32),
  7209. &bus->blocksize, sizeof(int32), FALSE) != BCME_OK) {
  7210. bus->blocksize = 0;
  7211. DHD_ERROR(("%s: fail on fn %d %s get\n", __FUNCTION__, fnum, "sd_blocksize"));
  7212. } else {
  7213. DHD_INFO(("%s: Initial value for fn %d %s is %d\n",
  7214. __FUNCTION__, fnum, "sd_blocksize", bus->blocksize));
  7215. dhdsdio_tune_fifoparam(bus);
  7216. }
  7217. bus->roundup = MIN(max_roundup, bus->blocksize);
  7218. #ifdef DHDENABLE_TAILPAD
  7219. if (bus->pad_pkt)
  7220. PKTFREE(osh, bus->pad_pkt, FALSE);
  7221. bus->pad_pkt = PKTGET(osh, SDIO_MAX_BLOCK_SIZE, FALSE);
  7222. if (bus->pad_pkt == NULL)
  7223. DHD_ERROR(("failed to allocate padding packet\n"));
  7224. else {
  7225. int alignment_offset = 0;
  7226. uintptr pktprt = (uintptr)PKTDATA(osh, bus->pad_pkt);
  7227. if (!(pktprt&1) && (pktprt = (pktprt % DHD_SDALIGN)))
  7228. PKTPUSH(osh, bus->pad_pkt, alignment_offset);
  7229. PKTSETNEXT(osh, bus->pad_pkt, NULL);
  7230. }
  7231. #endif /* DHDENABLE_TAILPAD */
  7232. /* Query if bus module supports packet chaining, default to use if supported */
  7233. if (bcmsdh_iovar_op(sdh, "sd_rxchain", NULL, 0,
  7234. &bus->sd_rxchain, sizeof(int32), FALSE) != BCME_OK) {
  7235. bus->sd_rxchain = FALSE;
  7236. } else {
  7237. DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
  7238. __FUNCTION__, (bus->sd_rxchain ? "supports" : "does not support")));
  7239. }
  7240. bus->use_rxchain = (bool)bus->sd_rxchain;
  7241. bus->txinrx_thres = CUSTOM_TXINRX_THRES;
  7242. /* TX first in dhdsdio_readframes() */
  7243. bus->dotxinrx = TRUE;
  7244. return TRUE;
  7245. }
  7246. int
  7247. dhd_bus_download_firmware(struct dhd_bus *bus, osl_t *osh,
  7248. char *pfw_path, char *pnv_path)
  7249. {
  7250. int ret;
  7251. bus->fw_path = pfw_path;
  7252. bus->nv_path = pnv_path;
  7253. ret = dhdsdio_download_firmware(bus, osh, bus->sdh);
  7254. return ret;
  7255. }
  7256. static int
  7257. dhdsdio_download_firmware(struct dhd_bus *bus, osl_t *osh, void *sdh)
  7258. {
  7259. int ret;
  7260. #if defined(SUPPORT_MULTIPLE_REVISION)
  7261. if (concate_revision(bus, bus->fw_path, bus->nv_path) != 0) {
  7262. DHD_ERROR(("%s: fail to concatnate revison \n",
  7263. __FUNCTION__));
  7264. return BCME_BADARG;
  7265. }
  7266. #endif /* SUPPORT_MULTIPLE_REVISION */
  7267. #if defined(DHD_BLOB_EXISTENCE_CHECK)
  7268. dhd_set_blob_support(bus->dhd, bus->fw_path);
  7269. #endif /* DHD_BLOB_EXISTENCE_CHECK */
  7270. DHD_TRACE_HW4(("%s: firmware path=%s, nvram path=%s\n",
  7271. __FUNCTION__, bus->fw_path, bus->nv_path));
  7272. DHD_OS_WAKE_LOCK(bus->dhd);
  7273. /* Download the firmware */
  7274. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  7275. ret = _dhdsdio_download_firmware(bus);
  7276. dhdsdio_clkctl(bus, CLK_SDONLY, FALSE);
  7277. DHD_OS_WAKE_UNLOCK(bus->dhd);
  7278. return ret;
  7279. }
  7280. /* Detach and free everything */
  7281. static void
  7282. dhdsdio_release(dhd_bus_t *bus, osl_t *osh)
  7283. {
  7284. bool dongle_isolation = FALSE;
  7285. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  7286. if (bus) {
  7287. ASSERT(osh);
  7288. if (bus->dhd) {
  7289. #if defined(DEBUGGER) || defined(DHD_DSCOPE)
  7290. debugger_close();
  7291. #endif /* DEBUGGER || DHD_DSCOPE */
  7292. dongle_isolation = bus->dhd->dongle_isolation;
  7293. dhd_detach(bus->dhd);
  7294. }
  7295. /* De-register interrupt handler */
  7296. bcmsdh_intr_disable(bus->sdh);
  7297. bcmsdh_intr_dereg(bus->sdh);
  7298. if (bus->dhd) {
  7299. dhdsdio_release_dongle(bus, osh, dongle_isolation, TRUE);
  7300. dhd_free(bus->dhd);
  7301. bus->dhd = NULL;
  7302. }
  7303. dhdsdio_release_malloc(bus, osh);
  7304. #ifdef DHD_DEBUG
  7305. if (bus->console.buf != NULL)
  7306. MFREE(osh, bus->console.buf, bus->console.bufsize);
  7307. #endif // endif
  7308. #ifdef DHDENABLE_TAILPAD
  7309. if (bus->pad_pkt)
  7310. PKTFREE(osh, bus->pad_pkt, FALSE);
  7311. #endif /* DHDENABLE_TAILPAD */
  7312. MFREE(osh, bus, sizeof(dhd_bus_t));
  7313. }
  7314. DHD_TRACE(("%s: Disconnected\n", __FUNCTION__));
  7315. }
  7316. static void
  7317. dhdsdio_release_malloc(dhd_bus_t *bus, osl_t *osh)
  7318. {
  7319. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  7320. if (bus->dhd && bus->dhd->dongle_reset)
  7321. return;
  7322. if (bus->rxbuf) {
  7323. #ifndef CONFIG_DHD_USE_STATIC_BUF
  7324. MFREE(osh, bus->rxbuf, bus->rxblen);
  7325. #endif // endif
  7326. bus->rxctl = bus->rxbuf = NULL;
  7327. bus->rxlen = 0;
  7328. }
  7329. if (bus->databuf) {
  7330. #ifndef CONFIG_DHD_USE_STATIC_BUF
  7331. MFREE(osh, bus->databuf, MAX_DATA_BUF);
  7332. #endif // endif
  7333. bus->databuf = NULL;
  7334. }
  7335. if (bus->vars && bus->varsz) {
  7336. MFREE(osh, bus->vars, bus->varsz);
  7337. bus->vars = NULL;
  7338. }
  7339. }
  7340. static void
  7341. dhdsdio_release_dongle(dhd_bus_t *bus, osl_t *osh, bool dongle_isolation, bool reset_flag)
  7342. {
  7343. DHD_TRACE(("%s: Enter bus->dhd %p bus->dhd->dongle_reset %d \n", __FUNCTION__,
  7344. bus->dhd, bus->dhd->dongle_reset));
  7345. if ((bus->dhd && bus->dhd->dongle_reset) && reset_flag)
  7346. return;
  7347. if (bus->sih) {
  7348. /* In Win10, system will be BSOD if using "sysprep" to do OS image */
  7349. /* Skip this will not cause the BSOD. */
  7350. #if !defined(BCMLXSDMMC)
  7351. if (bus->dhd) {
  7352. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  7353. }
  7354. if (KSO_ENAB(bus) && (dongle_isolation == FALSE))
  7355. si_watchdog(bus->sih, 4);
  7356. #endif /* !defined(BCMLXSDMMC) */
  7357. if (bus->dhd) {
  7358. dhdsdio_clkctl(bus, CLK_NONE, FALSE);
  7359. }
  7360. si_detach(bus->sih);
  7361. bus->sih = NULL;
  7362. if (bus->vars && bus->varsz)
  7363. MFREE(osh, bus->vars, bus->varsz);
  7364. bus->vars = NULL;
  7365. }
  7366. DHD_TRACE(("%s: Disconnected\n", __FUNCTION__));
  7367. }
  7368. static void
  7369. dhdsdio_disconnect(void *ptr)
  7370. {
  7371. dhd_bus_t *bus = (dhd_bus_t *)ptr;
  7372. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  7373. #if defined(MULTIPLE_SUPPLICANT)
  7374. if (mutex_is_locked(&_dhd_sdio_mutex_lock_) == 0) {
  7375. DHD_ERROR(("%s : no mutex held. set lock\n", __FUNCTION__));
  7376. } else {
  7377. DHD_ERROR(("%s : mutex is locked!. wait for unlocking\n", __FUNCTION__));
  7378. }
  7379. mutex_lock(&_dhd_sdio_mutex_lock_);
  7380. #endif // endif
  7381. if (bus) {
  7382. ASSERT(bus->dhd);
  7383. /* Advertise bus cleanup during rmmod */
  7384. dhdsdio_advertise_bus_cleanup(bus->dhd);
  7385. dhdsdio_release(bus, bus->dhd->osh);
  7386. }
  7387. #if defined(MULTIPLE_SUPPLICANT)
  7388. mutex_unlock(&_dhd_sdio_mutex_lock_);
  7389. DHD_ERROR(("%s : the lock is released.\n", __FUNCTION__));
  7390. #endif /* LINUX */
  7391. DHD_TRACE(("%s: Disconnected\n", __FUNCTION__));
  7392. }
  7393. static int
  7394. dhdsdio_suspend(void *context)
  7395. {
  7396. int ret = 0;
  7397. #ifdef SUPPORT_P2P_GO_PS
  7398. int wait_time = 0;
  7399. #endif /* SUPPORT_P2P_GO_PS */
  7400. dhd_bus_t *bus = (dhd_bus_t*)context;
  7401. unsigned long flags;
  7402. DHD_ERROR(("%s Enter\n", __FUNCTION__));
  7403. if (bus->dhd == NULL) {
  7404. DHD_ERROR(("bus not inited\n"));
  7405. return BCME_ERROR;
  7406. }
  7407. if (bus->dhd->prot == NULL) {
  7408. DHD_ERROR(("prot is not inited\n"));
  7409. return BCME_ERROR;
  7410. }
  7411. if (bus->dhd->up == FALSE) {
  7412. return BCME_OK;
  7413. }
  7414. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  7415. if (bus->dhd->busstate != DHD_BUS_DATA && bus->dhd->busstate != DHD_BUS_SUSPEND) {
  7416. DHD_ERROR(("not in a readystate to LPBK is not inited\n"));
  7417. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  7418. return BCME_ERROR;
  7419. }
  7420. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  7421. if (bus->dhd->dongle_reset) {
  7422. DHD_ERROR(("Dongle is in reset state.\n"));
  7423. return -EIO;
  7424. }
  7425. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  7426. /* stop all interface network queue. */
  7427. dhd_txflowcontrol(bus->dhd, ALL_INTERFACES, ON);
  7428. bus->dhd->busstate = DHD_BUS_SUSPEND;
  7429. if (DHD_BUS_BUSY_CHECK_IN_TX(bus->dhd)) {
  7430. DHD_ERROR(("Tx Request is not ended\n"));
  7431. bus->dhd->busstate = DHD_BUS_DATA;
  7432. /* resume all interface network queue. */
  7433. dhd_txflowcontrol(bus->dhd, ALL_INTERFACES, OFF);
  7434. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  7435. return -EBUSY;
  7436. }
  7437. DHD_BUS_BUSY_SET_SUSPEND_IN_PROGRESS(bus->dhd);
  7438. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  7439. #ifdef SUPPORT_P2P_GO_PS
  7440. if (bus->idletime > 0) {
  7441. wait_time = msecs_to_jiffies(bus->idletime * dhd_watchdog_ms);
  7442. }
  7443. #endif /* SUPPORT_P2P_GO_PS */
  7444. ret = dhd_os_check_wakelock(bus->dhd);
  7445. #ifdef SUPPORT_P2P_GO_PS
  7446. if ((!ret) && (bus->dhd->up) && (bus->dhd->op_mode != DHD_FLAG_HOSTAP_MODE)) {
  7447. if (wait_event_timeout(bus->bus_sleep, bus->sleeping, wait_time) == 0) {
  7448. if (!bus->sleeping) {
  7449. ret = 1;
  7450. }
  7451. }
  7452. }
  7453. #endif /* SUPPORT_P2P_GO_PS */
  7454. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  7455. if (ret) {
  7456. bus->dhd->busstate = DHD_BUS_DATA;
  7457. /* resume all interface network queue. */
  7458. dhd_txflowcontrol(bus->dhd, ALL_INTERFACES, OFF);
  7459. }
  7460. DHD_BUS_BUSY_CLEAR_SUSPEND_IN_PROGRESS(bus->dhd);
  7461. dhd_os_busbusy_wake(bus->dhd);
  7462. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  7463. return ret;
  7464. }
  7465. static int
  7466. dhdsdio_resume(void *context)
  7467. {
  7468. dhd_bus_t *bus = (dhd_bus_t*)context;
  7469. ulong flags;
  7470. DHD_ERROR(("%s Enter\n", __FUNCTION__));
  7471. if (bus->dhd->up == FALSE) {
  7472. return BCME_OK;
  7473. }
  7474. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  7475. DHD_BUS_BUSY_SET_RESUME_IN_PROGRESS(bus->dhd);
  7476. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  7477. #if defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID)
  7478. if (dhd_os_check_if_up(bus->dhd))
  7479. bcmsdh_oob_intr_set(bus->sdh, TRUE);
  7480. #endif /* defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID) */
  7481. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  7482. DHD_BUS_BUSY_CLEAR_RESUME_IN_PROGRESS(bus->dhd);
  7483. bus->dhd->busstate = DHD_BUS_DATA;
  7484. dhd_os_busbusy_wake(bus->dhd);
  7485. /* resume all interface network queue. */
  7486. dhd_txflowcontrol(bus->dhd, ALL_INTERFACES, OFF);
  7487. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  7488. return 0;
  7489. }
  7490. /* Register/Unregister functions are called by the main DHD entry
  7491. * point (e.g. module insertion) to link with the bus driver, in
  7492. * order to look for or await the device.
  7493. */
  7494. static bcmsdh_driver_t dhd_sdio = {
  7495. dhdsdio_probe,
  7496. dhdsdio_disconnect,
  7497. dhdsdio_suspend,
  7498. dhdsdio_resume
  7499. };
  7500. int
  7501. dhd_bus_register(void)
  7502. {
  7503. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  7504. return bcmsdh_register(&dhd_sdio);
  7505. }
  7506. void
  7507. dhd_bus_unregister(void)
  7508. {
  7509. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  7510. bcmsdh_unregister();
  7511. }
  7512. #if defined(BCMLXSDMMC)
  7513. /* Register a dummy SDIO client driver in order to be notified of new SDIO device */
  7514. int dhd_bus_reg_sdio_notify(void* semaphore)
  7515. {
  7516. return bcmsdh_reg_sdio_notify(semaphore);
  7517. }
  7518. void dhd_bus_unreg_sdio_notify(void)
  7519. {
  7520. bcmsdh_unreg_sdio_notify();
  7521. }
  7522. #endif /* defined(BCMLXSDMMC) */
  7523. static int
  7524. dhdsdio_download_code_file(struct dhd_bus *bus, char *pfw_path)
  7525. {
  7526. int bcmerror = -1;
  7527. int offset = 0;
  7528. int len;
  7529. void *image = NULL;
  7530. uint8 *memblock = NULL, *memptr;
  7531. uint memblock_size = MEMBLOCK;
  7532. #ifdef DHD_DEBUG_DOWNLOADTIME
  7533. unsigned long initial_jiffies = 0;
  7534. uint firmware_sz = 0;
  7535. #endif // endif
  7536. DHD_INFO(("%s: download firmware %s\n", __FUNCTION__, pfw_path));
  7537. image = dhd_os_open_image1(bus->dhd, pfw_path);
  7538. if (image == NULL) {
  7539. DHD_ERROR(("%s: Failed to open fw file !\n", __FUNCTION__));
  7540. goto err;
  7541. }
  7542. /* Update the dongle image download block size depending on the F1 block size */
  7543. if (sd_f1_blocksize == 512)
  7544. memblock_size = MAX_MEMBLOCK;
  7545. memptr = memblock = MALLOC(bus->dhd->osh, memblock_size + DHD_SDALIGN);
  7546. if (memblock == NULL) {
  7547. DHD_ERROR(("%s: Failed to allocate memory %d bytes\n", __FUNCTION__,
  7548. memblock_size));
  7549. goto err;
  7550. }
  7551. if ((uint32)(uintptr)memblock % DHD_SDALIGN)
  7552. memptr += (DHD_SDALIGN - ((uint32)(uintptr)memblock % DHD_SDALIGN));
  7553. #ifdef DHD_DEBUG_DOWNLOADTIME
  7554. initial_jiffies = jiffies;
  7555. #endif // endif
  7556. /* Download image */
  7557. while ((len = dhd_os_get_image_block((char*)memptr, memblock_size, image))) {
  7558. if (len < 0) {
  7559. DHD_ERROR(("%s: dhd_os_get_image_block failed (%d)\n", __FUNCTION__, len));
  7560. bcmerror = BCME_ERROR;
  7561. goto err;
  7562. }
  7563. /* check if CR4 */
  7564. if (si_setcore(bus->sih, ARMCR4_CORE_ID, 0)) {
  7565. /* if address is 0, store the reset instruction to be written in 0 */
  7566. if (offset == 0) {
  7567. bus->resetinstr = *(((uint32*)memptr));
  7568. /* Add start of RAM address to the address given by user */
  7569. offset += bus->dongle_ram_base;
  7570. }
  7571. }
  7572. bcmerror = dhdsdio_membytes(bus, TRUE, offset, memptr, len);
  7573. if (bcmerror) {
  7574. DHD_ERROR(("%s: error %d on writing %d membytes at 0x%08x\n",
  7575. __FUNCTION__, bcmerror, memblock_size, offset));
  7576. goto err;
  7577. }
  7578. offset += memblock_size;
  7579. #ifdef DHD_DEBUG_DOWNLOADTIME
  7580. firmware_sz += len;
  7581. #endif // endif
  7582. }
  7583. #ifdef DHD_DEBUG_DOWNLOADTIME
  7584. DHD_ERROR(("Firmware download time for %u bytes: %u ms\n",
  7585. firmware_sz, jiffies_to_msecs(jiffies - initial_jiffies)));
  7586. #endif // endif
  7587. err:
  7588. if (memblock)
  7589. MFREE(bus->dhd->osh, memblock, memblock_size + DHD_SDALIGN);
  7590. if (image)
  7591. dhd_os_close_image1(bus->dhd, image);
  7592. return bcmerror;
  7593. }
  7594. #ifdef DHD_UCODE_DOWNLOAD
  7595. /* Currently supported only for the chips in which ucode RAM is AXI addressable */
  7596. static uint32
  7597. dhdsdio_ucode_base(struct dhd_bus *bus)
  7598. {
  7599. uint32 ucode_base = 0;
  7600. switch ((uint16)bus->sih->chip) {
  7601. case BCM43012_CHIP_ID:
  7602. ucode_base = 0xE8020000;
  7603. break;
  7604. default:
  7605. DHD_ERROR(("%s: Unsupported!\n", __func__));
  7606. break;
  7607. }
  7608. return ucode_base;
  7609. }
  7610. static int
  7611. dhdsdio_download_ucode_file(struct dhd_bus *bus, char *ucode_path)
  7612. {
  7613. int bcmerror = -1;
  7614. int offset = 0;
  7615. int len;
  7616. uint32 ucode_base;
  7617. void *image = NULL;
  7618. uint8 *memblock = NULL, *memptr;
  7619. uint memblock_size = MEMBLOCK;
  7620. #ifdef DHD_DEBUG_DOWNLOADTIME
  7621. unsigned long initial_jiffies = 0;
  7622. uint firmware_sz = 0;
  7623. #endif // endif
  7624. DHD_INFO(("%s: download firmware %s\n", __FUNCTION__, ucode_path));
  7625. ucode_base = dhdsdio_ucode_base(bus);
  7626. image = dhd_os_open_image1(bus->dhd, ucode_path);
  7627. if (image == NULL)
  7628. goto err;
  7629. /* Update the dongle image download block size depending on the F1 block size */
  7630. if (sd_f1_blocksize == 512)
  7631. memblock_size = MAX_MEMBLOCK;
  7632. memptr = memblock = MALLOC(bus->dhd->osh, memblock_size + DHD_SDALIGN);
  7633. if (memblock == NULL) {
  7634. DHD_ERROR(("%s: Failed to allocate memory %d bytes\n", __FUNCTION__,
  7635. memblock_size));
  7636. goto err;
  7637. }
  7638. if ((uint32)(uintptr)memblock % DHD_SDALIGN)
  7639. memptr += (DHD_SDALIGN - ((uint32)(uintptr)memblock % DHD_SDALIGN));
  7640. #ifdef DHD_DEBUG_DOWNLOADTIME
  7641. initial_jiffies = jiffies;
  7642. #endif // endif
  7643. /* Download image */
  7644. while ((len = dhd_os_get_image_block((char*)memptr, memblock_size, image))) {
  7645. if (len < 0) {
  7646. DHD_ERROR(("%s: dhd_os_get_image_block failed (%d)\n", __FUNCTION__, len));
  7647. bcmerror = BCME_ERROR;
  7648. goto err;
  7649. }
  7650. bcmerror = dhdsdio_membytes(bus, TRUE, (ucode_base + offset), memptr, len);
  7651. if (bcmerror) {
  7652. DHD_ERROR(("%s: error %d on writing %d membytes at 0x%08x\n",
  7653. __FUNCTION__, bcmerror, memblock_size, offset));
  7654. goto err;
  7655. }
  7656. offset += memblock_size;
  7657. #ifdef DHD_DEBUG_DOWNLOADTIME
  7658. firmware_sz += len;
  7659. #endif // endif
  7660. }
  7661. #ifdef DHD_DEBUG_DOWNLOADTIME
  7662. DHD_ERROR(("ucode download time for %u bytes: %u ms\n",
  7663. firmware_sz, jiffies_to_msecs(jiffies - initial_jiffies)));
  7664. #endif // endif
  7665. err:
  7666. if (memblock)
  7667. MFREE(bus->dhd->osh, memblock, memblock_size + DHD_SDALIGN);
  7668. if (image)
  7669. dhd_os_close_image1(bus->dhd, image);
  7670. return bcmerror;
  7671. } /* dhdsdio_download_ucode_file */
  7672. void
  7673. dhd_bus_ucode_download(struct dhd_bus *bus)
  7674. {
  7675. uint32 shaddr = 0, shdata = 0;
  7676. shaddr = bus->dongle_ram_base + bus->ramsize - 4;
  7677. dhdsdio_membytes(bus, FALSE, shaddr, (uint8 *)&shdata, 4);
  7678. DHD_TRACE(("%s: shdata:[0x%08x :0x%08x]\n", __func__, shaddr, shdata));
  7679. if (shdata == UCODE_DOWNLOAD_REQUEST)
  7680. {
  7681. DHD_ERROR(("%s: Received ucode download request!\n", __func__));
  7682. /* Download the ucode */
  7683. if (!dhd_get_ucode_path(bus->dhd)) {
  7684. DHD_ERROR(("%s: bus->uc_path not set!\n", __func__));
  7685. return;
  7686. }
  7687. dhdsdio_download_ucode_file(bus, dhd_get_ucode_path(bus->dhd));
  7688. DHD_ERROR(("%s: Ucode downloaded successfully!\n", __func__));
  7689. shdata = UCODE_DOWNLOAD_COMPLETE;
  7690. dhdsdio_membytes(bus, TRUE, shaddr, (uint8 *)&shdata, 4);
  7691. }
  7692. }
  7693. #endif /* DHD_UCODE_DOWNLOAD */
  7694. static int
  7695. dhdsdio_download_nvram(struct dhd_bus *bus)
  7696. {
  7697. int bcmerror = -1;
  7698. uint len;
  7699. void * image = NULL;
  7700. char * memblock = NULL;
  7701. char *bufp;
  7702. char *pnv_path;
  7703. bool nvram_file_exists;
  7704. pnv_path = bus->nv_path;
  7705. nvram_file_exists = ((pnv_path != NULL) && (pnv_path[0] != '\0'));
  7706. /* For Get nvram from UEFI */
  7707. if (nvram_file_exists)
  7708. image = dhd_os_open_image1(bus->dhd, pnv_path);
  7709. memblock = MALLOC(bus->dhd->osh, MAX_NVRAMBUF_SIZE);
  7710. if (memblock == NULL) {
  7711. DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
  7712. __FUNCTION__, MAX_NVRAMBUF_SIZE));
  7713. goto err;
  7714. }
  7715. /* For Get nvram from image or UEFI (when image == NULL ) */
  7716. len = dhd_os_get_image_block(memblock, MAX_NVRAMBUF_SIZE, image);
  7717. if (len > 0 && len < MAX_NVRAMBUF_SIZE) {
  7718. bufp = (char *)memblock;
  7719. bufp[len] = 0;
  7720. len = process_nvram_vars(bufp, len);
  7721. if (len % 4) {
  7722. len += 4 - (len % 4);
  7723. }
  7724. bufp += len;
  7725. *bufp++ = 0;
  7726. if (len)
  7727. bcmerror = dhdsdio_downloadvars(bus, memblock, len + 1);
  7728. if (bcmerror) {
  7729. DHD_ERROR(("%s: error downloading vars: %d\n",
  7730. __FUNCTION__, bcmerror));
  7731. }
  7732. } else {
  7733. DHD_ERROR(("%s: error reading nvram file: %d\n",
  7734. __FUNCTION__, len));
  7735. bcmerror = BCME_SDIO_ERROR;
  7736. }
  7737. err:
  7738. if (memblock)
  7739. MFREE(bus->dhd->osh, memblock, MAX_NVRAMBUF_SIZE);
  7740. if (image)
  7741. dhd_os_close_image1(bus->dhd, image);
  7742. return bcmerror;
  7743. }
  7744. static int
  7745. _dhdsdio_download_firmware(struct dhd_bus *bus)
  7746. {
  7747. int bcmerror = -1;
  7748. bool embed = FALSE; /* download embedded firmware */
  7749. bool dlok = FALSE; /* download firmware succeeded */
  7750. /* Out immediately if no image to download */
  7751. if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0')) {
  7752. return bcmerror;
  7753. }
  7754. /* Keep arm in reset */
  7755. if (dhdsdio_download_state(bus, TRUE)) {
  7756. DHD_ERROR(("%s: error placing ARM core in reset\n", __FUNCTION__));
  7757. goto err;
  7758. }
  7759. /* External image takes precedence if specified */
  7760. if ((bus->fw_path != NULL) && (bus->fw_path[0] != '\0')) {
  7761. if (dhdsdio_download_code_file(bus, bus->fw_path)) {
  7762. DHD_ERROR(("%s: dongle image file download failed\n", __FUNCTION__));
  7763. goto err;
  7764. } else {
  7765. embed = FALSE;
  7766. dlok = TRUE;
  7767. }
  7768. }
  7769. BCM_REFERENCE(embed);
  7770. if (!dlok) {
  7771. DHD_ERROR(("%s: dongle image download failed\n", __FUNCTION__));
  7772. goto err;
  7773. }
  7774. /* External nvram takes precedence if specified */
  7775. if (dhdsdio_download_nvram(bus)) {
  7776. DHD_ERROR(("%s: dongle nvram file download failed\n", __FUNCTION__));
  7777. goto err;
  7778. }
  7779. /* Take arm out of reset */
  7780. if (dhdsdio_download_state(bus, FALSE)) {
  7781. DHD_ERROR(("%s: error getting out of ARM core reset\n", __FUNCTION__));
  7782. goto err;
  7783. }
  7784. bcmerror = 0;
  7785. err:
  7786. return bcmerror;
  7787. }
  7788. static int
  7789. dhd_bcmsdh_recv_buf(dhd_bus_t *bus, uint32 addr, uint fn, uint flags, uint8 *buf, uint nbytes,
  7790. void *pkt, bcmsdh_cmplt_fn_t complete_fn, void *handle)
  7791. {
  7792. int status;
  7793. if (!KSO_ENAB(bus)) {
  7794. DHD_ERROR(("%s: Device asleep\n", __FUNCTION__));
  7795. return BCME_NODEVICE;
  7796. }
  7797. status = bcmsdh_recv_buf(bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete_fn, handle);
  7798. return status;
  7799. }
  7800. static int
  7801. dhd_bcmsdh_send_buf(dhd_bus_t *bus, uint32 addr, uint fn, uint flags, uint8 *buf, uint nbytes,
  7802. void *pkt, bcmsdh_cmplt_fn_t complete_fn, void *handle, int max_retry)
  7803. {
  7804. int ret;
  7805. int i = 0;
  7806. int retries = 0;
  7807. bcmsdh_info_t *sdh;
  7808. if (!KSO_ENAB(bus)) {
  7809. DHD_ERROR(("%s: Device asleep\n", __FUNCTION__));
  7810. return BCME_NODEVICE;
  7811. }
  7812. sdh = bus->sdh;
  7813. do {
  7814. ret = bcmsdh_send_buf(bus->sdh, addr, fn, flags, buf, nbytes,
  7815. pkt, complete_fn, handle);
  7816. bus->f2txdata++;
  7817. ASSERT(ret != BCME_PENDING);
  7818. if (ret == BCME_NODEVICE) {
  7819. DHD_ERROR(("%s: Device asleep already\n", __FUNCTION__));
  7820. } else if (ret < 0) {
  7821. /* On failure, abort the command and terminate the frame */
  7822. DHD_ERROR(("%s: sdio error %d, abort command and terminate frame.\n",
  7823. __FUNCTION__, ret));
  7824. bus->tx_sderrs++;
  7825. bus->f1regdata++;
  7826. bus->dhd->tx_errors++;
  7827. bcmsdh_abort(sdh, SDIO_FUNC_2);
  7828. bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL,
  7829. SFC_WF_TERM, NULL);
  7830. for (i = 0; i < READ_FRM_CNT_RETRIES; i++) {
  7831. uint8 hi, lo;
  7832. hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_WFRAMEBCHI,
  7833. NULL);
  7834. lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_WFRAMEBCLO,
  7835. NULL);
  7836. bus->f1regdata += 2;
  7837. if ((hi == 0) && (lo == 0))
  7838. break;
  7839. }
  7840. }
  7841. } while ((ret < 0) && retrydata && ++retries < max_retry);
  7842. return ret;
  7843. }
  7844. uint8
  7845. dhd_bus_is_ioready(struct dhd_bus *bus)
  7846. {
  7847. uint8 enable;
  7848. bcmsdh_info_t *sdh;
  7849. ASSERT(bus);
  7850. ASSERT(bus->sih != NULL);
  7851. enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
  7852. sdh = bus->sdh;
  7853. return (enable == bcmsdh_cfg_read(sdh, SDIO_FUNC_0, SDIOD_CCCR_IORDY, NULL));
  7854. }
  7855. uint
  7856. dhd_bus_chip(struct dhd_bus *bus)
  7857. {
  7858. ASSERT(bus->sih != NULL);
  7859. return bus->sih->chip;
  7860. }
  7861. uint
  7862. dhd_bus_chiprev(struct dhd_bus *bus)
  7863. {
  7864. ASSERT(bus);
  7865. ASSERT(bus->sih != NULL);
  7866. return bus->sih->chiprev;
  7867. }
  7868. void *
  7869. dhd_bus_pub(struct dhd_bus *bus)
  7870. {
  7871. return bus->dhd;
  7872. }
  7873. void *
  7874. dhd_bus_sih(struct dhd_bus *bus)
  7875. {
  7876. return (void *)bus->sih;
  7877. }
  7878. void *
  7879. dhd_bus_txq(struct dhd_bus *bus)
  7880. {
  7881. return &bus->txq;
  7882. }
  7883. uint
  7884. dhd_bus_hdrlen(struct dhd_bus *bus)
  7885. {
  7886. return (bus->txglom_enable) ? SDPCM_HDRLEN_TXGLOM : SDPCM_HDRLEN;
  7887. }
  7888. void
  7889. dhd_bus_set_dotxinrx(struct dhd_bus *bus, bool val)
  7890. {
  7891. bus->dotxinrx = val;
  7892. }
  7893. /*
  7894. * dhdsdio_advertise_bus_cleanup advertises that clean up is under progress
  7895. * to other bus user contexts like Tx, Rx, IOVAR, WD etc and it waits for other contexts
  7896. * to gracefully exit. All the bus usage contexts before marking busstate as busy, will check for
  7897. * whether the busstate is DHD_BUS_DOWN or DHD_BUS_DOWN_IN_PROGRESS, if so
  7898. * they will exit from there itself without marking dhd_bus_busy_state as BUSY.
  7899. */
  7900. static void
  7901. dhdsdio_advertise_bus_cleanup(dhd_pub_t *dhdp)
  7902. {
  7903. unsigned long flags;
  7904. int timeleft;
  7905. DHD_LINUX_GENERAL_LOCK(dhdp, flags);
  7906. dhdp->busstate = DHD_BUS_DOWN_IN_PROGRESS;
  7907. DHD_LINUX_GENERAL_UNLOCK(dhdp, flags);
  7908. timeleft = dhd_os_busbusy_wait_negation(dhdp, &dhdp->dhd_bus_busy_state);
  7909. if ((timeleft == 0) || (timeleft == 1)) {
  7910. DHD_ERROR(("%s : Timeout due to dhd_bus_busy_state=0x%x\n",
  7911. __FUNCTION__, dhdp->dhd_bus_busy_state));
  7912. ASSERT(0);
  7913. }
  7914. return;
  7915. }
  7916. int
  7917. dhd_bus_devreset(dhd_pub_t *dhdp, uint8 flag)
  7918. {
  7919. int bcmerror = 0;
  7920. dhd_bus_t *bus;
  7921. unsigned long flags;
  7922. bus = dhdp->bus;
  7923. if (flag == TRUE) {
  7924. if (!bus->dhd->dongle_reset) {
  7925. DHD_ERROR(("%s: == Power OFF ==\n", __FUNCTION__));
  7926. dhdsdio_advertise_bus_cleanup(bus->dhd);
  7927. dhd_os_sdlock(dhdp);
  7928. dhd_os_wd_timer(dhdp, 0);
  7929. #if defined(OEM_ANDROID)
  7930. #if !defined(IGNORE_ETH0_DOWN)
  7931. /* Force flow control as protection when stop come before ifconfig_down */
  7932. dhd_txflowcontrol(bus->dhd, ALL_INTERFACES, ON);
  7933. #endif /* !defined(IGNORE_ETH0_DOWN) */
  7934. #endif /* OEM_ANDROID */
  7935. /* Expect app to have torn down any connection before calling */
  7936. /* Stop the bus, disable F2 */
  7937. dhd_bus_stop(bus, FALSE);
  7938. #if defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID)
  7939. /* Clean up any pending IRQ */
  7940. dhd_enable_oob_intr(bus, FALSE);
  7941. bcmsdh_oob_intr_set(bus->sdh, FALSE);
  7942. bcmsdh_oob_intr_unregister(bus->sdh);
  7943. #endif /* defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID) */
  7944. /* Clean tx/rx buffer pointers, detach from the dongle */
  7945. dhdsdio_release_dongle(bus, bus->dhd->osh, TRUE, TRUE);
  7946. bus->dhd->dongle_reset = TRUE;
  7947. DHD_ERROR(("%s: making dhdpub up FALSE\n", __FUNCTION__));
  7948. bus->dhd->up = FALSE;
  7949. dhd_txglom_enable(dhdp, FALSE);
  7950. dhd_os_sdunlock(dhdp);
  7951. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  7952. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  7953. bus->dhd->busstate = DHD_BUS_DOWN;
  7954. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  7955. DHD_TRACE(("%s: WLAN OFF DONE\n", __FUNCTION__));
  7956. /* App can now remove power from device */
  7957. } else
  7958. bcmerror = BCME_SDIO_ERROR;
  7959. } else {
  7960. /* App must have restored power to device before calling */
  7961. DHD_ERROR(("\n\n%s: == Power ON ==\n", __FUNCTION__));
  7962. if (bus->dhd->dongle_reset) {
  7963. /* Turn on WLAN */
  7964. dhd_os_sdlock(dhdp);
  7965. /* Reset SD client -- required if devreset is called
  7966. * via 'dhd devreset' iovar
  7967. */
  7968. bcmsdh_reset(bus->sdh);
  7969. /* Attempt to re-attach & download */
  7970. if (dhdsdio_probe_attach(bus, bus->dhd->osh, bus->sdh,
  7971. (uint32 *)(uintptr)si_enum_base(bus->cl_devid),
  7972. bus->cl_devid)) {
  7973. DHD_LINUX_GENERAL_LOCK(bus->dhd, flags);
  7974. DHD_ERROR(("%s: making DHD_BUS_DOWN\n", __FUNCTION__));
  7975. bus->dhd->busstate = DHD_BUS_DOWN;
  7976. DHD_LINUX_GENERAL_UNLOCK(bus->dhd, flags);
  7977. /* Attempt to download binary to the dongle */
  7978. if (dhdsdio_probe_init(bus, bus->dhd->osh, bus->sdh) &&
  7979. dhdsdio_download_firmware(bus, bus->dhd->osh, bus->sdh) >= 0) {
  7980. /* Re-init bus, enable F2 transfer */
  7981. bcmerror = dhd_bus_init((dhd_pub_t *) bus->dhd, FALSE);
  7982. if (bcmerror == BCME_OK) {
  7983. #if defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID)
  7984. dhd_enable_oob_intr(bus, TRUE);
  7985. bcmsdh_oob_intr_register(bus->sdh,
  7986. dhdsdio_isr, bus);
  7987. bcmsdh_oob_intr_set(bus->sdh, TRUE);
  7988. #endif /* defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID) */
  7989. bus->dhd->dongle_reset = FALSE;
  7990. bus->dhd->up = TRUE;
  7991. #if defined(OEM_ANDROID) && !defined(IGNORE_ETH0_DOWN)
  7992. /* Restore flow control */
  7993. dhd_txflowcontrol(bus->dhd, ALL_INTERFACES, OFF);
  7994. #endif /* defined(OEM_ANDROID) && (!defined(IGNORE_ETH0_DOWN)) */
  7995. dhd_os_wd_timer(dhdp, dhd_watchdog_ms);
  7996. DHD_TRACE(("%s: WLAN ON DONE\n", __FUNCTION__));
  7997. } else {
  7998. dhd_bus_stop(bus, FALSE);
  7999. dhdsdio_release_dongle(bus, bus->dhd->osh,
  8000. TRUE, FALSE);
  8001. }
  8002. } else {
  8003. DHD_ERROR(("%s Failed to download binary to the dongle\n",
  8004. __FUNCTION__));
  8005. if (bus->sih != NULL) {
  8006. si_detach(bus->sih);
  8007. bus->sih = NULL;
  8008. }
  8009. bcmerror = BCME_SDIO_ERROR;
  8010. }
  8011. } else
  8012. bcmerror = BCME_SDIO_ERROR;
  8013. dhd_os_sdunlock(dhdp);
  8014. } else {
  8015. DHD_INFO(("%s called when dongle is not in reset\n",
  8016. __FUNCTION__));
  8017. #if defined(OEM_ANDROID)
  8018. DHD_INFO(("Will call dhd_bus_start instead\n"));
  8019. dhd_bus_resume(dhdp, 1);
  8020. if ((bcmerror = dhd_bus_start(dhdp)) != 0)
  8021. DHD_ERROR(("%s: dhd_bus_start fail with %d\n",
  8022. __FUNCTION__, bcmerror));
  8023. #endif /* defined(OEM_ANDROID) */
  8024. }
  8025. }
  8026. return bcmerror;
  8027. }
  8028. int dhd_bus_suspend(dhd_pub_t *dhdpub)
  8029. {
  8030. return bcmsdh_stop(dhdpub->bus->sdh);
  8031. }
  8032. int dhd_bus_resume(dhd_pub_t *dhdpub, int stage)
  8033. {
  8034. return bcmsdh_start(dhdpub->bus->sdh, stage);
  8035. }
  8036. /* Get Chip ID version */
  8037. uint dhd_bus_chip_id(dhd_pub_t *dhdp)
  8038. {
  8039. dhd_bus_t *bus = dhdp->bus;
  8040. return bus->sih->chip;
  8041. }
  8042. /* Get Chip Rev ID version */
  8043. uint dhd_bus_chiprev_id(dhd_pub_t *dhdp)
  8044. {
  8045. dhd_bus_t *bus = dhdp->bus;
  8046. return bus->sih->chiprev;
  8047. }
  8048. /* Get Chip Pkg ID version */
  8049. uint dhd_bus_chippkg_id(dhd_pub_t *dhdp)
  8050. {
  8051. dhd_bus_t *bus = dhdp->bus;
  8052. return bus->sih->chippkg;
  8053. }
  8054. int dhd_bus_get_ids(struct dhd_bus *bus, uint32 *bus_type, uint32 *bus_num, uint32 *slot_num)
  8055. {
  8056. *bus_type = bus->bus;
  8057. *bus_num = bus->bus_num;
  8058. *slot_num = bus->slot_num;
  8059. return 0;
  8060. }
  8061. int
  8062. dhd_bus_membytes(dhd_pub_t *dhdp, bool set, uint32 address, uint8 *data, uint size)
  8063. {
  8064. dhd_bus_t *bus;
  8065. bus = dhdp->bus;
  8066. return dhdsdio_membytes(bus, set, address, data, size);
  8067. }
  8068. #if defined(SUPPORT_MULTIPLE_REVISION)
  8069. static int
  8070. concate_revision_bcm4335(dhd_bus_t *bus, char *fw_path, char *nv_path)
  8071. {
  8072. uint chipver;
  8073. #if defined(SUPPORT_MULTIPLE_CHIPS)
  8074. char chipver_tag[10] = "_4335";
  8075. #else
  8076. char chipver_tag[4] = {0, };
  8077. #endif /* defined(SUPPORT_MULTIPLE_CHIPS) */
  8078. DHD_TRACE(("%s: BCM4335 Multiple Revision Check\n", __FUNCTION__));
  8079. if (bus->sih->chip != BCM4335_CHIP_ID) {
  8080. DHD_ERROR(("%s:Chip is not BCM4335\n", __FUNCTION__));
  8081. return -1;
  8082. }
  8083. chipver = bus->sih->chiprev;
  8084. DHD_ERROR(("CHIP VER = [0x%x]\n", chipver));
  8085. if (chipver == 0x0) {
  8086. DHD_ERROR(("----- CHIP bcm4335_A0 -----\n"));
  8087. strcat(chipver_tag, "_a0");
  8088. } else if (chipver == 0x1) {
  8089. DHD_ERROR(("----- CHIP bcm4335_B0 -----\n"));
  8090. #if defined(SUPPORT_MULTIPLE_CHIPS)
  8091. strcat(chipver_tag, "_b0");
  8092. #endif /* defined(SUPPORT_MULTIPLE_CHIPS) */
  8093. }
  8094. strcat(fw_path, chipver_tag);
  8095. strcat(nv_path, chipver_tag);
  8096. return 0;
  8097. }
  8098. static int
  8099. concate_revision_bcm4339(dhd_bus_t *bus, char *fw_path, char *nv_path)
  8100. {
  8101. uint chipver;
  8102. #if defined(SUPPORT_MULTIPLE_CHIPS)
  8103. char chipver_tag[10] = "_4339";
  8104. #else
  8105. char chipver_tag[4] = {0, };
  8106. #endif /* defined(SUPPORT_MULTIPLE_CHIPS) */
  8107. DHD_TRACE(("%s: BCM4339 Multiple Revision Check\n", __FUNCTION__));
  8108. if (bus->sih->chip != BCM4339_CHIP_ID) {
  8109. DHD_ERROR(("%s:Chip is not BCM4339\n", __FUNCTION__));
  8110. return -1;
  8111. }
  8112. chipver = bus->sih->chiprev;
  8113. DHD_ERROR(("CHIP VER = [0x%x]\n", chipver));
  8114. if (chipver == 0x1) {
  8115. DHD_ERROR(("----- CHIP bcm4339_A0 -----\n"));
  8116. strcat(chipver_tag, "_a0");
  8117. } else {
  8118. DHD_ERROR(("----- CHIP bcm4339 unknown revision %d -----\n",
  8119. chipver));
  8120. }
  8121. strcat(fw_path, chipver_tag);
  8122. strcat(nv_path, chipver_tag);
  8123. return 0;
  8124. }
  8125. static int concate_revision_bcm4350(dhd_bus_t *bus, char *fw_path, char *nv_path)
  8126. {
  8127. uint32 chip_ver;
  8128. #if defined(SUPPORT_MULTIPLE_CHIPS)
  8129. char chipver_tag[10] = {0, };
  8130. #else
  8131. char chipver_tag[4] = {0, };
  8132. #endif /* defined(SUPPORT_MULTIPLE_CHIPS) */
  8133. chip_ver = bus->sih->chiprev;
  8134. #if defined(SUPPORT_MULTIPLE_CHIPS)
  8135. if (chip_ver == 3)
  8136. strcat(chipver_tag, "_4354");
  8137. else
  8138. strcat(chipver_tag, "_4350");
  8139. #endif // endif
  8140. if (chip_ver == 3) {
  8141. DHD_ERROR(("----- CHIP 4354 A0 -----\n"));
  8142. strcat(chipver_tag, "_a0");
  8143. } else {
  8144. DHD_ERROR(("----- Unknown chip version, ver=%x -----\n", chip_ver));
  8145. }
  8146. strcat(fw_path, chipver_tag);
  8147. strcat(nv_path, chipver_tag);
  8148. return 0;
  8149. }
  8150. static int concate_revision_bcm4354(dhd_bus_t *bus, char *fw_path, char *nv_path)
  8151. {
  8152. uint32 chip_ver;
  8153. #if defined(SUPPORT_MULTIPLE_CHIPS)
  8154. char chipver_tag[10] = "_4354";
  8155. #else
  8156. char chipver_tag[4] = {0, };
  8157. #endif /* SUPPORT_MULTIPLE_CHIPS */
  8158. chip_ver = bus->sih->chiprev;
  8159. if (chip_ver == 1) {
  8160. DHD_ERROR(("----- CHIP 4354 A1 -----\n"));
  8161. strcat(chipver_tag, "_a1");
  8162. } else {
  8163. DHD_ERROR(("----- Unknown chip version, ver=%x -----\n", chip_ver));
  8164. }
  8165. strcat(fw_path, chipver_tag);
  8166. strcat(nv_path, chipver_tag);
  8167. return 0;
  8168. }
  8169. static int
  8170. concate_revision_bcm43454(dhd_bus_t *bus, char *fw_path, char *nv_path)
  8171. {
  8172. char chipver_tag[10] = {0, };
  8173. #ifdef SUPPORT_MULTIPLE_BOARD_REV_FROM_DT
  8174. int base_system_rev_for_nv = 0;
  8175. #endif /* SUPPORT_MULTIPLE_BOARD_REV_FROM_DT */
  8176. DHD_TRACE(("%s: BCM43454 Multiple Revision Check\n", __FUNCTION__));
  8177. if (bus->sih->chip != BCM43454_CHIP_ID) {
  8178. DHD_ERROR(("%s:Chip is not BCM43454!\n", __FUNCTION__));
  8179. return -1;
  8180. }
  8181. #ifdef SUPPORT_MULTIPLE_BOARD_REV_FROM_DT
  8182. base_system_rev_for_nv = dhd_get_system_rev();
  8183. if (base_system_rev_for_nv > 0) {
  8184. DHD_ERROR(("----- Board Rev [%d] -----\n", base_system_rev_for_nv));
  8185. sprintf(chipver_tag, "_r%02d", base_system_rev_for_nv);
  8186. }
  8187. #endif /* SUPPORT_MULTIPLE_BOARD_REV_FROM_DT */
  8188. #ifdef SUPPORT_MULTIPLE_BOARD_REV_FROM_HW
  8189. DHD_ERROR(("----- Rev [%d] Fot MULTIPLE Board. -----\n", system_hw_rev));
  8190. if ((system_hw_rev >= 8) && (system_hw_rev <= 11)) {
  8191. DHD_ERROR(("This HW is Rev 08 ~ 11. this is For FD-HW\n"));
  8192. strcat(chipver_tag, "_FD");
  8193. }
  8194. #endif /* SUPPORT_MULTIPLE_BOARD_REV_FROM_HW */
  8195. strcat(nv_path, chipver_tag);
  8196. return 0;
  8197. }
  8198. int
  8199. concate_revision(dhd_bus_t *bus, char *fw_path, char *nv_path)
  8200. {
  8201. int res = 0;
  8202. if (!bus || !bus->sih) {
  8203. DHD_ERROR(("%s:Bus is Invalid\n", __FUNCTION__));
  8204. return -1;
  8205. }
  8206. switch (bus->sih->chip) {
  8207. case BCM4335_CHIP_ID:
  8208. res = concate_revision_bcm4335(bus, fw_path, nv_path);
  8209. break;
  8210. case BCM4339_CHIP_ID:
  8211. res = concate_revision_bcm4339(bus, fw_path, nv_path);
  8212. break;
  8213. case BCM4350_CHIP_ID:
  8214. res = concate_revision_bcm4350(bus, fw_path, nv_path);
  8215. break;
  8216. case BCM4354_CHIP_ID:
  8217. res = concate_revision_bcm4354(bus, fw_path, nv_path);
  8218. break;
  8219. case BCM43454_CHIP_ID:
  8220. res = concate_revision_bcm43454(bus, fw_path, nv_path);
  8221. break;
  8222. default:
  8223. DHD_ERROR(("REVISION SPECIFIC feature is not required\n"));
  8224. return res;
  8225. }
  8226. if (res == 0) {
  8227. }
  8228. return res;
  8229. }
  8230. #endif /* SUPPORT_MULTIPLE_REVISION */
  8231. void
  8232. dhd_bus_update_fw_nv_path(struct dhd_bus *bus, char *pfw_path, char *pnv_path)
  8233. {
  8234. bus->fw_path = pfw_path;
  8235. bus->nv_path = pnv_path;
  8236. }
  8237. int
  8238. dhd_enableOOB(dhd_pub_t *dhd, bool sleep)
  8239. {
  8240. dhd_bus_t *bus = dhd->bus;
  8241. sdpcmd_regs_t *regs = bus->regs;
  8242. uint retries = 0;
  8243. if (sleep) {
  8244. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  8245. /* Tell device to start using OOB wakeup */
  8246. W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
  8247. if (retries > retry_limit) {
  8248. DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
  8249. return BCME_BUSY;
  8250. }
  8251. /* Turn off our contribution to the HT clock request */
  8252. dhdsdio_clkctl(bus, CLK_SDONLY, FALSE);
  8253. } else {
  8254. /* Make sure the controller has the bus up */
  8255. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  8256. /* Send misc interrupt to indicate OOB not needed */
  8257. W_SDREG(0, &regs->tosbmailboxdata, retries);
  8258. if (retries <= retry_limit)
  8259. W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
  8260. if (retries > retry_limit)
  8261. DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
  8262. /* Make sure we have SD bus access */
  8263. dhdsdio_clkctl(bus, CLK_SDONLY, FALSE);
  8264. }
  8265. return BCME_OK;
  8266. }
  8267. void
  8268. dhd_bus_pktq_flush(dhd_pub_t *dhdp)
  8269. {
  8270. dhd_bus_t *bus = dhdp->bus;
  8271. bool wlfc_enabled = FALSE;
  8272. #ifdef PROP_TXSTATUS
  8273. wlfc_enabled = (dhd_wlfc_cleanup_txq(dhdp, NULL, 0) != WLFC_UNSUPPORTED);
  8274. #endif // endif
  8275. if (!wlfc_enabled) {
  8276. #ifdef DHDTCPACK_SUPPRESS
  8277. /* Clean tcp_ack_info_tbl in order to prevent access to flushed pkt,
  8278. * when there is a newly coming packet from network stack.
  8279. */
  8280. dhd_tcpack_info_tbl_clean(bus->dhd);
  8281. #endif /* DHDTCPACK_SUPPRESS */
  8282. /* Clear the data packet queues */
  8283. pktq_flush(dhdp->osh, &bus->txq, TRUE);
  8284. }
  8285. }
  8286. #ifdef BCMSDIO
  8287. int
  8288. dhd_sr_config(dhd_pub_t *dhd, bool on)
  8289. {
  8290. dhd_bus_t *bus = dhd->bus;
  8291. if (!bus->_srenab)
  8292. return -1;
  8293. return dhdsdio_clk_devsleep_iovar(bus, on);
  8294. }
  8295. uint16
  8296. dhd_get_chipid(dhd_pub_t *dhd)
  8297. {
  8298. dhd_bus_t *bus = dhd->bus;
  8299. if (bus && bus->sih)
  8300. return (uint16)bus->sih->chip;
  8301. else
  8302. return 0;
  8303. }
  8304. #endif /* BCMSDIO */
  8305. #ifdef DEBUGGER
  8306. static uint32
  8307. dhd_sdio_reg_read(struct dhd_bus *bus, ulong addr)
  8308. {
  8309. uint32 rval;
  8310. dhd_os_sdlock(bus->dhd);
  8311. BUS_WAKE(bus);
  8312. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  8313. rval = bcmsdh_reg_read(bus->sdh, addr, 4);
  8314. dhd_os_sdunlock(bus->dhd);
  8315. return rval;
  8316. }
  8317. static void
  8318. dhd_sdio_reg_write(struct dhd_bus *bus, ulong addr, uint32 val)
  8319. {
  8320. dhd_os_sdlock(bus->dhd);
  8321. BUS_WAKE(bus);
  8322. dhdsdio_clkctl(bus, CLK_AVAIL, FALSE);
  8323. bcmsdh_reg_write(bus->sdh, addr, 4, val);
  8324. dhd_os_sdunlock(bus->dhd);
  8325. }
  8326. #endif /* DEBUGGER */
  8327. #if defined(BT_OVER_SDIO)
  8328. uint8 dhd_bus_cfg_read(void *h, uint fun_num, uint32 addr, int *err)
  8329. {
  8330. uint8 intrd;
  8331. dhd_pub_t *dhdp = (dhd_pub_t *)h;
  8332. dhd_bus_t *bus = (dhd_bus_t *)dhdp->bus;
  8333. dhd_os_sdlock(bus->dhd);
  8334. intrd = bcmsdh_cfg_read(bus->sdh, fun_num, addr, err);
  8335. dhd_os_sdunlock(bus->dhd);
  8336. return intrd;
  8337. } EXPORT_SYMBOL(dhd_bus_cfg_read);
  8338. void dhd_bus_cfg_write(void *h, uint fun_num, uint32 addr, uint8 val, int *err)
  8339. {
  8340. dhd_pub_t *dhdp = (dhd_pub_t *)h;
  8341. dhd_bus_t *bus = (dhd_bus_t *)dhdp->bus;
  8342. dhd_os_sdlock(bus->dhd);
  8343. bcmsdh_cfg_write(bus->sdh, fun_num, addr, val, err);
  8344. dhd_os_sdunlock(bus->dhd);
  8345. } EXPORT_SYMBOL(dhd_bus_cfg_write);
  8346. static int
  8347. extract_hex_field(char * line, uint16 start_pos, uint16 num_chars, uint16 * value)
  8348. {
  8349. char field [8];
  8350. strncpy(field, line + start_pos, num_chars);
  8351. field [num_chars] = '\0';
  8352. return (sscanf (field, "%hX", value) == 1);
  8353. }
  8354. static int
  8355. read_more_btbytes(struct dhd_bus *bus, void * file, char *line, int * addr_mode, uint16 * hi_addr,
  8356. uint32 * dest_addr, uint8 *data_bytes, uint32 * num_bytes)
  8357. {
  8358. int str_len;
  8359. uint16 num_data_bytes, addr, data_pos, type, w, i;
  8360. uint32 abs_base_addr32 = 0;
  8361. *num_bytes = 0;
  8362. while (!*num_bytes)
  8363. {
  8364. str_len = dhd_os_gets_image(bus->dhd, line, BTFW_MAX_STR_LEN, file);
  8365. DHD_TRACE(("%s: Len :0x%x %s\n", __FUNCTION__, str_len, line));
  8366. if (str_len == 0) {
  8367. break;
  8368. } else if (str_len > 9) {
  8369. extract_hex_field(line, 1, 2, &num_data_bytes);
  8370. extract_hex_field(line, 3, 4, &addr);
  8371. extract_hex_field(line, 7, 2, &type);
  8372. data_pos = 9;
  8373. for (i = 0; i < num_data_bytes; i++) {
  8374. extract_hex_field(line, data_pos, 2, &w);
  8375. data_bytes [i] = (uint8)(w & 0x00FF);
  8376. data_pos += 2;
  8377. }
  8378. if (type == BTFW_HEX_LINE_TYPE_EXTENDED_ADDRESS) {
  8379. *hi_addr = (data_bytes [0] << 8) | data_bytes [1];
  8380. *addr_mode = BTFW_ADDR_MODE_EXTENDED;
  8381. } else if (type == BTFW_HEX_LINE_TYPE_EXTENDED_SEGMENT_ADDRESS) {
  8382. *hi_addr = (data_bytes [0] << 8) | data_bytes [1];
  8383. *addr_mode = BTFW_ADDR_MODE_SEGMENT;
  8384. } else if (type == BTFW_HEX_LINE_TYPE_ABSOLUTE_32BIT_ADDRESS) {
  8385. abs_base_addr32 = (data_bytes [0] << 24) | (data_bytes [1] << 16) |
  8386. (data_bytes [2] << 8) | data_bytes [3];
  8387. *addr_mode = BTFW_ADDR_MODE_LINEAR32;
  8388. } else if (type == BTFW_HEX_LINE_TYPE_DATA) {
  8389. *dest_addr = addr;
  8390. if (*addr_mode == BTFW_ADDR_MODE_EXTENDED)
  8391. *dest_addr += (*hi_addr << 16);
  8392. else if (*addr_mode == BTFW_ADDR_MODE_SEGMENT)
  8393. *dest_addr += (*hi_addr << 4);
  8394. else if (*addr_mode == BTFW_ADDR_MODE_LINEAR32)
  8395. *dest_addr += abs_base_addr32;
  8396. *num_bytes = num_data_bytes;
  8397. }
  8398. }
  8399. }
  8400. return (*num_bytes > 0);
  8401. }
  8402. static int
  8403. _dhdsdio_download_btfw(struct dhd_bus *bus)
  8404. {
  8405. int bcm_error = -1;
  8406. void *image = NULL;
  8407. uint8 *mem_blk = NULL, *mem_ptr = NULL, *data_ptr = NULL;
  8408. uint32 offset_addr = 0, offset_len = 0, bytes_to_write = 0;
  8409. char *line = NULL;
  8410. uint32 dest_addr = 0, num_bytes;
  8411. uint16 hiAddress = 0;
  8412. uint32 start_addr, start_data, end_addr, end_data, i, index, pad,
  8413. bt2wlan_pwrup_adr;
  8414. int addr_mode = BTFW_ADDR_MODE_EXTENDED;
  8415. /* Out immediately if no image to download */
  8416. if ((bus->btfw_path == NULL) || (bus->btfw_path[0] == '\0')) {
  8417. return 0;
  8418. }
  8419. image = dhd_os_open_image1(bus->dhd, bus->btfw_path);
  8420. if (image == NULL)
  8421. goto err;
  8422. mem_ptr = mem_blk = MALLOC(bus->dhd->osh, BTFW_DOWNLOAD_BLK_SIZE + DHD_SDALIGN);
  8423. if (mem_blk == NULL) {
  8424. DHD_ERROR(("%s: Failed to allocate memory %d bytes\n", __FUNCTION__,
  8425. BTFW_DOWNLOAD_BLK_SIZE + DHD_SDALIGN));
  8426. goto err;
  8427. }
  8428. if ((uint32)(uintptr)mem_blk % DHD_SDALIGN)
  8429. mem_ptr += (DHD_SDALIGN - ((uint32)(uintptr)mem_blk % DHD_SDALIGN));
  8430. data_ptr = MALLOC(bus->dhd->osh, BTFW_DOWNLOAD_BLK_SIZE - 8);
  8431. if (data_ptr == NULL) {
  8432. DHD_ERROR(("%s: Failed to allocate memory %d bytes\n", __FUNCTION__,
  8433. BTFW_DOWNLOAD_BLK_SIZE - 8));
  8434. goto err;
  8435. }
  8436. /* Write to BT register to hold WLAN wake high during BT FW download */
  8437. bt2wlan_pwrup_adr = BTMEM_OFFSET + BT2WLAN_PWRUP_ADDR;
  8438. bcmsdh_reg_write(bus->sdh, bt2wlan_pwrup_adr, 4, BT2WLAN_PWRUP_WAKE);
  8439. /*
  8440. * Wait for at least 2msec for the clock to be ready/Available.
  8441. */
  8442. OSL_DELAY(2000);
  8443. line = MALLOC(bus->dhd->osh, BTFW_MAX_STR_LEN);
  8444. if (line == NULL) {
  8445. DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
  8446. __FUNCTION__, BTFW_MAX_STR_LEN));
  8447. goto err;
  8448. }
  8449. memset(line, 0, BTFW_MAX_STR_LEN);
  8450. while (read_more_btbytes (bus, image, line, &addr_mode, &hiAddress, &dest_addr,
  8451. data_ptr, &num_bytes)) {
  8452. DHD_TRACE(("read %d bytes at address %08X\n", num_bytes, dest_addr));
  8453. start_addr = BTMEM_OFFSET + dest_addr;
  8454. index = 0;
  8455. /* Make sure the start address is 4 byte aligned to avoid alignment issues
  8456. * with SD host controllers
  8457. */
  8458. if (!ISALIGNED(start_addr, 4)) {
  8459. pad = start_addr % 4;
  8460. start_addr = ROUNDDN(start_addr, 4);
  8461. start_data = bcmsdh_reg_read(bus->sdh, start_addr, 4);
  8462. for (i = 0; i < pad; i++, index++) {
  8463. mem_ptr[index] = (uint8)((uint8 *)&start_data)[i];
  8464. }
  8465. }
  8466. bcopy(data_ptr, &(mem_ptr[index]), num_bytes);
  8467. index += num_bytes;
  8468. /* Make sure the length is multiple of 4bytes to avoid alignment issues
  8469. * with SD host controllers
  8470. */
  8471. end_addr = start_addr + index;
  8472. if (!ISALIGNED(end_addr, 4)) {
  8473. end_addr = ROUNDDN(end_addr, 4);
  8474. end_data = bcmsdh_reg_read(bus->sdh, end_addr, 4);
  8475. for (i = (index % 4); i < 4; i++, index++) {
  8476. mem_ptr[index] = (uint8)((uint8 *)&end_data)[i];
  8477. }
  8478. }
  8479. offset_addr = start_addr & 0xFFF;
  8480. offset_len = offset_addr + index;
  8481. if (offset_len <= 0x1000) {
  8482. bcm_error = dhdsdio_membytes(bus, TRUE, start_addr, mem_ptr, index);
  8483. if (bcm_error) {
  8484. DHD_ERROR(("%s: error %d on writing %d membytes at 0x%08x\n",
  8485. __FUNCTION__, bcm_error, num_bytes, start_addr));
  8486. goto err;
  8487. }
  8488. }
  8489. else {
  8490. bytes_to_write = 0x1000 - offset_addr;
  8491. bcm_error = dhdsdio_membytes(bus, TRUE, start_addr, mem_ptr,
  8492. bytes_to_write);
  8493. if (bcm_error) {
  8494. DHD_ERROR(("%s: error %d on writing %d membytes at 0x%08x\n",
  8495. __FUNCTION__, bcm_error, num_bytes, start_addr));
  8496. goto err;
  8497. }
  8498. OSL_DELAY(10000);
  8499. bcm_error = dhdsdio_membytes(bus, TRUE, (start_addr + bytes_to_write),
  8500. (mem_ptr + bytes_to_write), (index - bytes_to_write));
  8501. if (bcm_error) {
  8502. DHD_ERROR(("%s: error %d on writing %d membytes at 0x%08x\n",
  8503. __FUNCTION__, bcm_error, num_bytes, start_addr));
  8504. goto err;
  8505. }
  8506. }
  8507. memset(line, 0, BTFW_MAX_STR_LEN);
  8508. }
  8509. bcm_error = 0;
  8510. err:
  8511. if (mem_blk)
  8512. MFREE(bus->dhd->osh, mem_blk, BTFW_DOWNLOAD_BLK_SIZE + DHD_SDALIGN);
  8513. if (data_ptr)
  8514. MFREE(bus->dhd->osh, data_ptr, BTFW_DOWNLOAD_BLK_SIZE - 8);
  8515. if (line)
  8516. MFREE(bus->dhd->osh, line, BTFW_MAX_STR_LEN);
  8517. if (image)
  8518. dhd_os_close_image1(bus->dhd, image);
  8519. return bcm_error;
  8520. }
  8521. static int
  8522. dhdsdio_download_btfw(struct dhd_bus *bus, osl_t *osh, void *sdh)
  8523. {
  8524. int ret;
  8525. DHD_TRACE(("%s: btfw path=%s\n",
  8526. __FUNCTION__, bus->btfw_path));
  8527. DHD_OS_WAKE_LOCK(bus->dhd);
  8528. dhd_os_sdlock(bus->dhd);
  8529. /* Download the firmware */
  8530. ret = _dhdsdio_download_btfw(bus);
  8531. dhd_os_sdunlock(bus->dhd);
  8532. DHD_OS_WAKE_UNLOCK(bus->dhd);
  8533. return ret;
  8534. }
  8535. int
  8536. dhd_bus_download_btfw(struct dhd_bus *bus, osl_t *osh,
  8537. char *pbtfw_path)
  8538. {
  8539. int ret;
  8540. bus->btfw_path = pbtfw_path;
  8541. ret = dhdsdio_download_btfw(bus, osh, bus->sdh);
  8542. return ret;
  8543. }
  8544. #endif /* defined (BT_OVER_SDIO) */
  8545. void
  8546. dhd_bus_dump_trap_info(dhd_bus_t *bus, struct bcmstrbuf *strbuf)
  8547. {
  8548. trap_t *tr = &bus->dhd->last_trap_info;
  8549. bcm_bprintf(strbuf,
  8550. "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
  8551. "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
  8552. "r0 0x%x, r1 0x%x, r2 0x%x, r3 0x%x, "
  8553. "r4 0x%x, r5 0x%x, r6 0x%x, r7 0x%x\n\n",
  8554. ltoh32(tr->type), ltoh32(tr->epc), ltoh32(tr->cpsr), ltoh32(tr->spsr),
  8555. ltoh32(tr->r13), ltoh32(tr->r14), ltoh32(tr->pc),
  8556. ltoh32(bus->dongle_trap_addr),
  8557. ltoh32(tr->r0), ltoh32(tr->r1), ltoh32(tr->r2), ltoh32(tr->r3),
  8558. ltoh32(tr->r4), ltoh32(tr->r5), ltoh32(tr->r6), ltoh32(tr->r7));
  8559. }
  8560. static int
  8561. dhd_bcmsdh_send_buffer(void *bus, uint8 *frame, uint16 len)
  8562. {
  8563. int ret = -1;
  8564. ret = dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(((dhd_bus_t*)bus)->sdh),
  8565. SDIO_FUNC_2, F2SYNC, frame, len, NULL, NULL, NULL, TXRETRIES);
  8566. if (ret == BCME_OK)
  8567. ((dhd_bus_t*)bus)->tx_seq = (((dhd_bus_t*)bus)->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
  8568. return ret;
  8569. }
  8570. /* Function to set the min res mask depending on the chip ID used */
  8571. bool
  8572. dhd_bus_set_default_min_res_mask(struct dhd_bus *bus)
  8573. {
  8574. if ((bus == NULL) || (bus->sih == NULL)) {
  8575. DHD_ERROR(("%s(): Invalid Arguments \r\n", __FUNCTION__));
  8576. return FALSE;
  8577. }
  8578. switch (bus->sih->chip) {
  8579. case BCM4339_CHIP_ID:
  8580. bcmsdh_reg_write(bus->sdh, SI_ENUM_BASE(bus->sih) + 0x618, 4, 0x3fcaf377);
  8581. if (bcmsdh_regfail(bus->sdh)) {
  8582. DHD_ERROR(("%s:%d Setting min_res_mask failed\n", __FUNCTION__, __LINE__));
  8583. return FALSE;
  8584. }
  8585. break;
  8586. case BCM43012_CHIP_ID:
  8587. bcmsdh_reg_write(bus->sdh,
  8588. si_get_pmu_reg_addr(bus->sih, OFFSETOF(pmuregs_t, min_res_mask)),
  8589. 4, DEFAULT_43012_MIN_RES_MASK);
  8590. if (bcmsdh_regfail(bus->sdh)) {
  8591. DHD_ERROR(("%s:%d Setting min_res_mask failed\n", __FUNCTION__, __LINE__));
  8592. return FALSE;
  8593. }
  8594. break;
  8595. default:
  8596. DHD_ERROR(("%s: Unhandled chip id\n", __FUNCTION__));
  8597. return FALSE;
  8598. }
  8599. return TRUE;
  8600. }
  8601. /* Function to reset PMU registers */
  8602. void
  8603. dhd_bus_pmu_reg_reset(dhd_pub_t *dhdp)
  8604. {
  8605. struct dhd_bus *bus = dhdp->bus;
  8606. bcmsdh_reg_write(bus->sdh, si_get_pmu_reg_addr(bus->sih,
  8607. OFFSETOF(pmuregs_t, swscratch)), 4, 0x0);
  8608. if (bcmsdh_regfail(bus->sdh)) {
  8609. DHD_ERROR(("%s:%d Setting min_res_mask failed\n", __FUNCTION__, __LINE__));
  8610. }
  8611. }
  8612. #ifdef DHD_ULP
  8613. /* Function to disable console messages on entering ULP mode */
  8614. void
  8615. dhd_bus_ulp_disable_console(dhd_pub_t *dhdp)
  8616. {
  8617. #ifdef DHD_DEBUG
  8618. DHD_ERROR(("Flushing and disabling console messages\n"));
  8619. /* Save the console print interval */
  8620. dhd_ulp_save_console_interval(dhdp);
  8621. /* Flush the console buffer before disabling */
  8622. dhdsdio_readconsole(dhdp->bus);
  8623. dhdp->dhd_console_ms = 0;
  8624. #endif /* DHD_DEBUG */
  8625. }
  8626. /* Function for redownloading firmaware */
  8627. static int
  8628. dhd_bus_ulp_reinit_fw(dhd_bus_t *bus)
  8629. {
  8630. int bcmerror = 0;
  8631. /* After firmware redownload tx/rx seq are reset accordingly these values are
  8632. reset on DHD side tx_max is initially set to 4, which later is updated by FW
  8633. */
  8634. bus->tx_seq = bus->rx_seq = 0;
  8635. bus->tx_max = 4;
  8636. if (dhd_bus_download_firmware(bus, bus->dhd->osh,
  8637. bus->fw_path, bus->nv_path) >= 0) {
  8638. /* Re-init bus, enable F2 transfer */
  8639. bcmerror = dhd_bus_init((dhd_pub_t *) bus->dhd, FALSE);
  8640. if (bcmerror == BCME_OK) {
  8641. bus->dhd->up = TRUE;
  8642. dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
  8643. dhd_ulp_set_ulp_state(bus->dhd, DHD_ULP_READY);
  8644. #if defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID)
  8645. dhd_enable_oob_intr(bus, TRUE);
  8646. bcmsdh_oob_intr_set(bus->sdh, TRUE);
  8647. #endif /* defined(OOB_INTR_ONLY) || defined(BCMSPI_ANDROID) */
  8648. #ifdef DHD_DEBUG
  8649. /* Re-enable the console messages on FW redownload to default value */
  8650. dhd_ulp_restore_console_interval(bus->dhd);
  8651. #endif /* DHD_DEBUG */
  8652. } else {
  8653. DHD_ERROR(("bus init failed\n"));
  8654. dhd_bus_stop(bus, FALSE);
  8655. dhdsdio_release_dongle(bus, bus->dhd->osh,
  8656. TRUE, FALSE);
  8657. }
  8658. } else
  8659. bcmerror = BCME_SDIO_ERROR;
  8660. return bcmerror;
  8661. }
  8662. #endif /* DHD_ULP */
  8663. int
  8664. dhd_bus_readwrite_bp_addr(dhd_pub_t *dhdp, uint addr, uint size, uint* data, bool read)
  8665. {
  8666. int bcmerror = 0;
  8667. struct dhd_bus *bus = dhdp->bus;
  8668. if (read) {
  8669. *data = (int32)bcmsdh_reg_read(bus->sdh, addr, size);
  8670. } else {
  8671. bcmsdh_reg_write(bus->sdh, addr, size, *data);
  8672. }
  8673. if (bcmsdh_regfail(bus->sdh))
  8674. bcmerror = BCME_SDIO_ERROR;
  8675. return bcmerror;
  8676. }
  8677. int dhd_get_idletime(dhd_pub_t *dhd)
  8678. {
  8679. return dhd->bus->idletime;
  8680. }
  8681. #ifdef DHD_WAKE_STATUS
  8682. wake_counts_t*
  8683. dhd_bus_get_wakecount(dhd_pub_t *dhd)
  8684. {
  8685. return &dhd->bus->wake_counts;
  8686. }
  8687. int
  8688. dhd_bus_get_bus_wake(dhd_pub_t *dhd)
  8689. {
  8690. return bcmsdh_set_get_wake(dhd->bus->sdh, 0);
  8691. }
  8692. #endif /* DHD_WAKE_STATUS */