main.c 214 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  12. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  14. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/pci_ids.h>
  19. #include <linux/if_ether.h>
  20. #include <net/cfg80211.h>
  21. #include <net/mac80211.h>
  22. #include <brcm_hw_ids.h>
  23. #include <aiutils.h>
  24. #include <chipcommon.h>
  25. #include "rate.h"
  26. #include "scb.h"
  27. #include "phy/phy_hal.h"
  28. #include "channel.h"
  29. #include "antsel.h"
  30. #include "stf.h"
  31. #include "ampdu.h"
  32. #include "mac80211_if.h"
  33. #include "ucode_loader.h"
  34. #include "main.h"
  35. #include "soc.h"
  36. #include "dma.h"
  37. #include "debug.h"
  38. #include "brcms_trace_events.h"
  39. /* watchdog timer, in unit of ms */
  40. #define TIMER_INTERVAL_WATCHDOG 1000
  41. /* radio monitor timer, in unit of ms */
  42. #define TIMER_INTERVAL_RADIOCHK 800
  43. /* beacon interval, in unit of 1024TU */
  44. #define BEACON_INTERVAL_DEFAULT 100
  45. /* n-mode support capability */
  46. /* 2x2 includes both 1x1 & 2x2 devices
  47. * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
  48. * control it independently
  49. */
  50. #define WL_11N_2x2 1
  51. #define WL_11N_3x3 3
  52. #define WL_11N_4x4 4
  53. #define EDCF_ACI_MASK 0x60
  54. #define EDCF_ACI_SHIFT 5
  55. #define EDCF_ECWMIN_MASK 0x0f
  56. #define EDCF_ECWMAX_SHIFT 4
  57. #define EDCF_AIFSN_MASK 0x0f
  58. #define EDCF_AIFSN_MAX 15
  59. #define EDCF_ECWMAX_MASK 0xf0
  60. #define EDCF_AC_BE_TXOP_STA 0x0000
  61. #define EDCF_AC_BK_TXOP_STA 0x0000
  62. #define EDCF_AC_VO_ACI_STA 0x62
  63. #define EDCF_AC_VO_ECW_STA 0x32
  64. #define EDCF_AC_VI_ACI_STA 0x42
  65. #define EDCF_AC_VI_ECW_STA 0x43
  66. #define EDCF_AC_BK_ECW_STA 0xA4
  67. #define EDCF_AC_VI_TXOP_STA 0x005e
  68. #define EDCF_AC_VO_TXOP_STA 0x002f
  69. #define EDCF_AC_BE_ACI_STA 0x03
  70. #define EDCF_AC_BE_ECW_STA 0xA4
  71. #define EDCF_AC_BK_ACI_STA 0x27
  72. #define EDCF_AC_VO_TXOP_AP 0x002f
  73. #define EDCF_TXOP2USEC(txop) ((txop) << 5)
  74. #define EDCF_ECW2CW(exp) ((1 << (exp)) - 1)
  75. #define APHY_SYMBOL_TIME 4
  76. #define APHY_PREAMBLE_TIME 16
  77. #define APHY_SIGNAL_TIME 4
  78. #define APHY_SIFS_TIME 16
  79. #define APHY_SERVICE_NBITS 16
  80. #define APHY_TAIL_NBITS 6
  81. #define BPHY_SIFS_TIME 10
  82. #define BPHY_PLCP_SHORT_TIME 96
  83. #define PREN_PREAMBLE 24
  84. #define PREN_MM_EXT 12
  85. #define PREN_PREAMBLE_EXT 4
  86. #define DOT11_MAC_HDR_LEN 24
  87. #define DOT11_ACK_LEN 10
  88. #define DOT11_BA_LEN 4
  89. #define DOT11_OFDM_SIGNAL_EXTENSION 6
  90. #define DOT11_MIN_FRAG_LEN 256
  91. #define DOT11_RTS_LEN 16
  92. #define DOT11_CTS_LEN 10
  93. #define DOT11_BA_BITMAP_LEN 128
  94. #define DOT11_MAXNUMFRAGS 16
  95. #define DOT11_MAX_FRAG_LEN 2346
  96. #define BPHY_PLCP_TIME 192
  97. #define RIFS_11N_TIME 2
  98. /* length of the BCN template area */
  99. #define BCN_TMPL_LEN 512
  100. /* brcms_bss_info flag bit values */
  101. #define BRCMS_BSS_HT 0x0020 /* BSS is HT (MIMO) capable */
  102. /* chip rx buffer offset */
  103. #define BRCMS_HWRXOFF 38
  104. /* rfdisable delay timer 500 ms, runs of ALP clock */
  105. #define RFDISABLE_DEFAULT 10000000
  106. #define BRCMS_TEMPSENSE_PERIOD 10 /* 10 second timeout */
  107. /* synthpu_dly times in us */
  108. #define SYNTHPU_DLY_APHY_US 3700
  109. #define SYNTHPU_DLY_BPHY_US 1050
  110. #define SYNTHPU_DLY_NPHY_US 2048
  111. #define SYNTHPU_DLY_LPPHY_US 300
  112. #define ANTCNT 10 /* vanilla M_MAX_ANTCNT val */
  113. /* Per-AC retry limit register definitions; uses defs.h bitfield macros */
  114. #define EDCF_SHORT_S 0
  115. #define EDCF_SFB_S 4
  116. #define EDCF_LONG_S 8
  117. #define EDCF_LFB_S 12
  118. #define EDCF_SHORT_M BITFIELD_MASK(4)
  119. #define EDCF_SFB_M BITFIELD_MASK(4)
  120. #define EDCF_LONG_M BITFIELD_MASK(4)
  121. #define EDCF_LFB_M BITFIELD_MASK(4)
  122. #define RETRY_SHORT_DEF 7 /* Default Short retry Limit */
  123. #define RETRY_SHORT_MAX 255 /* Maximum Short retry Limit */
  124. #define RETRY_LONG_DEF 4 /* Default Long retry count */
  125. #define RETRY_SHORT_FB 3 /* Short count for fb rate */
  126. #define RETRY_LONG_FB 2 /* Long count for fb rate */
  127. #define APHY_CWMIN 15
  128. #define PHY_CWMAX 1023
  129. #define EDCF_AIFSN_MIN 1
  130. #define FRAGNUM_MASK 0xF
  131. #define APHY_SLOT_TIME 9
  132. #define BPHY_SLOT_TIME 20
  133. #define WL_SPURAVOID_OFF 0
  134. #define WL_SPURAVOID_ON1 1
  135. #define WL_SPURAVOID_ON2 2
  136. /* invalid core flags, use the saved coreflags */
  137. #define BRCMS_USE_COREFLAGS 0xffffffff
  138. /* values for PLCPHdr_override */
  139. #define BRCMS_PLCP_AUTO -1
  140. #define BRCMS_PLCP_SHORT 0
  141. #define BRCMS_PLCP_LONG 1
  142. /* values for g_protection_override and n_protection_override */
  143. #define BRCMS_PROTECTION_AUTO -1
  144. #define BRCMS_PROTECTION_OFF 0
  145. #define BRCMS_PROTECTION_ON 1
  146. #define BRCMS_PROTECTION_MMHDR_ONLY 2
  147. #define BRCMS_PROTECTION_CTS_ONLY 3
  148. /* values for g_protection_control and n_protection_control */
  149. #define BRCMS_PROTECTION_CTL_OFF 0
  150. #define BRCMS_PROTECTION_CTL_LOCAL 1
  151. #define BRCMS_PROTECTION_CTL_OVERLAP 2
  152. /* values for n_protection */
  153. #define BRCMS_N_PROTECTION_OFF 0
  154. #define BRCMS_N_PROTECTION_OPTIONAL 1
  155. #define BRCMS_N_PROTECTION_20IN40 2
  156. #define BRCMS_N_PROTECTION_MIXEDMODE 3
  157. /* values for band specific 40MHz capabilities */
  158. #define BRCMS_N_BW_20ALL 0
  159. #define BRCMS_N_BW_40ALL 1
  160. #define BRCMS_N_BW_20IN2G_40IN5G 2
  161. /* bitflags for SGI support (sgi_rx iovar) */
  162. #define BRCMS_N_SGI_20 0x01
  163. #define BRCMS_N_SGI_40 0x02
  164. /* defines used by the nrate iovar */
  165. /* MSC in use,indicates b0-6 holds an mcs */
  166. #define NRATE_MCS_INUSE 0x00000080
  167. /* rate/mcs value */
  168. #define NRATE_RATE_MASK 0x0000007f
  169. /* stf mode mask: siso, cdd, stbc, sdm */
  170. #define NRATE_STF_MASK 0x0000ff00
  171. /* stf mode shift */
  172. #define NRATE_STF_SHIFT 8
  173. /* bit indicate to override mcs only */
  174. #define NRATE_OVERRIDE_MCS_ONLY 0x40000000
  175. #define NRATE_SGI_MASK 0x00800000 /* sgi mode */
  176. #define NRATE_SGI_SHIFT 23 /* sgi mode */
  177. #define NRATE_LDPC_CODING 0x00400000 /* adv coding in use */
  178. #define NRATE_LDPC_SHIFT 22 /* ldpc shift */
  179. #define NRATE_STF_SISO 0 /* stf mode SISO */
  180. #define NRATE_STF_CDD 1 /* stf mode CDD */
  181. #define NRATE_STF_STBC 2 /* stf mode STBC */
  182. #define NRATE_STF_SDM 3 /* stf mode SDM */
  183. #define MAX_DMA_SEGS 4
  184. /* # of entries in Tx FIFO */
  185. #define NTXD 64
  186. /* Max # of entries in Rx FIFO based on 4kb page size */
  187. #define NRXD 256
  188. /* Amount of headroom to leave in Tx FIFO */
  189. #define TX_HEADROOM 4
  190. /* try to keep this # rbufs posted to the chip */
  191. #define NRXBUFPOST 32
  192. /* max # frames to process in brcms_c_recv() */
  193. #define RXBND 8
  194. /* max # tx status to process in wlc_txstatus() */
  195. #define TXSBND 8
  196. /* brcmu_format_flags() bit description structure */
  197. struct brcms_c_bit_desc {
  198. u32 bit;
  199. const char *name;
  200. };
  201. /*
  202. * The following table lists the buffer memory allocated to xmt fifos in HW.
  203. * the size is in units of 256bytes(one block), total size is HW dependent
  204. * ucode has default fifo partition, sw can overwrite if necessary
  205. *
  206. * This is documented in twiki under the topic UcodeTxFifo. Please ensure
  207. * the twiki is updated before making changes.
  208. */
  209. /* Starting corerev for the fifo size table */
  210. #define XMTFIFOTBL_STARTREV 17
  211. struct d11init {
  212. __le16 addr;
  213. __le16 size;
  214. __le32 value;
  215. };
  216. struct edcf_acparam {
  217. u8 ACI;
  218. u8 ECW;
  219. u16 TXOP;
  220. } __packed;
  221. /* debug/trace */
  222. uint brcm_msg_level;
  223. /* TX FIFO number to WME/802.1E Access Category */
  224. static const u8 wme_fifo2ac[] = {
  225. IEEE80211_AC_BK,
  226. IEEE80211_AC_BE,
  227. IEEE80211_AC_VI,
  228. IEEE80211_AC_VO,
  229. IEEE80211_AC_BE,
  230. IEEE80211_AC_BE
  231. };
  232. /* ieee80211 Access Category to TX FIFO number */
  233. static const u8 wme_ac2fifo[] = {
  234. TX_AC_VO_FIFO,
  235. TX_AC_VI_FIFO,
  236. TX_AC_BE_FIFO,
  237. TX_AC_BK_FIFO
  238. };
  239. static const u16 xmtfifo_sz[][NFIFO] = {
  240. /* corerev 17: 5120, 49152, 49152, 5376, 4352, 1280 */
  241. {20, 192, 192, 21, 17, 5},
  242. /* corerev 18: */
  243. {0, 0, 0, 0, 0, 0},
  244. /* corerev 19: */
  245. {0, 0, 0, 0, 0, 0},
  246. /* corerev 20: 5120, 49152, 49152, 5376, 4352, 1280 */
  247. {20, 192, 192, 21, 17, 5},
  248. /* corerev 21: 2304, 14848, 5632, 3584, 3584, 1280 */
  249. {9, 58, 22, 14, 14, 5},
  250. /* corerev 22: 5120, 49152, 49152, 5376, 4352, 1280 */
  251. {20, 192, 192, 21, 17, 5},
  252. /* corerev 23: 5120, 49152, 49152, 5376, 4352, 1280 */
  253. {20, 192, 192, 21, 17, 5},
  254. /* corerev 24: 2304, 14848, 5632, 3584, 3584, 1280 */
  255. {9, 58, 22, 14, 14, 5},
  256. /* corerev 25: */
  257. {0, 0, 0, 0, 0, 0},
  258. /* corerev 26: */
  259. {0, 0, 0, 0, 0, 0},
  260. /* corerev 27: */
  261. {0, 0, 0, 0, 0, 0},
  262. /* corerev 28: 2304, 14848, 5632, 3584, 3584, 1280 */
  263. {9, 58, 22, 14, 14, 5},
  264. };
  265. #ifdef DEBUG
  266. static const char * const fifo_names[] = {
  267. "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
  268. #else
  269. static const char fifo_names[6][1];
  270. #endif
  271. #ifdef DEBUG
  272. /* pointer to most recently allocated wl/wlc */
  273. static struct brcms_c_info *wlc_info_dbg = (struct brcms_c_info *) (NULL);
  274. #endif
  275. /* Mapping of ieee80211 AC numbers to tx fifos */
  276. static const u8 ac_to_fifo_mapping[IEEE80211_NUM_ACS] = {
  277. [IEEE80211_AC_VO] = TX_AC_VO_FIFO,
  278. [IEEE80211_AC_VI] = TX_AC_VI_FIFO,
  279. [IEEE80211_AC_BE] = TX_AC_BE_FIFO,
  280. [IEEE80211_AC_BK] = TX_AC_BK_FIFO,
  281. };
  282. /* Mapping of tx fifos to ieee80211 AC numbers */
  283. static const u8 fifo_to_ac_mapping[IEEE80211_NUM_ACS] = {
  284. [TX_AC_BK_FIFO] = IEEE80211_AC_BK,
  285. [TX_AC_BE_FIFO] = IEEE80211_AC_BE,
  286. [TX_AC_VI_FIFO] = IEEE80211_AC_VI,
  287. [TX_AC_VO_FIFO] = IEEE80211_AC_VO,
  288. };
  289. static u8 brcms_ac_to_fifo(u8 ac)
  290. {
  291. if (ac >= ARRAY_SIZE(ac_to_fifo_mapping))
  292. return TX_AC_BE_FIFO;
  293. return ac_to_fifo_mapping[ac];
  294. }
  295. static u8 brcms_fifo_to_ac(u8 fifo)
  296. {
  297. if (fifo >= ARRAY_SIZE(fifo_to_ac_mapping))
  298. return IEEE80211_AC_BE;
  299. return fifo_to_ac_mapping[fifo];
  300. }
  301. /* Find basic rate for a given rate */
  302. static u8 brcms_basic_rate(struct brcms_c_info *wlc, u32 rspec)
  303. {
  304. if (is_mcs_rate(rspec))
  305. return wlc->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK]
  306. .leg_ofdm];
  307. return wlc->band->basic_rate[rspec & RSPEC_RATE_MASK];
  308. }
  309. static u16 frametype(u32 rspec, u8 mimoframe)
  310. {
  311. if (is_mcs_rate(rspec))
  312. return mimoframe;
  313. return is_cck_rate(rspec) ? FT_CCK : FT_OFDM;
  314. }
  315. /* currently the best mechanism for determining SIFS is the band in use */
  316. static u16 get_sifs(struct brcms_band *band)
  317. {
  318. return band->bandtype == BRCM_BAND_5G ? APHY_SIFS_TIME :
  319. BPHY_SIFS_TIME;
  320. }
  321. /*
  322. * Detect Card removed.
  323. * Even checking an sbconfig register read will not false trigger when the core
  324. * is in reset it breaks CF address mechanism. Accessing gphy phyversion will
  325. * cause SB error if aphy is in reset on 4306B0-DB. Need a simple accessible
  326. * reg with fixed 0/1 pattern (some platforms return all 0).
  327. * If clocks are present, call the sb routine which will figure out if the
  328. * device is removed.
  329. */
  330. static bool brcms_deviceremoved(struct brcms_c_info *wlc)
  331. {
  332. u32 macctrl;
  333. if (!wlc->hw->clk)
  334. return ai_deviceremoved(wlc->hw->sih);
  335. macctrl = bcma_read32(wlc->hw->d11core,
  336. D11REGOFFS(maccontrol));
  337. return (macctrl & (MCTL_PSM_JMP_0 | MCTL_IHR_EN)) != MCTL_IHR_EN;
  338. }
  339. /* sum the individual fifo tx pending packet counts */
  340. static int brcms_txpktpendtot(struct brcms_c_info *wlc)
  341. {
  342. int i;
  343. int pending = 0;
  344. for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
  345. if (wlc->hw->di[i])
  346. pending += dma_txpending(wlc->hw->di[i]);
  347. return pending;
  348. }
  349. static bool brcms_is_mband_unlocked(struct brcms_c_info *wlc)
  350. {
  351. return wlc->pub->_nbands > 1 && !wlc->bandlocked;
  352. }
  353. static int brcms_chspec_bw(u16 chanspec)
  354. {
  355. if (CHSPEC_IS40(chanspec))
  356. return BRCMS_40_MHZ;
  357. if (CHSPEC_IS20(chanspec))
  358. return BRCMS_20_MHZ;
  359. return BRCMS_10_MHZ;
  360. }
  361. static void brcms_c_bsscfg_mfree(struct brcms_bss_cfg *cfg)
  362. {
  363. if (cfg == NULL)
  364. return;
  365. kfree(cfg->current_bss);
  366. kfree(cfg);
  367. }
  368. static void brcms_c_detach_mfree(struct brcms_c_info *wlc)
  369. {
  370. if (wlc == NULL)
  371. return;
  372. brcms_c_bsscfg_mfree(wlc->bsscfg);
  373. kfree(wlc->pub);
  374. kfree(wlc->modulecb);
  375. kfree(wlc->default_bss);
  376. kfree(wlc->protection);
  377. kfree(wlc->stf);
  378. kfree(wlc->bandstate[0]);
  379. if (wlc->corestate)
  380. kfree(wlc->corestate->macstat_snapshot);
  381. kfree(wlc->corestate);
  382. if (wlc->hw)
  383. kfree(wlc->hw->bandstate[0]);
  384. kfree(wlc->hw);
  385. if (wlc->beacon)
  386. dev_kfree_skb_any(wlc->beacon);
  387. if (wlc->probe_resp)
  388. dev_kfree_skb_any(wlc->probe_resp);
  389. kfree(wlc);
  390. }
  391. static struct brcms_bss_cfg *brcms_c_bsscfg_malloc(uint unit)
  392. {
  393. struct brcms_bss_cfg *cfg;
  394. cfg = kzalloc(sizeof(struct brcms_bss_cfg), GFP_ATOMIC);
  395. if (cfg == NULL)
  396. goto fail;
  397. cfg->current_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
  398. if (cfg->current_bss == NULL)
  399. goto fail;
  400. return cfg;
  401. fail:
  402. brcms_c_bsscfg_mfree(cfg);
  403. return NULL;
  404. }
  405. static struct brcms_c_info *
  406. brcms_c_attach_malloc(uint unit, uint *err, uint devid)
  407. {
  408. struct brcms_c_info *wlc;
  409. wlc = kzalloc(sizeof(struct brcms_c_info), GFP_ATOMIC);
  410. if (wlc == NULL) {
  411. *err = 1002;
  412. goto fail;
  413. }
  414. /* allocate struct brcms_c_pub state structure */
  415. wlc->pub = kzalloc(sizeof(struct brcms_pub), GFP_ATOMIC);
  416. if (wlc->pub == NULL) {
  417. *err = 1003;
  418. goto fail;
  419. }
  420. wlc->pub->wlc = wlc;
  421. /* allocate struct brcms_hardware state structure */
  422. wlc->hw = kzalloc(sizeof(struct brcms_hardware), GFP_ATOMIC);
  423. if (wlc->hw == NULL) {
  424. *err = 1005;
  425. goto fail;
  426. }
  427. wlc->hw->wlc = wlc;
  428. wlc->hw->bandstate[0] =
  429. kzalloc(sizeof(struct brcms_hw_band) * MAXBANDS, GFP_ATOMIC);
  430. if (wlc->hw->bandstate[0] == NULL) {
  431. *err = 1006;
  432. goto fail;
  433. } else {
  434. int i;
  435. for (i = 1; i < MAXBANDS; i++)
  436. wlc->hw->bandstate[i] = (struct brcms_hw_band *)
  437. ((unsigned long)wlc->hw->bandstate[0] +
  438. (sizeof(struct brcms_hw_band) * i));
  439. }
  440. wlc->modulecb =
  441. kzalloc(sizeof(struct modulecb) * BRCMS_MAXMODULES, GFP_ATOMIC);
  442. if (wlc->modulecb == NULL) {
  443. *err = 1009;
  444. goto fail;
  445. }
  446. wlc->default_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
  447. if (wlc->default_bss == NULL) {
  448. *err = 1010;
  449. goto fail;
  450. }
  451. wlc->bsscfg = brcms_c_bsscfg_malloc(unit);
  452. if (wlc->bsscfg == NULL) {
  453. *err = 1011;
  454. goto fail;
  455. }
  456. wlc->protection = kzalloc(sizeof(struct brcms_protection),
  457. GFP_ATOMIC);
  458. if (wlc->protection == NULL) {
  459. *err = 1016;
  460. goto fail;
  461. }
  462. wlc->stf = kzalloc(sizeof(struct brcms_stf), GFP_ATOMIC);
  463. if (wlc->stf == NULL) {
  464. *err = 1017;
  465. goto fail;
  466. }
  467. wlc->bandstate[0] =
  468. kzalloc(sizeof(struct brcms_band)*MAXBANDS, GFP_ATOMIC);
  469. if (wlc->bandstate[0] == NULL) {
  470. *err = 1025;
  471. goto fail;
  472. } else {
  473. int i;
  474. for (i = 1; i < MAXBANDS; i++)
  475. wlc->bandstate[i] = (struct brcms_band *)
  476. ((unsigned long)wlc->bandstate[0]
  477. + (sizeof(struct brcms_band)*i));
  478. }
  479. wlc->corestate = kzalloc(sizeof(struct brcms_core), GFP_ATOMIC);
  480. if (wlc->corestate == NULL) {
  481. *err = 1026;
  482. goto fail;
  483. }
  484. wlc->corestate->macstat_snapshot =
  485. kzalloc(sizeof(struct macstat), GFP_ATOMIC);
  486. if (wlc->corestate->macstat_snapshot == NULL) {
  487. *err = 1027;
  488. goto fail;
  489. }
  490. return wlc;
  491. fail:
  492. brcms_c_detach_mfree(wlc);
  493. return NULL;
  494. }
  495. /*
  496. * Update the slot timing for standard 11b/g (20us slots)
  497. * or shortslot 11g (9us slots)
  498. * The PSM needs to be suspended for this call.
  499. */
  500. static void brcms_b_update_slot_timing(struct brcms_hardware *wlc_hw,
  501. bool shortslot)
  502. {
  503. struct bcma_device *core = wlc_hw->d11core;
  504. if (shortslot) {
  505. /* 11g short slot: 11a timing */
  506. bcma_write16(core, D11REGOFFS(ifs_slot), 0x0207);
  507. brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, APHY_SLOT_TIME);
  508. } else {
  509. /* 11g long slot: 11b timing */
  510. bcma_write16(core, D11REGOFFS(ifs_slot), 0x0212);
  511. brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, BPHY_SLOT_TIME);
  512. }
  513. }
  514. /*
  515. * calculate frame duration of a given rate and length, return
  516. * time in usec unit
  517. */
  518. static uint brcms_c_calc_frame_time(struct brcms_c_info *wlc, u32 ratespec,
  519. u8 preamble_type, uint mac_len)
  520. {
  521. uint nsyms, dur = 0, Ndps, kNdps;
  522. uint rate = rspec2rate(ratespec);
  523. if (rate == 0) {
  524. brcms_err(wlc->hw->d11core, "wl%d: WAR: using rate of 1 mbps\n",
  525. wlc->pub->unit);
  526. rate = BRCM_RATE_1M;
  527. }
  528. if (is_mcs_rate(ratespec)) {
  529. uint mcs = ratespec & RSPEC_RATE_MASK;
  530. int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
  531. dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
  532. if (preamble_type == BRCMS_MM_PREAMBLE)
  533. dur += PREN_MM_EXT;
  534. /* 1000Ndbps = kbps * 4 */
  535. kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
  536. rspec_issgi(ratespec)) * 4;
  537. if (rspec_stc(ratespec) == 0)
  538. nsyms =
  539. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  540. APHY_TAIL_NBITS) * 1000, kNdps);
  541. else
  542. /* STBC needs to have even number of symbols */
  543. nsyms =
  544. 2 *
  545. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  546. APHY_TAIL_NBITS) * 1000, 2 * kNdps);
  547. dur += APHY_SYMBOL_TIME * nsyms;
  548. if (wlc->band->bandtype == BRCM_BAND_2G)
  549. dur += DOT11_OFDM_SIGNAL_EXTENSION;
  550. } else if (is_ofdm_rate(rate)) {
  551. dur = APHY_PREAMBLE_TIME;
  552. dur += APHY_SIGNAL_TIME;
  553. /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
  554. Ndps = rate * 2;
  555. /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
  556. nsyms =
  557. CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
  558. Ndps);
  559. dur += APHY_SYMBOL_TIME * nsyms;
  560. if (wlc->band->bandtype == BRCM_BAND_2G)
  561. dur += DOT11_OFDM_SIGNAL_EXTENSION;
  562. } else {
  563. /*
  564. * calc # bits * 2 so factor of 2 in rate (1/2 mbps)
  565. * will divide out
  566. */
  567. mac_len = mac_len * 8 * 2;
  568. /* calc ceiling of bits/rate = microseconds of air time */
  569. dur = (mac_len + rate - 1) / rate;
  570. if (preamble_type & BRCMS_SHORT_PREAMBLE)
  571. dur += BPHY_PLCP_SHORT_TIME;
  572. else
  573. dur += BPHY_PLCP_TIME;
  574. }
  575. return dur;
  576. }
  577. static void brcms_c_write_inits(struct brcms_hardware *wlc_hw,
  578. const struct d11init *inits)
  579. {
  580. struct bcma_device *core = wlc_hw->d11core;
  581. int i;
  582. uint offset;
  583. u16 size;
  584. u32 value;
  585. brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  586. for (i = 0; inits[i].addr != cpu_to_le16(0xffff); i++) {
  587. size = le16_to_cpu(inits[i].size);
  588. offset = le16_to_cpu(inits[i].addr);
  589. value = le32_to_cpu(inits[i].value);
  590. if (size == 2)
  591. bcma_write16(core, offset, value);
  592. else if (size == 4)
  593. bcma_write32(core, offset, value);
  594. else
  595. break;
  596. }
  597. }
  598. static void brcms_c_write_mhf(struct brcms_hardware *wlc_hw, u16 *mhfs)
  599. {
  600. u8 idx;
  601. u16 addr[] = {
  602. M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
  603. M_HOST_FLAGS5
  604. };
  605. for (idx = 0; idx < MHFMAX; idx++)
  606. brcms_b_write_shm(wlc_hw, addr[idx], mhfs[idx]);
  607. }
  608. static void brcms_c_ucode_bsinit(struct brcms_hardware *wlc_hw)
  609. {
  610. struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
  611. /* init microcode host flags */
  612. brcms_c_write_mhf(wlc_hw, wlc_hw->band->mhfs);
  613. /* do band-specific ucode IHR, SHM, and SCR inits */
  614. if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
  615. if (BRCMS_ISNPHY(wlc_hw->band))
  616. brcms_c_write_inits(wlc_hw, ucode->d11n0bsinitvals16);
  617. else
  618. brcms_err(wlc_hw->d11core,
  619. "%s: wl%d: unsupported phy in corerev %d\n",
  620. __func__, wlc_hw->unit,
  621. wlc_hw->corerev);
  622. } else {
  623. if (D11REV_IS(wlc_hw->corerev, 24)) {
  624. if (BRCMS_ISLCNPHY(wlc_hw->band))
  625. brcms_c_write_inits(wlc_hw,
  626. ucode->d11lcn0bsinitvals24);
  627. else
  628. brcms_err(wlc_hw->d11core,
  629. "%s: wl%d: unsupported phy in core rev %d\n",
  630. __func__, wlc_hw->unit,
  631. wlc_hw->corerev);
  632. } else {
  633. brcms_err(wlc_hw->d11core,
  634. "%s: wl%d: unsupported corerev %d\n",
  635. __func__, wlc_hw->unit, wlc_hw->corerev);
  636. }
  637. }
  638. }
  639. static void brcms_b_core_ioctl(struct brcms_hardware *wlc_hw, u32 m, u32 v)
  640. {
  641. struct bcma_device *core = wlc_hw->d11core;
  642. u32 ioctl = bcma_aread32(core, BCMA_IOCTL) & ~m;
  643. bcma_awrite32(core, BCMA_IOCTL, ioctl | v);
  644. }
  645. static void brcms_b_core_phy_clk(struct brcms_hardware *wlc_hw, bool clk)
  646. {
  647. brcms_dbg_info(wlc_hw->d11core, "wl%d: clk %d\n", wlc_hw->unit, clk);
  648. wlc_hw->phyclk = clk;
  649. if (OFF == clk) { /* clear gmode bit, put phy into reset */
  650. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC | SICF_GMODE),
  651. (SICF_PRST | SICF_FGC));
  652. udelay(1);
  653. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_PRST);
  654. udelay(1);
  655. } else { /* take phy out of reset */
  656. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_FGC);
  657. udelay(1);
  658. brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
  659. udelay(1);
  660. }
  661. }
  662. /* low-level band switch utility routine */
  663. static void brcms_c_setxband(struct brcms_hardware *wlc_hw, uint bandunit)
  664. {
  665. brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
  666. bandunit);
  667. wlc_hw->band = wlc_hw->bandstate[bandunit];
  668. /*
  669. * BMAC_NOTE:
  670. * until we eliminate need for wlc->band refs in low level code
  671. */
  672. wlc_hw->wlc->band = wlc_hw->wlc->bandstate[bandunit];
  673. /* set gmode core flag */
  674. if (wlc_hw->sbclk && !wlc_hw->noreset) {
  675. u32 gmode = 0;
  676. if (bandunit == 0)
  677. gmode = SICF_GMODE;
  678. brcms_b_core_ioctl(wlc_hw, SICF_GMODE, gmode);
  679. }
  680. }
  681. /* switch to new band but leave it inactive */
  682. static u32 brcms_c_setband_inact(struct brcms_c_info *wlc, uint bandunit)
  683. {
  684. struct brcms_hardware *wlc_hw = wlc->hw;
  685. u32 macintmask;
  686. u32 macctrl;
  687. brcms_dbg_mac80211(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  688. macctrl = bcma_read32(wlc_hw->d11core,
  689. D11REGOFFS(maccontrol));
  690. WARN_ON((macctrl & MCTL_EN_MAC) != 0);
  691. /* disable interrupts */
  692. macintmask = brcms_intrsoff(wlc->wl);
  693. /* radio off */
  694. wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
  695. brcms_b_core_phy_clk(wlc_hw, OFF);
  696. brcms_c_setxband(wlc_hw, bandunit);
  697. return macintmask;
  698. }
  699. /* process an individual struct tx_status */
  700. static bool
  701. brcms_c_dotxstatus(struct brcms_c_info *wlc, struct tx_status *txs)
  702. {
  703. struct sk_buff *p = NULL;
  704. uint queue = NFIFO;
  705. struct dma_pub *dma = NULL;
  706. struct d11txh *txh = NULL;
  707. struct scb *scb = NULL;
  708. bool free_pdu;
  709. int tx_rts, tx_frame_count, tx_rts_count;
  710. uint totlen, supr_status;
  711. bool lastframe;
  712. struct ieee80211_hdr *h;
  713. u16 mcl;
  714. struct ieee80211_tx_info *tx_info;
  715. struct ieee80211_tx_rate *txrate;
  716. int i;
  717. bool fatal = true;
  718. trace_brcms_txstatus(&wlc->hw->d11core->dev, txs->framelen,
  719. txs->frameid, txs->status, txs->lasttxtime,
  720. txs->sequence, txs->phyerr, txs->ackphyrxsh);
  721. /* discard intermediate indications for ucode with one legitimate case:
  722. * e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
  723. * but the subsequent tx of DATA failed. so it will start rts/cts
  724. * from the beginning (resetting the rts transmission count)
  725. */
  726. if (!(txs->status & TX_STATUS_AMPDU)
  727. && (txs->status & TX_STATUS_INTERMEDIATE)) {
  728. brcms_dbg_tx(wlc->hw->d11core, "INTERMEDIATE but not AMPDU\n");
  729. fatal = false;
  730. goto out;
  731. }
  732. queue = txs->frameid & TXFID_QUEUE_MASK;
  733. if (queue >= NFIFO) {
  734. brcms_err(wlc->hw->d11core, "queue %u >= NFIFO\n", queue);
  735. goto out;
  736. }
  737. dma = wlc->hw->di[queue];
  738. p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);
  739. if (p == NULL) {
  740. brcms_err(wlc->hw->d11core, "dma_getnexttxp returned null!\n");
  741. goto out;
  742. }
  743. txh = (struct d11txh *) (p->data);
  744. mcl = le16_to_cpu(txh->MacTxControlLow);
  745. if (txs->phyerr)
  746. brcms_dbg_tx(wlc->hw->d11core, "phyerr 0x%x, rate 0x%x\n",
  747. txs->phyerr, txh->MainRates);
  748. if (txs->frameid != le16_to_cpu(txh->TxFrameID)) {
  749. brcms_err(wlc->hw->d11core, "frameid != txh->TxFrameID\n");
  750. goto out;
  751. }
  752. tx_info = IEEE80211_SKB_CB(p);
  753. h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
  754. if (tx_info->rate_driver_data[0])
  755. scb = &wlc->pri_scb;
  756. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  757. brcms_c_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
  758. fatal = false;
  759. goto out;
  760. }
  761. /*
  762. * brcms_c_ampdu_dotxstatus() will trace tx descriptors for AMPDU
  763. * frames; this traces them for the rest.
  764. */
  765. trace_brcms_txdesc(&wlc->hw->d11core->dev, txh, sizeof(*txh));
  766. supr_status = txs->status & TX_STATUS_SUPR_MASK;
  767. if (supr_status == TX_STATUS_SUPR_BADCH) {
  768. unsigned xfts = le16_to_cpu(txh->XtraFrameTypes);
  769. brcms_dbg_tx(wlc->hw->d11core,
  770. "Pkt tx suppressed, dest chan %u, current %d\n",
  771. (xfts >> XFTS_CHANNEL_SHIFT) & 0xff,
  772. CHSPEC_CHANNEL(wlc->default_bss->chanspec));
  773. }
  774. tx_rts = le16_to_cpu(txh->MacTxControlLow) & TXC_SENDRTS;
  775. tx_frame_count =
  776. (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
  777. tx_rts_count =
  778. (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
  779. lastframe = !ieee80211_has_morefrags(h->frame_control);
  780. if (!lastframe) {
  781. brcms_err(wlc->hw->d11core, "Not last frame!\n");
  782. } else {
  783. /*
  784. * Set information to be consumed by Minstrel ht.
  785. *
  786. * The "fallback limit" is the number of tx attempts a given
  787. * MPDU is sent at the "primary" rate. Tx attempts beyond that
  788. * limit are sent at the "secondary" rate.
  789. * A 'short frame' does not exceed RTS treshold.
  790. */
  791. u16 sfbl, /* Short Frame Rate Fallback Limit */
  792. lfbl, /* Long Frame Rate Fallback Limit */
  793. fbl;
  794. if (queue < IEEE80211_NUM_ACS) {
  795. sfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
  796. EDCF_SFB);
  797. lfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
  798. EDCF_LFB);
  799. } else {
  800. sfbl = wlc->SFBL;
  801. lfbl = wlc->LFBL;
  802. }
  803. txrate = tx_info->status.rates;
  804. if (txrate[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  805. fbl = lfbl;
  806. else
  807. fbl = sfbl;
  808. ieee80211_tx_info_clear_status(tx_info);
  809. if ((tx_frame_count > fbl) && (txrate[1].idx >= 0)) {
  810. /*
  811. * rate selection requested a fallback rate
  812. * and we used it
  813. */
  814. txrate[0].count = fbl;
  815. txrate[1].count = tx_frame_count - fbl;
  816. } else {
  817. /*
  818. * rate selection did not request fallback rate, or
  819. * we didn't need it
  820. */
  821. txrate[0].count = tx_frame_count;
  822. /*
  823. * rc80211_minstrel.c:minstrel_tx_status() expects
  824. * unused rates to be marked with idx = -1
  825. */
  826. txrate[1].idx = -1;
  827. txrate[1].count = 0;
  828. }
  829. /* clear the rest of the rates */
  830. for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
  831. txrate[i].idx = -1;
  832. txrate[i].count = 0;
  833. }
  834. if (txs->status & TX_STATUS_ACK_RCV)
  835. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  836. }
  837. totlen = p->len;
  838. free_pdu = true;
  839. if (lastframe) {
  840. /* remove PLCP & Broadcom tx descriptor header */
  841. skb_pull(p, D11_PHY_HDR_LEN);
  842. skb_pull(p, D11_TXH_LEN);
  843. ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
  844. } else {
  845. brcms_err(wlc->hw->d11core,
  846. "%s: Not last frame => not calling tx_status\n",
  847. __func__);
  848. }
  849. fatal = false;
  850. out:
  851. if (fatal) {
  852. if (txh)
  853. trace_brcms_txdesc(&wlc->hw->d11core->dev, txh,
  854. sizeof(*txh));
  855. brcmu_pkt_buf_free_skb(p);
  856. }
  857. if (dma && queue < NFIFO) {
  858. u16 ac_queue = brcms_fifo_to_ac(queue);
  859. if (dma->txavail > TX_HEADROOM && queue < TX_BCMC_FIFO &&
  860. ieee80211_queue_stopped(wlc->pub->ieee_hw, ac_queue))
  861. ieee80211_wake_queue(wlc->pub->ieee_hw, ac_queue);
  862. dma_kick_tx(dma);
  863. }
  864. return fatal;
  865. }
  866. /* process tx completion events in BMAC
  867. * Return true if more tx status need to be processed. false otherwise.
  868. */
  869. static bool
  870. brcms_b_txstatus(struct brcms_hardware *wlc_hw, bool bound, bool *fatal)
  871. {
  872. struct bcma_device *core;
  873. struct tx_status txstatus, *txs;
  874. u32 s1, s2;
  875. uint n = 0;
  876. /*
  877. * Param 'max_tx_num' indicates max. # tx status to process before
  878. * break out.
  879. */
  880. uint max_tx_num = bound ? TXSBND : -1;
  881. txs = &txstatus;
  882. core = wlc_hw->d11core;
  883. *fatal = false;
  884. while (n < max_tx_num) {
  885. s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
  886. if (s1 == 0xffffffff) {
  887. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  888. __func__);
  889. *fatal = true;
  890. return false;
  891. }
  892. /* only process when valid */
  893. if (!(s1 & TXS_V))
  894. break;
  895. s2 = bcma_read32(core, D11REGOFFS(frmtxstatus2));
  896. txs->status = s1 & TXS_STATUS_MASK;
  897. txs->frameid = (s1 & TXS_FID_MASK) >> TXS_FID_SHIFT;
  898. txs->sequence = s2 & TXS_SEQ_MASK;
  899. txs->phyerr = (s2 & TXS_PTX_MASK) >> TXS_PTX_SHIFT;
  900. txs->lasttxtime = 0;
  901. *fatal = brcms_c_dotxstatus(wlc_hw->wlc, txs);
  902. if (*fatal == true)
  903. return false;
  904. n++;
  905. }
  906. return n >= max_tx_num;
  907. }
  908. static void brcms_c_tbtt(struct brcms_c_info *wlc)
  909. {
  910. if (wlc->bsscfg->type == BRCMS_TYPE_ADHOC)
  911. /*
  912. * DirFrmQ is now valid...defer setting until end
  913. * of ATIM window
  914. */
  915. wlc->qvalid |= MCMD_DIRFRMQVAL;
  916. }
  917. /* set initial host flags value */
  918. static void
  919. brcms_c_mhfdef(struct brcms_c_info *wlc, u16 *mhfs, u16 mhf2_init)
  920. {
  921. struct brcms_hardware *wlc_hw = wlc->hw;
  922. memset(mhfs, 0, MHFMAX * sizeof(u16));
  923. mhfs[MHF2] |= mhf2_init;
  924. /* prohibit use of slowclock on multifunction boards */
  925. if (wlc_hw->boardflags & BFL_NOPLLDOWN)
  926. mhfs[MHF1] |= MHF1_FORCEFASTCLK;
  927. if (BRCMS_ISNPHY(wlc_hw->band) && NREV_LT(wlc_hw->band->phyrev, 2)) {
  928. mhfs[MHF2] |= MHF2_NPHY40MHZ_WAR;
  929. mhfs[MHF1] |= MHF1_IQSWAP_WAR;
  930. }
  931. }
  932. static uint
  933. dmareg(uint direction, uint fifonum)
  934. {
  935. if (direction == DMA_TX)
  936. return offsetof(struct d11regs, fifo64regs[fifonum].dmaxmt);
  937. return offsetof(struct d11regs, fifo64regs[fifonum].dmarcv);
  938. }
  939. static bool brcms_b_attach_dmapio(struct brcms_c_info *wlc, uint j, bool wme)
  940. {
  941. uint i;
  942. char name[8];
  943. /*
  944. * ucode host flag 2 needed for pio mode, independent of band and fifo
  945. */
  946. u16 pio_mhf2 = 0;
  947. struct brcms_hardware *wlc_hw = wlc->hw;
  948. uint unit = wlc_hw->unit;
  949. /* name and offsets for dma_attach */
  950. snprintf(name, sizeof(name), "wl%d", unit);
  951. if (wlc_hw->di[0] == NULL) { /* Init FIFOs */
  952. int dma_attach_err = 0;
  953. /*
  954. * FIFO 0
  955. * TX: TX_AC_BK_FIFO (TX AC Background data packets)
  956. * RX: RX_FIFO (RX data packets)
  957. */
  958. wlc_hw->di[0] = dma_attach(name, wlc,
  959. (wme ? dmareg(DMA_TX, 0) : 0),
  960. dmareg(DMA_RX, 0),
  961. (wme ? NTXD : 0), NRXD,
  962. RXBUFSZ, -1, NRXBUFPOST,
  963. BRCMS_HWRXOFF);
  964. dma_attach_err |= (NULL == wlc_hw->di[0]);
  965. /*
  966. * FIFO 1
  967. * TX: TX_AC_BE_FIFO (TX AC Best-Effort data packets)
  968. * (legacy) TX_DATA_FIFO (TX data packets)
  969. * RX: UNUSED
  970. */
  971. wlc_hw->di[1] = dma_attach(name, wlc,
  972. dmareg(DMA_TX, 1), 0,
  973. NTXD, 0, 0, -1, 0, 0);
  974. dma_attach_err |= (NULL == wlc_hw->di[1]);
  975. /*
  976. * FIFO 2
  977. * TX: TX_AC_VI_FIFO (TX AC Video data packets)
  978. * RX: UNUSED
  979. */
  980. wlc_hw->di[2] = dma_attach(name, wlc,
  981. dmareg(DMA_TX, 2), 0,
  982. NTXD, 0, 0, -1, 0, 0);
  983. dma_attach_err |= (NULL == wlc_hw->di[2]);
  984. /*
  985. * FIFO 3
  986. * TX: TX_AC_VO_FIFO (TX AC Voice data packets)
  987. * (legacy) TX_CTL_FIFO (TX control & mgmt packets)
  988. */
  989. wlc_hw->di[3] = dma_attach(name, wlc,
  990. dmareg(DMA_TX, 3),
  991. 0, NTXD, 0, 0, -1,
  992. 0, 0);
  993. dma_attach_err |= (NULL == wlc_hw->di[3]);
  994. /* Cleaner to leave this as if with AP defined */
  995. if (dma_attach_err) {
  996. brcms_err(wlc_hw->d11core,
  997. "wl%d: wlc_attach: dma_attach failed\n",
  998. unit);
  999. return false;
  1000. }
  1001. /* get pointer to dma engine tx flow control variable */
  1002. for (i = 0; i < NFIFO; i++)
  1003. if (wlc_hw->di[i])
  1004. wlc_hw->txavail[i] =
  1005. (uint *) dma_getvar(wlc_hw->di[i],
  1006. "&txavail");
  1007. }
  1008. /* initial ucode host flags */
  1009. brcms_c_mhfdef(wlc, wlc_hw->band->mhfs, pio_mhf2);
  1010. return true;
  1011. }
  1012. static void brcms_b_detach_dmapio(struct brcms_hardware *wlc_hw)
  1013. {
  1014. uint j;
  1015. for (j = 0; j < NFIFO; j++) {
  1016. if (wlc_hw->di[j]) {
  1017. dma_detach(wlc_hw->di[j]);
  1018. wlc_hw->di[j] = NULL;
  1019. }
  1020. }
  1021. }
  1022. /*
  1023. * Initialize brcms_c_info default values ...
  1024. * may get overrides later in this function
  1025. * BMAC_NOTES, move low out and resolve the dangling ones
  1026. */
  1027. static void brcms_b_info_init(struct brcms_hardware *wlc_hw)
  1028. {
  1029. struct brcms_c_info *wlc = wlc_hw->wlc;
  1030. /* set default sw macintmask value */
  1031. wlc->defmacintmask = DEF_MACINTMASK;
  1032. /* various 802.11g modes */
  1033. wlc_hw->shortslot = false;
  1034. wlc_hw->SFBL = RETRY_SHORT_FB;
  1035. wlc_hw->LFBL = RETRY_LONG_FB;
  1036. /* default mac retry limits */
  1037. wlc_hw->SRL = RETRY_SHORT_DEF;
  1038. wlc_hw->LRL = RETRY_LONG_DEF;
  1039. wlc_hw->chanspec = ch20mhz_chspec(1);
  1040. }
  1041. static void brcms_b_wait_for_wake(struct brcms_hardware *wlc_hw)
  1042. {
  1043. /* delay before first read of ucode state */
  1044. udelay(40);
  1045. /* wait until ucode is no longer asleep */
  1046. SPINWAIT((brcms_b_read_shm(wlc_hw, M_UCODE_DBGST) ==
  1047. DBGST_ASLEEP), wlc_hw->wlc->fastpwrup_dly);
  1048. }
  1049. /* control chip clock to save power, enable dynamic clock or force fast clock */
  1050. static void brcms_b_clkctl_clk(struct brcms_hardware *wlc_hw, enum bcma_clkmode mode)
  1051. {
  1052. if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU) {
  1053. /* new chips with PMU, CCS_FORCEHT will distribute the HT clock
  1054. * on backplane, but mac core will still run on ALP(not HT) when
  1055. * it enters powersave mode, which means the FCA bit may not be
  1056. * set. Should wakeup mac if driver wants it to run on HT.
  1057. */
  1058. if (wlc_hw->clk) {
  1059. if (mode == BCMA_CLKMODE_FAST) {
  1060. bcma_set32(wlc_hw->d11core,
  1061. D11REGOFFS(clk_ctl_st),
  1062. CCS_FORCEHT);
  1063. udelay(64);
  1064. SPINWAIT(
  1065. ((bcma_read32(wlc_hw->d11core,
  1066. D11REGOFFS(clk_ctl_st)) &
  1067. CCS_HTAVAIL) == 0),
  1068. PMU_MAX_TRANSITION_DLY);
  1069. WARN_ON(!(bcma_read32(wlc_hw->d11core,
  1070. D11REGOFFS(clk_ctl_st)) &
  1071. CCS_HTAVAIL));
  1072. } else {
  1073. if ((ai_get_pmurev(wlc_hw->sih) == 0) &&
  1074. (bcma_read32(wlc_hw->d11core,
  1075. D11REGOFFS(clk_ctl_st)) &
  1076. (CCS_FORCEHT | CCS_HTAREQ)))
  1077. SPINWAIT(
  1078. ((bcma_read32(wlc_hw->d11core,
  1079. offsetof(struct d11regs,
  1080. clk_ctl_st)) &
  1081. CCS_HTAVAIL) == 0),
  1082. PMU_MAX_TRANSITION_DLY);
  1083. bcma_mask32(wlc_hw->d11core,
  1084. D11REGOFFS(clk_ctl_st),
  1085. ~CCS_FORCEHT);
  1086. }
  1087. }
  1088. wlc_hw->forcefastclk = (mode == BCMA_CLKMODE_FAST);
  1089. } else {
  1090. /* old chips w/o PMU, force HT through cc,
  1091. * then use FCA to verify mac is running fast clock
  1092. */
  1093. wlc_hw->forcefastclk = ai_clkctl_cc(wlc_hw->sih, mode);
  1094. /* check fast clock is available (if core is not in reset) */
  1095. if (wlc_hw->forcefastclk && wlc_hw->clk)
  1096. WARN_ON(!(bcma_aread32(wlc_hw->d11core, BCMA_IOST) &
  1097. SISF_FCLKA));
  1098. /*
  1099. * keep the ucode wake bit on if forcefastclk is on since we
  1100. * do not want ucode to put us back to slow clock when it dozes
  1101. * for PM mode. Code below matches the wake override bit with
  1102. * current forcefastclk state. Only setting bit in wake_override
  1103. * instead of waking ucode immediately since old code had this
  1104. * behavior. Older code set wlc->forcefastclk but only had the
  1105. * wake happen if the wakup_ucode work (protected by an up
  1106. * check) was executed just below.
  1107. */
  1108. if (wlc_hw->forcefastclk)
  1109. mboolset(wlc_hw->wake_override,
  1110. BRCMS_WAKE_OVERRIDE_FORCEFAST);
  1111. else
  1112. mboolclr(wlc_hw->wake_override,
  1113. BRCMS_WAKE_OVERRIDE_FORCEFAST);
  1114. }
  1115. }
  1116. /* set or clear ucode host flag bits
  1117. * it has an optimization for no-change write
  1118. * it only writes through shared memory when the core has clock;
  1119. * pre-CLK changes should use wlc_write_mhf to get around the optimization
  1120. *
  1121. *
  1122. * bands values are: BRCM_BAND_AUTO <--- Current band only
  1123. * BRCM_BAND_5G <--- 5G band only
  1124. * BRCM_BAND_2G <--- 2G band only
  1125. * BRCM_BAND_ALL <--- All bands
  1126. */
  1127. void
  1128. brcms_b_mhf(struct brcms_hardware *wlc_hw, u8 idx, u16 mask, u16 val,
  1129. int bands)
  1130. {
  1131. u16 save;
  1132. u16 addr[MHFMAX] = {
  1133. M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
  1134. M_HOST_FLAGS5
  1135. };
  1136. struct brcms_hw_band *band;
  1137. if ((val & ~mask) || idx >= MHFMAX)
  1138. return; /* error condition */
  1139. switch (bands) {
  1140. /* Current band only or all bands,
  1141. * then set the band to current band
  1142. */
  1143. case BRCM_BAND_AUTO:
  1144. case BRCM_BAND_ALL:
  1145. band = wlc_hw->band;
  1146. break;
  1147. case BRCM_BAND_5G:
  1148. band = wlc_hw->bandstate[BAND_5G_INDEX];
  1149. break;
  1150. case BRCM_BAND_2G:
  1151. band = wlc_hw->bandstate[BAND_2G_INDEX];
  1152. break;
  1153. default:
  1154. band = NULL; /* error condition */
  1155. }
  1156. if (band) {
  1157. save = band->mhfs[idx];
  1158. band->mhfs[idx] = (band->mhfs[idx] & ~mask) | val;
  1159. /* optimization: only write through if changed, and
  1160. * changed band is the current band
  1161. */
  1162. if (wlc_hw->clk && (band->mhfs[idx] != save)
  1163. && (band == wlc_hw->band))
  1164. brcms_b_write_shm(wlc_hw, addr[idx],
  1165. (u16) band->mhfs[idx]);
  1166. }
  1167. if (bands == BRCM_BAND_ALL) {
  1168. wlc_hw->bandstate[0]->mhfs[idx] =
  1169. (wlc_hw->bandstate[0]->mhfs[idx] & ~mask) | val;
  1170. wlc_hw->bandstate[1]->mhfs[idx] =
  1171. (wlc_hw->bandstate[1]->mhfs[idx] & ~mask) | val;
  1172. }
  1173. }
  1174. /* set the maccontrol register to desired reset state and
  1175. * initialize the sw cache of the register
  1176. */
  1177. static void brcms_c_mctrl_reset(struct brcms_hardware *wlc_hw)
  1178. {
  1179. /* IHR accesses are always enabled, PSM disabled, HPS off and WAKE on */
  1180. wlc_hw->maccontrol = 0;
  1181. wlc_hw->suspended_fifos = 0;
  1182. wlc_hw->wake_override = 0;
  1183. wlc_hw->mute_override = 0;
  1184. brcms_b_mctrl(wlc_hw, ~0, MCTL_IHR_EN | MCTL_WAKE);
  1185. }
  1186. /*
  1187. * write the software state of maccontrol and
  1188. * overrides to the maccontrol register
  1189. */
  1190. static void brcms_c_mctrl_write(struct brcms_hardware *wlc_hw)
  1191. {
  1192. u32 maccontrol = wlc_hw->maccontrol;
  1193. /* OR in the wake bit if overridden */
  1194. if (wlc_hw->wake_override)
  1195. maccontrol |= MCTL_WAKE;
  1196. /* set AP and INFRA bits for mute if needed */
  1197. if (wlc_hw->mute_override) {
  1198. maccontrol &= ~(MCTL_AP);
  1199. maccontrol |= MCTL_INFRA;
  1200. }
  1201. bcma_write32(wlc_hw->d11core, D11REGOFFS(maccontrol),
  1202. maccontrol);
  1203. }
  1204. /* set or clear maccontrol bits */
  1205. void brcms_b_mctrl(struct brcms_hardware *wlc_hw, u32 mask, u32 val)
  1206. {
  1207. u32 maccontrol;
  1208. u32 new_maccontrol;
  1209. if (val & ~mask)
  1210. return; /* error condition */
  1211. maccontrol = wlc_hw->maccontrol;
  1212. new_maccontrol = (maccontrol & ~mask) | val;
  1213. /* if the new maccontrol value is the same as the old, nothing to do */
  1214. if (new_maccontrol == maccontrol)
  1215. return;
  1216. /* something changed, cache the new value */
  1217. wlc_hw->maccontrol = new_maccontrol;
  1218. /* write the new values with overrides applied */
  1219. brcms_c_mctrl_write(wlc_hw);
  1220. }
  1221. void brcms_c_ucode_wake_override_set(struct brcms_hardware *wlc_hw,
  1222. u32 override_bit)
  1223. {
  1224. if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE)) {
  1225. mboolset(wlc_hw->wake_override, override_bit);
  1226. return;
  1227. }
  1228. mboolset(wlc_hw->wake_override, override_bit);
  1229. brcms_c_mctrl_write(wlc_hw);
  1230. brcms_b_wait_for_wake(wlc_hw);
  1231. }
  1232. void brcms_c_ucode_wake_override_clear(struct brcms_hardware *wlc_hw,
  1233. u32 override_bit)
  1234. {
  1235. mboolclr(wlc_hw->wake_override, override_bit);
  1236. if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE))
  1237. return;
  1238. brcms_c_mctrl_write(wlc_hw);
  1239. }
  1240. /* When driver needs ucode to stop beaconing, it has to make sure that
  1241. * MCTL_AP is clear and MCTL_INFRA is set
  1242. * Mode MCTL_AP MCTL_INFRA
  1243. * AP 1 1
  1244. * STA 0 1 <--- This will ensure no beacons
  1245. * IBSS 0 0
  1246. */
  1247. static void brcms_c_ucode_mute_override_set(struct brcms_hardware *wlc_hw)
  1248. {
  1249. wlc_hw->mute_override = 1;
  1250. /* if maccontrol already has AP == 0 and INFRA == 1 without this
  1251. * override, then there is no change to write
  1252. */
  1253. if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
  1254. return;
  1255. brcms_c_mctrl_write(wlc_hw);
  1256. }
  1257. /* Clear the override on AP and INFRA bits */
  1258. static void brcms_c_ucode_mute_override_clear(struct brcms_hardware *wlc_hw)
  1259. {
  1260. if (wlc_hw->mute_override == 0)
  1261. return;
  1262. wlc_hw->mute_override = 0;
  1263. /* if maccontrol already has AP == 0 and INFRA == 1 without this
  1264. * override, then there is no change to write
  1265. */
  1266. if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
  1267. return;
  1268. brcms_c_mctrl_write(wlc_hw);
  1269. }
  1270. /*
  1271. * Write a MAC address to the given match reg offset in the RXE match engine.
  1272. */
  1273. static void
  1274. brcms_b_set_addrmatch(struct brcms_hardware *wlc_hw, int match_reg_offset,
  1275. const u8 *addr)
  1276. {
  1277. struct bcma_device *core = wlc_hw->d11core;
  1278. u16 mac_l;
  1279. u16 mac_m;
  1280. u16 mac_h;
  1281. brcms_dbg_rx(core, "wl%d: brcms_b_set_addrmatch\n", wlc_hw->unit);
  1282. mac_l = addr[0] | (addr[1] << 8);
  1283. mac_m = addr[2] | (addr[3] << 8);
  1284. mac_h = addr[4] | (addr[5] << 8);
  1285. /* enter the MAC addr into the RXE match registers */
  1286. bcma_write16(core, D11REGOFFS(rcm_ctl),
  1287. RCM_INC_DATA | match_reg_offset);
  1288. bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_l);
  1289. bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_m);
  1290. bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_h);
  1291. }
  1292. void
  1293. brcms_b_write_template_ram(struct brcms_hardware *wlc_hw, int offset, int len,
  1294. void *buf)
  1295. {
  1296. struct bcma_device *core = wlc_hw->d11core;
  1297. u32 word;
  1298. __le32 word_le;
  1299. __be32 word_be;
  1300. bool be_bit;
  1301. brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
  1302. bcma_write32(core, D11REGOFFS(tplatewrptr), offset);
  1303. /* if MCTL_BIGEND bit set in mac control register,
  1304. * the chip swaps data in fifo, as well as data in
  1305. * template ram
  1306. */
  1307. be_bit = (bcma_read32(core, D11REGOFFS(maccontrol)) & MCTL_BIGEND) != 0;
  1308. while (len > 0) {
  1309. memcpy(&word, buf, sizeof(u32));
  1310. if (be_bit) {
  1311. word_be = cpu_to_be32(word);
  1312. word = *(u32 *)&word_be;
  1313. } else {
  1314. word_le = cpu_to_le32(word);
  1315. word = *(u32 *)&word_le;
  1316. }
  1317. bcma_write32(core, D11REGOFFS(tplatewrdata), word);
  1318. buf = (u8 *) buf + sizeof(u32);
  1319. len -= sizeof(u32);
  1320. }
  1321. }
  1322. static void brcms_b_set_cwmin(struct brcms_hardware *wlc_hw, u16 newmin)
  1323. {
  1324. wlc_hw->band->CWmin = newmin;
  1325. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  1326. OBJADDR_SCR_SEL | S_DOT11_CWMIN);
  1327. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  1328. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmin);
  1329. }
  1330. static void brcms_b_set_cwmax(struct brcms_hardware *wlc_hw, u16 newmax)
  1331. {
  1332. wlc_hw->band->CWmax = newmax;
  1333. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  1334. OBJADDR_SCR_SEL | S_DOT11_CWMAX);
  1335. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  1336. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmax);
  1337. }
  1338. void brcms_b_bw_set(struct brcms_hardware *wlc_hw, u16 bw)
  1339. {
  1340. bool fastclk;
  1341. /* request FAST clock if not on */
  1342. fastclk = wlc_hw->forcefastclk;
  1343. if (!fastclk)
  1344. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  1345. wlc_phy_bw_state_set(wlc_hw->band->pi, bw);
  1346. brcms_b_phy_reset(wlc_hw);
  1347. wlc_phy_init(wlc_hw->band->pi, wlc_phy_chanspec_get(wlc_hw->band->pi));
  1348. /* restore the clk */
  1349. if (!fastclk)
  1350. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
  1351. }
  1352. static void brcms_b_upd_synthpu(struct brcms_hardware *wlc_hw)
  1353. {
  1354. u16 v;
  1355. struct brcms_c_info *wlc = wlc_hw->wlc;
  1356. /* update SYNTHPU_DLY */
  1357. if (BRCMS_ISLCNPHY(wlc->band))
  1358. v = SYNTHPU_DLY_LPPHY_US;
  1359. else if (BRCMS_ISNPHY(wlc->band) && (NREV_GE(wlc->band->phyrev, 3)))
  1360. v = SYNTHPU_DLY_NPHY_US;
  1361. else
  1362. v = SYNTHPU_DLY_BPHY_US;
  1363. brcms_b_write_shm(wlc_hw, M_SYNTHPU_DLY, v);
  1364. }
  1365. static void brcms_c_ucode_txant_set(struct brcms_hardware *wlc_hw)
  1366. {
  1367. u16 phyctl;
  1368. u16 phytxant = wlc_hw->bmac_phytxant;
  1369. u16 mask = PHY_TXC_ANT_MASK;
  1370. /* set the Probe Response frame phy control word */
  1371. phyctl = brcms_b_read_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS);
  1372. phyctl = (phyctl & ~mask) | phytxant;
  1373. brcms_b_write_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS, phyctl);
  1374. /* set the Response (ACK/CTS) frame phy control word */
  1375. phyctl = brcms_b_read_shm(wlc_hw, M_RSP_PCTLWD);
  1376. phyctl = (phyctl & ~mask) | phytxant;
  1377. brcms_b_write_shm(wlc_hw, M_RSP_PCTLWD, phyctl);
  1378. }
  1379. static u16 brcms_b_ofdm_ratetable_offset(struct brcms_hardware *wlc_hw,
  1380. u8 rate)
  1381. {
  1382. uint i;
  1383. u8 plcp_rate = 0;
  1384. struct plcp_signal_rate_lookup {
  1385. u8 rate;
  1386. u8 signal_rate;
  1387. };
  1388. /* OFDM RATE sub-field of PLCP SIGNAL field, per 802.11 sec 17.3.4.1 */
  1389. const struct plcp_signal_rate_lookup rate_lookup[] = {
  1390. {BRCM_RATE_6M, 0xB},
  1391. {BRCM_RATE_9M, 0xF},
  1392. {BRCM_RATE_12M, 0xA},
  1393. {BRCM_RATE_18M, 0xE},
  1394. {BRCM_RATE_24M, 0x9},
  1395. {BRCM_RATE_36M, 0xD},
  1396. {BRCM_RATE_48M, 0x8},
  1397. {BRCM_RATE_54M, 0xC}
  1398. };
  1399. for (i = 0; i < ARRAY_SIZE(rate_lookup); i++) {
  1400. if (rate == rate_lookup[i].rate) {
  1401. plcp_rate = rate_lookup[i].signal_rate;
  1402. break;
  1403. }
  1404. }
  1405. /* Find the SHM pointer to the rate table entry by looking in the
  1406. * Direct-map Table
  1407. */
  1408. return 2 * brcms_b_read_shm(wlc_hw, M_RT_DIRMAP_A + (plcp_rate * 2));
  1409. }
  1410. static void brcms_upd_ofdm_pctl1_table(struct brcms_hardware *wlc_hw)
  1411. {
  1412. u8 rate;
  1413. u8 rates[8] = {
  1414. BRCM_RATE_6M, BRCM_RATE_9M, BRCM_RATE_12M, BRCM_RATE_18M,
  1415. BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M
  1416. };
  1417. u16 entry_ptr;
  1418. u16 pctl1;
  1419. uint i;
  1420. if (!BRCMS_PHY_11N_CAP(wlc_hw->band))
  1421. return;
  1422. /* walk the phy rate table and update the entries */
  1423. for (i = 0; i < ARRAY_SIZE(rates); i++) {
  1424. rate = rates[i];
  1425. entry_ptr = brcms_b_ofdm_ratetable_offset(wlc_hw, rate);
  1426. /* read the SHM Rate Table entry OFDM PCTL1 values */
  1427. pctl1 =
  1428. brcms_b_read_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS);
  1429. /* modify the value */
  1430. pctl1 &= ~PHY_TXC1_MODE_MASK;
  1431. pctl1 |= (wlc_hw->hw_stf_ss_opmode << PHY_TXC1_MODE_SHIFT);
  1432. /* Update the SHM Rate Table entry OFDM PCTL1 values */
  1433. brcms_b_write_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS,
  1434. pctl1);
  1435. }
  1436. }
  1437. /* band-specific init */
  1438. static void brcms_b_bsinit(struct brcms_c_info *wlc, u16 chanspec)
  1439. {
  1440. struct brcms_hardware *wlc_hw = wlc->hw;
  1441. brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
  1442. wlc_hw->band->bandunit);
  1443. brcms_c_ucode_bsinit(wlc_hw);
  1444. wlc_phy_init(wlc_hw->band->pi, chanspec);
  1445. brcms_c_ucode_txant_set(wlc_hw);
  1446. /*
  1447. * cwmin is band-specific, update hardware
  1448. * with value for current band
  1449. */
  1450. brcms_b_set_cwmin(wlc_hw, wlc_hw->band->CWmin);
  1451. brcms_b_set_cwmax(wlc_hw, wlc_hw->band->CWmax);
  1452. brcms_b_update_slot_timing(wlc_hw,
  1453. wlc_hw->band->bandtype == BRCM_BAND_5G ?
  1454. true : wlc_hw->shortslot);
  1455. /* write phytype and phyvers */
  1456. brcms_b_write_shm(wlc_hw, M_PHYTYPE, (u16) wlc_hw->band->phytype);
  1457. brcms_b_write_shm(wlc_hw, M_PHYVER, (u16) wlc_hw->band->phyrev);
  1458. /*
  1459. * initialize the txphyctl1 rate table since
  1460. * shmem is shared between bands
  1461. */
  1462. brcms_upd_ofdm_pctl1_table(wlc_hw);
  1463. brcms_b_upd_synthpu(wlc_hw);
  1464. }
  1465. /* Perform a soft reset of the PHY PLL */
  1466. void brcms_b_core_phypll_reset(struct brcms_hardware *wlc_hw)
  1467. {
  1468. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_addr),
  1469. ~0, 0);
  1470. udelay(1);
  1471. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
  1472. 0x4, 0);
  1473. udelay(1);
  1474. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
  1475. 0x4, 4);
  1476. udelay(1);
  1477. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
  1478. 0x4, 0);
  1479. udelay(1);
  1480. }
  1481. /* light way to turn on phy clock without reset for NPHY only
  1482. * refer to brcms_b_core_phy_clk for full version
  1483. */
  1484. void brcms_b_phyclk_fgc(struct brcms_hardware *wlc_hw, bool clk)
  1485. {
  1486. /* support(necessary for NPHY and HYPHY) only */
  1487. if (!BRCMS_ISNPHY(wlc_hw->band))
  1488. return;
  1489. if (ON == clk)
  1490. brcms_b_core_ioctl(wlc_hw, SICF_FGC, SICF_FGC);
  1491. else
  1492. brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
  1493. }
  1494. void brcms_b_macphyclk_set(struct brcms_hardware *wlc_hw, bool clk)
  1495. {
  1496. if (ON == clk)
  1497. brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, SICF_MPCLKE);
  1498. else
  1499. brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, 0);
  1500. }
  1501. void brcms_b_phy_reset(struct brcms_hardware *wlc_hw)
  1502. {
  1503. struct brcms_phy_pub *pih = wlc_hw->band->pi;
  1504. u32 phy_bw_clkbits;
  1505. bool phy_in_reset = false;
  1506. brcms_dbg_info(wlc_hw->d11core, "wl%d: reset phy\n", wlc_hw->unit);
  1507. if (pih == NULL)
  1508. return;
  1509. phy_bw_clkbits = wlc_phy_clk_bwbits(wlc_hw->band->pi);
  1510. /* Specific reset sequence required for NPHY rev 3 and 4 */
  1511. if (BRCMS_ISNPHY(wlc_hw->band) && NREV_GE(wlc_hw->band->phyrev, 3) &&
  1512. NREV_LE(wlc_hw->band->phyrev, 4)) {
  1513. /* Set the PHY bandwidth */
  1514. brcms_b_core_ioctl(wlc_hw, SICF_BWMASK, phy_bw_clkbits);
  1515. udelay(1);
  1516. /* Perform a soft reset of the PHY PLL */
  1517. brcms_b_core_phypll_reset(wlc_hw);
  1518. /* reset the PHY */
  1519. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_PCLKE),
  1520. (SICF_PRST | SICF_PCLKE));
  1521. phy_in_reset = true;
  1522. } else {
  1523. brcms_b_core_ioctl(wlc_hw,
  1524. (SICF_PRST | SICF_PCLKE | SICF_BWMASK),
  1525. (SICF_PRST | SICF_PCLKE | phy_bw_clkbits));
  1526. }
  1527. udelay(2);
  1528. brcms_b_core_phy_clk(wlc_hw, ON);
  1529. if (pih)
  1530. wlc_phy_anacore(pih, ON);
  1531. }
  1532. /* switch to and initialize new band */
  1533. static void brcms_b_setband(struct brcms_hardware *wlc_hw, uint bandunit,
  1534. u16 chanspec) {
  1535. struct brcms_c_info *wlc = wlc_hw->wlc;
  1536. u32 macintmask;
  1537. /* Enable the d11 core before accessing it */
  1538. if (!bcma_core_is_enabled(wlc_hw->d11core)) {
  1539. bcma_core_enable(wlc_hw->d11core, 0);
  1540. brcms_c_mctrl_reset(wlc_hw);
  1541. }
  1542. macintmask = brcms_c_setband_inact(wlc, bandunit);
  1543. if (!wlc_hw->up)
  1544. return;
  1545. brcms_b_core_phy_clk(wlc_hw, ON);
  1546. /* band-specific initializations */
  1547. brcms_b_bsinit(wlc, chanspec);
  1548. /*
  1549. * If there are any pending software interrupt bits,
  1550. * then replace these with a harmless nonzero value
  1551. * so brcms_c_dpc() will re-enable interrupts when done.
  1552. */
  1553. if (wlc->macintstatus)
  1554. wlc->macintstatus = MI_DMAINT;
  1555. /* restore macintmask */
  1556. brcms_intrsrestore(wlc->wl, macintmask);
  1557. /* ucode should still be suspended.. */
  1558. WARN_ON((bcma_read32(wlc_hw->d11core, D11REGOFFS(maccontrol)) &
  1559. MCTL_EN_MAC) != 0);
  1560. }
  1561. static bool brcms_c_isgoodchip(struct brcms_hardware *wlc_hw)
  1562. {
  1563. /* reject unsupported corerev */
  1564. if (!CONF_HAS(D11CONF, wlc_hw->corerev)) {
  1565. wiphy_err(wlc_hw->wlc->wiphy, "unsupported core rev %d\n",
  1566. wlc_hw->corerev);
  1567. return false;
  1568. }
  1569. return true;
  1570. }
  1571. /* Validate some board info parameters */
  1572. static bool brcms_c_validboardtype(struct brcms_hardware *wlc_hw)
  1573. {
  1574. uint boardrev = wlc_hw->boardrev;
  1575. /* 4 bits each for board type, major, minor, and tiny version */
  1576. uint brt = (boardrev & 0xf000) >> 12;
  1577. uint b0 = (boardrev & 0xf00) >> 8;
  1578. uint b1 = (boardrev & 0xf0) >> 4;
  1579. uint b2 = boardrev & 0xf;
  1580. /* voards from other vendors are always considered valid */
  1581. if (ai_get_boardvendor(wlc_hw->sih) != PCI_VENDOR_ID_BROADCOM)
  1582. return true;
  1583. /* do some boardrev sanity checks when boardvendor is Broadcom */
  1584. if (boardrev == 0)
  1585. return false;
  1586. if (boardrev <= 0xff)
  1587. return true;
  1588. if ((brt > 2) || (brt == 0) || (b0 > 9) || (b0 == 0) || (b1 > 9)
  1589. || (b2 > 9))
  1590. return false;
  1591. return true;
  1592. }
  1593. static void brcms_c_get_macaddr(struct brcms_hardware *wlc_hw, u8 etheraddr[ETH_ALEN])
  1594. {
  1595. struct ssb_sprom *sprom = &wlc_hw->d11core->bus->sprom;
  1596. /* If macaddr exists, use it (Sromrev4, CIS, ...). */
  1597. if (!is_zero_ether_addr(sprom->il0mac)) {
  1598. memcpy(etheraddr, sprom->il0mac, ETH_ALEN);
  1599. return;
  1600. }
  1601. if (wlc_hw->_nbands > 1)
  1602. memcpy(etheraddr, sprom->et1mac, ETH_ALEN);
  1603. else
  1604. memcpy(etheraddr, sprom->il0mac, ETH_ALEN);
  1605. }
  1606. /* power both the pll and external oscillator on/off */
  1607. static void brcms_b_xtal(struct brcms_hardware *wlc_hw, bool want)
  1608. {
  1609. brcms_dbg_info(wlc_hw->d11core, "wl%d: want %d\n", wlc_hw->unit, want);
  1610. /*
  1611. * dont power down if plldown is false or
  1612. * we must poll hw radio disable
  1613. */
  1614. if (!want && wlc_hw->pllreq)
  1615. return;
  1616. wlc_hw->sbclk = want;
  1617. if (!wlc_hw->sbclk) {
  1618. wlc_hw->clk = false;
  1619. if (wlc_hw->band && wlc_hw->band->pi)
  1620. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
  1621. }
  1622. }
  1623. /*
  1624. * Return true if radio is disabled, otherwise false.
  1625. * hw radio disable signal is an external pin, users activate it asynchronously
  1626. * this function could be called when driver is down and w/o clock
  1627. * it operates on different registers depending on corerev and boardflag.
  1628. */
  1629. static bool brcms_b_radio_read_hwdisabled(struct brcms_hardware *wlc_hw)
  1630. {
  1631. bool v, clk, xtal;
  1632. u32 flags = 0;
  1633. xtal = wlc_hw->sbclk;
  1634. if (!xtal)
  1635. brcms_b_xtal(wlc_hw, ON);
  1636. /* may need to take core out of reset first */
  1637. clk = wlc_hw->clk;
  1638. if (!clk) {
  1639. /*
  1640. * mac no longer enables phyclk automatically when driver
  1641. * accesses phyreg throughput mac. This can be skipped since
  1642. * only mac reg is accessed below
  1643. */
  1644. if (D11REV_GE(wlc_hw->corerev, 18))
  1645. flags |= SICF_PCLKE;
  1646. /*
  1647. * TODO: test suspend/resume
  1648. *
  1649. * AI chip doesn't restore bar0win2 on
  1650. * hibernation/resume, need sw fixup
  1651. */
  1652. bcma_core_enable(wlc_hw->d11core, flags);
  1653. brcms_c_mctrl_reset(wlc_hw);
  1654. }
  1655. v = ((bcma_read32(wlc_hw->d11core,
  1656. D11REGOFFS(phydebug)) & PDBG_RFD) != 0);
  1657. /* put core back into reset */
  1658. if (!clk)
  1659. bcma_core_disable(wlc_hw->d11core, 0);
  1660. if (!xtal)
  1661. brcms_b_xtal(wlc_hw, OFF);
  1662. return v;
  1663. }
  1664. static bool wlc_dma_rxreset(struct brcms_hardware *wlc_hw, uint fifo)
  1665. {
  1666. struct dma_pub *di = wlc_hw->di[fifo];
  1667. return dma_rxreset(di);
  1668. }
  1669. /* d11 core reset
  1670. * ensure fask clock during reset
  1671. * reset dma
  1672. * reset d11(out of reset)
  1673. * reset phy(out of reset)
  1674. * clear software macintstatus for fresh new start
  1675. * one testing hack wlc_hw->noreset will bypass the d11/phy reset
  1676. */
  1677. void brcms_b_corereset(struct brcms_hardware *wlc_hw, u32 flags)
  1678. {
  1679. uint i;
  1680. bool fastclk;
  1681. if (flags == BRCMS_USE_COREFLAGS)
  1682. flags = (wlc_hw->band->pi ? wlc_hw->band->core_flags : 0);
  1683. brcms_dbg_info(wlc_hw->d11core, "wl%d: core reset\n", wlc_hw->unit);
  1684. /* request FAST clock if not on */
  1685. fastclk = wlc_hw->forcefastclk;
  1686. if (!fastclk)
  1687. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  1688. /* reset the dma engines except first time thru */
  1689. if (bcma_core_is_enabled(wlc_hw->d11core)) {
  1690. for (i = 0; i < NFIFO; i++)
  1691. if ((wlc_hw->di[i]) && (!dma_txreset(wlc_hw->di[i])))
  1692. brcms_err(wlc_hw->d11core, "wl%d: %s: "
  1693. "dma_txreset[%d]: cannot stop dma\n",
  1694. wlc_hw->unit, __func__, i);
  1695. if ((wlc_hw->di[RX_FIFO])
  1696. && (!wlc_dma_rxreset(wlc_hw, RX_FIFO)))
  1697. brcms_err(wlc_hw->d11core, "wl%d: %s: dma_rxreset"
  1698. "[%d]: cannot stop dma\n",
  1699. wlc_hw->unit, __func__, RX_FIFO);
  1700. }
  1701. /* if noreset, just stop the psm and return */
  1702. if (wlc_hw->noreset) {
  1703. wlc_hw->wlc->macintstatus = 0; /* skip wl_dpc after down */
  1704. brcms_b_mctrl(wlc_hw, MCTL_PSM_RUN | MCTL_EN_MAC, 0);
  1705. return;
  1706. }
  1707. /*
  1708. * mac no longer enables phyclk automatically when driver accesses
  1709. * phyreg throughput mac, AND phy_reset is skipped at early stage when
  1710. * band->pi is invalid. need to enable PHY CLK
  1711. */
  1712. if (D11REV_GE(wlc_hw->corerev, 18))
  1713. flags |= SICF_PCLKE;
  1714. /*
  1715. * reset the core
  1716. * In chips with PMU, the fastclk request goes through d11 core
  1717. * reg 0x1e0, which is cleared by the core_reset. have to re-request it.
  1718. *
  1719. * This adds some delay and we can optimize it by also requesting
  1720. * fastclk through chipcommon during this period if necessary. But
  1721. * that has to work coordinate with other driver like mips/arm since
  1722. * they may touch chipcommon as well.
  1723. */
  1724. wlc_hw->clk = false;
  1725. bcma_core_enable(wlc_hw->d11core, flags);
  1726. wlc_hw->clk = true;
  1727. if (wlc_hw->band && wlc_hw->band->pi)
  1728. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, true);
  1729. brcms_c_mctrl_reset(wlc_hw);
  1730. if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU)
  1731. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  1732. brcms_b_phy_reset(wlc_hw);
  1733. /* turn on PHY_PLL */
  1734. brcms_b_core_phypll_ctl(wlc_hw, true);
  1735. /* clear sw intstatus */
  1736. wlc_hw->wlc->macintstatus = 0;
  1737. /* restore the clk setting */
  1738. if (!fastclk)
  1739. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
  1740. }
  1741. /* txfifo sizes needs to be modified(increased) since the newer cores
  1742. * have more memory.
  1743. */
  1744. static void brcms_b_corerev_fifofixup(struct brcms_hardware *wlc_hw)
  1745. {
  1746. struct bcma_device *core = wlc_hw->d11core;
  1747. u16 fifo_nu;
  1748. u16 txfifo_startblk = TXFIFO_START_BLK, txfifo_endblk;
  1749. u16 txfifo_def, txfifo_def1;
  1750. u16 txfifo_cmd;
  1751. /* tx fifos start at TXFIFO_START_BLK from the Base address */
  1752. txfifo_startblk = TXFIFO_START_BLK;
  1753. /* sequence of operations: reset fifo, set fifo size, reset fifo */
  1754. for (fifo_nu = 0; fifo_nu < NFIFO; fifo_nu++) {
  1755. txfifo_endblk = txfifo_startblk + wlc_hw->xmtfifo_sz[fifo_nu];
  1756. txfifo_def = (txfifo_startblk & 0xff) |
  1757. (((txfifo_endblk - 1) & 0xff) << TXFIFO_FIFOTOP_SHIFT);
  1758. txfifo_def1 = ((txfifo_startblk >> 8) & 0x1) |
  1759. ((((txfifo_endblk -
  1760. 1) >> 8) & 0x1) << TXFIFO_FIFOTOP_SHIFT);
  1761. txfifo_cmd =
  1762. TXFIFOCMD_RESET_MASK | (fifo_nu << TXFIFOCMD_FIFOSEL_SHIFT);
  1763. bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
  1764. bcma_write16(core, D11REGOFFS(xmtfifodef), txfifo_def);
  1765. bcma_write16(core, D11REGOFFS(xmtfifodef1), txfifo_def1);
  1766. bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
  1767. txfifo_startblk += wlc_hw->xmtfifo_sz[fifo_nu];
  1768. }
  1769. /*
  1770. * need to propagate to shm location to be in sync since ucode/hw won't
  1771. * do this
  1772. */
  1773. brcms_b_write_shm(wlc_hw, M_FIFOSIZE0,
  1774. wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]);
  1775. brcms_b_write_shm(wlc_hw, M_FIFOSIZE1,
  1776. wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]);
  1777. brcms_b_write_shm(wlc_hw, M_FIFOSIZE2,
  1778. ((wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO] << 8) | wlc_hw->
  1779. xmtfifo_sz[TX_AC_BK_FIFO]));
  1780. brcms_b_write_shm(wlc_hw, M_FIFOSIZE3,
  1781. ((wlc_hw->xmtfifo_sz[TX_ATIM_FIFO] << 8) | wlc_hw->
  1782. xmtfifo_sz[TX_BCMC_FIFO]));
  1783. }
  1784. /* This function is used for changing the tsf frac register
  1785. * If spur avoidance mode is off, the mac freq will be 80/120/160Mhz
  1786. * If spur avoidance mode is on1, the mac freq will be 82/123/164Mhz
  1787. * If spur avoidance mode is on2, the mac freq will be 84/126/168Mhz
  1788. * HTPHY Formula is 2^26/freq(MHz) e.g.
  1789. * For spuron2 - 126MHz -> 2^26/126 = 532610.0
  1790. * - 532610 = 0x82082 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x2082
  1791. * For spuron: 123MHz -> 2^26/123 = 545600.5
  1792. * - 545601 = 0x85341 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x5341
  1793. * For spur off: 120MHz -> 2^26/120 = 559240.5
  1794. * - 559241 = 0x88889 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x8889
  1795. */
  1796. void brcms_b_switch_macfreq(struct brcms_hardware *wlc_hw, u8 spurmode)
  1797. {
  1798. struct bcma_device *core = wlc_hw->d11core;
  1799. if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43224) ||
  1800. (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225)) {
  1801. if (spurmode == WL_SPURAVOID_ON2) { /* 126Mhz */
  1802. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x2082);
  1803. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
  1804. } else if (spurmode == WL_SPURAVOID_ON1) { /* 123Mhz */
  1805. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x5341);
  1806. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
  1807. } else { /* 120Mhz */
  1808. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x8889);
  1809. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
  1810. }
  1811. } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
  1812. if (spurmode == WL_SPURAVOID_ON1) { /* 82Mhz */
  1813. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x7CE0);
  1814. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
  1815. } else { /* 80Mhz */
  1816. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0xCCCD);
  1817. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
  1818. }
  1819. }
  1820. }
  1821. void brcms_c_start_station(struct brcms_c_info *wlc, u8 *addr)
  1822. {
  1823. memcpy(wlc->pub->cur_etheraddr, addr, sizeof(wlc->pub->cur_etheraddr));
  1824. wlc->bsscfg->type = BRCMS_TYPE_STATION;
  1825. }
  1826. void brcms_c_start_ap(struct brcms_c_info *wlc, u8 *addr, const u8 *bssid,
  1827. u8 *ssid, size_t ssid_len)
  1828. {
  1829. brcms_c_set_ssid(wlc, ssid, ssid_len);
  1830. memcpy(wlc->pub->cur_etheraddr, addr, sizeof(wlc->pub->cur_etheraddr));
  1831. memcpy(wlc->bsscfg->BSSID, bssid, sizeof(wlc->bsscfg->BSSID));
  1832. wlc->bsscfg->type = BRCMS_TYPE_AP;
  1833. brcms_b_mctrl(wlc->hw, MCTL_AP | MCTL_INFRA, MCTL_AP | MCTL_INFRA);
  1834. }
  1835. void brcms_c_start_adhoc(struct brcms_c_info *wlc, u8 *addr)
  1836. {
  1837. memcpy(wlc->pub->cur_etheraddr, addr, sizeof(wlc->pub->cur_etheraddr));
  1838. wlc->bsscfg->type = BRCMS_TYPE_ADHOC;
  1839. brcms_b_mctrl(wlc->hw, MCTL_AP | MCTL_INFRA, 0);
  1840. }
  1841. /* Initialize GPIOs that are controlled by D11 core */
  1842. static void brcms_c_gpio_init(struct brcms_c_info *wlc)
  1843. {
  1844. struct brcms_hardware *wlc_hw = wlc->hw;
  1845. u32 gc, gm;
  1846. /* use GPIO select 0 to get all gpio signals from the gpio out reg */
  1847. brcms_b_mctrl(wlc_hw, MCTL_GPOUT_SEL_MASK, 0);
  1848. /*
  1849. * Common GPIO setup:
  1850. * G0 = LED 0 = WLAN Activity
  1851. * G1 = LED 1 = WLAN 2.4 GHz Radio State
  1852. * G2 = LED 2 = WLAN 5 GHz Radio State
  1853. * G4 = radio disable input (HI enabled, LO disabled)
  1854. */
  1855. gc = gm = 0;
  1856. /* Allocate GPIOs for mimo antenna diversity feature */
  1857. if (wlc_hw->antsel_type == ANTSEL_2x3) {
  1858. /* Enable antenna diversity, use 2x3 mode */
  1859. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
  1860. MHF3_ANTSEL_EN, BRCM_BAND_ALL);
  1861. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE,
  1862. MHF3_ANTSEL_MODE, BRCM_BAND_ALL);
  1863. /* init superswitch control */
  1864. wlc_phy_antsel_init(wlc_hw->band->pi, false);
  1865. } else if (wlc_hw->antsel_type == ANTSEL_2x4) {
  1866. gm |= gc |= (BOARD_GPIO_12 | BOARD_GPIO_13);
  1867. /*
  1868. * The board itself is powered by these GPIOs
  1869. * (when not sending pattern) so set them high
  1870. */
  1871. bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_oe),
  1872. (BOARD_GPIO_12 | BOARD_GPIO_13));
  1873. bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_out),
  1874. (BOARD_GPIO_12 | BOARD_GPIO_13));
  1875. /* Enable antenna diversity, use 2x4 mode */
  1876. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
  1877. MHF3_ANTSEL_EN, BRCM_BAND_ALL);
  1878. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE, 0,
  1879. BRCM_BAND_ALL);
  1880. /* Configure the desired clock to be 4Mhz */
  1881. brcms_b_write_shm(wlc_hw, M_ANTSEL_CLKDIV,
  1882. ANTSEL_CLKDIV_4MHZ);
  1883. }
  1884. /*
  1885. * gpio 9 controls the PA. ucode is responsible
  1886. * for wiggling out and oe
  1887. */
  1888. if (wlc_hw->boardflags & BFL_PACTRL)
  1889. gm |= gc |= BOARD_GPIO_PACTRL;
  1890. /* apply to gpiocontrol register */
  1891. bcma_chipco_gpio_control(&wlc_hw->d11core->bus->drv_cc, gm, gc);
  1892. }
  1893. static void brcms_ucode_write(struct brcms_hardware *wlc_hw,
  1894. const __le32 ucode[], const size_t nbytes)
  1895. {
  1896. struct bcma_device *core = wlc_hw->d11core;
  1897. uint i;
  1898. uint count;
  1899. brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  1900. count = (nbytes / sizeof(u32));
  1901. bcma_write32(core, D11REGOFFS(objaddr),
  1902. OBJADDR_AUTO_INC | OBJADDR_UCM_SEL);
  1903. (void)bcma_read32(core, D11REGOFFS(objaddr));
  1904. for (i = 0; i < count; i++)
  1905. bcma_write32(core, D11REGOFFS(objdata), le32_to_cpu(ucode[i]));
  1906. }
  1907. static void brcms_ucode_download(struct brcms_hardware *wlc_hw)
  1908. {
  1909. struct brcms_c_info *wlc;
  1910. struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
  1911. wlc = wlc_hw->wlc;
  1912. if (wlc_hw->ucode_loaded)
  1913. return;
  1914. if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
  1915. if (BRCMS_ISNPHY(wlc_hw->band)) {
  1916. brcms_ucode_write(wlc_hw, ucode->bcm43xx_16_mimo,
  1917. ucode->bcm43xx_16_mimosz);
  1918. wlc_hw->ucode_loaded = true;
  1919. } else
  1920. brcms_err(wlc_hw->d11core,
  1921. "%s: wl%d: unsupported phy in corerev %d\n",
  1922. __func__, wlc_hw->unit, wlc_hw->corerev);
  1923. } else if (D11REV_IS(wlc_hw->corerev, 24)) {
  1924. if (BRCMS_ISLCNPHY(wlc_hw->band)) {
  1925. brcms_ucode_write(wlc_hw, ucode->bcm43xx_24_lcn,
  1926. ucode->bcm43xx_24_lcnsz);
  1927. wlc_hw->ucode_loaded = true;
  1928. } else {
  1929. brcms_err(wlc_hw->d11core,
  1930. "%s: wl%d: unsupported phy in corerev %d\n",
  1931. __func__, wlc_hw->unit, wlc_hw->corerev);
  1932. }
  1933. }
  1934. }
  1935. void brcms_b_txant_set(struct brcms_hardware *wlc_hw, u16 phytxant)
  1936. {
  1937. /* update sw state */
  1938. wlc_hw->bmac_phytxant = phytxant;
  1939. /* push to ucode if up */
  1940. if (!wlc_hw->up)
  1941. return;
  1942. brcms_c_ucode_txant_set(wlc_hw);
  1943. }
  1944. u16 brcms_b_get_txant(struct brcms_hardware *wlc_hw)
  1945. {
  1946. return (u16) wlc_hw->wlc->stf->txant;
  1947. }
  1948. void brcms_b_antsel_type_set(struct brcms_hardware *wlc_hw, u8 antsel_type)
  1949. {
  1950. wlc_hw->antsel_type = antsel_type;
  1951. /* Update the antsel type for phy module to use */
  1952. wlc_phy_antsel_type_set(wlc_hw->band->pi, antsel_type);
  1953. }
  1954. static void brcms_b_fifoerrors(struct brcms_hardware *wlc_hw)
  1955. {
  1956. bool fatal = false;
  1957. uint unit;
  1958. uint intstatus, idx;
  1959. struct bcma_device *core = wlc_hw->d11core;
  1960. unit = wlc_hw->unit;
  1961. for (idx = 0; idx < NFIFO; idx++) {
  1962. /* read intstatus register and ignore any non-error bits */
  1963. intstatus =
  1964. bcma_read32(core,
  1965. D11REGOFFS(intctrlregs[idx].intstatus)) &
  1966. I_ERRORS;
  1967. if (!intstatus)
  1968. continue;
  1969. brcms_dbg_int(core, "wl%d: intstatus%d 0x%x\n",
  1970. unit, idx, intstatus);
  1971. if (intstatus & I_RO) {
  1972. brcms_err(core, "wl%d: fifo %d: receive fifo "
  1973. "overflow\n", unit, idx);
  1974. fatal = true;
  1975. }
  1976. if (intstatus & I_PC) {
  1977. brcms_err(core, "wl%d: fifo %d: descriptor error\n",
  1978. unit, idx);
  1979. fatal = true;
  1980. }
  1981. if (intstatus & I_PD) {
  1982. brcms_err(core, "wl%d: fifo %d: data error\n", unit,
  1983. idx);
  1984. fatal = true;
  1985. }
  1986. if (intstatus & I_DE) {
  1987. brcms_err(core, "wl%d: fifo %d: descriptor protocol "
  1988. "error\n", unit, idx);
  1989. fatal = true;
  1990. }
  1991. if (intstatus & I_RU)
  1992. brcms_err(core, "wl%d: fifo %d: receive descriptor "
  1993. "underflow\n", idx, unit);
  1994. if (intstatus & I_XU) {
  1995. brcms_err(core, "wl%d: fifo %d: transmit fifo "
  1996. "underflow\n", idx, unit);
  1997. fatal = true;
  1998. }
  1999. if (fatal) {
  2000. brcms_fatal_error(wlc_hw->wlc->wl); /* big hammer */
  2001. break;
  2002. } else
  2003. bcma_write32(core,
  2004. D11REGOFFS(intctrlregs[idx].intstatus),
  2005. intstatus);
  2006. }
  2007. }
  2008. void brcms_c_intrson(struct brcms_c_info *wlc)
  2009. {
  2010. struct brcms_hardware *wlc_hw = wlc->hw;
  2011. wlc->macintmask = wlc->defmacintmask;
  2012. bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
  2013. }
  2014. u32 brcms_c_intrsoff(struct brcms_c_info *wlc)
  2015. {
  2016. struct brcms_hardware *wlc_hw = wlc->hw;
  2017. u32 macintmask;
  2018. if (!wlc_hw->clk)
  2019. return 0;
  2020. macintmask = wlc->macintmask; /* isr can still happen */
  2021. bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), 0);
  2022. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(macintmask));
  2023. udelay(1); /* ensure int line is no longer driven */
  2024. wlc->macintmask = 0;
  2025. /* return previous macintmask; resolve race between us and our isr */
  2026. return wlc->macintstatus ? 0 : macintmask;
  2027. }
  2028. void brcms_c_intrsrestore(struct brcms_c_info *wlc, u32 macintmask)
  2029. {
  2030. struct brcms_hardware *wlc_hw = wlc->hw;
  2031. if (!wlc_hw->clk)
  2032. return;
  2033. wlc->macintmask = macintmask;
  2034. bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
  2035. }
  2036. /* assumes that the d11 MAC is enabled */
  2037. static void brcms_b_tx_fifo_suspend(struct brcms_hardware *wlc_hw,
  2038. uint tx_fifo)
  2039. {
  2040. u8 fifo = 1 << tx_fifo;
  2041. /* Two clients of this code, 11h Quiet period and scanning. */
  2042. /* only suspend if not already suspended */
  2043. if ((wlc_hw->suspended_fifos & fifo) == fifo)
  2044. return;
  2045. /* force the core awake only if not already */
  2046. if (wlc_hw->suspended_fifos == 0)
  2047. brcms_c_ucode_wake_override_set(wlc_hw,
  2048. BRCMS_WAKE_OVERRIDE_TXFIFO);
  2049. wlc_hw->suspended_fifos |= fifo;
  2050. if (wlc_hw->di[tx_fifo]) {
  2051. /*
  2052. * Suspending AMPDU transmissions in the middle can cause
  2053. * underflow which may result in mismatch between ucode and
  2054. * driver so suspend the mac before suspending the FIFO
  2055. */
  2056. if (BRCMS_PHY_11N_CAP(wlc_hw->band))
  2057. brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
  2058. dma_txsuspend(wlc_hw->di[tx_fifo]);
  2059. if (BRCMS_PHY_11N_CAP(wlc_hw->band))
  2060. brcms_c_enable_mac(wlc_hw->wlc);
  2061. }
  2062. }
  2063. static void brcms_b_tx_fifo_resume(struct brcms_hardware *wlc_hw,
  2064. uint tx_fifo)
  2065. {
  2066. /* BMAC_NOTE: BRCMS_TX_FIFO_ENAB is done in brcms_c_dpc() for DMA case
  2067. * but need to be done here for PIO otherwise the watchdog will catch
  2068. * the inconsistency and fire
  2069. */
  2070. /* Two clients of this code, 11h Quiet period and scanning. */
  2071. if (wlc_hw->di[tx_fifo])
  2072. dma_txresume(wlc_hw->di[tx_fifo]);
  2073. /* allow core to sleep again */
  2074. if (wlc_hw->suspended_fifos == 0)
  2075. return;
  2076. else {
  2077. wlc_hw->suspended_fifos &= ~(1 << tx_fifo);
  2078. if (wlc_hw->suspended_fifos == 0)
  2079. brcms_c_ucode_wake_override_clear(wlc_hw,
  2080. BRCMS_WAKE_OVERRIDE_TXFIFO);
  2081. }
  2082. }
  2083. /* precondition: requires the mac core to be enabled */
  2084. static void brcms_b_mute(struct brcms_hardware *wlc_hw, bool mute_tx)
  2085. {
  2086. static const u8 null_ether_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  2087. u8 *ethaddr = wlc_hw->wlc->pub->cur_etheraddr;
  2088. if (mute_tx) {
  2089. /* suspend tx fifos */
  2090. brcms_b_tx_fifo_suspend(wlc_hw, TX_DATA_FIFO);
  2091. brcms_b_tx_fifo_suspend(wlc_hw, TX_CTL_FIFO);
  2092. brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_BK_FIFO);
  2093. brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_VI_FIFO);
  2094. /* zero the address match register so we do not send ACKs */
  2095. brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, null_ether_addr);
  2096. } else {
  2097. /* resume tx fifos */
  2098. brcms_b_tx_fifo_resume(wlc_hw, TX_DATA_FIFO);
  2099. brcms_b_tx_fifo_resume(wlc_hw, TX_CTL_FIFO);
  2100. brcms_b_tx_fifo_resume(wlc_hw, TX_AC_BK_FIFO);
  2101. brcms_b_tx_fifo_resume(wlc_hw, TX_AC_VI_FIFO);
  2102. /* Restore address */
  2103. brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, ethaddr);
  2104. }
  2105. wlc_phy_mute_upd(wlc_hw->band->pi, mute_tx, 0);
  2106. if (mute_tx)
  2107. brcms_c_ucode_mute_override_set(wlc_hw);
  2108. else
  2109. brcms_c_ucode_mute_override_clear(wlc_hw);
  2110. }
  2111. void
  2112. brcms_c_mute(struct brcms_c_info *wlc, bool mute_tx)
  2113. {
  2114. brcms_b_mute(wlc->hw, mute_tx);
  2115. }
  2116. /*
  2117. * Read and clear macintmask and macintstatus and intstatus registers.
  2118. * This routine should be called with interrupts off
  2119. * Return:
  2120. * -1 if brcms_deviceremoved(wlc) evaluates to true;
  2121. * 0 if the interrupt is not for us, or we are in some special cases;
  2122. * device interrupt status bits otherwise.
  2123. */
  2124. static inline u32 wlc_intstatus(struct brcms_c_info *wlc, bool in_isr)
  2125. {
  2126. struct brcms_hardware *wlc_hw = wlc->hw;
  2127. struct bcma_device *core = wlc_hw->d11core;
  2128. u32 macintstatus, mask;
  2129. /* macintstatus includes a DMA interrupt summary bit */
  2130. macintstatus = bcma_read32(core, D11REGOFFS(macintstatus));
  2131. mask = in_isr ? wlc->macintmask : wlc->defmacintmask;
  2132. trace_brcms_macintstatus(&core->dev, in_isr, macintstatus, mask);
  2133. /* detect cardbus removed, in power down(suspend) and in reset */
  2134. if (brcms_deviceremoved(wlc))
  2135. return -1;
  2136. /* brcms_deviceremoved() succeeds even when the core is still resetting,
  2137. * handle that case here.
  2138. */
  2139. if (macintstatus == 0xffffffff)
  2140. return 0;
  2141. /* defer unsolicited interrupts */
  2142. macintstatus &= mask;
  2143. /* if not for us */
  2144. if (macintstatus == 0)
  2145. return 0;
  2146. /* turn off the interrupts */
  2147. bcma_write32(core, D11REGOFFS(macintmask), 0);
  2148. (void)bcma_read32(core, D11REGOFFS(macintmask));
  2149. wlc->macintmask = 0;
  2150. /* clear device interrupts */
  2151. bcma_write32(core, D11REGOFFS(macintstatus), macintstatus);
  2152. /* MI_DMAINT is indication of non-zero intstatus */
  2153. if (macintstatus & MI_DMAINT)
  2154. /*
  2155. * only fifo interrupt enabled is I_RI in
  2156. * RX_FIFO. If MI_DMAINT is set, assume it
  2157. * is set and clear the interrupt.
  2158. */
  2159. bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intstatus),
  2160. DEF_RXINTMASK);
  2161. return macintstatus;
  2162. }
  2163. /* Update wlc->macintstatus and wlc->intstatus[]. */
  2164. /* Return true if they are updated successfully. false otherwise */
  2165. bool brcms_c_intrsupd(struct brcms_c_info *wlc)
  2166. {
  2167. u32 macintstatus;
  2168. /* read and clear macintstatus and intstatus registers */
  2169. macintstatus = wlc_intstatus(wlc, false);
  2170. /* device is removed */
  2171. if (macintstatus == 0xffffffff)
  2172. return false;
  2173. /* update interrupt status in software */
  2174. wlc->macintstatus |= macintstatus;
  2175. return true;
  2176. }
  2177. /*
  2178. * First-level interrupt processing.
  2179. * Return true if this was our interrupt
  2180. * and if further brcms_c_dpc() processing is required,
  2181. * false otherwise.
  2182. */
  2183. bool brcms_c_isr(struct brcms_c_info *wlc)
  2184. {
  2185. struct brcms_hardware *wlc_hw = wlc->hw;
  2186. u32 macintstatus;
  2187. if (!wlc_hw->up || !wlc->macintmask)
  2188. return false;
  2189. /* read and clear macintstatus and intstatus registers */
  2190. macintstatus = wlc_intstatus(wlc, true);
  2191. if (macintstatus == 0xffffffff) {
  2192. brcms_err(wlc_hw->d11core,
  2193. "DEVICEREMOVED detected in the ISR code path\n");
  2194. return false;
  2195. }
  2196. /* it is not for us */
  2197. if (macintstatus == 0)
  2198. return false;
  2199. /* save interrupt status bits */
  2200. wlc->macintstatus = macintstatus;
  2201. return true;
  2202. }
  2203. void brcms_c_suspend_mac_and_wait(struct brcms_c_info *wlc)
  2204. {
  2205. struct brcms_hardware *wlc_hw = wlc->hw;
  2206. struct bcma_device *core = wlc_hw->d11core;
  2207. u32 mc, mi;
  2208. brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
  2209. wlc_hw->band->bandunit);
  2210. /*
  2211. * Track overlapping suspend requests
  2212. */
  2213. wlc_hw->mac_suspend_depth++;
  2214. if (wlc_hw->mac_suspend_depth > 1)
  2215. return;
  2216. /* force the core awake */
  2217. brcms_c_ucode_wake_override_set(wlc_hw, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
  2218. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2219. if (mc == 0xffffffff) {
  2220. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  2221. __func__);
  2222. brcms_down(wlc->wl);
  2223. return;
  2224. }
  2225. WARN_ON(mc & MCTL_PSM_JMP_0);
  2226. WARN_ON(!(mc & MCTL_PSM_RUN));
  2227. WARN_ON(!(mc & MCTL_EN_MAC));
  2228. mi = bcma_read32(core, D11REGOFFS(macintstatus));
  2229. if (mi == 0xffffffff) {
  2230. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  2231. __func__);
  2232. brcms_down(wlc->wl);
  2233. return;
  2234. }
  2235. WARN_ON(mi & MI_MACSSPNDD);
  2236. brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, 0);
  2237. SPINWAIT(!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD),
  2238. BRCMS_MAX_MAC_SUSPEND);
  2239. if (!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD)) {
  2240. brcms_err(core, "wl%d: wlc_suspend_mac_and_wait: waited %d uS"
  2241. " and MI_MACSSPNDD is still not on.\n",
  2242. wlc_hw->unit, BRCMS_MAX_MAC_SUSPEND);
  2243. brcms_err(core, "wl%d: psmdebug 0x%08x, phydebug 0x%08x, "
  2244. "psm_brc 0x%04x\n", wlc_hw->unit,
  2245. bcma_read32(core, D11REGOFFS(psmdebug)),
  2246. bcma_read32(core, D11REGOFFS(phydebug)),
  2247. bcma_read16(core, D11REGOFFS(psm_brc)));
  2248. }
  2249. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2250. if (mc == 0xffffffff) {
  2251. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  2252. __func__);
  2253. brcms_down(wlc->wl);
  2254. return;
  2255. }
  2256. WARN_ON(mc & MCTL_PSM_JMP_0);
  2257. WARN_ON(!(mc & MCTL_PSM_RUN));
  2258. WARN_ON(mc & MCTL_EN_MAC);
  2259. }
  2260. void brcms_c_enable_mac(struct brcms_c_info *wlc)
  2261. {
  2262. struct brcms_hardware *wlc_hw = wlc->hw;
  2263. struct bcma_device *core = wlc_hw->d11core;
  2264. u32 mc, mi;
  2265. brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
  2266. wlc->band->bandunit);
  2267. /*
  2268. * Track overlapping suspend requests
  2269. */
  2270. wlc_hw->mac_suspend_depth--;
  2271. if (wlc_hw->mac_suspend_depth > 0)
  2272. return;
  2273. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2274. WARN_ON(mc & MCTL_PSM_JMP_0);
  2275. WARN_ON(mc & MCTL_EN_MAC);
  2276. WARN_ON(!(mc & MCTL_PSM_RUN));
  2277. brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, MCTL_EN_MAC);
  2278. bcma_write32(core, D11REGOFFS(macintstatus), MI_MACSSPNDD);
  2279. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2280. WARN_ON(mc & MCTL_PSM_JMP_0);
  2281. WARN_ON(!(mc & MCTL_EN_MAC));
  2282. WARN_ON(!(mc & MCTL_PSM_RUN));
  2283. mi = bcma_read32(core, D11REGOFFS(macintstatus));
  2284. WARN_ON(mi & MI_MACSSPNDD);
  2285. brcms_c_ucode_wake_override_clear(wlc_hw,
  2286. BRCMS_WAKE_OVERRIDE_MACSUSPEND);
  2287. }
  2288. void brcms_b_band_stf_ss_set(struct brcms_hardware *wlc_hw, u8 stf_mode)
  2289. {
  2290. wlc_hw->hw_stf_ss_opmode = stf_mode;
  2291. if (wlc_hw->clk)
  2292. brcms_upd_ofdm_pctl1_table(wlc_hw);
  2293. }
  2294. static bool brcms_b_validate_chip_access(struct brcms_hardware *wlc_hw)
  2295. {
  2296. struct bcma_device *core = wlc_hw->d11core;
  2297. u32 w, val;
  2298. struct wiphy *wiphy = wlc_hw->wlc->wiphy;
  2299. /* Validate dchip register access */
  2300. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2301. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2302. w = bcma_read32(core, D11REGOFFS(objdata));
  2303. /* Can we write and read back a 32bit register? */
  2304. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2305. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2306. bcma_write32(core, D11REGOFFS(objdata), (u32) 0xaa5555aa);
  2307. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2308. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2309. val = bcma_read32(core, D11REGOFFS(objdata));
  2310. if (val != (u32) 0xaa5555aa) {
  2311. wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
  2312. "expected 0xaa5555aa\n", wlc_hw->unit, val);
  2313. return false;
  2314. }
  2315. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2316. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2317. bcma_write32(core, D11REGOFFS(objdata), (u32) 0x55aaaa55);
  2318. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2319. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2320. val = bcma_read32(core, D11REGOFFS(objdata));
  2321. if (val != (u32) 0x55aaaa55) {
  2322. wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
  2323. "expected 0x55aaaa55\n", wlc_hw->unit, val);
  2324. return false;
  2325. }
  2326. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2327. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2328. bcma_write32(core, D11REGOFFS(objdata), w);
  2329. /* clear CFPStart */
  2330. bcma_write32(core, D11REGOFFS(tsf_cfpstart), 0);
  2331. w = bcma_read32(core, D11REGOFFS(maccontrol));
  2332. if ((w != (MCTL_IHR_EN | MCTL_WAKE)) &&
  2333. (w != (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE))) {
  2334. wiphy_err(wiphy, "wl%d: validate_chip_access: maccontrol = "
  2335. "0x%x, expected 0x%x or 0x%x\n", wlc_hw->unit, w,
  2336. (MCTL_IHR_EN | MCTL_WAKE),
  2337. (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE));
  2338. return false;
  2339. }
  2340. return true;
  2341. }
  2342. #define PHYPLL_WAIT_US 100000
  2343. void brcms_b_core_phypll_ctl(struct brcms_hardware *wlc_hw, bool on)
  2344. {
  2345. struct bcma_device *core = wlc_hw->d11core;
  2346. u32 tmp;
  2347. brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
  2348. tmp = 0;
  2349. if (on) {
  2350. if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
  2351. bcma_set32(core, D11REGOFFS(clk_ctl_st),
  2352. CCS_ERSRC_REQ_HT |
  2353. CCS_ERSRC_REQ_D11PLL |
  2354. CCS_ERSRC_REQ_PHYPLL);
  2355. SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
  2356. CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT,
  2357. PHYPLL_WAIT_US);
  2358. tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
  2359. if ((tmp & CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT)
  2360. brcms_err(core, "%s: turn on PHY PLL failed\n",
  2361. __func__);
  2362. } else {
  2363. bcma_set32(core, D11REGOFFS(clk_ctl_st),
  2364. tmp | CCS_ERSRC_REQ_D11PLL |
  2365. CCS_ERSRC_REQ_PHYPLL);
  2366. SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
  2367. (CCS_ERSRC_AVAIL_D11PLL |
  2368. CCS_ERSRC_AVAIL_PHYPLL)) !=
  2369. (CCS_ERSRC_AVAIL_D11PLL |
  2370. CCS_ERSRC_AVAIL_PHYPLL), PHYPLL_WAIT_US);
  2371. tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
  2372. if ((tmp &
  2373. (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
  2374. !=
  2375. (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
  2376. brcms_err(core, "%s: turn on PHY PLL failed\n",
  2377. __func__);
  2378. }
  2379. } else {
  2380. /*
  2381. * Since the PLL may be shared, other cores can still
  2382. * be requesting it; so we'll deassert the request but
  2383. * not wait for status to comply.
  2384. */
  2385. bcma_mask32(core, D11REGOFFS(clk_ctl_st),
  2386. ~CCS_ERSRC_REQ_PHYPLL);
  2387. (void)bcma_read32(core, D11REGOFFS(clk_ctl_st));
  2388. }
  2389. }
  2390. static void brcms_c_coredisable(struct brcms_hardware *wlc_hw)
  2391. {
  2392. bool dev_gone;
  2393. brcms_dbg_info(wlc_hw->d11core, "wl%d: disable core\n", wlc_hw->unit);
  2394. dev_gone = brcms_deviceremoved(wlc_hw->wlc);
  2395. if (dev_gone)
  2396. return;
  2397. if (wlc_hw->noreset)
  2398. return;
  2399. /* radio off */
  2400. wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
  2401. /* turn off analog core */
  2402. wlc_phy_anacore(wlc_hw->band->pi, OFF);
  2403. /* turn off PHYPLL to save power */
  2404. brcms_b_core_phypll_ctl(wlc_hw, false);
  2405. wlc_hw->clk = false;
  2406. bcma_core_disable(wlc_hw->d11core, 0);
  2407. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
  2408. }
  2409. static void brcms_c_flushqueues(struct brcms_c_info *wlc)
  2410. {
  2411. struct brcms_hardware *wlc_hw = wlc->hw;
  2412. uint i;
  2413. /* free any posted tx packets */
  2414. for (i = 0; i < NFIFO; i++) {
  2415. if (wlc_hw->di[i]) {
  2416. dma_txreclaim(wlc_hw->di[i], DMA_RANGE_ALL);
  2417. if (i < TX_BCMC_FIFO)
  2418. ieee80211_wake_queue(wlc->pub->ieee_hw,
  2419. brcms_fifo_to_ac(i));
  2420. }
  2421. }
  2422. /* free any posted rx packets */
  2423. dma_rxreclaim(wlc_hw->di[RX_FIFO]);
  2424. }
  2425. static u16
  2426. brcms_b_read_objmem(struct brcms_hardware *wlc_hw, uint offset, u32 sel)
  2427. {
  2428. struct bcma_device *core = wlc_hw->d11core;
  2429. u16 objoff = D11REGOFFS(objdata);
  2430. bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
  2431. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2432. if (offset & 2)
  2433. objoff += 2;
  2434. return bcma_read16(core, objoff);
  2435. }
  2436. static void
  2437. brcms_b_write_objmem(struct brcms_hardware *wlc_hw, uint offset, u16 v,
  2438. u32 sel)
  2439. {
  2440. struct bcma_device *core = wlc_hw->d11core;
  2441. u16 objoff = D11REGOFFS(objdata);
  2442. bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
  2443. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2444. if (offset & 2)
  2445. objoff += 2;
  2446. bcma_wflush16(core, objoff, v);
  2447. }
  2448. /*
  2449. * Read a single u16 from shared memory.
  2450. * SHM 'offset' needs to be an even address
  2451. */
  2452. u16 brcms_b_read_shm(struct brcms_hardware *wlc_hw, uint offset)
  2453. {
  2454. return brcms_b_read_objmem(wlc_hw, offset, OBJADDR_SHM_SEL);
  2455. }
  2456. /*
  2457. * Write a single u16 to shared memory.
  2458. * SHM 'offset' needs to be an even address
  2459. */
  2460. void brcms_b_write_shm(struct brcms_hardware *wlc_hw, uint offset, u16 v)
  2461. {
  2462. brcms_b_write_objmem(wlc_hw, offset, v, OBJADDR_SHM_SEL);
  2463. }
  2464. /*
  2465. * Copy a buffer to shared memory of specified type .
  2466. * SHM 'offset' needs to be an even address and
  2467. * Buffer length 'len' must be an even number of bytes
  2468. * 'sel' selects the type of memory
  2469. */
  2470. void
  2471. brcms_b_copyto_objmem(struct brcms_hardware *wlc_hw, uint offset,
  2472. const void *buf, int len, u32 sel)
  2473. {
  2474. u16 v;
  2475. const u8 *p = (const u8 *)buf;
  2476. int i;
  2477. if (len <= 0 || (offset & 1) || (len & 1))
  2478. return;
  2479. for (i = 0; i < len; i += 2) {
  2480. v = p[i] | (p[i + 1] << 8);
  2481. brcms_b_write_objmem(wlc_hw, offset + i, v, sel);
  2482. }
  2483. }
  2484. /*
  2485. * Copy a piece of shared memory of specified type to a buffer .
  2486. * SHM 'offset' needs to be an even address and
  2487. * Buffer length 'len' must be an even number of bytes
  2488. * 'sel' selects the type of memory
  2489. */
  2490. void
  2491. brcms_b_copyfrom_objmem(struct brcms_hardware *wlc_hw, uint offset, void *buf,
  2492. int len, u32 sel)
  2493. {
  2494. u16 v;
  2495. u8 *p = (u8 *) buf;
  2496. int i;
  2497. if (len <= 0 || (offset & 1) || (len & 1))
  2498. return;
  2499. for (i = 0; i < len; i += 2) {
  2500. v = brcms_b_read_objmem(wlc_hw, offset + i, sel);
  2501. p[i] = v & 0xFF;
  2502. p[i + 1] = (v >> 8) & 0xFF;
  2503. }
  2504. }
  2505. /* Copy a buffer to shared memory.
  2506. * SHM 'offset' needs to be an even address and
  2507. * Buffer length 'len' must be an even number of bytes
  2508. */
  2509. static void brcms_c_copyto_shm(struct brcms_c_info *wlc, uint offset,
  2510. const void *buf, int len)
  2511. {
  2512. brcms_b_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
  2513. }
  2514. static void brcms_b_retrylimit_upd(struct brcms_hardware *wlc_hw,
  2515. u16 SRL, u16 LRL)
  2516. {
  2517. wlc_hw->SRL = SRL;
  2518. wlc_hw->LRL = LRL;
  2519. /* write retry limit to SCR, shouldn't need to suspend */
  2520. if (wlc_hw->up) {
  2521. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  2522. OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
  2523. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  2524. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->SRL);
  2525. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  2526. OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
  2527. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  2528. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->LRL);
  2529. }
  2530. }
  2531. static void brcms_b_pllreq(struct brcms_hardware *wlc_hw, bool set, u32 req_bit)
  2532. {
  2533. if (set) {
  2534. if (mboolisset(wlc_hw->pllreq, req_bit))
  2535. return;
  2536. mboolset(wlc_hw->pllreq, req_bit);
  2537. if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
  2538. if (!wlc_hw->sbclk)
  2539. brcms_b_xtal(wlc_hw, ON);
  2540. }
  2541. } else {
  2542. if (!mboolisset(wlc_hw->pllreq, req_bit))
  2543. return;
  2544. mboolclr(wlc_hw->pllreq, req_bit);
  2545. if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
  2546. if (wlc_hw->sbclk)
  2547. brcms_b_xtal(wlc_hw, OFF);
  2548. }
  2549. }
  2550. }
  2551. static void brcms_b_antsel_set(struct brcms_hardware *wlc_hw, u32 antsel_avail)
  2552. {
  2553. wlc_hw->antsel_avail = antsel_avail;
  2554. }
  2555. /*
  2556. * conditions under which the PM bit should be set in outgoing frames
  2557. * and STAY_AWAKE is meaningful
  2558. */
  2559. static bool brcms_c_ps_allowed(struct brcms_c_info *wlc)
  2560. {
  2561. /* not supporting PS so always return false for now */
  2562. return false;
  2563. }
  2564. static void brcms_c_statsupd(struct brcms_c_info *wlc)
  2565. {
  2566. int i;
  2567. struct macstat *macstats;
  2568. #ifdef DEBUG
  2569. u16 delta;
  2570. u16 rxf0ovfl;
  2571. u16 txfunfl[NFIFO];
  2572. #endif /* DEBUG */
  2573. /* if driver down, make no sense to update stats */
  2574. if (!wlc->pub->up)
  2575. return;
  2576. macstats = wlc->core->macstat_snapshot;
  2577. #ifdef DEBUG
  2578. /* save last rx fifo 0 overflow count */
  2579. rxf0ovfl = macstats->rxf0ovfl;
  2580. /* save last tx fifo underflow count */
  2581. for (i = 0; i < NFIFO; i++)
  2582. txfunfl[i] = macstats->txfunfl[i];
  2583. #endif /* DEBUG */
  2584. /* Read mac stats from contiguous shared memory */
  2585. brcms_b_copyfrom_objmem(wlc->hw, M_UCODE_MACSTAT, macstats,
  2586. sizeof(*macstats), OBJADDR_SHM_SEL);
  2587. #ifdef DEBUG
  2588. /* check for rx fifo 0 overflow */
  2589. delta = (u16)(macstats->rxf0ovfl - rxf0ovfl);
  2590. if (delta)
  2591. brcms_err(wlc->hw->d11core, "wl%d: %u rx fifo 0 overflows!\n",
  2592. wlc->pub->unit, delta);
  2593. /* check for tx fifo underflows */
  2594. for (i = 0; i < NFIFO; i++) {
  2595. delta = macstats->txfunfl[i] - txfunfl[i];
  2596. if (delta)
  2597. brcms_err(wlc->hw->d11core,
  2598. "wl%d: %u tx fifo %d underflows!\n",
  2599. wlc->pub->unit, delta, i);
  2600. }
  2601. #endif /* DEBUG */
  2602. /* merge counters from dma module */
  2603. for (i = 0; i < NFIFO; i++) {
  2604. if (wlc->hw->di[i])
  2605. dma_counterreset(wlc->hw->di[i]);
  2606. }
  2607. }
  2608. static void brcms_b_reset(struct brcms_hardware *wlc_hw)
  2609. {
  2610. /* reset the core */
  2611. if (!brcms_deviceremoved(wlc_hw->wlc))
  2612. brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
  2613. /* purge the dma rings */
  2614. brcms_c_flushqueues(wlc_hw->wlc);
  2615. }
  2616. void brcms_c_reset(struct brcms_c_info *wlc)
  2617. {
  2618. brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
  2619. /* slurp up hw mac counters before core reset */
  2620. brcms_c_statsupd(wlc);
  2621. /* reset our snapshot of macstat counters */
  2622. memset(wlc->core->macstat_snapshot, 0, sizeof(struct macstat));
  2623. brcms_b_reset(wlc->hw);
  2624. }
  2625. void brcms_c_init_scb(struct scb *scb)
  2626. {
  2627. int i;
  2628. memset(scb, 0, sizeof(struct scb));
  2629. scb->flags = SCB_WMECAP | SCB_HTCAP;
  2630. for (i = 0; i < NUMPRIO; i++) {
  2631. scb->seqnum[i] = 0;
  2632. scb->seqctl[i] = 0xFFFF;
  2633. }
  2634. scb->seqctl_nonqos = 0xFFFF;
  2635. scb->magic = SCB_MAGIC;
  2636. }
  2637. /* d11 core init
  2638. * reset PSM
  2639. * download ucode/PCM
  2640. * let ucode run to suspended
  2641. * download ucode inits
  2642. * config other core registers
  2643. * init dma
  2644. */
  2645. static void brcms_b_coreinit(struct brcms_c_info *wlc)
  2646. {
  2647. struct brcms_hardware *wlc_hw = wlc->hw;
  2648. struct bcma_device *core = wlc_hw->d11core;
  2649. u32 sflags;
  2650. u32 bcnint_us;
  2651. uint i = 0;
  2652. bool fifosz_fixup = false;
  2653. int err = 0;
  2654. u16 buf[NFIFO];
  2655. struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
  2656. brcms_dbg_info(core, "wl%d: core init\n", wlc_hw->unit);
  2657. /* reset PSM */
  2658. brcms_b_mctrl(wlc_hw, ~0, (MCTL_IHR_EN | MCTL_PSM_JMP_0 | MCTL_WAKE));
  2659. brcms_ucode_download(wlc_hw);
  2660. /*
  2661. * FIFOSZ fixup. driver wants to controls the fifo allocation.
  2662. */
  2663. fifosz_fixup = true;
  2664. /* let the PSM run to the suspended state, set mode to BSS STA */
  2665. bcma_write32(core, D11REGOFFS(macintstatus), -1);
  2666. brcms_b_mctrl(wlc_hw, ~0,
  2667. (MCTL_IHR_EN | MCTL_INFRA | MCTL_PSM_RUN | MCTL_WAKE));
  2668. /* wait for ucode to self-suspend after auto-init */
  2669. SPINWAIT(((bcma_read32(core, D11REGOFFS(macintstatus)) &
  2670. MI_MACSSPNDD) == 0), 1000 * 1000);
  2671. if ((bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD) == 0)
  2672. brcms_err(core, "wl%d: wlc_coreinit: ucode did not self-"
  2673. "suspend!\n", wlc_hw->unit);
  2674. brcms_c_gpio_init(wlc);
  2675. sflags = bcma_aread32(core, BCMA_IOST);
  2676. if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
  2677. if (BRCMS_ISNPHY(wlc_hw->band))
  2678. brcms_c_write_inits(wlc_hw, ucode->d11n0initvals16);
  2679. else
  2680. brcms_err(core, "%s: wl%d: unsupported phy in corerev"
  2681. " %d\n", __func__, wlc_hw->unit,
  2682. wlc_hw->corerev);
  2683. } else if (D11REV_IS(wlc_hw->corerev, 24)) {
  2684. if (BRCMS_ISLCNPHY(wlc_hw->band))
  2685. brcms_c_write_inits(wlc_hw, ucode->d11lcn0initvals24);
  2686. else
  2687. brcms_err(core, "%s: wl%d: unsupported phy in corerev"
  2688. " %d\n", __func__, wlc_hw->unit,
  2689. wlc_hw->corerev);
  2690. } else {
  2691. brcms_err(core, "%s: wl%d: unsupported corerev %d\n",
  2692. __func__, wlc_hw->unit, wlc_hw->corerev);
  2693. }
  2694. /* For old ucode, txfifo sizes needs to be modified(increased) */
  2695. if (fifosz_fixup)
  2696. brcms_b_corerev_fifofixup(wlc_hw);
  2697. /* check txfifo allocations match between ucode and driver */
  2698. buf[TX_AC_BE_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE0);
  2699. if (buf[TX_AC_BE_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]) {
  2700. i = TX_AC_BE_FIFO;
  2701. err = -1;
  2702. }
  2703. buf[TX_AC_VI_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE1);
  2704. if (buf[TX_AC_VI_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]) {
  2705. i = TX_AC_VI_FIFO;
  2706. err = -1;
  2707. }
  2708. buf[TX_AC_BK_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE2);
  2709. buf[TX_AC_VO_FIFO] = (buf[TX_AC_BK_FIFO] >> 8) & 0xff;
  2710. buf[TX_AC_BK_FIFO] &= 0xff;
  2711. if (buf[TX_AC_BK_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BK_FIFO]) {
  2712. i = TX_AC_BK_FIFO;
  2713. err = -1;
  2714. }
  2715. if (buf[TX_AC_VO_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO]) {
  2716. i = TX_AC_VO_FIFO;
  2717. err = -1;
  2718. }
  2719. buf[TX_BCMC_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE3);
  2720. buf[TX_ATIM_FIFO] = (buf[TX_BCMC_FIFO] >> 8) & 0xff;
  2721. buf[TX_BCMC_FIFO] &= 0xff;
  2722. if (buf[TX_BCMC_FIFO] != wlc_hw->xmtfifo_sz[TX_BCMC_FIFO]) {
  2723. i = TX_BCMC_FIFO;
  2724. err = -1;
  2725. }
  2726. if (buf[TX_ATIM_FIFO] != wlc_hw->xmtfifo_sz[TX_ATIM_FIFO]) {
  2727. i = TX_ATIM_FIFO;
  2728. err = -1;
  2729. }
  2730. if (err != 0)
  2731. brcms_err(core, "wlc_coreinit: txfifo mismatch: ucode size %d"
  2732. " driver size %d index %d\n", buf[i],
  2733. wlc_hw->xmtfifo_sz[i], i);
  2734. /* make sure we can still talk to the mac */
  2735. WARN_ON(bcma_read32(core, D11REGOFFS(maccontrol)) == 0xffffffff);
  2736. /* band-specific inits done by wlc_bsinit() */
  2737. /* Set up frame burst size and antenna swap threshold init values */
  2738. brcms_b_write_shm(wlc_hw, M_MBURST_SIZE, MAXTXFRAMEBURST);
  2739. brcms_b_write_shm(wlc_hw, M_MAX_ANTCNT, ANTCNT);
  2740. /* enable one rx interrupt per received frame */
  2741. bcma_write32(core, D11REGOFFS(intrcvlazy[0]), (1 << IRL_FC_SHIFT));
  2742. /* set the station mode (BSS STA) */
  2743. brcms_b_mctrl(wlc_hw,
  2744. (MCTL_INFRA | MCTL_DISCARD_PMQ | MCTL_AP),
  2745. (MCTL_INFRA | MCTL_DISCARD_PMQ));
  2746. /* set up Beacon interval */
  2747. bcnint_us = 0x8000 << 10;
  2748. bcma_write32(core, D11REGOFFS(tsf_cfprep),
  2749. (bcnint_us << CFPREP_CBI_SHIFT));
  2750. bcma_write32(core, D11REGOFFS(tsf_cfpstart), bcnint_us);
  2751. bcma_write32(core, D11REGOFFS(macintstatus), MI_GP1);
  2752. /* write interrupt mask */
  2753. bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intmask),
  2754. DEF_RXINTMASK);
  2755. /* allow the MAC to control the PHY clock (dynamic on/off) */
  2756. brcms_b_macphyclk_set(wlc_hw, ON);
  2757. /* program dynamic clock control fast powerup delay register */
  2758. wlc->fastpwrup_dly = ai_clkctl_fast_pwrup_delay(wlc_hw->sih);
  2759. bcma_write16(core, D11REGOFFS(scc_fastpwrup_dly), wlc->fastpwrup_dly);
  2760. /* tell the ucode the corerev */
  2761. brcms_b_write_shm(wlc_hw, M_MACHW_VER, (u16) wlc_hw->corerev);
  2762. /* tell the ucode MAC capabilities */
  2763. brcms_b_write_shm(wlc_hw, M_MACHW_CAP_L,
  2764. (u16) (wlc_hw->machwcap & 0xffff));
  2765. brcms_b_write_shm(wlc_hw, M_MACHW_CAP_H,
  2766. (u16) ((wlc_hw->
  2767. machwcap >> 16) & 0xffff));
  2768. /* write retry limits to SCR, this done after PSM init */
  2769. bcma_write32(core, D11REGOFFS(objaddr),
  2770. OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
  2771. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2772. bcma_write32(core, D11REGOFFS(objdata), wlc_hw->SRL);
  2773. bcma_write32(core, D11REGOFFS(objaddr),
  2774. OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
  2775. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2776. bcma_write32(core, D11REGOFFS(objdata), wlc_hw->LRL);
  2777. /* write rate fallback retry limits */
  2778. brcms_b_write_shm(wlc_hw, M_SFRMTXCNTFBRTHSD, wlc_hw->SFBL);
  2779. brcms_b_write_shm(wlc_hw, M_LFRMTXCNTFBRTHSD, wlc_hw->LFBL);
  2780. bcma_mask16(core, D11REGOFFS(ifs_ctl), 0x0FFF);
  2781. bcma_write16(core, D11REGOFFS(ifs_aifsn), EDCF_AIFSN_MIN);
  2782. /* init the tx dma engines */
  2783. for (i = 0; i < NFIFO; i++) {
  2784. if (wlc_hw->di[i])
  2785. dma_txinit(wlc_hw->di[i]);
  2786. }
  2787. /* init the rx dma engine(s) and post receive buffers */
  2788. dma_rxinit(wlc_hw->di[RX_FIFO]);
  2789. dma_rxfill(wlc_hw->di[RX_FIFO]);
  2790. }
  2791. static void brcms_b_init(struct brcms_hardware *wlc_hw, u16 chanspec)
  2792. {
  2793. u32 macintmask;
  2794. bool fastclk;
  2795. struct brcms_c_info *wlc = wlc_hw->wlc;
  2796. /* request FAST clock if not on */
  2797. fastclk = wlc_hw->forcefastclk;
  2798. if (!fastclk)
  2799. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  2800. /* disable interrupts */
  2801. macintmask = brcms_intrsoff(wlc->wl);
  2802. /* set up the specified band and chanspec */
  2803. brcms_c_setxband(wlc_hw, chspec_bandunit(chanspec));
  2804. wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
  2805. /* do one-time phy inits and calibration */
  2806. wlc_phy_cal_init(wlc_hw->band->pi);
  2807. /* core-specific initialization */
  2808. brcms_b_coreinit(wlc);
  2809. /* band-specific inits */
  2810. brcms_b_bsinit(wlc, chanspec);
  2811. /* restore macintmask */
  2812. brcms_intrsrestore(wlc->wl, macintmask);
  2813. /* seed wake_override with BRCMS_WAKE_OVERRIDE_MACSUSPEND since the mac
  2814. * is suspended and brcms_c_enable_mac() will clear this override bit.
  2815. */
  2816. mboolset(wlc_hw->wake_override, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
  2817. /*
  2818. * initialize mac_suspend_depth to 1 to match ucode
  2819. * initial suspended state
  2820. */
  2821. wlc_hw->mac_suspend_depth = 1;
  2822. /* restore the clk */
  2823. if (!fastclk)
  2824. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
  2825. }
  2826. static void brcms_c_set_phy_chanspec(struct brcms_c_info *wlc,
  2827. u16 chanspec)
  2828. {
  2829. /* Save our copy of the chanspec */
  2830. wlc->chanspec = chanspec;
  2831. /* Set the chanspec and power limits for this locale */
  2832. brcms_c_channel_set_chanspec(wlc->cmi, chanspec, BRCMS_TXPWR_MAX);
  2833. if (wlc->stf->ss_algosel_auto)
  2834. brcms_c_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
  2835. chanspec);
  2836. brcms_c_stf_ss_update(wlc, wlc->band);
  2837. }
  2838. static void
  2839. brcms_default_rateset(struct brcms_c_info *wlc, struct brcms_c_rateset *rs)
  2840. {
  2841. brcms_c_rateset_default(rs, NULL, wlc->band->phytype,
  2842. wlc->band->bandtype, false, BRCMS_RATE_MASK_FULL,
  2843. (bool) (wlc->pub->_n_enab & SUPPORT_11N),
  2844. brcms_chspec_bw(wlc->default_bss->chanspec),
  2845. wlc->stf->txstreams);
  2846. }
  2847. /* derive wlc->band->basic_rate[] table from 'rateset' */
  2848. static void brcms_c_rate_lookup_init(struct brcms_c_info *wlc,
  2849. struct brcms_c_rateset *rateset)
  2850. {
  2851. u8 rate;
  2852. u8 mandatory;
  2853. u8 cck_basic = 0;
  2854. u8 ofdm_basic = 0;
  2855. u8 *br = wlc->band->basic_rate;
  2856. uint i;
  2857. /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
  2858. memset(br, 0, BRCM_MAXRATE + 1);
  2859. /* For each basic rate in the rates list, make an entry in the
  2860. * best basic lookup.
  2861. */
  2862. for (i = 0; i < rateset->count; i++) {
  2863. /* only make an entry for a basic rate */
  2864. if (!(rateset->rates[i] & BRCMS_RATE_FLAG))
  2865. continue;
  2866. /* mask off basic bit */
  2867. rate = (rateset->rates[i] & BRCMS_RATE_MASK);
  2868. if (rate > BRCM_MAXRATE) {
  2869. brcms_err(wlc->hw->d11core, "brcms_c_rate_lookup_init: "
  2870. "invalid rate 0x%X in rate set\n",
  2871. rateset->rates[i]);
  2872. continue;
  2873. }
  2874. br[rate] = rate;
  2875. }
  2876. /* The rate lookup table now has non-zero entries for each
  2877. * basic rate, equal to the basic rate: br[basicN] = basicN
  2878. *
  2879. * To look up the best basic rate corresponding to any
  2880. * particular rate, code can use the basic_rate table
  2881. * like this
  2882. *
  2883. * basic_rate = wlc->band->basic_rate[tx_rate]
  2884. *
  2885. * Make sure there is a best basic rate entry for
  2886. * every rate by walking up the table from low rates
  2887. * to high, filling in holes in the lookup table
  2888. */
  2889. for (i = 0; i < wlc->band->hw_rateset.count; i++) {
  2890. rate = wlc->band->hw_rateset.rates[i];
  2891. if (br[rate] != 0) {
  2892. /* This rate is a basic rate.
  2893. * Keep track of the best basic rate so far by
  2894. * modulation type.
  2895. */
  2896. if (is_ofdm_rate(rate))
  2897. ofdm_basic = rate;
  2898. else
  2899. cck_basic = rate;
  2900. continue;
  2901. }
  2902. /* This rate is not a basic rate so figure out the
  2903. * best basic rate less than this rate and fill in
  2904. * the hole in the table
  2905. */
  2906. br[rate] = is_ofdm_rate(rate) ? ofdm_basic : cck_basic;
  2907. if (br[rate] != 0)
  2908. continue;
  2909. if (is_ofdm_rate(rate)) {
  2910. /*
  2911. * In 11g and 11a, the OFDM mandatory rates
  2912. * are 6, 12, and 24 Mbps
  2913. */
  2914. if (rate >= BRCM_RATE_24M)
  2915. mandatory = BRCM_RATE_24M;
  2916. else if (rate >= BRCM_RATE_12M)
  2917. mandatory = BRCM_RATE_12M;
  2918. else
  2919. mandatory = BRCM_RATE_6M;
  2920. } else {
  2921. /* In 11b, all CCK rates are mandatory 1 - 11 Mbps */
  2922. mandatory = rate;
  2923. }
  2924. br[rate] = mandatory;
  2925. }
  2926. }
  2927. static void brcms_c_bandinit_ordered(struct brcms_c_info *wlc,
  2928. u16 chanspec)
  2929. {
  2930. struct brcms_c_rateset default_rateset;
  2931. uint parkband;
  2932. uint i, band_order[2];
  2933. /*
  2934. * We might have been bandlocked during down and the chip
  2935. * power-cycled (hibernate). Figure out the right band to park on
  2936. */
  2937. if (wlc->bandlocked || wlc->pub->_nbands == 1) {
  2938. /* updated in brcms_c_bandlock() */
  2939. parkband = wlc->band->bandunit;
  2940. band_order[0] = band_order[1] = parkband;
  2941. } else {
  2942. /* park on the band of the specified chanspec */
  2943. parkband = chspec_bandunit(chanspec);
  2944. /* order so that parkband initialize last */
  2945. band_order[0] = parkband ^ 1;
  2946. band_order[1] = parkband;
  2947. }
  2948. /* make each band operational, software state init */
  2949. for (i = 0; i < wlc->pub->_nbands; i++) {
  2950. uint j = band_order[i];
  2951. wlc->band = wlc->bandstate[j];
  2952. brcms_default_rateset(wlc, &default_rateset);
  2953. /* fill in hw_rate */
  2954. brcms_c_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
  2955. false, BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
  2956. (bool) (wlc->pub->_n_enab & SUPPORT_11N));
  2957. /* init basic rate lookup */
  2958. brcms_c_rate_lookup_init(wlc, &default_rateset);
  2959. }
  2960. /* sync up phy/radio chanspec */
  2961. brcms_c_set_phy_chanspec(wlc, chanspec);
  2962. }
  2963. /*
  2964. * Set or clear filtering related maccontrol bits based on
  2965. * specified filter flags
  2966. */
  2967. void brcms_c_mac_promisc(struct brcms_c_info *wlc, uint filter_flags)
  2968. {
  2969. u32 promisc_bits = 0;
  2970. wlc->filter_flags = filter_flags;
  2971. if (filter_flags & FIF_OTHER_BSS)
  2972. promisc_bits |= MCTL_PROMISC;
  2973. if (filter_flags & FIF_BCN_PRBRESP_PROMISC)
  2974. promisc_bits |= MCTL_BCNS_PROMISC;
  2975. if (filter_flags & FIF_FCSFAIL)
  2976. promisc_bits |= MCTL_KEEPBADFCS;
  2977. if (filter_flags & (FIF_CONTROL | FIF_PSPOLL))
  2978. promisc_bits |= MCTL_KEEPCONTROL;
  2979. brcms_b_mctrl(wlc->hw,
  2980. MCTL_PROMISC | MCTL_BCNS_PROMISC |
  2981. MCTL_KEEPCONTROL | MCTL_KEEPBADFCS,
  2982. promisc_bits);
  2983. }
  2984. /*
  2985. * ucode, hwmac update
  2986. * Channel dependent updates for ucode and hw
  2987. */
  2988. static void brcms_c_ucode_mac_upd(struct brcms_c_info *wlc)
  2989. {
  2990. /* enable or disable any active IBSSs depending on whether or not
  2991. * we are on the home channel
  2992. */
  2993. if (wlc->home_chanspec == wlc_phy_chanspec_get(wlc->band->pi)) {
  2994. if (wlc->pub->associated) {
  2995. /*
  2996. * BMAC_NOTE: This is something that should be fixed
  2997. * in ucode inits. I think that the ucode inits set
  2998. * up the bcn templates and shm values with a bogus
  2999. * beacon. This should not be done in the inits. If
  3000. * ucode needs to set up a beacon for testing, the
  3001. * test routines should write it down, not expect the
  3002. * inits to populate a bogus beacon.
  3003. */
  3004. if (BRCMS_PHY_11N_CAP(wlc->band))
  3005. brcms_b_write_shm(wlc->hw,
  3006. M_BCN_TXTSF_OFFSET, 0);
  3007. }
  3008. } else {
  3009. /* disable an active IBSS if we are not on the home channel */
  3010. }
  3011. }
  3012. static void brcms_c_write_rate_shm(struct brcms_c_info *wlc, u8 rate,
  3013. u8 basic_rate)
  3014. {
  3015. u8 phy_rate, index;
  3016. u8 basic_phy_rate, basic_index;
  3017. u16 dir_table, basic_table;
  3018. u16 basic_ptr;
  3019. /* Shared memory address for the table we are reading */
  3020. dir_table = is_ofdm_rate(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
  3021. /* Shared memory address for the table we are writing */
  3022. basic_table = is_ofdm_rate(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
  3023. /*
  3024. * for a given rate, the LS-nibble of the PLCP SIGNAL field is
  3025. * the index into the rate table.
  3026. */
  3027. phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
  3028. basic_phy_rate = rate_info[basic_rate] & BRCMS_RATE_MASK;
  3029. index = phy_rate & 0xf;
  3030. basic_index = basic_phy_rate & 0xf;
  3031. /* Find the SHM pointer to the ACK rate entry by looking in the
  3032. * Direct-map Table
  3033. */
  3034. basic_ptr = brcms_b_read_shm(wlc->hw, (dir_table + basic_index * 2));
  3035. /* Update the SHM BSS-basic-rate-set mapping table with the pointer
  3036. * to the correct basic rate for the given incoming rate
  3037. */
  3038. brcms_b_write_shm(wlc->hw, (basic_table + index * 2), basic_ptr);
  3039. }
  3040. static const struct brcms_c_rateset *
  3041. brcms_c_rateset_get_hwrs(struct brcms_c_info *wlc)
  3042. {
  3043. const struct brcms_c_rateset *rs_dflt;
  3044. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  3045. if (wlc->band->bandtype == BRCM_BAND_5G)
  3046. rs_dflt = &ofdm_mimo_rates;
  3047. else
  3048. rs_dflt = &cck_ofdm_mimo_rates;
  3049. } else if (wlc->band->gmode)
  3050. rs_dflt = &cck_ofdm_rates;
  3051. else
  3052. rs_dflt = &cck_rates;
  3053. return rs_dflt;
  3054. }
  3055. static void brcms_c_set_ratetable(struct brcms_c_info *wlc)
  3056. {
  3057. const struct brcms_c_rateset *rs_dflt;
  3058. struct brcms_c_rateset rs;
  3059. u8 rate, basic_rate;
  3060. uint i;
  3061. rs_dflt = brcms_c_rateset_get_hwrs(wlc);
  3062. brcms_c_rateset_copy(rs_dflt, &rs);
  3063. brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
  3064. /* walk the phy rate table and update SHM basic rate lookup table */
  3065. for (i = 0; i < rs.count; i++) {
  3066. rate = rs.rates[i] & BRCMS_RATE_MASK;
  3067. /* for a given rate brcms_basic_rate returns the rate at
  3068. * which a response ACK/CTS should be sent.
  3069. */
  3070. basic_rate = brcms_basic_rate(wlc, rate);
  3071. if (basic_rate == 0)
  3072. /* This should only happen if we are using a
  3073. * restricted rateset.
  3074. */
  3075. basic_rate = rs.rates[0] & BRCMS_RATE_MASK;
  3076. brcms_c_write_rate_shm(wlc, rate, basic_rate);
  3077. }
  3078. }
  3079. /* band-specific init */
  3080. static void brcms_c_bsinit(struct brcms_c_info *wlc)
  3081. {
  3082. brcms_dbg_info(wlc->hw->d11core, "wl%d: bandunit %d\n",
  3083. wlc->pub->unit, wlc->band->bandunit);
  3084. /* write ucode ACK/CTS rate table */
  3085. brcms_c_set_ratetable(wlc);
  3086. /* update some band specific mac configuration */
  3087. brcms_c_ucode_mac_upd(wlc);
  3088. /* init antenna selection */
  3089. brcms_c_antsel_init(wlc->asi);
  3090. }
  3091. /* formula: IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
  3092. static int
  3093. brcms_c_duty_cycle_set(struct brcms_c_info *wlc, int duty_cycle, bool isOFDM,
  3094. bool writeToShm)
  3095. {
  3096. int idle_busy_ratio_x_16 = 0;
  3097. uint offset =
  3098. isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
  3099. M_TX_IDLE_BUSY_RATIO_X_16_CCK;
  3100. if (duty_cycle > 100 || duty_cycle < 0) {
  3101. brcms_err(wlc->hw->d11core,
  3102. "wl%d: duty cycle value off limit\n",
  3103. wlc->pub->unit);
  3104. return -EINVAL;
  3105. }
  3106. if (duty_cycle)
  3107. idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
  3108. /* Only write to shared memory when wl is up */
  3109. if (writeToShm)
  3110. brcms_b_write_shm(wlc->hw, offset, (u16) idle_busy_ratio_x_16);
  3111. if (isOFDM)
  3112. wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
  3113. else
  3114. wlc->tx_duty_cycle_cck = (u16) duty_cycle;
  3115. return 0;
  3116. }
  3117. /* push sw hps and wake state through hardware */
  3118. static void brcms_c_set_ps_ctrl(struct brcms_c_info *wlc)
  3119. {
  3120. u32 v1, v2;
  3121. bool hps;
  3122. bool awake_before;
  3123. hps = brcms_c_ps_allowed(wlc);
  3124. brcms_dbg_mac80211(wlc->hw->d11core, "wl%d: hps %d\n", wlc->pub->unit,
  3125. hps);
  3126. v1 = bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
  3127. v2 = MCTL_WAKE;
  3128. if (hps)
  3129. v2 |= MCTL_HPS;
  3130. brcms_b_mctrl(wlc->hw, MCTL_WAKE | MCTL_HPS, v2);
  3131. awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
  3132. if (!awake_before)
  3133. brcms_b_wait_for_wake(wlc->hw);
  3134. }
  3135. /*
  3136. * Write this BSS config's MAC address to core.
  3137. * Updates RXE match engine.
  3138. */
  3139. static int brcms_c_set_mac(struct brcms_bss_cfg *bsscfg)
  3140. {
  3141. int err = 0;
  3142. struct brcms_c_info *wlc = bsscfg->wlc;
  3143. /* enter the MAC addr into the RXE match registers */
  3144. brcms_c_set_addrmatch(wlc, RCM_MAC_OFFSET, wlc->pub->cur_etheraddr);
  3145. brcms_c_ampdu_macaddr_upd(wlc);
  3146. return err;
  3147. }
  3148. /* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
  3149. * Updates RXE match engine.
  3150. */
  3151. static void brcms_c_set_bssid(struct brcms_bss_cfg *bsscfg)
  3152. {
  3153. /* we need to update BSSID in RXE match registers */
  3154. brcms_c_set_addrmatch(bsscfg->wlc, RCM_BSSID_OFFSET, bsscfg->BSSID);
  3155. }
  3156. void brcms_c_set_ssid(struct brcms_c_info *wlc, u8 *ssid, size_t ssid_len)
  3157. {
  3158. u8 len = min_t(u8, sizeof(wlc->bsscfg->SSID), ssid_len);
  3159. memset(wlc->bsscfg->SSID, 0, sizeof(wlc->bsscfg->SSID));
  3160. memcpy(wlc->bsscfg->SSID, ssid, len);
  3161. wlc->bsscfg->SSID_len = len;
  3162. }
  3163. static void brcms_b_set_shortslot(struct brcms_hardware *wlc_hw, bool shortslot)
  3164. {
  3165. wlc_hw->shortslot = shortslot;
  3166. if (wlc_hw->band->bandtype == BRCM_BAND_2G && wlc_hw->up) {
  3167. brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
  3168. brcms_b_update_slot_timing(wlc_hw, shortslot);
  3169. brcms_c_enable_mac(wlc_hw->wlc);
  3170. }
  3171. }
  3172. /*
  3173. * Suspend the the MAC and update the slot timing
  3174. * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
  3175. */
  3176. static void brcms_c_switch_shortslot(struct brcms_c_info *wlc, bool shortslot)
  3177. {
  3178. /* use the override if it is set */
  3179. if (wlc->shortslot_override != BRCMS_SHORTSLOT_AUTO)
  3180. shortslot = (wlc->shortslot_override == BRCMS_SHORTSLOT_ON);
  3181. if (wlc->shortslot == shortslot)
  3182. return;
  3183. wlc->shortslot = shortslot;
  3184. brcms_b_set_shortslot(wlc->hw, shortslot);
  3185. }
  3186. static void brcms_c_set_home_chanspec(struct brcms_c_info *wlc, u16 chanspec)
  3187. {
  3188. if (wlc->home_chanspec != chanspec) {
  3189. wlc->home_chanspec = chanspec;
  3190. if (wlc->pub->associated)
  3191. wlc->bsscfg->current_bss->chanspec = chanspec;
  3192. }
  3193. }
  3194. void
  3195. brcms_b_set_chanspec(struct brcms_hardware *wlc_hw, u16 chanspec,
  3196. bool mute_tx, struct txpwr_limits *txpwr)
  3197. {
  3198. uint bandunit;
  3199. brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: 0x%x\n", wlc_hw->unit,
  3200. chanspec);
  3201. wlc_hw->chanspec = chanspec;
  3202. /* Switch bands if necessary */
  3203. if (wlc_hw->_nbands > 1) {
  3204. bandunit = chspec_bandunit(chanspec);
  3205. if (wlc_hw->band->bandunit != bandunit) {
  3206. /* brcms_b_setband disables other bandunit,
  3207. * use light band switch if not up yet
  3208. */
  3209. if (wlc_hw->up) {
  3210. wlc_phy_chanspec_radio_set(wlc_hw->
  3211. bandstate[bandunit]->
  3212. pi, chanspec);
  3213. brcms_b_setband(wlc_hw, bandunit, chanspec);
  3214. } else {
  3215. brcms_c_setxband(wlc_hw, bandunit);
  3216. }
  3217. }
  3218. }
  3219. wlc_phy_initcal_enable(wlc_hw->band->pi, !mute_tx);
  3220. if (!wlc_hw->up) {
  3221. if (wlc_hw->clk)
  3222. wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr,
  3223. chanspec);
  3224. wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
  3225. } else {
  3226. wlc_phy_chanspec_set(wlc_hw->band->pi, chanspec);
  3227. wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr, chanspec);
  3228. /* Update muting of the channel */
  3229. brcms_b_mute(wlc_hw, mute_tx);
  3230. }
  3231. }
  3232. /* switch to and initialize new band */
  3233. static void brcms_c_setband(struct brcms_c_info *wlc,
  3234. uint bandunit)
  3235. {
  3236. wlc->band = wlc->bandstate[bandunit];
  3237. if (!wlc->pub->up)
  3238. return;
  3239. /* wait for at least one beacon before entering sleeping state */
  3240. brcms_c_set_ps_ctrl(wlc);
  3241. /* band-specific initializations */
  3242. brcms_c_bsinit(wlc);
  3243. }
  3244. static void brcms_c_set_chanspec(struct brcms_c_info *wlc, u16 chanspec)
  3245. {
  3246. uint bandunit;
  3247. bool switchband = false;
  3248. u16 old_chanspec = wlc->chanspec;
  3249. if (!brcms_c_valid_chanspec_db(wlc->cmi, chanspec)) {
  3250. brcms_err(wlc->hw->d11core, "wl%d: %s: Bad channel %d\n",
  3251. wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
  3252. return;
  3253. }
  3254. /* Switch bands if necessary */
  3255. if (wlc->pub->_nbands > 1) {
  3256. bandunit = chspec_bandunit(chanspec);
  3257. if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
  3258. switchband = true;
  3259. if (wlc->bandlocked) {
  3260. brcms_err(wlc->hw->d11core,
  3261. "wl%d: %s: chspec %d band is locked!\n",
  3262. wlc->pub->unit, __func__,
  3263. CHSPEC_CHANNEL(chanspec));
  3264. return;
  3265. }
  3266. /*
  3267. * should the setband call come after the
  3268. * brcms_b_chanspec() ? if the setband updates
  3269. * (brcms_c_bsinit) use low level calls to inspect and
  3270. * set state, the state inspected may be from the wrong
  3271. * band, or the following brcms_b_set_chanspec() may
  3272. * undo the work.
  3273. */
  3274. brcms_c_setband(wlc, bandunit);
  3275. }
  3276. }
  3277. /* sync up phy/radio chanspec */
  3278. brcms_c_set_phy_chanspec(wlc, chanspec);
  3279. /* init antenna selection */
  3280. if (brcms_chspec_bw(old_chanspec) != brcms_chspec_bw(chanspec)) {
  3281. brcms_c_antsel_init(wlc->asi);
  3282. /* Fix the hardware rateset based on bw.
  3283. * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
  3284. */
  3285. brcms_c_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
  3286. wlc->band->mimo_cap_40 ? brcms_chspec_bw(chanspec) : 0);
  3287. }
  3288. /* update some mac configuration since chanspec changed */
  3289. brcms_c_ucode_mac_upd(wlc);
  3290. }
  3291. /*
  3292. * This function changes the phytxctl for beacon based on current
  3293. * beacon ratespec AND txant setting as per this table:
  3294. * ratespec CCK ant = wlc->stf->txant
  3295. * OFDM ant = 3
  3296. */
  3297. void brcms_c_beacon_phytxctl_txant_upd(struct brcms_c_info *wlc,
  3298. u32 bcn_rspec)
  3299. {
  3300. u16 phyctl;
  3301. u16 phytxant = wlc->stf->phytxant;
  3302. u16 mask = PHY_TXC_ANT_MASK;
  3303. /* for non-siso rates or default setting, use the available chains */
  3304. if (BRCMS_PHY_11N_CAP(wlc->band))
  3305. phytxant = brcms_c_stf_phytxchain_sel(wlc, bcn_rspec);
  3306. phyctl = brcms_b_read_shm(wlc->hw, M_BCN_PCTLWD);
  3307. phyctl = (phyctl & ~mask) | phytxant;
  3308. brcms_b_write_shm(wlc->hw, M_BCN_PCTLWD, phyctl);
  3309. }
  3310. /*
  3311. * centralized protection config change function to simplify debugging, no
  3312. * consistency checking this should be called only on changes to avoid overhead
  3313. * in periodic function
  3314. */
  3315. void brcms_c_protection_upd(struct brcms_c_info *wlc, uint idx, int val)
  3316. {
  3317. /*
  3318. * Cannot use brcms_dbg_* here because this function is called
  3319. * before wlc is sufficiently initialized.
  3320. */
  3321. BCMMSG(wlc->wiphy, "idx %d, val %d\n", idx, val);
  3322. switch (idx) {
  3323. case BRCMS_PROT_G_SPEC:
  3324. wlc->protection->_g = (bool) val;
  3325. break;
  3326. case BRCMS_PROT_G_OVR:
  3327. wlc->protection->g_override = (s8) val;
  3328. break;
  3329. case BRCMS_PROT_G_USER:
  3330. wlc->protection->gmode_user = (u8) val;
  3331. break;
  3332. case BRCMS_PROT_OVERLAP:
  3333. wlc->protection->overlap = (s8) val;
  3334. break;
  3335. case BRCMS_PROT_N_USER:
  3336. wlc->protection->nmode_user = (s8) val;
  3337. break;
  3338. case BRCMS_PROT_N_CFG:
  3339. wlc->protection->n_cfg = (s8) val;
  3340. break;
  3341. case BRCMS_PROT_N_CFG_OVR:
  3342. wlc->protection->n_cfg_override = (s8) val;
  3343. break;
  3344. case BRCMS_PROT_N_NONGF:
  3345. wlc->protection->nongf = (bool) val;
  3346. break;
  3347. case BRCMS_PROT_N_NONGF_OVR:
  3348. wlc->protection->nongf_override = (s8) val;
  3349. break;
  3350. case BRCMS_PROT_N_PAM_OVR:
  3351. wlc->protection->n_pam_override = (s8) val;
  3352. break;
  3353. case BRCMS_PROT_N_OBSS:
  3354. wlc->protection->n_obss = (bool) val;
  3355. break;
  3356. default:
  3357. break;
  3358. }
  3359. }
  3360. static void brcms_c_ht_update_sgi_rx(struct brcms_c_info *wlc, int val)
  3361. {
  3362. if (wlc->pub->up) {
  3363. brcms_c_update_beacon(wlc);
  3364. brcms_c_update_probe_resp(wlc, true);
  3365. }
  3366. }
  3367. static void brcms_c_ht_update_ldpc(struct brcms_c_info *wlc, s8 val)
  3368. {
  3369. wlc->stf->ldpc = val;
  3370. if (wlc->pub->up) {
  3371. brcms_c_update_beacon(wlc);
  3372. brcms_c_update_probe_resp(wlc, true);
  3373. wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
  3374. }
  3375. }
  3376. void brcms_c_wme_setparams(struct brcms_c_info *wlc, u16 aci,
  3377. const struct ieee80211_tx_queue_params *params,
  3378. bool suspend)
  3379. {
  3380. int i;
  3381. struct shm_acparams acp_shm;
  3382. u16 *shm_entry;
  3383. /* Only apply params if the core is out of reset and has clocks */
  3384. if (!wlc->clk) {
  3385. brcms_err(wlc->hw->d11core, "wl%d: %s : no-clock\n",
  3386. wlc->pub->unit, __func__);
  3387. return;
  3388. }
  3389. memset(&acp_shm, 0, sizeof(struct shm_acparams));
  3390. /* fill in shm ac params struct */
  3391. acp_shm.txop = params->txop;
  3392. /* convert from units of 32us to us for ucode */
  3393. wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
  3394. EDCF_TXOP2USEC(acp_shm.txop);
  3395. acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
  3396. if (aci == IEEE80211_AC_VI && acp_shm.txop == 0
  3397. && acp_shm.aifs < EDCF_AIFSN_MAX)
  3398. acp_shm.aifs++;
  3399. if (acp_shm.aifs < EDCF_AIFSN_MIN
  3400. || acp_shm.aifs > EDCF_AIFSN_MAX) {
  3401. brcms_err(wlc->hw->d11core, "wl%d: edcf_setparams: bad "
  3402. "aifs %d\n", wlc->pub->unit, acp_shm.aifs);
  3403. } else {
  3404. acp_shm.cwmin = params->cw_min;
  3405. acp_shm.cwmax = params->cw_max;
  3406. acp_shm.cwcur = acp_shm.cwmin;
  3407. acp_shm.bslots =
  3408. bcma_read16(wlc->hw->d11core, D11REGOFFS(tsf_random)) &
  3409. acp_shm.cwcur;
  3410. acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
  3411. /* Indicate the new params to the ucode */
  3412. acp_shm.status = brcms_b_read_shm(wlc->hw, (M_EDCF_QINFO +
  3413. wme_ac2fifo[aci] *
  3414. M_EDCF_QLEN +
  3415. M_EDCF_STATUS_OFF));
  3416. acp_shm.status |= WME_STATUS_NEWAC;
  3417. /* Fill in shm acparam table */
  3418. shm_entry = (u16 *) &acp_shm;
  3419. for (i = 0; i < (int)sizeof(struct shm_acparams); i += 2)
  3420. brcms_b_write_shm(wlc->hw,
  3421. M_EDCF_QINFO +
  3422. wme_ac2fifo[aci] * M_EDCF_QLEN + i,
  3423. *shm_entry++);
  3424. }
  3425. if (suspend)
  3426. brcms_c_suspend_mac_and_wait(wlc);
  3427. brcms_c_update_beacon(wlc);
  3428. brcms_c_update_probe_resp(wlc, false);
  3429. if (suspend)
  3430. brcms_c_enable_mac(wlc);
  3431. }
  3432. static void brcms_c_edcf_setparams(struct brcms_c_info *wlc, bool suspend)
  3433. {
  3434. u16 aci;
  3435. int i_ac;
  3436. struct ieee80211_tx_queue_params txq_pars;
  3437. static const struct edcf_acparam default_edcf_acparams[] = {
  3438. {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA, EDCF_AC_BE_TXOP_STA},
  3439. {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA, EDCF_AC_BK_TXOP_STA},
  3440. {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA, EDCF_AC_VI_TXOP_STA},
  3441. {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA, EDCF_AC_VO_TXOP_STA}
  3442. }; /* ucode needs these parameters during its initialization */
  3443. const struct edcf_acparam *edcf_acp = &default_edcf_acparams[0];
  3444. for (i_ac = 0; i_ac < IEEE80211_NUM_ACS; i_ac++, edcf_acp++) {
  3445. /* find out which ac this set of params applies to */
  3446. aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
  3447. /* fill in shm ac params struct */
  3448. txq_pars.txop = edcf_acp->TXOP;
  3449. txq_pars.aifs = edcf_acp->ACI;
  3450. /* CWmin = 2^(ECWmin) - 1 */
  3451. txq_pars.cw_min = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
  3452. /* CWmax = 2^(ECWmax) - 1 */
  3453. txq_pars.cw_max = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
  3454. >> EDCF_ECWMAX_SHIFT);
  3455. brcms_c_wme_setparams(wlc, aci, &txq_pars, suspend);
  3456. }
  3457. if (suspend) {
  3458. brcms_c_suspend_mac_and_wait(wlc);
  3459. brcms_c_enable_mac(wlc);
  3460. }
  3461. }
  3462. static void brcms_c_radio_monitor_start(struct brcms_c_info *wlc)
  3463. {
  3464. /* Don't start the timer if HWRADIO feature is disabled */
  3465. if (wlc->radio_monitor)
  3466. return;
  3467. wlc->radio_monitor = true;
  3468. brcms_b_pllreq(wlc->hw, true, BRCMS_PLLREQ_RADIO_MON);
  3469. brcms_add_timer(wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
  3470. }
  3471. static bool brcms_c_radio_monitor_stop(struct brcms_c_info *wlc)
  3472. {
  3473. if (!wlc->radio_monitor)
  3474. return true;
  3475. wlc->radio_monitor = false;
  3476. brcms_b_pllreq(wlc->hw, false, BRCMS_PLLREQ_RADIO_MON);
  3477. return brcms_del_timer(wlc->radio_timer);
  3478. }
  3479. /* read hwdisable state and propagate to wlc flag */
  3480. static void brcms_c_radio_hwdisable_upd(struct brcms_c_info *wlc)
  3481. {
  3482. if (wlc->pub->hw_off)
  3483. return;
  3484. if (brcms_b_radio_read_hwdisabled(wlc->hw))
  3485. mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
  3486. else
  3487. mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
  3488. }
  3489. /* update hwradio status and return it */
  3490. bool brcms_c_check_radio_disabled(struct brcms_c_info *wlc)
  3491. {
  3492. brcms_c_radio_hwdisable_upd(wlc);
  3493. return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ?
  3494. true : false;
  3495. }
  3496. /* periodical query hw radio button while driver is "down" */
  3497. static void brcms_c_radio_timer(void *arg)
  3498. {
  3499. struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
  3500. if (brcms_deviceremoved(wlc)) {
  3501. brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
  3502. wlc->pub->unit, __func__);
  3503. brcms_down(wlc->wl);
  3504. return;
  3505. }
  3506. brcms_c_radio_hwdisable_upd(wlc);
  3507. }
  3508. /* common low-level watchdog code */
  3509. static void brcms_b_watchdog(struct brcms_c_info *wlc)
  3510. {
  3511. struct brcms_hardware *wlc_hw = wlc->hw;
  3512. if (!wlc_hw->up)
  3513. return;
  3514. /* increment second count */
  3515. wlc_hw->now++;
  3516. /* Check for FIFO error interrupts */
  3517. brcms_b_fifoerrors(wlc_hw);
  3518. /* make sure RX dma has buffers */
  3519. dma_rxfill(wlc->hw->di[RX_FIFO]);
  3520. wlc_phy_watchdog(wlc_hw->band->pi);
  3521. }
  3522. /* common watchdog code */
  3523. static void brcms_c_watchdog(struct brcms_c_info *wlc)
  3524. {
  3525. brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
  3526. if (!wlc->pub->up)
  3527. return;
  3528. if (brcms_deviceremoved(wlc)) {
  3529. brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
  3530. wlc->pub->unit, __func__);
  3531. brcms_down(wlc->wl);
  3532. return;
  3533. }
  3534. /* increment second count */
  3535. wlc->pub->now++;
  3536. brcms_c_radio_hwdisable_upd(wlc);
  3537. /* if radio is disable, driver may be down, quit here */
  3538. if (wlc->pub->radio_disabled)
  3539. return;
  3540. brcms_b_watchdog(wlc);
  3541. /*
  3542. * occasionally sample mac stat counters to
  3543. * detect 16-bit counter wrap
  3544. */
  3545. if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
  3546. brcms_c_statsupd(wlc);
  3547. if (BRCMS_ISNPHY(wlc->band) &&
  3548. ((wlc->pub->now - wlc->tempsense_lasttime) >=
  3549. BRCMS_TEMPSENSE_PERIOD)) {
  3550. wlc->tempsense_lasttime = wlc->pub->now;
  3551. brcms_c_tempsense_upd(wlc);
  3552. }
  3553. }
  3554. static void brcms_c_watchdog_by_timer(void *arg)
  3555. {
  3556. struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
  3557. brcms_c_watchdog(wlc);
  3558. }
  3559. static bool brcms_c_timers_init(struct brcms_c_info *wlc, int unit)
  3560. {
  3561. wlc->wdtimer = brcms_init_timer(wlc->wl, brcms_c_watchdog_by_timer,
  3562. wlc, "watchdog");
  3563. if (!wlc->wdtimer) {
  3564. wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for wdtimer "
  3565. "failed\n", unit);
  3566. goto fail;
  3567. }
  3568. wlc->radio_timer = brcms_init_timer(wlc->wl, brcms_c_radio_timer,
  3569. wlc, "radio");
  3570. if (!wlc->radio_timer) {
  3571. wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for radio_timer "
  3572. "failed\n", unit);
  3573. goto fail;
  3574. }
  3575. return true;
  3576. fail:
  3577. return false;
  3578. }
  3579. /*
  3580. * Initialize brcms_c_info default values ...
  3581. * may get overrides later in this function
  3582. */
  3583. static void brcms_c_info_init(struct brcms_c_info *wlc, int unit)
  3584. {
  3585. int i;
  3586. /* Save our copy of the chanspec */
  3587. wlc->chanspec = ch20mhz_chspec(1);
  3588. /* various 802.11g modes */
  3589. wlc->shortslot = false;
  3590. wlc->shortslot_override = BRCMS_SHORTSLOT_AUTO;
  3591. brcms_c_protection_upd(wlc, BRCMS_PROT_G_OVR, BRCMS_PROTECTION_AUTO);
  3592. brcms_c_protection_upd(wlc, BRCMS_PROT_G_SPEC, false);
  3593. brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG_OVR,
  3594. BRCMS_PROTECTION_AUTO);
  3595. brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG, BRCMS_N_PROTECTION_OFF);
  3596. brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF_OVR,
  3597. BRCMS_PROTECTION_AUTO);
  3598. brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF, false);
  3599. brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, AUTO);
  3600. brcms_c_protection_upd(wlc, BRCMS_PROT_OVERLAP,
  3601. BRCMS_PROTECTION_CTL_OVERLAP);
  3602. /* 802.11g draft 4.0 NonERP elt advertisement */
  3603. wlc->include_legacy_erp = true;
  3604. wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
  3605. wlc->stf->txant = ANT_TX_DEF;
  3606. wlc->prb_resp_timeout = BRCMS_PRB_RESP_TIMEOUT;
  3607. wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
  3608. for (i = 0; i < NFIFO; i++)
  3609. wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
  3610. wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
  3611. /* default rate fallback retry limits */
  3612. wlc->SFBL = RETRY_SHORT_FB;
  3613. wlc->LFBL = RETRY_LONG_FB;
  3614. /* default mac retry limits */
  3615. wlc->SRL = RETRY_SHORT_DEF;
  3616. wlc->LRL = RETRY_LONG_DEF;
  3617. /* WME QoS mode is Auto by default */
  3618. wlc->pub->_ampdu = AMPDU_AGG_HOST;
  3619. }
  3620. static uint brcms_c_attach_module(struct brcms_c_info *wlc)
  3621. {
  3622. uint err = 0;
  3623. uint unit;
  3624. unit = wlc->pub->unit;
  3625. wlc->asi = brcms_c_antsel_attach(wlc);
  3626. if (wlc->asi == NULL) {
  3627. wiphy_err(wlc->wiphy, "wl%d: attach: antsel_attach "
  3628. "failed\n", unit);
  3629. err = 44;
  3630. goto fail;
  3631. }
  3632. wlc->ampdu = brcms_c_ampdu_attach(wlc);
  3633. if (wlc->ampdu == NULL) {
  3634. wiphy_err(wlc->wiphy, "wl%d: attach: ampdu_attach "
  3635. "failed\n", unit);
  3636. err = 50;
  3637. goto fail;
  3638. }
  3639. if ((brcms_c_stf_attach(wlc) != 0)) {
  3640. wiphy_err(wlc->wiphy, "wl%d: attach: stf_attach "
  3641. "failed\n", unit);
  3642. err = 68;
  3643. goto fail;
  3644. }
  3645. fail:
  3646. return err;
  3647. }
  3648. struct brcms_pub *brcms_c_pub(struct brcms_c_info *wlc)
  3649. {
  3650. return wlc->pub;
  3651. }
  3652. /* low level attach
  3653. * run backplane attach, init nvram
  3654. * run phy attach
  3655. * initialize software state for each core and band
  3656. * put the whole chip in reset(driver down state), no clock
  3657. */
  3658. static int brcms_b_attach(struct brcms_c_info *wlc, struct bcma_device *core,
  3659. uint unit, bool piomode)
  3660. {
  3661. struct brcms_hardware *wlc_hw;
  3662. uint err = 0;
  3663. uint j;
  3664. bool wme = false;
  3665. struct shared_phy_params sha_params;
  3666. struct wiphy *wiphy = wlc->wiphy;
  3667. struct pci_dev *pcidev = core->bus->host_pci;
  3668. struct ssb_sprom *sprom = &core->bus->sprom;
  3669. if (core->bus->hosttype == BCMA_HOSTTYPE_PCI)
  3670. brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
  3671. pcidev->vendor,
  3672. pcidev->device);
  3673. else
  3674. brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
  3675. core->bus->boardinfo.vendor,
  3676. core->bus->boardinfo.type);
  3677. wme = true;
  3678. wlc_hw = wlc->hw;
  3679. wlc_hw->wlc = wlc;
  3680. wlc_hw->unit = unit;
  3681. wlc_hw->band = wlc_hw->bandstate[0];
  3682. wlc_hw->_piomode = piomode;
  3683. /* populate struct brcms_hardware with default values */
  3684. brcms_b_info_init(wlc_hw);
  3685. /*
  3686. * Do the hardware portion of the attach. Also initialize software
  3687. * state that depends on the particular hardware we are running.
  3688. */
  3689. wlc_hw->sih = ai_attach(core->bus);
  3690. if (wlc_hw->sih == NULL) {
  3691. wiphy_err(wiphy, "wl%d: brcms_b_attach: si_attach failed\n",
  3692. unit);
  3693. err = 11;
  3694. goto fail;
  3695. }
  3696. /* verify again the device is supported */
  3697. if (!brcms_c_chipmatch(core)) {
  3698. wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported device\n",
  3699. unit);
  3700. err = 12;
  3701. goto fail;
  3702. }
  3703. if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
  3704. wlc_hw->vendorid = pcidev->vendor;
  3705. wlc_hw->deviceid = pcidev->device;
  3706. } else {
  3707. wlc_hw->vendorid = core->bus->boardinfo.vendor;
  3708. wlc_hw->deviceid = core->bus->boardinfo.type;
  3709. }
  3710. wlc_hw->d11core = core;
  3711. wlc_hw->corerev = core->id.rev;
  3712. /* validate chip, chiprev and corerev */
  3713. if (!brcms_c_isgoodchip(wlc_hw)) {
  3714. err = 13;
  3715. goto fail;
  3716. }
  3717. /* initialize power control registers */
  3718. ai_clkctl_init(wlc_hw->sih);
  3719. /* request fastclock and force fastclock for the rest of attach
  3720. * bring the d11 core out of reset.
  3721. * For PMU chips, the first wlc_clkctl_clk is no-op since core-clk
  3722. * is still false; But it will be called again inside wlc_corereset,
  3723. * after d11 is out of reset.
  3724. */
  3725. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  3726. brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
  3727. if (!brcms_b_validate_chip_access(wlc_hw)) {
  3728. wiphy_err(wiphy, "wl%d: brcms_b_attach: validate_chip_access "
  3729. "failed\n", unit);
  3730. err = 14;
  3731. goto fail;
  3732. }
  3733. /* get the board rev, used just below */
  3734. j = sprom->board_rev;
  3735. /* promote srom boardrev of 0xFF to 1 */
  3736. if (j == BOARDREV_PROMOTABLE)
  3737. j = BOARDREV_PROMOTED;
  3738. wlc_hw->boardrev = (u16) j;
  3739. if (!brcms_c_validboardtype(wlc_hw)) {
  3740. wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported Broadcom "
  3741. "board type (0x%x)" " or revision level (0x%x)\n",
  3742. unit, ai_get_boardtype(wlc_hw->sih),
  3743. wlc_hw->boardrev);
  3744. err = 15;
  3745. goto fail;
  3746. }
  3747. wlc_hw->sromrev = sprom->revision;
  3748. wlc_hw->boardflags = sprom->boardflags_lo + (sprom->boardflags_hi << 16);
  3749. wlc_hw->boardflags2 = sprom->boardflags2_lo + (sprom->boardflags2_hi << 16);
  3750. if (wlc_hw->boardflags & BFL_NOPLLDOWN)
  3751. brcms_b_pllreq(wlc_hw, true, BRCMS_PLLREQ_SHARED);
  3752. /* check device id(srom, nvram etc.) to set bands */
  3753. if (wlc_hw->deviceid == BCM43224_D11N_ID ||
  3754. wlc_hw->deviceid == BCM43224_D11N_ID_VEN1 ||
  3755. wlc_hw->deviceid == BCM43224_CHIP_ID)
  3756. /* Dualband boards */
  3757. wlc_hw->_nbands = 2;
  3758. else
  3759. wlc_hw->_nbands = 1;
  3760. if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225))
  3761. wlc_hw->_nbands = 1;
  3762. /* BMAC_NOTE: remove init of pub values when brcms_c_attach()
  3763. * unconditionally does the init of these values
  3764. */
  3765. wlc->vendorid = wlc_hw->vendorid;
  3766. wlc->deviceid = wlc_hw->deviceid;
  3767. wlc->pub->sih = wlc_hw->sih;
  3768. wlc->pub->corerev = wlc_hw->corerev;
  3769. wlc->pub->sromrev = wlc_hw->sromrev;
  3770. wlc->pub->boardrev = wlc_hw->boardrev;
  3771. wlc->pub->boardflags = wlc_hw->boardflags;
  3772. wlc->pub->boardflags2 = wlc_hw->boardflags2;
  3773. wlc->pub->_nbands = wlc_hw->_nbands;
  3774. wlc_hw->physhim = wlc_phy_shim_attach(wlc_hw, wlc->wl, wlc);
  3775. if (wlc_hw->physhim == NULL) {
  3776. wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_shim_attach "
  3777. "failed\n", unit);
  3778. err = 25;
  3779. goto fail;
  3780. }
  3781. /* pass all the parameters to wlc_phy_shared_attach in one struct */
  3782. sha_params.sih = wlc_hw->sih;
  3783. sha_params.physhim = wlc_hw->physhim;
  3784. sha_params.unit = unit;
  3785. sha_params.corerev = wlc_hw->corerev;
  3786. sha_params.vid = wlc_hw->vendorid;
  3787. sha_params.did = wlc_hw->deviceid;
  3788. sha_params.chip = ai_get_chip_id(wlc_hw->sih);
  3789. sha_params.chiprev = ai_get_chiprev(wlc_hw->sih);
  3790. sha_params.chippkg = ai_get_chippkg(wlc_hw->sih);
  3791. sha_params.sromrev = wlc_hw->sromrev;
  3792. sha_params.boardtype = ai_get_boardtype(wlc_hw->sih);
  3793. sha_params.boardrev = wlc_hw->boardrev;
  3794. sha_params.boardflags = wlc_hw->boardflags;
  3795. sha_params.boardflags2 = wlc_hw->boardflags2;
  3796. /* alloc and save pointer to shared phy state area */
  3797. wlc_hw->phy_sh = wlc_phy_shared_attach(&sha_params);
  3798. if (!wlc_hw->phy_sh) {
  3799. err = 16;
  3800. goto fail;
  3801. }
  3802. /* initialize software state for each core and band */
  3803. for (j = 0; j < wlc_hw->_nbands; j++) {
  3804. /*
  3805. * band0 is always 2.4Ghz
  3806. * band1, if present, is 5Ghz
  3807. */
  3808. brcms_c_setxband(wlc_hw, j);
  3809. wlc_hw->band->bandunit = j;
  3810. wlc_hw->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
  3811. wlc->band->bandunit = j;
  3812. wlc->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
  3813. wlc->core->coreidx = core->core_index;
  3814. wlc_hw->machwcap = bcma_read32(core, D11REGOFFS(machwcap));
  3815. wlc_hw->machwcap_backup = wlc_hw->machwcap;
  3816. /* init tx fifo size */
  3817. WARN_ON(wlc_hw->corerev < XMTFIFOTBL_STARTREV ||
  3818. (wlc_hw->corerev - XMTFIFOTBL_STARTREV) >
  3819. ARRAY_SIZE(xmtfifo_sz));
  3820. wlc_hw->xmtfifo_sz =
  3821. xmtfifo_sz[(wlc_hw->corerev - XMTFIFOTBL_STARTREV)];
  3822. WARN_ON(!wlc_hw->xmtfifo_sz[0]);
  3823. /* Get a phy for this band */
  3824. wlc_hw->band->pi =
  3825. wlc_phy_attach(wlc_hw->phy_sh, core,
  3826. wlc_hw->band->bandtype,
  3827. wlc->wiphy);
  3828. if (wlc_hw->band->pi == NULL) {
  3829. wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_"
  3830. "attach failed\n", unit);
  3831. err = 17;
  3832. goto fail;
  3833. }
  3834. wlc_phy_machwcap_set(wlc_hw->band->pi, wlc_hw->machwcap);
  3835. wlc_phy_get_phyversion(wlc_hw->band->pi, &wlc_hw->band->phytype,
  3836. &wlc_hw->band->phyrev,
  3837. &wlc_hw->band->radioid,
  3838. &wlc_hw->band->radiorev);
  3839. wlc_hw->band->abgphy_encore =
  3840. wlc_phy_get_encore(wlc_hw->band->pi);
  3841. wlc->band->abgphy_encore = wlc_phy_get_encore(wlc_hw->band->pi);
  3842. wlc_hw->band->core_flags =
  3843. wlc_phy_get_coreflags(wlc_hw->band->pi);
  3844. /* verify good phy_type & supported phy revision */
  3845. if (BRCMS_ISNPHY(wlc_hw->band)) {
  3846. if (NCONF_HAS(wlc_hw->band->phyrev))
  3847. goto good_phy;
  3848. else
  3849. goto bad_phy;
  3850. } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
  3851. if (LCNCONF_HAS(wlc_hw->band->phyrev))
  3852. goto good_phy;
  3853. else
  3854. goto bad_phy;
  3855. } else {
  3856. bad_phy:
  3857. wiphy_err(wiphy, "wl%d: brcms_b_attach: unsupported "
  3858. "phy type/rev (%d/%d)\n", unit,
  3859. wlc_hw->band->phytype, wlc_hw->band->phyrev);
  3860. err = 18;
  3861. goto fail;
  3862. }
  3863. good_phy:
  3864. /*
  3865. * BMAC_NOTE: wlc->band->pi should not be set below and should
  3866. * be done in the high level attach. However we can not make
  3867. * that change until all low level access is changed to
  3868. * wlc_hw->band->pi. Instead do the wlc->band->pi init below,
  3869. * keeping wlc_hw->band->pi as well for incremental update of
  3870. * low level fns, and cut over low only init when all fns
  3871. * updated.
  3872. */
  3873. wlc->band->pi = wlc_hw->band->pi;
  3874. wlc->band->phytype = wlc_hw->band->phytype;
  3875. wlc->band->phyrev = wlc_hw->band->phyrev;
  3876. wlc->band->radioid = wlc_hw->band->radioid;
  3877. wlc->band->radiorev = wlc_hw->band->radiorev;
  3878. brcms_dbg_info(core, "wl%d: phy %u/%u radio %x/%u\n", unit,
  3879. wlc->band->phytype, wlc->band->phyrev,
  3880. wlc->band->radioid, wlc->band->radiorev);
  3881. /* default contention windows size limits */
  3882. wlc_hw->band->CWmin = APHY_CWMIN;
  3883. wlc_hw->band->CWmax = PHY_CWMAX;
  3884. if (!brcms_b_attach_dmapio(wlc, j, wme)) {
  3885. err = 19;
  3886. goto fail;
  3887. }
  3888. }
  3889. /* disable core to match driver "down" state */
  3890. brcms_c_coredisable(wlc_hw);
  3891. /* Match driver "down" state */
  3892. bcma_host_pci_down(wlc_hw->d11core->bus);
  3893. /* turn off pll and xtal to match driver "down" state */
  3894. brcms_b_xtal(wlc_hw, OFF);
  3895. /* *******************************************************************
  3896. * The hardware is in the DOWN state at this point. D11 core
  3897. * or cores are in reset with clocks off, and the board PLLs
  3898. * are off if possible.
  3899. *
  3900. * Beyond this point, wlc->sbclk == false and chip registers
  3901. * should not be touched.
  3902. *********************************************************************
  3903. */
  3904. /* init etheraddr state variables */
  3905. brcms_c_get_macaddr(wlc_hw, wlc_hw->etheraddr);
  3906. if (is_broadcast_ether_addr(wlc_hw->etheraddr) ||
  3907. is_zero_ether_addr(wlc_hw->etheraddr)) {
  3908. wiphy_err(wiphy, "wl%d: brcms_b_attach: bad macaddr\n",
  3909. unit);
  3910. err = 22;
  3911. goto fail;
  3912. }
  3913. brcms_dbg_info(wlc_hw->d11core, "deviceid 0x%x nbands %d board 0x%x\n",
  3914. wlc_hw->deviceid, wlc_hw->_nbands,
  3915. ai_get_boardtype(wlc_hw->sih));
  3916. return err;
  3917. fail:
  3918. wiphy_err(wiphy, "wl%d: brcms_b_attach: failed with err %d\n", unit,
  3919. err);
  3920. return err;
  3921. }
  3922. static bool brcms_c_attach_stf_ant_init(struct brcms_c_info *wlc)
  3923. {
  3924. int aa;
  3925. uint unit;
  3926. int bandtype;
  3927. struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
  3928. unit = wlc->pub->unit;
  3929. bandtype = wlc->band->bandtype;
  3930. /* get antennas available */
  3931. if (bandtype == BRCM_BAND_5G)
  3932. aa = sprom->ant_available_a;
  3933. else
  3934. aa = sprom->ant_available_bg;
  3935. if ((aa < 1) || (aa > 15)) {
  3936. wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
  3937. " srom (0x%x), using 3\n", unit, __func__, aa);
  3938. aa = 3;
  3939. }
  3940. /* reset the defaults if we have a single antenna */
  3941. if (aa == 1) {
  3942. wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
  3943. wlc->stf->txant = ANT_TX_FORCE_0;
  3944. } else if (aa == 2) {
  3945. wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
  3946. wlc->stf->txant = ANT_TX_FORCE_1;
  3947. } else {
  3948. }
  3949. /* Compute Antenna Gain */
  3950. if (bandtype == BRCM_BAND_5G)
  3951. wlc->band->antgain = sprom->antenna_gain.a1;
  3952. else
  3953. wlc->band->antgain = sprom->antenna_gain.a0;
  3954. return true;
  3955. }
  3956. static void brcms_c_bss_default_init(struct brcms_c_info *wlc)
  3957. {
  3958. u16 chanspec;
  3959. struct brcms_band *band;
  3960. struct brcms_bss_info *bi = wlc->default_bss;
  3961. /* init default and target BSS with some sane initial values */
  3962. memset(bi, 0, sizeof(*bi));
  3963. bi->beacon_period = BEACON_INTERVAL_DEFAULT;
  3964. /* fill the default channel as the first valid channel
  3965. * starting from the 2G channels
  3966. */
  3967. chanspec = ch20mhz_chspec(1);
  3968. wlc->home_chanspec = bi->chanspec = chanspec;
  3969. /* find the band of our default channel */
  3970. band = wlc->band;
  3971. if (wlc->pub->_nbands > 1 &&
  3972. band->bandunit != chspec_bandunit(chanspec))
  3973. band = wlc->bandstate[OTHERBANDUNIT(wlc)];
  3974. /* init bss rates to the band specific default rate set */
  3975. brcms_c_rateset_default(&bi->rateset, NULL, band->phytype,
  3976. band->bandtype, false, BRCMS_RATE_MASK_FULL,
  3977. (bool) (wlc->pub->_n_enab & SUPPORT_11N),
  3978. brcms_chspec_bw(chanspec), wlc->stf->txstreams);
  3979. if (wlc->pub->_n_enab & SUPPORT_11N)
  3980. bi->flags |= BRCMS_BSS_HT;
  3981. }
  3982. static void brcms_c_update_mimo_band_bwcap(struct brcms_c_info *wlc, u8 bwcap)
  3983. {
  3984. uint i;
  3985. struct brcms_band *band;
  3986. for (i = 0; i < wlc->pub->_nbands; i++) {
  3987. band = wlc->bandstate[i];
  3988. if (band->bandtype == BRCM_BAND_5G) {
  3989. if ((bwcap == BRCMS_N_BW_40ALL)
  3990. || (bwcap == BRCMS_N_BW_20IN2G_40IN5G))
  3991. band->mimo_cap_40 = true;
  3992. else
  3993. band->mimo_cap_40 = false;
  3994. } else {
  3995. if (bwcap == BRCMS_N_BW_40ALL)
  3996. band->mimo_cap_40 = true;
  3997. else
  3998. band->mimo_cap_40 = false;
  3999. }
  4000. }
  4001. }
  4002. static void brcms_c_timers_deinit(struct brcms_c_info *wlc)
  4003. {
  4004. /* free timer state */
  4005. if (wlc->wdtimer) {
  4006. brcms_free_timer(wlc->wdtimer);
  4007. wlc->wdtimer = NULL;
  4008. }
  4009. if (wlc->radio_timer) {
  4010. brcms_free_timer(wlc->radio_timer);
  4011. wlc->radio_timer = NULL;
  4012. }
  4013. }
  4014. static void brcms_c_detach_module(struct brcms_c_info *wlc)
  4015. {
  4016. if (wlc->asi) {
  4017. brcms_c_antsel_detach(wlc->asi);
  4018. wlc->asi = NULL;
  4019. }
  4020. if (wlc->ampdu) {
  4021. brcms_c_ampdu_detach(wlc->ampdu);
  4022. wlc->ampdu = NULL;
  4023. }
  4024. brcms_c_stf_detach(wlc);
  4025. }
  4026. /*
  4027. * low level detach
  4028. */
  4029. static void brcms_b_detach(struct brcms_c_info *wlc)
  4030. {
  4031. uint i;
  4032. struct brcms_hw_band *band;
  4033. struct brcms_hardware *wlc_hw = wlc->hw;
  4034. brcms_b_detach_dmapio(wlc_hw);
  4035. band = wlc_hw->band;
  4036. for (i = 0; i < wlc_hw->_nbands; i++) {
  4037. if (band->pi) {
  4038. /* Detach this band's phy */
  4039. wlc_phy_detach(band->pi);
  4040. band->pi = NULL;
  4041. }
  4042. band = wlc_hw->bandstate[OTHERBANDUNIT(wlc)];
  4043. }
  4044. /* Free shared phy state */
  4045. kfree(wlc_hw->phy_sh);
  4046. wlc_phy_shim_detach(wlc_hw->physhim);
  4047. if (wlc_hw->sih) {
  4048. ai_detach(wlc_hw->sih);
  4049. wlc_hw->sih = NULL;
  4050. }
  4051. }
  4052. /*
  4053. * Return a count of the number of driver callbacks still pending.
  4054. *
  4055. * General policy is that brcms_c_detach can only dealloc/free software states.
  4056. * It can NOT touch hardware registers since the d11core may be in reset and
  4057. * clock may not be available.
  4058. * One exception is sb register access, which is possible if crystal is turned
  4059. * on after "down" state, driver should avoid software timer with the exception
  4060. * of radio_monitor.
  4061. */
  4062. uint brcms_c_detach(struct brcms_c_info *wlc)
  4063. {
  4064. uint callbacks;
  4065. if (wlc == NULL)
  4066. return 0;
  4067. brcms_b_detach(wlc);
  4068. /* delete software timers */
  4069. callbacks = 0;
  4070. if (!brcms_c_radio_monitor_stop(wlc))
  4071. callbacks++;
  4072. brcms_c_channel_mgr_detach(wlc->cmi);
  4073. brcms_c_timers_deinit(wlc);
  4074. brcms_c_detach_module(wlc);
  4075. brcms_c_detach_mfree(wlc);
  4076. return callbacks;
  4077. }
  4078. /* update state that depends on the current value of "ap" */
  4079. static void brcms_c_ap_upd(struct brcms_c_info *wlc)
  4080. {
  4081. /* STA-BSS; short capable */
  4082. wlc->PLCPHdr_override = BRCMS_PLCP_SHORT;
  4083. }
  4084. /* Initialize just the hardware when coming out of POR or S3/S5 system states */
  4085. static void brcms_b_hw_up(struct brcms_hardware *wlc_hw)
  4086. {
  4087. if (wlc_hw->wlc->pub->hw_up)
  4088. return;
  4089. brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  4090. /*
  4091. * Enable pll and xtal, initialize the power control registers,
  4092. * and force fastclock for the remainder of brcms_c_up().
  4093. */
  4094. brcms_b_xtal(wlc_hw, ON);
  4095. ai_clkctl_init(wlc_hw->sih);
  4096. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  4097. /*
  4098. * TODO: test suspend/resume
  4099. *
  4100. * AI chip doesn't restore bar0win2 on
  4101. * hibernation/resume, need sw fixup
  4102. */
  4103. /*
  4104. * Inform phy that a POR reset has occurred so
  4105. * it does a complete phy init
  4106. */
  4107. wlc_phy_por_inform(wlc_hw->band->pi);
  4108. wlc_hw->ucode_loaded = false;
  4109. wlc_hw->wlc->pub->hw_up = true;
  4110. if ((wlc_hw->boardflags & BFL_FEM)
  4111. && (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
  4112. if (!
  4113. (wlc_hw->boardrev >= 0x1250
  4114. && (wlc_hw->boardflags & BFL_FEM_BT)))
  4115. ai_epa_4313war(wlc_hw->sih);
  4116. }
  4117. }
  4118. static int brcms_b_up_prep(struct brcms_hardware *wlc_hw)
  4119. {
  4120. brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  4121. /*
  4122. * Enable pll and xtal, initialize the power control registers,
  4123. * and force fastclock for the remainder of brcms_c_up().
  4124. */
  4125. brcms_b_xtal(wlc_hw, ON);
  4126. ai_clkctl_init(wlc_hw->sih);
  4127. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  4128. /*
  4129. * Configure pci/pcmcia here instead of in brcms_c_attach()
  4130. * to allow mfg hotswap: down, hotswap (chip power cycle), up.
  4131. */
  4132. bcma_host_pci_irq_ctl(wlc_hw->d11core->bus, wlc_hw->d11core,
  4133. true);
  4134. /*
  4135. * Need to read the hwradio status here to cover the case where the
  4136. * system is loaded with the hw radio disabled. We do not want to
  4137. * bring the driver up in this case.
  4138. */
  4139. if (brcms_b_radio_read_hwdisabled(wlc_hw)) {
  4140. /* put SB PCI in down state again */
  4141. bcma_host_pci_down(wlc_hw->d11core->bus);
  4142. brcms_b_xtal(wlc_hw, OFF);
  4143. return -ENOMEDIUM;
  4144. }
  4145. bcma_host_pci_up(wlc_hw->d11core->bus);
  4146. /* reset the d11 core */
  4147. brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
  4148. return 0;
  4149. }
  4150. static int brcms_b_up_finish(struct brcms_hardware *wlc_hw)
  4151. {
  4152. wlc_hw->up = true;
  4153. wlc_phy_hw_state_upd(wlc_hw->band->pi, true);
  4154. /* FULLY enable dynamic power control and d11 core interrupt */
  4155. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
  4156. brcms_intrson(wlc_hw->wlc->wl);
  4157. return 0;
  4158. }
  4159. /*
  4160. * Write WME tunable parameters for retransmit/max rate
  4161. * from wlc struct to ucode
  4162. */
  4163. static void brcms_c_wme_retries_write(struct brcms_c_info *wlc)
  4164. {
  4165. int ac;
  4166. /* Need clock to do this */
  4167. if (!wlc->clk)
  4168. return;
  4169. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  4170. brcms_b_write_shm(wlc->hw, M_AC_TXLMT_ADDR(ac),
  4171. wlc->wme_retries[ac]);
  4172. }
  4173. /* make interface operational */
  4174. int brcms_c_up(struct brcms_c_info *wlc)
  4175. {
  4176. struct ieee80211_channel *ch;
  4177. brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
  4178. /* HW is turned off so don't try to access it */
  4179. if (wlc->pub->hw_off || brcms_deviceremoved(wlc))
  4180. return -ENOMEDIUM;
  4181. if (!wlc->pub->hw_up) {
  4182. brcms_b_hw_up(wlc->hw);
  4183. wlc->pub->hw_up = true;
  4184. }
  4185. if ((wlc->pub->boardflags & BFL_FEM)
  4186. && (ai_get_chip_id(wlc->hw->sih) == BCMA_CHIP_ID_BCM4313)) {
  4187. if (wlc->pub->boardrev >= 0x1250
  4188. && (wlc->pub->boardflags & BFL_FEM_BT))
  4189. brcms_b_mhf(wlc->hw, MHF5, MHF5_4313_GPIOCTRL,
  4190. MHF5_4313_GPIOCTRL, BRCM_BAND_ALL);
  4191. else
  4192. brcms_b_mhf(wlc->hw, MHF4, MHF4_EXTPA_ENABLE,
  4193. MHF4_EXTPA_ENABLE, BRCM_BAND_ALL);
  4194. }
  4195. /*
  4196. * Need to read the hwradio status here to cover the case where the
  4197. * system is loaded with the hw radio disabled. We do not want to bring
  4198. * the driver up in this case. If radio is disabled, abort up, lower
  4199. * power, start radio timer and return 0(for NDIS) don't call
  4200. * radio_update to avoid looping brcms_c_up.
  4201. *
  4202. * brcms_b_up_prep() returns either 0 or -BCME_RADIOOFF only
  4203. */
  4204. if (!wlc->pub->radio_disabled) {
  4205. int status = brcms_b_up_prep(wlc->hw);
  4206. if (status == -ENOMEDIUM) {
  4207. if (!mboolisset
  4208. (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
  4209. struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
  4210. mboolset(wlc->pub->radio_disabled,
  4211. WL_RADIO_HW_DISABLE);
  4212. if (bsscfg->type == BRCMS_TYPE_STATION ||
  4213. bsscfg->type == BRCMS_TYPE_ADHOC)
  4214. brcms_err(wlc->hw->d11core,
  4215. "wl%d: up: rfdisable -> "
  4216. "bsscfg_disable()\n",
  4217. wlc->pub->unit);
  4218. }
  4219. }
  4220. }
  4221. if (wlc->pub->radio_disabled) {
  4222. brcms_c_radio_monitor_start(wlc);
  4223. return 0;
  4224. }
  4225. /* brcms_b_up_prep has done brcms_c_corereset(). so clk is on, set it */
  4226. wlc->clk = true;
  4227. brcms_c_radio_monitor_stop(wlc);
  4228. /* Set EDCF hostflags */
  4229. brcms_b_mhf(wlc->hw, MHF1, MHF1_EDCF, MHF1_EDCF, BRCM_BAND_ALL);
  4230. brcms_init(wlc->wl);
  4231. wlc->pub->up = true;
  4232. if (wlc->bandinit_pending) {
  4233. ch = wlc->pub->ieee_hw->conf.chandef.chan;
  4234. brcms_c_suspend_mac_and_wait(wlc);
  4235. brcms_c_set_chanspec(wlc, ch20mhz_chspec(ch->hw_value));
  4236. wlc->bandinit_pending = false;
  4237. brcms_c_enable_mac(wlc);
  4238. }
  4239. brcms_b_up_finish(wlc->hw);
  4240. /* Program the TX wme params with the current settings */
  4241. brcms_c_wme_retries_write(wlc);
  4242. /* start one second watchdog timer */
  4243. brcms_add_timer(wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
  4244. wlc->WDarmed = true;
  4245. /* ensure antenna config is up to date */
  4246. brcms_c_stf_phy_txant_upd(wlc);
  4247. /* ensure LDPC config is in sync */
  4248. brcms_c_ht_update_ldpc(wlc, wlc->stf->ldpc);
  4249. return 0;
  4250. }
  4251. static uint brcms_c_down_del_timer(struct brcms_c_info *wlc)
  4252. {
  4253. uint callbacks = 0;
  4254. return callbacks;
  4255. }
  4256. static int brcms_b_bmac_down_prep(struct brcms_hardware *wlc_hw)
  4257. {
  4258. bool dev_gone;
  4259. uint callbacks = 0;
  4260. if (!wlc_hw->up)
  4261. return callbacks;
  4262. dev_gone = brcms_deviceremoved(wlc_hw->wlc);
  4263. /* disable interrupts */
  4264. if (dev_gone)
  4265. wlc_hw->wlc->macintmask = 0;
  4266. else {
  4267. /* now disable interrupts */
  4268. brcms_intrsoff(wlc_hw->wlc->wl);
  4269. /* ensure we're running on the pll clock again */
  4270. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  4271. }
  4272. /* down phy at the last of this stage */
  4273. callbacks += wlc_phy_down(wlc_hw->band->pi);
  4274. return callbacks;
  4275. }
  4276. static int brcms_b_down_finish(struct brcms_hardware *wlc_hw)
  4277. {
  4278. uint callbacks = 0;
  4279. bool dev_gone;
  4280. if (!wlc_hw->up)
  4281. return callbacks;
  4282. wlc_hw->up = false;
  4283. wlc_phy_hw_state_upd(wlc_hw->band->pi, false);
  4284. dev_gone = brcms_deviceremoved(wlc_hw->wlc);
  4285. if (dev_gone) {
  4286. wlc_hw->sbclk = false;
  4287. wlc_hw->clk = false;
  4288. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
  4289. /* reclaim any posted packets */
  4290. brcms_c_flushqueues(wlc_hw->wlc);
  4291. } else {
  4292. /* Reset and disable the core */
  4293. if (bcma_core_is_enabled(wlc_hw->d11core)) {
  4294. if (bcma_read32(wlc_hw->d11core,
  4295. D11REGOFFS(maccontrol)) & MCTL_EN_MAC)
  4296. brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
  4297. callbacks += brcms_reset(wlc_hw->wlc->wl);
  4298. brcms_c_coredisable(wlc_hw);
  4299. }
  4300. /* turn off primary xtal and pll */
  4301. if (!wlc_hw->noreset) {
  4302. bcma_host_pci_down(wlc_hw->d11core->bus);
  4303. brcms_b_xtal(wlc_hw, OFF);
  4304. }
  4305. }
  4306. return callbacks;
  4307. }
  4308. /*
  4309. * Mark the interface nonoperational, stop the software mechanisms,
  4310. * disable the hardware, free any transient buffer state.
  4311. * Return a count of the number of driver callbacks still pending.
  4312. */
  4313. uint brcms_c_down(struct brcms_c_info *wlc)
  4314. {
  4315. uint callbacks = 0;
  4316. int i;
  4317. bool dev_gone = false;
  4318. brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
  4319. /* check if we are already in the going down path */
  4320. if (wlc->going_down) {
  4321. brcms_err(wlc->hw->d11core,
  4322. "wl%d: %s: Driver going down so return\n",
  4323. wlc->pub->unit, __func__);
  4324. return 0;
  4325. }
  4326. if (!wlc->pub->up)
  4327. return callbacks;
  4328. wlc->going_down = true;
  4329. callbacks += brcms_b_bmac_down_prep(wlc->hw);
  4330. dev_gone = brcms_deviceremoved(wlc);
  4331. /* Call any registered down handlers */
  4332. for (i = 0; i < BRCMS_MAXMODULES; i++) {
  4333. if (wlc->modulecb[i].down_fn)
  4334. callbacks +=
  4335. wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
  4336. }
  4337. /* cancel the watchdog timer */
  4338. if (wlc->WDarmed) {
  4339. if (!brcms_del_timer(wlc->wdtimer))
  4340. callbacks++;
  4341. wlc->WDarmed = false;
  4342. }
  4343. /* cancel all other timers */
  4344. callbacks += brcms_c_down_del_timer(wlc);
  4345. wlc->pub->up = false;
  4346. wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
  4347. callbacks += brcms_b_down_finish(wlc->hw);
  4348. /* brcms_b_down_finish has done brcms_c_coredisable(). so clk is off */
  4349. wlc->clk = false;
  4350. wlc->going_down = false;
  4351. return callbacks;
  4352. }
  4353. /* Set the current gmode configuration */
  4354. int brcms_c_set_gmode(struct brcms_c_info *wlc, u8 gmode, bool config)
  4355. {
  4356. int ret = 0;
  4357. uint i;
  4358. struct brcms_c_rateset rs;
  4359. /* Default to 54g Auto */
  4360. /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
  4361. s8 shortslot = BRCMS_SHORTSLOT_AUTO;
  4362. bool shortslot_restrict = false; /* Restrict association to stations
  4363. * that support shortslot
  4364. */
  4365. bool ofdm_basic = false; /* Make 6, 12, and 24 basic rates */
  4366. /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
  4367. int preamble = BRCMS_PLCP_LONG;
  4368. bool preamble_restrict = false; /* Restrict association to stations
  4369. * that support short preambles
  4370. */
  4371. struct brcms_band *band;
  4372. /* if N-support is enabled, allow Gmode set as long as requested
  4373. * Gmode is not GMODE_LEGACY_B
  4374. */
  4375. if ((wlc->pub->_n_enab & SUPPORT_11N) && gmode == GMODE_LEGACY_B)
  4376. return -ENOTSUPP;
  4377. /* verify that we are dealing with 2G band and grab the band pointer */
  4378. if (wlc->band->bandtype == BRCM_BAND_2G)
  4379. band = wlc->band;
  4380. else if ((wlc->pub->_nbands > 1) &&
  4381. (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == BRCM_BAND_2G))
  4382. band = wlc->bandstate[OTHERBANDUNIT(wlc)];
  4383. else
  4384. return -EINVAL;
  4385. /* update configuration value */
  4386. if (config)
  4387. brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER, gmode);
  4388. /* Clear rateset override */
  4389. memset(&rs, 0, sizeof(rs));
  4390. switch (gmode) {
  4391. case GMODE_LEGACY_B:
  4392. shortslot = BRCMS_SHORTSLOT_OFF;
  4393. brcms_c_rateset_copy(&gphy_legacy_rates, &rs);
  4394. break;
  4395. case GMODE_LRS:
  4396. break;
  4397. case GMODE_AUTO:
  4398. /* Accept defaults */
  4399. break;
  4400. case GMODE_ONLY:
  4401. ofdm_basic = true;
  4402. preamble = BRCMS_PLCP_SHORT;
  4403. preamble_restrict = true;
  4404. break;
  4405. case GMODE_PERFORMANCE:
  4406. shortslot = BRCMS_SHORTSLOT_ON;
  4407. shortslot_restrict = true;
  4408. ofdm_basic = true;
  4409. preamble = BRCMS_PLCP_SHORT;
  4410. preamble_restrict = true;
  4411. break;
  4412. default:
  4413. /* Error */
  4414. brcms_err(wlc->hw->d11core, "wl%d: %s: invalid gmode %d\n",
  4415. wlc->pub->unit, __func__, gmode);
  4416. return -ENOTSUPP;
  4417. }
  4418. band->gmode = gmode;
  4419. wlc->shortslot_override = shortslot;
  4420. /* Use the default 11g rateset */
  4421. if (!rs.count)
  4422. brcms_c_rateset_copy(&cck_ofdm_rates, &rs);
  4423. if (ofdm_basic) {
  4424. for (i = 0; i < rs.count; i++) {
  4425. if (rs.rates[i] == BRCM_RATE_6M
  4426. || rs.rates[i] == BRCM_RATE_12M
  4427. || rs.rates[i] == BRCM_RATE_24M)
  4428. rs.rates[i] |= BRCMS_RATE_FLAG;
  4429. }
  4430. }
  4431. /* Set default bss rateset */
  4432. wlc->default_bss->rateset.count = rs.count;
  4433. memcpy(wlc->default_bss->rateset.rates, rs.rates,
  4434. sizeof(wlc->default_bss->rateset.rates));
  4435. return ret;
  4436. }
  4437. int brcms_c_set_nmode(struct brcms_c_info *wlc)
  4438. {
  4439. uint i;
  4440. s32 nmode = AUTO;
  4441. if (wlc->stf->txstreams == WL_11N_3x3)
  4442. nmode = WL_11N_3x3;
  4443. else
  4444. nmode = WL_11N_2x2;
  4445. /* force GMODE_AUTO if NMODE is ON */
  4446. brcms_c_set_gmode(wlc, GMODE_AUTO, true);
  4447. if (nmode == WL_11N_3x3)
  4448. wlc->pub->_n_enab = SUPPORT_HT;
  4449. else
  4450. wlc->pub->_n_enab = SUPPORT_11N;
  4451. wlc->default_bss->flags |= BRCMS_BSS_HT;
  4452. /* add the mcs rates to the default and hw ratesets */
  4453. brcms_c_rateset_mcs_build(&wlc->default_bss->rateset,
  4454. wlc->stf->txstreams);
  4455. for (i = 0; i < wlc->pub->_nbands; i++)
  4456. memcpy(wlc->bandstate[i]->hw_rateset.mcs,
  4457. wlc->default_bss->rateset.mcs, MCSSET_LEN);
  4458. return 0;
  4459. }
  4460. static int
  4461. brcms_c_set_internal_rateset(struct brcms_c_info *wlc,
  4462. struct brcms_c_rateset *rs_arg)
  4463. {
  4464. struct brcms_c_rateset rs, new;
  4465. uint bandunit;
  4466. memcpy(&rs, rs_arg, sizeof(struct brcms_c_rateset));
  4467. /* check for bad count value */
  4468. if ((rs.count == 0) || (rs.count > BRCMS_NUMRATES))
  4469. return -EINVAL;
  4470. /* try the current band */
  4471. bandunit = wlc->band->bandunit;
  4472. memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
  4473. if (brcms_c_rate_hwrs_filter_sort_validate
  4474. (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
  4475. wlc->stf->txstreams))
  4476. goto good;
  4477. /* try the other band */
  4478. if (brcms_is_mband_unlocked(wlc)) {
  4479. bandunit = OTHERBANDUNIT(wlc);
  4480. memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
  4481. if (brcms_c_rate_hwrs_filter_sort_validate(&new,
  4482. &wlc->
  4483. bandstate[bandunit]->
  4484. hw_rateset, true,
  4485. wlc->stf->txstreams))
  4486. goto good;
  4487. }
  4488. return -EBADE;
  4489. good:
  4490. /* apply new rateset */
  4491. memcpy(&wlc->default_bss->rateset, &new,
  4492. sizeof(struct brcms_c_rateset));
  4493. memcpy(&wlc->bandstate[bandunit]->defrateset, &new,
  4494. sizeof(struct brcms_c_rateset));
  4495. return 0;
  4496. }
  4497. static void brcms_c_ofdm_rateset_war(struct brcms_c_info *wlc)
  4498. {
  4499. u8 r;
  4500. bool war = false;
  4501. if (wlc->pub->associated)
  4502. r = wlc->bsscfg->current_bss->rateset.rates[0];
  4503. else
  4504. r = wlc->default_bss->rateset.rates[0];
  4505. wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
  4506. }
  4507. int brcms_c_set_channel(struct brcms_c_info *wlc, u16 channel)
  4508. {
  4509. u16 chspec = ch20mhz_chspec(channel);
  4510. if (channel < 0 || channel > MAXCHANNEL)
  4511. return -EINVAL;
  4512. if (!brcms_c_valid_chanspec_db(wlc->cmi, chspec))
  4513. return -EINVAL;
  4514. if (!wlc->pub->up && brcms_is_mband_unlocked(wlc)) {
  4515. if (wlc->band->bandunit != chspec_bandunit(chspec))
  4516. wlc->bandinit_pending = true;
  4517. else
  4518. wlc->bandinit_pending = false;
  4519. }
  4520. wlc->default_bss->chanspec = chspec;
  4521. /* brcms_c_BSSinit() will sanitize the rateset before
  4522. * using it.. */
  4523. if (wlc->pub->up && (wlc_phy_chanspec_get(wlc->band->pi) != chspec)) {
  4524. brcms_c_set_home_chanspec(wlc, chspec);
  4525. brcms_c_suspend_mac_and_wait(wlc);
  4526. brcms_c_set_chanspec(wlc, chspec);
  4527. brcms_c_enable_mac(wlc);
  4528. }
  4529. return 0;
  4530. }
  4531. int brcms_c_set_rate_limit(struct brcms_c_info *wlc, u16 srl, u16 lrl)
  4532. {
  4533. int ac;
  4534. if (srl < 1 || srl > RETRY_SHORT_MAX ||
  4535. lrl < 1 || lrl > RETRY_SHORT_MAX)
  4536. return -EINVAL;
  4537. wlc->SRL = srl;
  4538. wlc->LRL = lrl;
  4539. brcms_b_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
  4540. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  4541. wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
  4542. EDCF_SHORT, wlc->SRL);
  4543. wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
  4544. EDCF_LONG, wlc->LRL);
  4545. }
  4546. brcms_c_wme_retries_write(wlc);
  4547. return 0;
  4548. }
  4549. void brcms_c_get_current_rateset(struct brcms_c_info *wlc,
  4550. struct brcm_rateset *currs)
  4551. {
  4552. struct brcms_c_rateset *rs;
  4553. if (wlc->pub->associated)
  4554. rs = &wlc->bsscfg->current_bss->rateset;
  4555. else
  4556. rs = &wlc->default_bss->rateset;
  4557. /* Copy only legacy rateset section */
  4558. currs->count = rs->count;
  4559. memcpy(&currs->rates, &rs->rates, rs->count);
  4560. }
  4561. int brcms_c_set_rateset(struct brcms_c_info *wlc, struct brcm_rateset *rs)
  4562. {
  4563. struct brcms_c_rateset internal_rs;
  4564. int bcmerror;
  4565. if (rs->count > BRCMS_NUMRATES)
  4566. return -ENOBUFS;
  4567. memset(&internal_rs, 0, sizeof(internal_rs));
  4568. /* Copy only legacy rateset section */
  4569. internal_rs.count = rs->count;
  4570. memcpy(&internal_rs.rates, &rs->rates, internal_rs.count);
  4571. /* merge rateset coming in with the current mcsset */
  4572. if (wlc->pub->_n_enab & SUPPORT_11N) {
  4573. struct brcms_bss_info *mcsset_bss;
  4574. if (wlc->pub->associated)
  4575. mcsset_bss = wlc->bsscfg->current_bss;
  4576. else
  4577. mcsset_bss = wlc->default_bss;
  4578. memcpy(internal_rs.mcs, &mcsset_bss->rateset.mcs[0],
  4579. MCSSET_LEN);
  4580. }
  4581. bcmerror = brcms_c_set_internal_rateset(wlc, &internal_rs);
  4582. if (!bcmerror)
  4583. brcms_c_ofdm_rateset_war(wlc);
  4584. return bcmerror;
  4585. }
  4586. static void brcms_c_time_lock(struct brcms_c_info *wlc)
  4587. {
  4588. bcma_set32(wlc->hw->d11core, D11REGOFFS(maccontrol), MCTL_TBTTHOLD);
  4589. /* Commit the write */
  4590. bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
  4591. }
  4592. static void brcms_c_time_unlock(struct brcms_c_info *wlc)
  4593. {
  4594. bcma_mask32(wlc->hw->d11core, D11REGOFFS(maccontrol), ~MCTL_TBTTHOLD);
  4595. /* Commit the write */
  4596. bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
  4597. }
  4598. int brcms_c_set_beacon_period(struct brcms_c_info *wlc, u16 period)
  4599. {
  4600. u32 bcnint_us;
  4601. if (period == 0)
  4602. return -EINVAL;
  4603. wlc->default_bss->beacon_period = period;
  4604. bcnint_us = period << 10;
  4605. brcms_c_time_lock(wlc);
  4606. bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_cfprep),
  4607. (bcnint_us << CFPREP_CBI_SHIFT));
  4608. bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_cfpstart), bcnint_us);
  4609. brcms_c_time_unlock(wlc);
  4610. return 0;
  4611. }
  4612. u16 brcms_c_get_phy_type(struct brcms_c_info *wlc, int phyidx)
  4613. {
  4614. return wlc->band->phytype;
  4615. }
  4616. void brcms_c_set_shortslot_override(struct brcms_c_info *wlc, s8 sslot_override)
  4617. {
  4618. wlc->shortslot_override = sslot_override;
  4619. /*
  4620. * shortslot is an 11g feature, so no more work if we are
  4621. * currently on the 5G band
  4622. */
  4623. if (wlc->band->bandtype == BRCM_BAND_5G)
  4624. return;
  4625. if (wlc->pub->up && wlc->pub->associated) {
  4626. /* let watchdog or beacon processing update shortslot */
  4627. } else if (wlc->pub->up) {
  4628. /* unassociated shortslot is off */
  4629. brcms_c_switch_shortslot(wlc, false);
  4630. } else {
  4631. /* driver is down, so just update the brcms_c_info
  4632. * value */
  4633. if (wlc->shortslot_override == BRCMS_SHORTSLOT_AUTO)
  4634. wlc->shortslot = false;
  4635. else
  4636. wlc->shortslot =
  4637. (wlc->shortslot_override ==
  4638. BRCMS_SHORTSLOT_ON);
  4639. }
  4640. }
  4641. /*
  4642. * register watchdog and down handlers.
  4643. */
  4644. int brcms_c_module_register(struct brcms_pub *pub,
  4645. const char *name, struct brcms_info *hdl,
  4646. int (*d_fn)(void *handle))
  4647. {
  4648. struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
  4649. int i;
  4650. /* find an empty entry and just add, no duplication check! */
  4651. for (i = 0; i < BRCMS_MAXMODULES; i++) {
  4652. if (wlc->modulecb[i].name[0] == '\0') {
  4653. strncpy(wlc->modulecb[i].name, name,
  4654. sizeof(wlc->modulecb[i].name) - 1);
  4655. wlc->modulecb[i].hdl = hdl;
  4656. wlc->modulecb[i].down_fn = d_fn;
  4657. return 0;
  4658. }
  4659. }
  4660. return -ENOSR;
  4661. }
  4662. /* unregister module callbacks */
  4663. int brcms_c_module_unregister(struct brcms_pub *pub, const char *name,
  4664. struct brcms_info *hdl)
  4665. {
  4666. struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
  4667. int i;
  4668. if (wlc == NULL)
  4669. return -ENODATA;
  4670. for (i = 0; i < BRCMS_MAXMODULES; i++) {
  4671. if (!strcmp(wlc->modulecb[i].name, name) &&
  4672. (wlc->modulecb[i].hdl == hdl)) {
  4673. memset(&wlc->modulecb[i], 0, sizeof(wlc->modulecb[i]));
  4674. return 0;
  4675. }
  4676. }
  4677. /* table not found! */
  4678. return -ENODATA;
  4679. }
  4680. static bool brcms_c_chipmatch_pci(struct bcma_device *core)
  4681. {
  4682. struct pci_dev *pcidev = core->bus->host_pci;
  4683. u16 vendor = pcidev->vendor;
  4684. u16 device = pcidev->device;
  4685. if (vendor != PCI_VENDOR_ID_BROADCOM) {
  4686. pr_err("unknown vendor id %04x\n", vendor);
  4687. return false;
  4688. }
  4689. if (device == BCM43224_D11N_ID_VEN1 || device == BCM43224_CHIP_ID)
  4690. return true;
  4691. if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
  4692. return true;
  4693. if (device == BCM4313_D11N2G_ID || device == BCM4313_CHIP_ID)
  4694. return true;
  4695. if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
  4696. return true;
  4697. pr_err("unknown device id %04x\n", device);
  4698. return false;
  4699. }
  4700. static bool brcms_c_chipmatch_soc(struct bcma_device *core)
  4701. {
  4702. struct bcma_chipinfo *chipinfo = &core->bus->chipinfo;
  4703. if (chipinfo->id == BCMA_CHIP_ID_BCM4716)
  4704. return true;
  4705. pr_err("unknown chip id %04x\n", chipinfo->id);
  4706. return false;
  4707. }
  4708. bool brcms_c_chipmatch(struct bcma_device *core)
  4709. {
  4710. switch (core->bus->hosttype) {
  4711. case BCMA_HOSTTYPE_PCI:
  4712. return brcms_c_chipmatch_pci(core);
  4713. case BCMA_HOSTTYPE_SOC:
  4714. return brcms_c_chipmatch_soc(core);
  4715. default:
  4716. pr_err("unknown host type: %i\n", core->bus->hosttype);
  4717. return false;
  4718. }
  4719. }
  4720. u16 brcms_b_rate_shm_offset(struct brcms_hardware *wlc_hw, u8 rate)
  4721. {
  4722. u16 table_ptr;
  4723. u8 phy_rate, index;
  4724. /* get the phy specific rate encoding for the PLCP SIGNAL field */
  4725. if (is_ofdm_rate(rate))
  4726. table_ptr = M_RT_DIRMAP_A;
  4727. else
  4728. table_ptr = M_RT_DIRMAP_B;
  4729. /* for a given rate, the LS-nibble of the PLCP SIGNAL field is
  4730. * the index into the rate table.
  4731. */
  4732. phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
  4733. index = phy_rate & 0xf;
  4734. /* Find the SHM pointer to the rate table entry by looking in the
  4735. * Direct-map Table
  4736. */
  4737. return 2 * brcms_b_read_shm(wlc_hw, table_ptr + (index * 2));
  4738. }
  4739. /*
  4740. * bcmc_fid_generate:
  4741. * Generate frame ID for a BCMC packet. The frag field is not used
  4742. * for MC frames so is used as part of the sequence number.
  4743. */
  4744. static inline u16
  4745. bcmc_fid_generate(struct brcms_c_info *wlc, struct brcms_bss_cfg *bsscfg,
  4746. struct d11txh *txh)
  4747. {
  4748. u16 frameid;
  4749. frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
  4750. TXFID_QUEUE_MASK);
  4751. frameid |=
  4752. (((wlc->
  4753. mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
  4754. TX_BCMC_FIFO;
  4755. return frameid;
  4756. }
  4757. static uint
  4758. brcms_c_calc_ack_time(struct brcms_c_info *wlc, u32 rspec,
  4759. u8 preamble_type)
  4760. {
  4761. uint dur = 0;
  4762. /*
  4763. * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
  4764. * is less than or equal to the rate of the immediately previous
  4765. * frame in the FES
  4766. */
  4767. rspec = brcms_basic_rate(wlc, rspec);
  4768. /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
  4769. dur =
  4770. brcms_c_calc_frame_time(wlc, rspec, preamble_type,
  4771. (DOT11_ACK_LEN + FCS_LEN));
  4772. return dur;
  4773. }
  4774. static uint
  4775. brcms_c_calc_cts_time(struct brcms_c_info *wlc, u32 rspec,
  4776. u8 preamble_type)
  4777. {
  4778. return brcms_c_calc_ack_time(wlc, rspec, preamble_type);
  4779. }
  4780. static uint
  4781. brcms_c_calc_ba_time(struct brcms_c_info *wlc, u32 rspec,
  4782. u8 preamble_type)
  4783. {
  4784. /*
  4785. * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
  4786. * is less than or equal to the rate of the immediately previous
  4787. * frame in the FES
  4788. */
  4789. rspec = brcms_basic_rate(wlc, rspec);
  4790. /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
  4791. return brcms_c_calc_frame_time(wlc, rspec, preamble_type,
  4792. (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
  4793. FCS_LEN));
  4794. }
  4795. /* brcms_c_compute_frame_dur()
  4796. *
  4797. * Calculate the 802.11 MAC header DUR field for MPDU
  4798. * DUR for a single frame = 1 SIFS + 1 ACK
  4799. * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
  4800. *
  4801. * rate MPDU rate in unit of 500kbps
  4802. * next_frag_len next MPDU length in bytes
  4803. * preamble_type use short/GF or long/MM PLCP header
  4804. */
  4805. static u16
  4806. brcms_c_compute_frame_dur(struct brcms_c_info *wlc, u32 rate,
  4807. u8 preamble_type, uint next_frag_len)
  4808. {
  4809. u16 dur, sifs;
  4810. sifs = get_sifs(wlc->band);
  4811. dur = sifs;
  4812. dur += (u16) brcms_c_calc_ack_time(wlc, rate, preamble_type);
  4813. if (next_frag_len) {
  4814. /* Double the current DUR to get 2 SIFS + 2 ACKs */
  4815. dur *= 2;
  4816. /* add another SIFS and the frag time */
  4817. dur += sifs;
  4818. dur +=
  4819. (u16) brcms_c_calc_frame_time(wlc, rate, preamble_type,
  4820. next_frag_len);
  4821. }
  4822. return dur;
  4823. }
  4824. /* The opposite of brcms_c_calc_frame_time */
  4825. static uint
  4826. brcms_c_calc_frame_len(struct brcms_c_info *wlc, u32 ratespec,
  4827. u8 preamble_type, uint dur)
  4828. {
  4829. uint nsyms, mac_len, Ndps, kNdps;
  4830. uint rate = rspec2rate(ratespec);
  4831. if (is_mcs_rate(ratespec)) {
  4832. uint mcs = ratespec & RSPEC_RATE_MASK;
  4833. int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
  4834. dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
  4835. /* payload calculation matches that of regular ofdm */
  4836. if (wlc->band->bandtype == BRCM_BAND_2G)
  4837. dur -= DOT11_OFDM_SIGNAL_EXTENSION;
  4838. /* kNdbps = kbps * 4 */
  4839. kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
  4840. rspec_issgi(ratespec)) * 4;
  4841. nsyms = dur / APHY_SYMBOL_TIME;
  4842. mac_len =
  4843. ((nsyms * kNdps) -
  4844. ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
  4845. } else if (is_ofdm_rate(ratespec)) {
  4846. dur -= APHY_PREAMBLE_TIME;
  4847. dur -= APHY_SIGNAL_TIME;
  4848. /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
  4849. Ndps = rate * 2;
  4850. nsyms = dur / APHY_SYMBOL_TIME;
  4851. mac_len =
  4852. ((nsyms * Ndps) -
  4853. (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
  4854. } else {
  4855. if (preamble_type & BRCMS_SHORT_PREAMBLE)
  4856. dur -= BPHY_PLCP_SHORT_TIME;
  4857. else
  4858. dur -= BPHY_PLCP_TIME;
  4859. mac_len = dur * rate;
  4860. /* divide out factor of 2 in rate (1/2 mbps) */
  4861. mac_len = mac_len / 8 / 2;
  4862. }
  4863. return mac_len;
  4864. }
  4865. /*
  4866. * Return true if the specified rate is supported by the specified band.
  4867. * BRCM_BAND_AUTO indicates the current band.
  4868. */
  4869. static bool brcms_c_valid_rate(struct brcms_c_info *wlc, u32 rspec, int band,
  4870. bool verbose)
  4871. {
  4872. struct brcms_c_rateset *hw_rateset;
  4873. uint i;
  4874. if ((band == BRCM_BAND_AUTO) || (band == wlc->band->bandtype))
  4875. hw_rateset = &wlc->band->hw_rateset;
  4876. else if (wlc->pub->_nbands > 1)
  4877. hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
  4878. else
  4879. /* other band specified and we are a single band device */
  4880. return false;
  4881. /* check if this is a mimo rate */
  4882. if (is_mcs_rate(rspec)) {
  4883. if ((rspec & RSPEC_RATE_MASK) >= MCS_TABLE_SIZE)
  4884. goto error;
  4885. return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
  4886. }
  4887. for (i = 0; i < hw_rateset->count; i++)
  4888. if (hw_rateset->rates[i] == rspec2rate(rspec))
  4889. return true;
  4890. error:
  4891. if (verbose)
  4892. brcms_err(wlc->hw->d11core, "wl%d: valid_rate: rate spec 0x%x "
  4893. "not in hw_rateset\n", wlc->pub->unit, rspec);
  4894. return false;
  4895. }
  4896. static u32
  4897. mac80211_wlc_set_nrate(struct brcms_c_info *wlc, struct brcms_band *cur_band,
  4898. u32 int_val)
  4899. {
  4900. struct bcma_device *core = wlc->hw->d11core;
  4901. u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
  4902. u8 rate = int_val & NRATE_RATE_MASK;
  4903. u32 rspec;
  4904. bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
  4905. bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
  4906. bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
  4907. == NRATE_OVERRIDE_MCS_ONLY);
  4908. int bcmerror = 0;
  4909. if (!ismcs)
  4910. return (u32) rate;
  4911. /* validate the combination of rate/mcs/stf is allowed */
  4912. if ((wlc->pub->_n_enab & SUPPORT_11N) && ismcs) {
  4913. /* mcs only allowed when nmode */
  4914. if (stf > PHY_TXC1_MODE_SDM) {
  4915. brcms_err(core, "wl%d: %s: Invalid stf\n",
  4916. wlc->pub->unit, __func__);
  4917. bcmerror = -EINVAL;
  4918. goto done;
  4919. }
  4920. /* mcs 32 is a special case, DUP mode 40 only */
  4921. if (rate == 32) {
  4922. if (!CHSPEC_IS40(wlc->home_chanspec) ||
  4923. ((stf != PHY_TXC1_MODE_SISO)
  4924. && (stf != PHY_TXC1_MODE_CDD))) {
  4925. brcms_err(core, "wl%d: %s: Invalid mcs 32\n",
  4926. wlc->pub->unit, __func__);
  4927. bcmerror = -EINVAL;
  4928. goto done;
  4929. }
  4930. /* mcs > 7 must use stf SDM */
  4931. } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
  4932. /* mcs > 7 must use stf SDM */
  4933. if (stf != PHY_TXC1_MODE_SDM) {
  4934. brcms_dbg_mac80211(core, "wl%d: enabling "
  4935. "SDM mode for mcs %d\n",
  4936. wlc->pub->unit, rate);
  4937. stf = PHY_TXC1_MODE_SDM;
  4938. }
  4939. } else {
  4940. /*
  4941. * MCS 0-7 may use SISO, CDD, and for
  4942. * phy_rev >= 3 STBC
  4943. */
  4944. if ((stf > PHY_TXC1_MODE_STBC) ||
  4945. (!BRCMS_STBC_CAP_PHY(wlc)
  4946. && (stf == PHY_TXC1_MODE_STBC))) {
  4947. brcms_err(core, "wl%d: %s: Invalid STBC\n",
  4948. wlc->pub->unit, __func__);
  4949. bcmerror = -EINVAL;
  4950. goto done;
  4951. }
  4952. }
  4953. } else if (is_ofdm_rate(rate)) {
  4954. if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
  4955. brcms_err(core, "wl%d: %s: Invalid OFDM\n",
  4956. wlc->pub->unit, __func__);
  4957. bcmerror = -EINVAL;
  4958. goto done;
  4959. }
  4960. } else if (is_cck_rate(rate)) {
  4961. if ((cur_band->bandtype != BRCM_BAND_2G)
  4962. || (stf != PHY_TXC1_MODE_SISO)) {
  4963. brcms_err(core, "wl%d: %s: Invalid CCK\n",
  4964. wlc->pub->unit, __func__);
  4965. bcmerror = -EINVAL;
  4966. goto done;
  4967. }
  4968. } else {
  4969. brcms_err(core, "wl%d: %s: Unknown rate type\n",
  4970. wlc->pub->unit, __func__);
  4971. bcmerror = -EINVAL;
  4972. goto done;
  4973. }
  4974. /* make sure multiple antennae are available for non-siso rates */
  4975. if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
  4976. brcms_err(core, "wl%d: %s: SISO antenna but !SISO "
  4977. "request\n", wlc->pub->unit, __func__);
  4978. bcmerror = -EINVAL;
  4979. goto done;
  4980. }
  4981. rspec = rate;
  4982. if (ismcs) {
  4983. rspec |= RSPEC_MIMORATE;
  4984. /* For STBC populate the STC field of the ratespec */
  4985. if (stf == PHY_TXC1_MODE_STBC) {
  4986. u8 stc;
  4987. stc = 1; /* Nss for single stream is always 1 */
  4988. rspec |= (stc << RSPEC_STC_SHIFT);
  4989. }
  4990. }
  4991. rspec |= (stf << RSPEC_STF_SHIFT);
  4992. if (override_mcs_only)
  4993. rspec |= RSPEC_OVERRIDE_MCS_ONLY;
  4994. if (issgi)
  4995. rspec |= RSPEC_SHORT_GI;
  4996. if ((rate != 0)
  4997. && !brcms_c_valid_rate(wlc, rspec, cur_band->bandtype, true))
  4998. return rate;
  4999. return rspec;
  5000. done:
  5001. return rate;
  5002. }
  5003. /*
  5004. * Compute PLCP, but only requires actual rate and length of pkt.
  5005. * Rate is given in the driver standard multiple of 500 kbps.
  5006. * le is set for 11 Mbps rate if necessary.
  5007. * Broken out for PRQ.
  5008. */
  5009. static void brcms_c_cck_plcp_set(struct brcms_c_info *wlc, int rate_500,
  5010. uint length, u8 *plcp)
  5011. {
  5012. u16 usec = 0;
  5013. u8 le = 0;
  5014. switch (rate_500) {
  5015. case BRCM_RATE_1M:
  5016. usec = length << 3;
  5017. break;
  5018. case BRCM_RATE_2M:
  5019. usec = length << 2;
  5020. break;
  5021. case BRCM_RATE_5M5:
  5022. usec = (length << 4) / 11;
  5023. if ((length << 4) - (usec * 11) > 0)
  5024. usec++;
  5025. break;
  5026. case BRCM_RATE_11M:
  5027. usec = (length << 3) / 11;
  5028. if ((length << 3) - (usec * 11) > 0) {
  5029. usec++;
  5030. if ((usec * 11) - (length << 3) >= 8)
  5031. le = D11B_PLCP_SIGNAL_LE;
  5032. }
  5033. break;
  5034. default:
  5035. brcms_err(wlc->hw->d11core,
  5036. "brcms_c_cck_plcp_set: unsupported rate %d\n",
  5037. rate_500);
  5038. rate_500 = BRCM_RATE_1M;
  5039. usec = length << 3;
  5040. break;
  5041. }
  5042. /* PLCP signal byte */
  5043. plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
  5044. /* PLCP service byte */
  5045. plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
  5046. /* PLCP length u16, little endian */
  5047. plcp[2] = usec & 0xff;
  5048. plcp[3] = (usec >> 8) & 0xff;
  5049. /* PLCP CRC16 */
  5050. plcp[4] = 0;
  5051. plcp[5] = 0;
  5052. }
  5053. /* Rate: 802.11 rate code, length: PSDU length in octets */
  5054. static void brcms_c_compute_mimo_plcp(u32 rspec, uint length, u8 *plcp)
  5055. {
  5056. u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
  5057. plcp[0] = mcs;
  5058. if (rspec_is40mhz(rspec) || (mcs == 32))
  5059. plcp[0] |= MIMO_PLCP_40MHZ;
  5060. BRCMS_SET_MIMO_PLCP_LEN(plcp, length);
  5061. plcp[3] = rspec_mimoplcp3(rspec); /* rspec already holds this byte */
  5062. plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
  5063. plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
  5064. plcp[5] = 0;
  5065. }
  5066. /* Rate: 802.11 rate code, length: PSDU length in octets */
  5067. static void
  5068. brcms_c_compute_ofdm_plcp(u32 rspec, u32 length, u8 *plcp)
  5069. {
  5070. u8 rate_signal;
  5071. u32 tmp = 0;
  5072. int rate = rspec2rate(rspec);
  5073. /*
  5074. * encode rate per 802.11a-1999 sec 17.3.4.1, with lsb
  5075. * transmitted first
  5076. */
  5077. rate_signal = rate_info[rate] & BRCMS_RATE_MASK;
  5078. memset(plcp, 0, D11_PHY_HDR_LEN);
  5079. D11A_PHY_HDR_SRATE((struct ofdm_phy_hdr *) plcp, rate_signal);
  5080. tmp = (length & 0xfff) << 5;
  5081. plcp[2] |= (tmp >> 16) & 0xff;
  5082. plcp[1] |= (tmp >> 8) & 0xff;
  5083. plcp[0] |= tmp & 0xff;
  5084. }
  5085. /* Rate: 802.11 rate code, length: PSDU length in octets */
  5086. static void brcms_c_compute_cck_plcp(struct brcms_c_info *wlc, u32 rspec,
  5087. uint length, u8 *plcp)
  5088. {
  5089. int rate = rspec2rate(rspec);
  5090. brcms_c_cck_plcp_set(wlc, rate, length, plcp);
  5091. }
  5092. static void
  5093. brcms_c_compute_plcp(struct brcms_c_info *wlc, u32 rspec,
  5094. uint length, u8 *plcp)
  5095. {
  5096. if (is_mcs_rate(rspec))
  5097. brcms_c_compute_mimo_plcp(rspec, length, plcp);
  5098. else if (is_ofdm_rate(rspec))
  5099. brcms_c_compute_ofdm_plcp(rspec, length, plcp);
  5100. else
  5101. brcms_c_compute_cck_plcp(wlc, rspec, length, plcp);
  5102. }
  5103. /* brcms_c_compute_rtscts_dur()
  5104. *
  5105. * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
  5106. * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
  5107. * DUR for CTS-TO-SELF w/ frame = 2 SIFS + next frame time + 1 ACK
  5108. *
  5109. * cts cts-to-self or rts/cts
  5110. * rts_rate rts or cts rate in unit of 500kbps
  5111. * rate next MPDU rate in unit of 500kbps
  5112. * frame_len next MPDU frame length in bytes
  5113. */
  5114. u16
  5115. brcms_c_compute_rtscts_dur(struct brcms_c_info *wlc, bool cts_only,
  5116. u32 rts_rate,
  5117. u32 frame_rate, u8 rts_preamble_type,
  5118. u8 frame_preamble_type, uint frame_len, bool ba)
  5119. {
  5120. u16 dur, sifs;
  5121. sifs = get_sifs(wlc->band);
  5122. if (!cts_only) {
  5123. /* RTS/CTS */
  5124. dur = 3 * sifs;
  5125. dur +=
  5126. (u16) brcms_c_calc_cts_time(wlc, rts_rate,
  5127. rts_preamble_type);
  5128. } else {
  5129. /* CTS-TO-SELF */
  5130. dur = 2 * sifs;
  5131. }
  5132. dur +=
  5133. (u16) brcms_c_calc_frame_time(wlc, frame_rate, frame_preamble_type,
  5134. frame_len);
  5135. if (ba)
  5136. dur +=
  5137. (u16) brcms_c_calc_ba_time(wlc, frame_rate,
  5138. BRCMS_SHORT_PREAMBLE);
  5139. else
  5140. dur +=
  5141. (u16) brcms_c_calc_ack_time(wlc, frame_rate,
  5142. frame_preamble_type);
  5143. return dur;
  5144. }
  5145. static u16 brcms_c_phytxctl1_calc(struct brcms_c_info *wlc, u32 rspec)
  5146. {
  5147. u16 phyctl1 = 0;
  5148. u16 bw;
  5149. if (BRCMS_ISLCNPHY(wlc->band)) {
  5150. bw = PHY_TXC1_BW_20MHZ;
  5151. } else {
  5152. bw = rspec_get_bw(rspec);
  5153. /* 10Mhz is not supported yet */
  5154. if (bw < PHY_TXC1_BW_20MHZ) {
  5155. brcms_err(wlc->hw->d11core, "phytxctl1_calc: bw %d is "
  5156. "not supported yet, set to 20L\n", bw);
  5157. bw = PHY_TXC1_BW_20MHZ;
  5158. }
  5159. }
  5160. if (is_mcs_rate(rspec)) {
  5161. uint mcs = rspec & RSPEC_RATE_MASK;
  5162. /* bw, stf, coding-type is part of rspec_phytxbyte2 returns */
  5163. phyctl1 = rspec_phytxbyte2(rspec);
  5164. /* set the upper byte of phyctl1 */
  5165. phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
  5166. } else if (is_cck_rate(rspec) && !BRCMS_ISLCNPHY(wlc->band)
  5167. && !BRCMS_ISSSLPNPHY(wlc->band)) {
  5168. /*
  5169. * In CCK mode LPPHY overloads OFDM Modulation bits with CCK
  5170. * Data Rate. Eventually MIMOPHY would also be converted to
  5171. * this format
  5172. */
  5173. /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
  5174. phyctl1 = (bw | (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
  5175. } else { /* legacy OFDM/CCK */
  5176. s16 phycfg;
  5177. /* get the phyctl byte from rate phycfg table */
  5178. phycfg = brcms_c_rate_legacy_phyctl(rspec2rate(rspec));
  5179. if (phycfg == -1) {
  5180. brcms_err(wlc->hw->d11core, "phytxctl1_calc: wrong "
  5181. "legacy OFDM/CCK rate\n");
  5182. phycfg = 0;
  5183. }
  5184. /* set the upper byte of phyctl1 */
  5185. phyctl1 =
  5186. (bw | (phycfg << 8) |
  5187. (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
  5188. }
  5189. return phyctl1;
  5190. }
  5191. /*
  5192. * Add struct d11txh, struct cck_phy_hdr.
  5193. *
  5194. * 'p' data must start with 802.11 MAC header
  5195. * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
  5196. *
  5197. * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
  5198. *
  5199. */
  5200. static u16
  5201. brcms_c_d11hdrs_mac80211(struct brcms_c_info *wlc, struct ieee80211_hw *hw,
  5202. struct sk_buff *p, struct scb *scb, uint frag,
  5203. uint nfrags, uint queue, uint next_frag_len)
  5204. {
  5205. struct ieee80211_hdr *h;
  5206. struct d11txh *txh;
  5207. u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
  5208. int len, phylen, rts_phylen;
  5209. u16 mch, phyctl, xfts, mainrates;
  5210. u16 seq = 0, mcl = 0, status = 0, frameid = 0;
  5211. u32 rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
  5212. u32 rts_rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
  5213. bool use_rts = false;
  5214. bool use_cts = false;
  5215. bool use_rifs = false;
  5216. bool short_preamble[2] = { false, false };
  5217. u8 preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
  5218. u8 rts_preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
  5219. u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
  5220. struct ieee80211_rts *rts = NULL;
  5221. bool qos;
  5222. uint ac;
  5223. bool hwtkmic = false;
  5224. u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
  5225. #define ANTCFG_NONE 0xFF
  5226. u8 antcfg = ANTCFG_NONE;
  5227. u8 fbantcfg = ANTCFG_NONE;
  5228. uint phyctl1_stf = 0;
  5229. u16 durid = 0;
  5230. struct ieee80211_tx_rate *txrate[2];
  5231. int k;
  5232. struct ieee80211_tx_info *tx_info;
  5233. bool is_mcs;
  5234. u16 mimo_txbw;
  5235. u8 mimo_preamble_type;
  5236. /* locate 802.11 MAC header */
  5237. h = (struct ieee80211_hdr *)(p->data);
  5238. qos = ieee80211_is_data_qos(h->frame_control);
  5239. /* compute length of frame in bytes for use in PLCP computations */
  5240. len = p->len;
  5241. phylen = len + FCS_LEN;
  5242. /* Get tx_info */
  5243. tx_info = IEEE80211_SKB_CB(p);
  5244. /* add PLCP */
  5245. plcp = skb_push(p, D11_PHY_HDR_LEN);
  5246. /* add Broadcom tx descriptor header */
  5247. txh = (struct d11txh *) skb_push(p, D11_TXH_LEN);
  5248. memset(txh, 0, D11_TXH_LEN);
  5249. /* setup frameid */
  5250. if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  5251. /* non-AP STA should never use BCMC queue */
  5252. if (queue == TX_BCMC_FIFO) {
  5253. brcms_err(wlc->hw->d11core,
  5254. "wl%d: %s: ASSERT queue == TX_BCMC!\n",
  5255. wlc->pub->unit, __func__);
  5256. frameid = bcmc_fid_generate(wlc, NULL, txh);
  5257. } else {
  5258. /* Increment the counter for first fragment */
  5259. if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  5260. scb->seqnum[p->priority]++;
  5261. /* extract fragment number from frame first */
  5262. seq = le16_to_cpu(h->seq_ctrl) & FRAGNUM_MASK;
  5263. seq |= (scb->seqnum[p->priority] << SEQNUM_SHIFT);
  5264. h->seq_ctrl = cpu_to_le16(seq);
  5265. frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
  5266. (queue & TXFID_QUEUE_MASK);
  5267. }
  5268. }
  5269. frameid |= queue & TXFID_QUEUE_MASK;
  5270. /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
  5271. if (ieee80211_is_beacon(h->frame_control))
  5272. mcl |= TXC_IGNOREPMQ;
  5273. txrate[0] = tx_info->control.rates;
  5274. txrate[1] = txrate[0] + 1;
  5275. /*
  5276. * if rate control algorithm didn't give us a fallback
  5277. * rate, use the primary rate
  5278. */
  5279. if (txrate[1]->idx < 0)
  5280. txrate[1] = txrate[0];
  5281. for (k = 0; k < hw->max_rates; k++) {
  5282. is_mcs = txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
  5283. if (!is_mcs) {
  5284. if ((txrate[k]->idx >= 0)
  5285. && (txrate[k]->idx <
  5286. hw->wiphy->bands[tx_info->band]->n_bitrates)) {
  5287. rspec[k] =
  5288. hw->wiphy->bands[tx_info->band]->
  5289. bitrates[txrate[k]->idx].hw_value;
  5290. short_preamble[k] =
  5291. txrate[k]->
  5292. flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
  5293. true : false;
  5294. } else {
  5295. rspec[k] = BRCM_RATE_1M;
  5296. }
  5297. } else {
  5298. rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band,
  5299. NRATE_MCS_INUSE | txrate[k]->idx);
  5300. }
  5301. /*
  5302. * Currently only support same setting for primay and
  5303. * fallback rates. Unify flags for each rate into a
  5304. * single value for the frame
  5305. */
  5306. use_rts |=
  5307. txrate[k]->
  5308. flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
  5309. use_cts |=
  5310. txrate[k]->
  5311. flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
  5312. /*
  5313. * (1) RATE:
  5314. * determine and validate primary rate
  5315. * and fallback rates
  5316. */
  5317. if (!rspec_active(rspec[k])) {
  5318. rspec[k] = BRCM_RATE_1M;
  5319. } else {
  5320. if (!is_multicast_ether_addr(h->addr1)) {
  5321. /* set tx antenna config */
  5322. brcms_c_antsel_antcfg_get(wlc->asi, false,
  5323. false, 0, 0, &antcfg, &fbantcfg);
  5324. }
  5325. }
  5326. }
  5327. phyctl1_stf = wlc->stf->ss_opmode;
  5328. if (wlc->pub->_n_enab & SUPPORT_11N) {
  5329. for (k = 0; k < hw->max_rates; k++) {
  5330. /*
  5331. * apply siso/cdd to single stream mcs's or ofdm
  5332. * if rspec is auto selected
  5333. */
  5334. if (((is_mcs_rate(rspec[k]) &&
  5335. is_single_stream(rspec[k] & RSPEC_RATE_MASK)) ||
  5336. is_ofdm_rate(rspec[k]))
  5337. && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
  5338. || !(rspec[k] & RSPEC_OVERRIDE))) {
  5339. rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
  5340. /* For SISO MCS use STBC if possible */
  5341. if (is_mcs_rate(rspec[k])
  5342. && BRCMS_STF_SS_STBC_TX(wlc, scb)) {
  5343. u8 stc;
  5344. /* Nss for single stream is always 1 */
  5345. stc = 1;
  5346. rspec[k] |= (PHY_TXC1_MODE_STBC <<
  5347. RSPEC_STF_SHIFT) |
  5348. (stc << RSPEC_STC_SHIFT);
  5349. } else
  5350. rspec[k] |=
  5351. (phyctl1_stf << RSPEC_STF_SHIFT);
  5352. }
  5353. /*
  5354. * Is the phy configured to use 40MHZ frames? If
  5355. * so then pick the desired txbw
  5356. */
  5357. if (brcms_chspec_bw(wlc->chanspec) == BRCMS_40_MHZ) {
  5358. /* default txbw is 20in40 SB */
  5359. mimo_ctlchbw = mimo_txbw =
  5360. CHSPEC_SB_UPPER(wlc_phy_chanspec_get(
  5361. wlc->band->pi))
  5362. ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
  5363. if (is_mcs_rate(rspec[k])) {
  5364. /* mcs 32 must be 40b/w DUP */
  5365. if ((rspec[k] & RSPEC_RATE_MASK)
  5366. == 32) {
  5367. mimo_txbw =
  5368. PHY_TXC1_BW_40MHZ_DUP;
  5369. /* use override */
  5370. } else if (wlc->mimo_40txbw != AUTO)
  5371. mimo_txbw = wlc->mimo_40txbw;
  5372. /* else check if dst is using 40 Mhz */
  5373. else if (scb->flags & SCB_IS40)
  5374. mimo_txbw = PHY_TXC1_BW_40MHZ;
  5375. } else if (is_ofdm_rate(rspec[k])) {
  5376. if (wlc->ofdm_40txbw != AUTO)
  5377. mimo_txbw = wlc->ofdm_40txbw;
  5378. } else if (wlc->cck_40txbw != AUTO) {
  5379. mimo_txbw = wlc->cck_40txbw;
  5380. }
  5381. } else {
  5382. /*
  5383. * mcs32 is 40 b/w only.
  5384. * This is possible for probe packets on
  5385. * a STA during SCAN
  5386. */
  5387. if ((rspec[k] & RSPEC_RATE_MASK) == 32)
  5388. /* mcs 0 */
  5389. rspec[k] = RSPEC_MIMORATE;
  5390. mimo_txbw = PHY_TXC1_BW_20MHZ;
  5391. }
  5392. /* Set channel width */
  5393. rspec[k] &= ~RSPEC_BW_MASK;
  5394. if ((k == 0) || ((k > 0) && is_mcs_rate(rspec[k])))
  5395. rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
  5396. else
  5397. rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
  5398. /* Disable short GI, not supported yet */
  5399. rspec[k] &= ~RSPEC_SHORT_GI;
  5400. mimo_preamble_type = BRCMS_MM_PREAMBLE;
  5401. if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
  5402. mimo_preamble_type = BRCMS_GF_PREAMBLE;
  5403. if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
  5404. && (!is_mcs_rate(rspec[k]))) {
  5405. brcms_warn(wlc->hw->d11core,
  5406. "wl%d: %s: IEEE80211_TX_RC_MCS != is_mcs_rate(rspec)\n",
  5407. wlc->pub->unit, __func__);
  5408. }
  5409. if (is_mcs_rate(rspec[k])) {
  5410. preamble_type[k] = mimo_preamble_type;
  5411. /*
  5412. * if SGI is selected, then forced mm
  5413. * for single stream
  5414. */
  5415. if ((rspec[k] & RSPEC_SHORT_GI)
  5416. && is_single_stream(rspec[k] &
  5417. RSPEC_RATE_MASK))
  5418. preamble_type[k] = BRCMS_MM_PREAMBLE;
  5419. }
  5420. /* should be better conditionalized */
  5421. if (!is_mcs_rate(rspec[0])
  5422. && (tx_info->control.rates[0].
  5423. flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
  5424. preamble_type[k] = BRCMS_SHORT_PREAMBLE;
  5425. }
  5426. } else {
  5427. for (k = 0; k < hw->max_rates; k++) {
  5428. /* Set ctrlchbw as 20Mhz */
  5429. rspec[k] &= ~RSPEC_BW_MASK;
  5430. rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
  5431. /* for nphy, stf of ofdm frames must follow policies */
  5432. if (BRCMS_ISNPHY(wlc->band) && is_ofdm_rate(rspec[k])) {
  5433. rspec[k] &= ~RSPEC_STF_MASK;
  5434. rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
  5435. }
  5436. }
  5437. }
  5438. /* Reset these for use with AMPDU's */
  5439. txrate[0]->count = 0;
  5440. txrate[1]->count = 0;
  5441. /* (2) PROTECTION, may change rspec */
  5442. if ((ieee80211_is_data(h->frame_control) ||
  5443. ieee80211_is_mgmt(h->frame_control)) &&
  5444. (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
  5445. use_rts = true;
  5446. /* (3) PLCP: determine PLCP header and MAC duration,
  5447. * fill struct d11txh */
  5448. brcms_c_compute_plcp(wlc, rspec[0], phylen, plcp);
  5449. brcms_c_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
  5450. memcpy(&txh->FragPLCPFallback,
  5451. plcp_fallback, sizeof(txh->FragPLCPFallback));
  5452. /* Length field now put in CCK FBR CRC field */
  5453. if (is_cck_rate(rspec[1])) {
  5454. txh->FragPLCPFallback[4] = phylen & 0xff;
  5455. txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
  5456. }
  5457. /* MIMO-RATE: need validation ?? */
  5458. mainrates = is_ofdm_rate(rspec[0]) ?
  5459. D11A_PHY_HDR_GRATE((struct ofdm_phy_hdr *) plcp) :
  5460. plcp[0];
  5461. /* DUR field for main rate */
  5462. if (!ieee80211_is_pspoll(h->frame_control) &&
  5463. !is_multicast_ether_addr(h->addr1) && !use_rifs) {
  5464. durid =
  5465. brcms_c_compute_frame_dur(wlc, rspec[0], preamble_type[0],
  5466. next_frag_len);
  5467. h->duration_id = cpu_to_le16(durid);
  5468. } else if (use_rifs) {
  5469. /* NAV protect to end of next max packet size */
  5470. durid =
  5471. (u16) brcms_c_calc_frame_time(wlc, rspec[0],
  5472. preamble_type[0],
  5473. DOT11_MAX_FRAG_LEN);
  5474. durid += RIFS_11N_TIME;
  5475. h->duration_id = cpu_to_le16(durid);
  5476. }
  5477. /* DUR field for fallback rate */
  5478. if (ieee80211_is_pspoll(h->frame_control))
  5479. txh->FragDurFallback = h->duration_id;
  5480. else if (is_multicast_ether_addr(h->addr1) || use_rifs)
  5481. txh->FragDurFallback = 0;
  5482. else {
  5483. durid = brcms_c_compute_frame_dur(wlc, rspec[1],
  5484. preamble_type[1], next_frag_len);
  5485. txh->FragDurFallback = cpu_to_le16(durid);
  5486. }
  5487. /* (4) MAC-HDR: MacTxControlLow */
  5488. if (frag == 0)
  5489. mcl |= TXC_STARTMSDU;
  5490. if (!is_multicast_ether_addr(h->addr1))
  5491. mcl |= TXC_IMMEDACK;
  5492. if (wlc->band->bandtype == BRCM_BAND_5G)
  5493. mcl |= TXC_FREQBAND_5G;
  5494. if (CHSPEC_IS40(wlc_phy_chanspec_get(wlc->band->pi)))
  5495. mcl |= TXC_BW_40;
  5496. /* set AMIC bit if using hardware TKIP MIC */
  5497. if (hwtkmic)
  5498. mcl |= TXC_AMIC;
  5499. txh->MacTxControlLow = cpu_to_le16(mcl);
  5500. /* MacTxControlHigh */
  5501. mch = 0;
  5502. /* Set fallback rate preamble type */
  5503. if ((preamble_type[1] == BRCMS_SHORT_PREAMBLE) ||
  5504. (preamble_type[1] == BRCMS_GF_PREAMBLE)) {
  5505. if (rspec2rate(rspec[1]) != BRCM_RATE_1M)
  5506. mch |= TXC_PREAMBLE_DATA_FB_SHORT;
  5507. }
  5508. /* MacFrameControl */
  5509. memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
  5510. txh->TxFesTimeNormal = cpu_to_le16(0);
  5511. txh->TxFesTimeFallback = cpu_to_le16(0);
  5512. /* TxFrameRA */
  5513. memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
  5514. /* TxFrameID */
  5515. txh->TxFrameID = cpu_to_le16(frameid);
  5516. /*
  5517. * TxStatus, Note the case of recreating the first frag of a suppressed
  5518. * frame then we may need to reset the retry cnt's via the status reg
  5519. */
  5520. txh->TxStatus = cpu_to_le16(status);
  5521. /*
  5522. * extra fields for ucode AMPDU aggregation, the new fields are added to
  5523. * the END of previous structure so that it's compatible in driver.
  5524. */
  5525. txh->MaxNMpdus = cpu_to_le16(0);
  5526. txh->MaxABytes_MRT = cpu_to_le16(0);
  5527. txh->MaxABytes_FBR = cpu_to_le16(0);
  5528. txh->MinMBytes = cpu_to_le16(0);
  5529. /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration,
  5530. * furnish struct d11txh */
  5531. /* RTS PLCP header and RTS frame */
  5532. if (use_rts || use_cts) {
  5533. if (use_rts && use_cts)
  5534. use_cts = false;
  5535. for (k = 0; k < 2; k++) {
  5536. rts_rspec[k] = brcms_c_rspec_to_rts_rspec(wlc, rspec[k],
  5537. false,
  5538. mimo_ctlchbw);
  5539. }
  5540. if (!is_ofdm_rate(rts_rspec[0]) &&
  5541. !((rspec2rate(rts_rspec[0]) == BRCM_RATE_1M) ||
  5542. (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
  5543. rts_preamble_type[0] = BRCMS_SHORT_PREAMBLE;
  5544. mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
  5545. }
  5546. if (!is_ofdm_rate(rts_rspec[1]) &&
  5547. !((rspec2rate(rts_rspec[1]) == BRCM_RATE_1M) ||
  5548. (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
  5549. rts_preamble_type[1] = BRCMS_SHORT_PREAMBLE;
  5550. mch |= TXC_PREAMBLE_RTS_FB_SHORT;
  5551. }
  5552. /* RTS/CTS additions to MacTxControlLow */
  5553. if (use_cts) {
  5554. txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
  5555. } else {
  5556. txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
  5557. txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
  5558. }
  5559. /* RTS PLCP header */
  5560. rts_plcp = txh->RTSPhyHeader;
  5561. if (use_cts)
  5562. rts_phylen = DOT11_CTS_LEN + FCS_LEN;
  5563. else
  5564. rts_phylen = DOT11_RTS_LEN + FCS_LEN;
  5565. brcms_c_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
  5566. /* fallback rate version of RTS PLCP header */
  5567. brcms_c_compute_plcp(wlc, rts_rspec[1], rts_phylen,
  5568. rts_plcp_fallback);
  5569. memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
  5570. sizeof(txh->RTSPLCPFallback));
  5571. /* RTS frame fields... */
  5572. rts = (struct ieee80211_rts *)&txh->rts_frame;
  5573. durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
  5574. rspec[0], rts_preamble_type[0],
  5575. preamble_type[0], phylen, false);
  5576. rts->duration = cpu_to_le16(durid);
  5577. /* fallback rate version of RTS DUR field */
  5578. durid = brcms_c_compute_rtscts_dur(wlc, use_cts,
  5579. rts_rspec[1], rspec[1],
  5580. rts_preamble_type[1],
  5581. preamble_type[1], phylen, false);
  5582. txh->RTSDurFallback = cpu_to_le16(durid);
  5583. if (use_cts) {
  5584. rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  5585. IEEE80211_STYPE_CTS);
  5586. memcpy(&rts->ra, &h->addr2, ETH_ALEN);
  5587. } else {
  5588. rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  5589. IEEE80211_STYPE_RTS);
  5590. memcpy(&rts->ra, &h->addr1, 2 * ETH_ALEN);
  5591. }
  5592. /* mainrate
  5593. * low 8 bits: main frag rate/mcs,
  5594. * high 8 bits: rts/cts rate/mcs
  5595. */
  5596. mainrates |= (is_ofdm_rate(rts_rspec[0]) ?
  5597. D11A_PHY_HDR_GRATE(
  5598. (struct ofdm_phy_hdr *) rts_plcp) :
  5599. rts_plcp[0]) << 8;
  5600. } else {
  5601. memset(txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
  5602. memset(&txh->rts_frame, 0, sizeof(struct ieee80211_rts));
  5603. memset(txh->RTSPLCPFallback, 0, sizeof(txh->RTSPLCPFallback));
  5604. txh->RTSDurFallback = 0;
  5605. }
  5606. #ifdef SUPPORT_40MHZ
  5607. /* add null delimiter count */
  5608. if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && is_mcs_rate(rspec))
  5609. txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
  5610. brcm_c_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
  5611. #endif
  5612. /*
  5613. * Now that RTS/RTS FB preamble types are updated, write
  5614. * the final value
  5615. */
  5616. txh->MacTxControlHigh = cpu_to_le16(mch);
  5617. /*
  5618. * MainRates (both the rts and frag plcp rates have
  5619. * been calculated now)
  5620. */
  5621. txh->MainRates = cpu_to_le16(mainrates);
  5622. /* XtraFrameTypes */
  5623. xfts = frametype(rspec[1], wlc->mimoft);
  5624. xfts |= (frametype(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
  5625. xfts |= (frametype(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
  5626. xfts |= CHSPEC_CHANNEL(wlc_phy_chanspec_get(wlc->band->pi)) <<
  5627. XFTS_CHANNEL_SHIFT;
  5628. txh->XtraFrameTypes = cpu_to_le16(xfts);
  5629. /* PhyTxControlWord */
  5630. phyctl = frametype(rspec[0], wlc->mimoft);
  5631. if ((preamble_type[0] == BRCMS_SHORT_PREAMBLE) ||
  5632. (preamble_type[0] == BRCMS_GF_PREAMBLE)) {
  5633. if (rspec2rate(rspec[0]) != BRCM_RATE_1M)
  5634. phyctl |= PHY_TXC_SHORT_HDR;
  5635. }
  5636. /* phytxant is properly bit shifted */
  5637. phyctl |= brcms_c_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
  5638. txh->PhyTxControlWord = cpu_to_le16(phyctl);
  5639. /* PhyTxControlWord_1 */
  5640. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  5641. u16 phyctl1 = 0;
  5642. phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[0]);
  5643. txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
  5644. phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[1]);
  5645. txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
  5646. if (use_rts || use_cts) {
  5647. phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[0]);
  5648. txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
  5649. phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[1]);
  5650. txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
  5651. }
  5652. /*
  5653. * For mcs frames, if mixedmode(overloaded with long preamble)
  5654. * is going to be set, fill in non-zero MModeLen and/or
  5655. * MModeFbrLen it will be unnecessary if they are separated
  5656. */
  5657. if (is_mcs_rate(rspec[0]) &&
  5658. (preamble_type[0] == BRCMS_MM_PREAMBLE)) {
  5659. u16 mmodelen =
  5660. brcms_c_calc_lsig_len(wlc, rspec[0], phylen);
  5661. txh->MModeLen = cpu_to_le16(mmodelen);
  5662. }
  5663. if (is_mcs_rate(rspec[1]) &&
  5664. (preamble_type[1] == BRCMS_MM_PREAMBLE)) {
  5665. u16 mmodefbrlen =
  5666. brcms_c_calc_lsig_len(wlc, rspec[1], phylen);
  5667. txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
  5668. }
  5669. }
  5670. ac = skb_get_queue_mapping(p);
  5671. if ((scb->flags & SCB_WMECAP) && qos && wlc->edcf_txop[ac]) {
  5672. uint frag_dur, dur, dur_fallback;
  5673. /* WME: Update TXOP threshold */
  5674. if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU) && frag == 0) {
  5675. frag_dur =
  5676. brcms_c_calc_frame_time(wlc, rspec[0],
  5677. preamble_type[0], phylen);
  5678. if (rts) {
  5679. /* 1 RTS or CTS-to-self frame */
  5680. dur =
  5681. brcms_c_calc_cts_time(wlc, rts_rspec[0],
  5682. rts_preamble_type[0]);
  5683. dur_fallback =
  5684. brcms_c_calc_cts_time(wlc, rts_rspec[1],
  5685. rts_preamble_type[1]);
  5686. /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
  5687. dur += le16_to_cpu(rts->duration);
  5688. dur_fallback +=
  5689. le16_to_cpu(txh->RTSDurFallback);
  5690. } else if (use_rifs) {
  5691. dur = frag_dur;
  5692. dur_fallback = 0;
  5693. } else {
  5694. /* frame + SIFS + ACK */
  5695. dur = frag_dur;
  5696. dur +=
  5697. brcms_c_compute_frame_dur(wlc, rspec[0],
  5698. preamble_type[0], 0);
  5699. dur_fallback =
  5700. brcms_c_calc_frame_time(wlc, rspec[1],
  5701. preamble_type[1],
  5702. phylen);
  5703. dur_fallback +=
  5704. brcms_c_compute_frame_dur(wlc, rspec[1],
  5705. preamble_type[1], 0);
  5706. }
  5707. /* NEED to set TxFesTimeNormal (hard) */
  5708. txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
  5709. /*
  5710. * NEED to set fallback rate version of
  5711. * TxFesTimeNormal (hard)
  5712. */
  5713. txh->TxFesTimeFallback =
  5714. cpu_to_le16((u16) dur_fallback);
  5715. /*
  5716. * update txop byte threshold (txop minus intraframe
  5717. * overhead)
  5718. */
  5719. if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
  5720. uint newfragthresh;
  5721. newfragthresh =
  5722. brcms_c_calc_frame_len(wlc,
  5723. rspec[0], preamble_type[0],
  5724. (wlc->edcf_txop[ac] -
  5725. (dur - frag_dur)));
  5726. /* range bound the fragthreshold */
  5727. if (newfragthresh < DOT11_MIN_FRAG_LEN)
  5728. newfragthresh =
  5729. DOT11_MIN_FRAG_LEN;
  5730. else if (newfragthresh >
  5731. wlc->usr_fragthresh)
  5732. newfragthresh =
  5733. wlc->usr_fragthresh;
  5734. /* update the fragthresh and do txc update */
  5735. if (wlc->fragthresh[queue] !=
  5736. (u16) newfragthresh)
  5737. wlc->fragthresh[queue] =
  5738. (u16) newfragthresh;
  5739. } else {
  5740. brcms_warn(wlc->hw->d11core,
  5741. "wl%d: %s txop invalid for rate %d\n",
  5742. wlc->pub->unit, fifo_names[queue],
  5743. rspec2rate(rspec[0]));
  5744. }
  5745. if (dur > wlc->edcf_txop[ac])
  5746. brcms_warn(wlc->hw->d11core,
  5747. "wl%d: %s: %s txop exceeded phylen %d/%d dur %d/%d\n",
  5748. wlc->pub->unit, __func__,
  5749. fifo_names[queue],
  5750. phylen, wlc->fragthresh[queue],
  5751. dur, wlc->edcf_txop[ac]);
  5752. }
  5753. }
  5754. return 0;
  5755. }
  5756. static int brcms_c_tx(struct brcms_c_info *wlc, struct sk_buff *skb)
  5757. {
  5758. struct dma_pub *dma;
  5759. int fifo, ret = -ENOSPC;
  5760. struct d11txh *txh;
  5761. u16 frameid = INVALIDFID;
  5762. fifo = brcms_ac_to_fifo(skb_get_queue_mapping(skb));
  5763. dma = wlc->hw->di[fifo];
  5764. txh = (struct d11txh *)(skb->data);
  5765. if (dma->txavail == 0) {
  5766. /*
  5767. * We sometimes get a frame from mac80211 after stopping
  5768. * the queues. This only ever seems to be a single frame
  5769. * and is seems likely to be a race. TX_HEADROOM should
  5770. * ensure that we have enough space to handle these stray
  5771. * packets, so warn if there isn't. If we're out of space
  5772. * in the tx ring and the tx queue isn't stopped then
  5773. * we've really got a bug; warn loudly if that happens.
  5774. */
  5775. brcms_warn(wlc->hw->d11core,
  5776. "Received frame for tx with no space in DMA ring\n");
  5777. WARN_ON(!ieee80211_queue_stopped(wlc->pub->ieee_hw,
  5778. skb_get_queue_mapping(skb)));
  5779. return -ENOSPC;
  5780. }
  5781. /* When a BC/MC frame is being committed to the BCMC fifo
  5782. * via DMA (NOT PIO), update ucode or BSS info as appropriate.
  5783. */
  5784. if (fifo == TX_BCMC_FIFO)
  5785. frameid = le16_to_cpu(txh->TxFrameID);
  5786. /* Commit BCMC sequence number in the SHM frame ID location */
  5787. if (frameid != INVALIDFID) {
  5788. /*
  5789. * To inform the ucode of the last mcast frame posted
  5790. * so that it can clear moredata bit
  5791. */
  5792. brcms_b_write_shm(wlc->hw, M_BCMC_FID, frameid);
  5793. }
  5794. ret = brcms_c_txfifo(wlc, fifo, skb);
  5795. /*
  5796. * The only reason for brcms_c_txfifo to fail is because
  5797. * there weren't any DMA descriptors, but we've already
  5798. * checked for that. So if it does fail yell loudly.
  5799. */
  5800. WARN_ON_ONCE(ret);
  5801. return ret;
  5802. }
  5803. bool brcms_c_sendpkt_mac80211(struct brcms_c_info *wlc, struct sk_buff *sdu,
  5804. struct ieee80211_hw *hw)
  5805. {
  5806. uint fifo;
  5807. struct scb *scb = &wlc->pri_scb;
  5808. fifo = brcms_ac_to_fifo(skb_get_queue_mapping(sdu));
  5809. brcms_c_d11hdrs_mac80211(wlc, hw, sdu, scb, 0, 1, fifo, 0);
  5810. if (!brcms_c_tx(wlc, sdu))
  5811. return true;
  5812. /* packet discarded */
  5813. dev_kfree_skb_any(sdu);
  5814. return false;
  5815. }
  5816. int
  5817. brcms_c_txfifo(struct brcms_c_info *wlc, uint fifo, struct sk_buff *p)
  5818. {
  5819. struct dma_pub *dma = wlc->hw->di[fifo];
  5820. int ret;
  5821. u16 queue;
  5822. ret = dma_txfast(wlc, dma, p);
  5823. if (ret < 0)
  5824. wiphy_err(wlc->wiphy, "txfifo: fatal, toss frames !!!\n");
  5825. /*
  5826. * Stop queue if DMA ring is full. Reserve some free descriptors,
  5827. * as we sometimes receive a frame from mac80211 after the queues
  5828. * are stopped.
  5829. */
  5830. queue = skb_get_queue_mapping(p);
  5831. if (dma->txavail <= TX_HEADROOM && fifo < TX_BCMC_FIFO &&
  5832. !ieee80211_queue_stopped(wlc->pub->ieee_hw, queue))
  5833. ieee80211_stop_queue(wlc->pub->ieee_hw, queue);
  5834. return ret;
  5835. }
  5836. u32
  5837. brcms_c_rspec_to_rts_rspec(struct brcms_c_info *wlc, u32 rspec,
  5838. bool use_rspec, u16 mimo_ctlchbw)
  5839. {
  5840. u32 rts_rspec = 0;
  5841. if (use_rspec)
  5842. /* use frame rate as rts rate */
  5843. rts_rspec = rspec;
  5844. else if (wlc->band->gmode && wlc->protection->_g && !is_cck_rate(rspec))
  5845. /* Use 11Mbps as the g protection RTS target rate and fallback.
  5846. * Use the brcms_basic_rate() lookup to find the best basic rate
  5847. * under the target in case 11 Mbps is not Basic.
  5848. * 6 and 9 Mbps are not usually selected by rate selection, but
  5849. * even if the OFDM rate we are protecting is 6 or 9 Mbps, 11
  5850. * is more robust.
  5851. */
  5852. rts_rspec = brcms_basic_rate(wlc, BRCM_RATE_11M);
  5853. else
  5854. /* calculate RTS rate and fallback rate based on the frame rate
  5855. * RTS must be sent at a basic rate since it is a
  5856. * control frame, sec 9.6 of 802.11 spec
  5857. */
  5858. rts_rspec = brcms_basic_rate(wlc, rspec);
  5859. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  5860. /* set rts txbw to correct side band */
  5861. rts_rspec &= ~RSPEC_BW_MASK;
  5862. /*
  5863. * if rspec/rspec_fallback is 40MHz, then send RTS on both
  5864. * 20MHz channel (DUP), otherwise send RTS on control channel
  5865. */
  5866. if (rspec_is40mhz(rspec) && !is_cck_rate(rts_rspec))
  5867. rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
  5868. else
  5869. rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
  5870. /* pick siso/cdd as default for ofdm */
  5871. if (is_ofdm_rate(rts_rspec)) {
  5872. rts_rspec &= ~RSPEC_STF_MASK;
  5873. rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
  5874. }
  5875. }
  5876. return rts_rspec;
  5877. }
  5878. /* Update beacon listen interval in shared memory */
  5879. static void brcms_c_bcn_li_upd(struct brcms_c_info *wlc)
  5880. {
  5881. /* wake up every DTIM is the default */
  5882. if (wlc->bcn_li_dtim == 1)
  5883. brcms_b_write_shm(wlc->hw, M_BCN_LI, 0);
  5884. else
  5885. brcms_b_write_shm(wlc->hw, M_BCN_LI,
  5886. (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
  5887. }
  5888. static void
  5889. brcms_b_read_tsf(struct brcms_hardware *wlc_hw, u32 *tsf_l_ptr,
  5890. u32 *tsf_h_ptr)
  5891. {
  5892. struct bcma_device *core = wlc_hw->d11core;
  5893. /* read the tsf timer low, then high to get an atomic read */
  5894. *tsf_l_ptr = bcma_read32(core, D11REGOFFS(tsf_timerlow));
  5895. *tsf_h_ptr = bcma_read32(core, D11REGOFFS(tsf_timerhigh));
  5896. }
  5897. /*
  5898. * recover 64bit TSF value from the 16bit TSF value in the rx header
  5899. * given the assumption that the TSF passed in header is within 65ms
  5900. * of the current tsf.
  5901. *
  5902. * 6 5 4 4 3 2 1
  5903. * 3.......6.......8.......0.......2.......4.......6.......8......0
  5904. * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
  5905. *
  5906. * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
  5907. * tsf_l is filled in by brcms_b_recv, which is done earlier in the
  5908. * receive call sequence after rx interrupt. Only the higher 16 bits
  5909. * are used. Finally, the tsf_h is read from the tsf register.
  5910. */
  5911. static u64 brcms_c_recover_tsf64(struct brcms_c_info *wlc,
  5912. struct d11rxhdr *rxh)
  5913. {
  5914. u32 tsf_h, tsf_l;
  5915. u16 rx_tsf_0_15, rx_tsf_16_31;
  5916. brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
  5917. rx_tsf_16_31 = (u16)(tsf_l >> 16);
  5918. rx_tsf_0_15 = rxh->RxTSFTime;
  5919. /*
  5920. * a greater tsf time indicates the low 16 bits of
  5921. * tsf_l wrapped, so decrement the high 16 bits.
  5922. */
  5923. if ((u16)tsf_l < rx_tsf_0_15) {
  5924. rx_tsf_16_31 -= 1;
  5925. if (rx_tsf_16_31 == 0xffff)
  5926. tsf_h -= 1;
  5927. }
  5928. return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
  5929. }
  5930. static void
  5931. prep_mac80211_status(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
  5932. struct sk_buff *p,
  5933. struct ieee80211_rx_status *rx_status)
  5934. {
  5935. int channel;
  5936. u32 rspec;
  5937. unsigned char *plcp;
  5938. /* fill in TSF and flag its presence */
  5939. rx_status->mactime = brcms_c_recover_tsf64(wlc, rxh);
  5940. rx_status->flag |= RX_FLAG_MACTIME_START;
  5941. channel = BRCMS_CHAN_CHANNEL(rxh->RxChan);
  5942. rx_status->band =
  5943. channel > 14 ? NL80211_BAND_5GHZ : NL80211_BAND_2GHZ;
  5944. rx_status->freq =
  5945. ieee80211_channel_to_frequency(channel, rx_status->band);
  5946. rx_status->signal = wlc_phy_rssi_compute(wlc->hw->band->pi, rxh);
  5947. /* noise */
  5948. /* qual */
  5949. rx_status->antenna =
  5950. (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
  5951. plcp = p->data;
  5952. rspec = brcms_c_compute_rspec(rxh, plcp);
  5953. if (is_mcs_rate(rspec)) {
  5954. rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
  5955. rx_status->flag |= RX_FLAG_HT;
  5956. if (rspec_is40mhz(rspec))
  5957. rx_status->flag |= RX_FLAG_40MHZ;
  5958. } else {
  5959. switch (rspec2rate(rspec)) {
  5960. case BRCM_RATE_1M:
  5961. rx_status->rate_idx = 0;
  5962. break;
  5963. case BRCM_RATE_2M:
  5964. rx_status->rate_idx = 1;
  5965. break;
  5966. case BRCM_RATE_5M5:
  5967. rx_status->rate_idx = 2;
  5968. break;
  5969. case BRCM_RATE_11M:
  5970. rx_status->rate_idx = 3;
  5971. break;
  5972. case BRCM_RATE_6M:
  5973. rx_status->rate_idx = 4;
  5974. break;
  5975. case BRCM_RATE_9M:
  5976. rx_status->rate_idx = 5;
  5977. break;
  5978. case BRCM_RATE_12M:
  5979. rx_status->rate_idx = 6;
  5980. break;
  5981. case BRCM_RATE_18M:
  5982. rx_status->rate_idx = 7;
  5983. break;
  5984. case BRCM_RATE_24M:
  5985. rx_status->rate_idx = 8;
  5986. break;
  5987. case BRCM_RATE_36M:
  5988. rx_status->rate_idx = 9;
  5989. break;
  5990. case BRCM_RATE_48M:
  5991. rx_status->rate_idx = 10;
  5992. break;
  5993. case BRCM_RATE_54M:
  5994. rx_status->rate_idx = 11;
  5995. break;
  5996. default:
  5997. brcms_err(wlc->hw->d11core,
  5998. "%s: Unknown rate\n", __func__);
  5999. }
  6000. /*
  6001. * For 5GHz, we should decrease the index as it is
  6002. * a subset of the 2.4G rates. See bitrates field
  6003. * of brcms_band_5GHz_nphy (in mac80211_if.c).
  6004. */
  6005. if (rx_status->band == NL80211_BAND_5GHZ)
  6006. rx_status->rate_idx -= BRCMS_LEGACY_5G_RATE_OFFSET;
  6007. /* Determine short preamble and rate_idx */
  6008. if (is_cck_rate(rspec)) {
  6009. if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
  6010. rx_status->flag |= RX_FLAG_SHORTPRE;
  6011. } else if (is_ofdm_rate(rspec)) {
  6012. rx_status->flag |= RX_FLAG_SHORTPRE;
  6013. } else {
  6014. brcms_err(wlc->hw->d11core, "%s: Unknown modulation\n",
  6015. __func__);
  6016. }
  6017. }
  6018. if (plcp3_issgi(plcp[3]))
  6019. rx_status->flag |= RX_FLAG_SHORT_GI;
  6020. if (rxh->RxStatus1 & RXS_DECERR) {
  6021. rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
  6022. brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_PLCP_CRC\n",
  6023. __func__);
  6024. }
  6025. if (rxh->RxStatus1 & RXS_FCSERR) {
  6026. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  6027. brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_FCS_CRC\n",
  6028. __func__);
  6029. }
  6030. }
  6031. static void
  6032. brcms_c_recvctl(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
  6033. struct sk_buff *p)
  6034. {
  6035. int len_mpdu;
  6036. struct ieee80211_rx_status rx_status;
  6037. struct ieee80211_hdr *hdr;
  6038. memset(&rx_status, 0, sizeof(rx_status));
  6039. prep_mac80211_status(wlc, rxh, p, &rx_status);
  6040. /* mac header+body length, exclude CRC and plcp header */
  6041. len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
  6042. skb_pull(p, D11_PHY_HDR_LEN);
  6043. __skb_trim(p, len_mpdu);
  6044. /* unmute transmit */
  6045. if (wlc->hw->suspended_fifos) {
  6046. hdr = (struct ieee80211_hdr *)p->data;
  6047. if (ieee80211_is_beacon(hdr->frame_control))
  6048. brcms_b_mute(wlc->hw, false);
  6049. }
  6050. memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
  6051. ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
  6052. }
  6053. /* calculate frame duration for Mixed-mode L-SIG spoofing, return
  6054. * number of bytes goes in the length field
  6055. *
  6056. * Formula given by HT PHY Spec v 1.13
  6057. * len = 3(nsyms + nstream + 3) - 3
  6058. */
  6059. u16
  6060. brcms_c_calc_lsig_len(struct brcms_c_info *wlc, u32 ratespec,
  6061. uint mac_len)
  6062. {
  6063. uint nsyms, len = 0, kNdps;
  6064. if (is_mcs_rate(ratespec)) {
  6065. uint mcs = ratespec & RSPEC_RATE_MASK;
  6066. int tot_streams = (mcs_2_txstreams(mcs) + 1) +
  6067. rspec_stc(ratespec);
  6068. /*
  6069. * the payload duration calculation matches that
  6070. * of regular ofdm
  6071. */
  6072. /* 1000Ndbps = kbps * 4 */
  6073. kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
  6074. rspec_issgi(ratespec)) * 4;
  6075. if (rspec_stc(ratespec) == 0)
  6076. nsyms =
  6077. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  6078. APHY_TAIL_NBITS) * 1000, kNdps);
  6079. else
  6080. /* STBC needs to have even number of symbols */
  6081. nsyms =
  6082. 2 *
  6083. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  6084. APHY_TAIL_NBITS) * 1000, 2 * kNdps);
  6085. /* (+3) account for HT-SIG(2) and HT-STF(1) */
  6086. nsyms += (tot_streams + 3);
  6087. /*
  6088. * 3 bytes/symbol @ legacy 6Mbps rate
  6089. * (-3) excluding service bits and tail bits
  6090. */
  6091. len = (3 * nsyms) - 3;
  6092. }
  6093. return (u16) len;
  6094. }
  6095. static void
  6096. brcms_c_mod_prb_rsp_rate_table(struct brcms_c_info *wlc, uint frame_len)
  6097. {
  6098. const struct brcms_c_rateset *rs_dflt;
  6099. struct brcms_c_rateset rs;
  6100. u8 rate;
  6101. u16 entry_ptr;
  6102. u8 plcp[D11_PHY_HDR_LEN];
  6103. u16 dur, sifs;
  6104. uint i;
  6105. sifs = get_sifs(wlc->band);
  6106. rs_dflt = brcms_c_rateset_get_hwrs(wlc);
  6107. brcms_c_rateset_copy(rs_dflt, &rs);
  6108. brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
  6109. /*
  6110. * walk the phy rate table and update MAC core SHM
  6111. * basic rate table entries
  6112. */
  6113. for (i = 0; i < rs.count; i++) {
  6114. rate = rs.rates[i] & BRCMS_RATE_MASK;
  6115. entry_ptr = brcms_b_rate_shm_offset(wlc->hw, rate);
  6116. /* Calculate the Probe Response PLCP for the given rate */
  6117. brcms_c_compute_plcp(wlc, rate, frame_len, plcp);
  6118. /*
  6119. * Calculate the duration of the Probe Response
  6120. * frame plus SIFS for the MAC
  6121. */
  6122. dur = (u16) brcms_c_calc_frame_time(wlc, rate,
  6123. BRCMS_LONG_PREAMBLE, frame_len);
  6124. dur += sifs;
  6125. /* Update the SHM Rate Table entry Probe Response values */
  6126. brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS,
  6127. (u16) (plcp[0] + (plcp[1] << 8)));
  6128. brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS + 2,
  6129. (u16) (plcp[2] + (plcp[3] << 8)));
  6130. brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_DUR_POS, dur);
  6131. }
  6132. }
  6133. int brcms_c_get_header_len(void)
  6134. {
  6135. return TXOFF;
  6136. }
  6137. static void brcms_c_beacon_write(struct brcms_c_info *wlc,
  6138. struct sk_buff *beacon, u16 tim_offset,
  6139. u16 dtim_period, bool bcn0, bool bcn1)
  6140. {
  6141. size_t len;
  6142. struct ieee80211_tx_info *tx_info;
  6143. struct brcms_hardware *wlc_hw = wlc->hw;
  6144. struct ieee80211_hw *ieee_hw = brcms_c_pub(wlc)->ieee_hw;
  6145. /* Get tx_info */
  6146. tx_info = IEEE80211_SKB_CB(beacon);
  6147. len = min_t(size_t, beacon->len, BCN_TMPL_LEN);
  6148. wlc->bcn_rspec = ieee80211_get_tx_rate(ieee_hw, tx_info)->hw_value;
  6149. brcms_c_compute_plcp(wlc, wlc->bcn_rspec,
  6150. len + FCS_LEN - D11_PHY_HDR_LEN, beacon->data);
  6151. /* "Regular" and 16 MBSS but not for 4 MBSS */
  6152. /* Update the phytxctl for the beacon based on the rspec */
  6153. brcms_c_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
  6154. if (bcn0) {
  6155. /* write the probe response into the template region */
  6156. brcms_b_write_template_ram(wlc_hw, T_BCN0_TPL_BASE,
  6157. (len + 3) & ~3, beacon->data);
  6158. /* write beacon length to SCR */
  6159. brcms_b_write_shm(wlc_hw, M_BCN0_FRM_BYTESZ, (u16) len);
  6160. }
  6161. if (bcn1) {
  6162. /* write the probe response into the template region */
  6163. brcms_b_write_template_ram(wlc_hw, T_BCN1_TPL_BASE,
  6164. (len + 3) & ~3, beacon->data);
  6165. /* write beacon length to SCR */
  6166. brcms_b_write_shm(wlc_hw, M_BCN1_FRM_BYTESZ, (u16) len);
  6167. }
  6168. if (tim_offset != 0) {
  6169. brcms_b_write_shm(wlc_hw, M_TIMBPOS_INBEACON,
  6170. tim_offset + D11B_PHY_HDR_LEN);
  6171. brcms_b_write_shm(wlc_hw, M_DOT11_DTIMPERIOD, dtim_period);
  6172. } else {
  6173. brcms_b_write_shm(wlc_hw, M_TIMBPOS_INBEACON,
  6174. len + D11B_PHY_HDR_LEN);
  6175. brcms_b_write_shm(wlc_hw, M_DOT11_DTIMPERIOD, 0);
  6176. }
  6177. }
  6178. static void brcms_c_update_beacon_hw(struct brcms_c_info *wlc,
  6179. struct sk_buff *beacon, u16 tim_offset,
  6180. u16 dtim_period)
  6181. {
  6182. struct brcms_hardware *wlc_hw = wlc->hw;
  6183. struct bcma_device *core = wlc_hw->d11core;
  6184. /* Hardware beaconing for this config */
  6185. u32 both_valid = MCMD_BCN0VLD | MCMD_BCN1VLD;
  6186. /* Check if both templates are in use, if so sched. an interrupt
  6187. * that will call back into this routine
  6188. */
  6189. if ((bcma_read32(core, D11REGOFFS(maccommand)) & both_valid) == both_valid)
  6190. /* clear any previous status */
  6191. bcma_write32(core, D11REGOFFS(macintstatus), MI_BCNTPL);
  6192. if (wlc->beacon_template_virgin) {
  6193. wlc->beacon_template_virgin = false;
  6194. brcms_c_beacon_write(wlc, beacon, tim_offset, dtim_period, true,
  6195. true);
  6196. /* mark beacon0 valid */
  6197. bcma_set32(core, D11REGOFFS(maccommand), MCMD_BCN0VLD);
  6198. return;
  6199. }
  6200. /* Check that after scheduling the interrupt both of the
  6201. * templates are still busy. if not clear the int. & remask
  6202. */
  6203. if ((bcma_read32(core, D11REGOFFS(maccommand)) & both_valid) == both_valid) {
  6204. wlc->defmacintmask |= MI_BCNTPL;
  6205. return;
  6206. }
  6207. if (!(bcma_read32(core, D11REGOFFS(maccommand)) & MCMD_BCN0VLD)) {
  6208. brcms_c_beacon_write(wlc, beacon, tim_offset, dtim_period, true,
  6209. false);
  6210. /* mark beacon0 valid */
  6211. bcma_set32(core, D11REGOFFS(maccommand), MCMD_BCN0VLD);
  6212. return;
  6213. }
  6214. if (!(bcma_read32(core, D11REGOFFS(maccommand)) & MCMD_BCN1VLD)) {
  6215. brcms_c_beacon_write(wlc, beacon, tim_offset, dtim_period,
  6216. false, true);
  6217. /* mark beacon0 valid */
  6218. bcma_set32(core, D11REGOFFS(maccommand), MCMD_BCN1VLD);
  6219. return;
  6220. }
  6221. return;
  6222. }
  6223. /*
  6224. * Update all beacons for the system.
  6225. */
  6226. void brcms_c_update_beacon(struct brcms_c_info *wlc)
  6227. {
  6228. struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
  6229. if (wlc->pub->up && (bsscfg->type == BRCMS_TYPE_AP ||
  6230. bsscfg->type == BRCMS_TYPE_ADHOC)) {
  6231. /* Clear the soft intmask */
  6232. wlc->defmacintmask &= ~MI_BCNTPL;
  6233. if (!wlc->beacon)
  6234. return;
  6235. brcms_c_update_beacon_hw(wlc, wlc->beacon,
  6236. wlc->beacon_tim_offset,
  6237. wlc->beacon_dtim_period);
  6238. }
  6239. }
  6240. void brcms_c_set_new_beacon(struct brcms_c_info *wlc, struct sk_buff *beacon,
  6241. u16 tim_offset, u16 dtim_period)
  6242. {
  6243. if (!beacon)
  6244. return;
  6245. if (wlc->beacon)
  6246. dev_kfree_skb_any(wlc->beacon);
  6247. wlc->beacon = beacon;
  6248. /* add PLCP */
  6249. skb_push(wlc->beacon, D11_PHY_HDR_LEN);
  6250. wlc->beacon_tim_offset = tim_offset;
  6251. wlc->beacon_dtim_period = dtim_period;
  6252. brcms_c_update_beacon(wlc);
  6253. }
  6254. void brcms_c_set_new_probe_resp(struct brcms_c_info *wlc,
  6255. struct sk_buff *probe_resp)
  6256. {
  6257. if (!probe_resp)
  6258. return;
  6259. if (wlc->probe_resp)
  6260. dev_kfree_skb_any(wlc->probe_resp);
  6261. wlc->probe_resp = probe_resp;
  6262. /* add PLCP */
  6263. skb_push(wlc->probe_resp, D11_PHY_HDR_LEN);
  6264. brcms_c_update_probe_resp(wlc, false);
  6265. }
  6266. void brcms_c_enable_probe_resp(struct brcms_c_info *wlc, bool enable)
  6267. {
  6268. /*
  6269. * prevent ucode from sending probe responses by setting the timeout
  6270. * to 1, it can not send it in that time frame.
  6271. */
  6272. wlc->prb_resp_timeout = enable ? BRCMS_PRB_RESP_TIMEOUT : 1;
  6273. brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
  6274. /* TODO: if (enable) => also deactivate receiving of probe request */
  6275. }
  6276. /* Write ssid into shared memory */
  6277. static void
  6278. brcms_c_shm_ssid_upd(struct brcms_c_info *wlc, struct brcms_bss_cfg *cfg)
  6279. {
  6280. u8 *ssidptr = cfg->SSID;
  6281. u16 base = M_SSID;
  6282. u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
  6283. /* padding the ssid with zero and copy it into shm */
  6284. memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
  6285. memcpy(ssidbuf, ssidptr, cfg->SSID_len);
  6286. brcms_c_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
  6287. brcms_b_write_shm(wlc->hw, M_SSIDLEN, (u16) cfg->SSID_len);
  6288. }
  6289. static void
  6290. brcms_c_bss_update_probe_resp(struct brcms_c_info *wlc,
  6291. struct brcms_bss_cfg *cfg,
  6292. struct sk_buff *probe_resp,
  6293. bool suspend)
  6294. {
  6295. int len;
  6296. len = min_t(size_t, probe_resp->len, BCN_TMPL_LEN);
  6297. if (suspend)
  6298. brcms_c_suspend_mac_and_wait(wlc);
  6299. /* write the probe response into the template region */
  6300. brcms_b_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
  6301. (len + 3) & ~3, probe_resp->data);
  6302. /* write the length of the probe response frame (+PLCP/-FCS) */
  6303. brcms_b_write_shm(wlc->hw, M_PRB_RESP_FRM_LEN, (u16) len);
  6304. /* write the SSID and SSID length */
  6305. brcms_c_shm_ssid_upd(wlc, cfg);
  6306. /*
  6307. * Write PLCP headers and durations for probe response frames
  6308. * at all rates. Use the actual frame length covered by the
  6309. * PLCP header for the call to brcms_c_mod_prb_rsp_rate_table()
  6310. * by subtracting the PLCP len and adding the FCS.
  6311. */
  6312. brcms_c_mod_prb_rsp_rate_table(wlc,
  6313. (u16)len + FCS_LEN - D11_PHY_HDR_LEN);
  6314. if (suspend)
  6315. brcms_c_enable_mac(wlc);
  6316. }
  6317. void brcms_c_update_probe_resp(struct brcms_c_info *wlc, bool suspend)
  6318. {
  6319. struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
  6320. /* update AP or IBSS probe responses */
  6321. if (wlc->pub->up && (bsscfg->type == BRCMS_TYPE_AP ||
  6322. bsscfg->type == BRCMS_TYPE_ADHOC)) {
  6323. if (!wlc->probe_resp)
  6324. return;
  6325. brcms_c_bss_update_probe_resp(wlc, bsscfg, wlc->probe_resp,
  6326. suspend);
  6327. }
  6328. }
  6329. int brcms_b_xmtfifo_sz_get(struct brcms_hardware *wlc_hw, uint fifo,
  6330. uint *blocks)
  6331. {
  6332. if (fifo >= NFIFO)
  6333. return -EINVAL;
  6334. *blocks = wlc_hw->xmtfifo_sz[fifo];
  6335. return 0;
  6336. }
  6337. void
  6338. brcms_c_set_addrmatch(struct brcms_c_info *wlc, int match_reg_offset,
  6339. const u8 *addr)
  6340. {
  6341. brcms_b_set_addrmatch(wlc->hw, match_reg_offset, addr);
  6342. if (match_reg_offset == RCM_BSSID_OFFSET)
  6343. memcpy(wlc->bsscfg->BSSID, addr, ETH_ALEN);
  6344. }
  6345. /*
  6346. * Flag 'scan in progress' to withhold dynamic phy calibration
  6347. */
  6348. void brcms_c_scan_start(struct brcms_c_info *wlc)
  6349. {
  6350. wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
  6351. }
  6352. void brcms_c_scan_stop(struct brcms_c_info *wlc)
  6353. {
  6354. wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
  6355. }
  6356. void brcms_c_associate_upd(struct brcms_c_info *wlc, bool state)
  6357. {
  6358. wlc->pub->associated = state;
  6359. }
  6360. /*
  6361. * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
  6362. * AMPDU traffic, packets pending in hardware have to be invalidated so that
  6363. * when later on hardware releases them, they can be handled appropriately.
  6364. */
  6365. void brcms_c_inval_dma_pkts(struct brcms_hardware *hw,
  6366. struct ieee80211_sta *sta,
  6367. void (*dma_callback_fn))
  6368. {
  6369. struct dma_pub *dmah;
  6370. int i;
  6371. for (i = 0; i < NFIFO; i++) {
  6372. dmah = hw->di[i];
  6373. if (dmah != NULL)
  6374. dma_walk_packets(dmah, dma_callback_fn, sta);
  6375. }
  6376. }
  6377. int brcms_c_get_curband(struct brcms_c_info *wlc)
  6378. {
  6379. return wlc->band->bandunit;
  6380. }
  6381. bool brcms_c_tx_flush_completed(struct brcms_c_info *wlc)
  6382. {
  6383. int i;
  6384. /* Kick DMA to send any pending AMPDU */
  6385. for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
  6386. if (wlc->hw->di[i])
  6387. dma_kick_tx(wlc->hw->di[i]);
  6388. return !brcms_txpktpendtot(wlc);
  6389. }
  6390. void brcms_c_set_beacon_listen_interval(struct brcms_c_info *wlc, u8 interval)
  6391. {
  6392. wlc->bcn_li_bcn = interval;
  6393. if (wlc->pub->up)
  6394. brcms_c_bcn_li_upd(wlc);
  6395. }
  6396. u64 brcms_c_tsf_get(struct brcms_c_info *wlc)
  6397. {
  6398. u32 tsf_h, tsf_l;
  6399. u64 tsf;
  6400. brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
  6401. tsf = tsf_h;
  6402. tsf <<= 32;
  6403. tsf |= tsf_l;
  6404. return tsf;
  6405. }
  6406. void brcms_c_tsf_set(struct brcms_c_info *wlc, u64 tsf)
  6407. {
  6408. u32 tsf_h, tsf_l;
  6409. brcms_c_time_lock(wlc);
  6410. tsf_l = tsf;
  6411. tsf_h = (tsf >> 32);
  6412. /* read the tsf timer low, then high to get an atomic read */
  6413. bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_timerlow), tsf_l);
  6414. bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_timerhigh), tsf_h);
  6415. brcms_c_time_unlock(wlc);
  6416. }
  6417. int brcms_c_set_tx_power(struct brcms_c_info *wlc, int txpwr)
  6418. {
  6419. uint qdbm;
  6420. /* Remove override bit and clip to max qdbm value */
  6421. qdbm = min_t(uint, txpwr * BRCMS_TXPWR_DB_FACTOR, 0xff);
  6422. return wlc_phy_txpower_set(wlc->band->pi, qdbm, false);
  6423. }
  6424. int brcms_c_get_tx_power(struct brcms_c_info *wlc)
  6425. {
  6426. uint qdbm;
  6427. bool override;
  6428. wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
  6429. /* Return qdbm units */
  6430. return (int)(qdbm / BRCMS_TXPWR_DB_FACTOR);
  6431. }
  6432. /* Process received frames */
  6433. /*
  6434. * Return true if more frames need to be processed. false otherwise.
  6435. * Param 'bound' indicates max. # frames to process before break out.
  6436. */
  6437. static void brcms_c_recv(struct brcms_c_info *wlc, struct sk_buff *p)
  6438. {
  6439. struct d11rxhdr *rxh;
  6440. struct ieee80211_hdr *h;
  6441. uint len;
  6442. bool is_amsdu;
  6443. /* frame starts with rxhdr */
  6444. rxh = (struct d11rxhdr *) (p->data);
  6445. /* strip off rxhdr */
  6446. skb_pull(p, BRCMS_HWRXOFF);
  6447. /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
  6448. if (rxh->RxStatus1 & RXS_PBPRES) {
  6449. if (p->len < 2) {
  6450. brcms_err(wlc->hw->d11core,
  6451. "wl%d: recv: rcvd runt of len %d\n",
  6452. wlc->pub->unit, p->len);
  6453. goto toss;
  6454. }
  6455. skb_pull(p, 2);
  6456. }
  6457. h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
  6458. len = p->len;
  6459. if (rxh->RxStatus1 & RXS_FCSERR) {
  6460. if (!(wlc->filter_flags & FIF_FCSFAIL))
  6461. goto toss;
  6462. }
  6463. /* check received pkt has at least frame control field */
  6464. if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control))
  6465. goto toss;
  6466. /* not supporting A-MSDU */
  6467. is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
  6468. if (is_amsdu)
  6469. goto toss;
  6470. brcms_c_recvctl(wlc, rxh, p);
  6471. return;
  6472. toss:
  6473. brcmu_pkt_buf_free_skb(p);
  6474. }
  6475. /* Process received frames */
  6476. /*
  6477. * Return true if more frames need to be processed. false otherwise.
  6478. * Param 'bound' indicates max. # frames to process before break out.
  6479. */
  6480. static bool
  6481. brcms_b_recv(struct brcms_hardware *wlc_hw, uint fifo, bool bound)
  6482. {
  6483. struct sk_buff *p;
  6484. struct sk_buff *next = NULL;
  6485. struct sk_buff_head recv_frames;
  6486. uint n = 0;
  6487. uint bound_limit = bound ? RXBND : -1;
  6488. bool morepending = false;
  6489. skb_queue_head_init(&recv_frames);
  6490. /* gather received frames */
  6491. do {
  6492. /* !give others some time to run! */
  6493. if (n >= bound_limit)
  6494. break;
  6495. morepending = dma_rx(wlc_hw->di[fifo], &recv_frames);
  6496. n++;
  6497. } while (morepending);
  6498. /* post more rbufs */
  6499. dma_rxfill(wlc_hw->di[fifo]);
  6500. /* process each frame */
  6501. skb_queue_walk_safe(&recv_frames, p, next) {
  6502. struct d11rxhdr_le *rxh_le;
  6503. struct d11rxhdr *rxh;
  6504. skb_unlink(p, &recv_frames);
  6505. rxh_le = (struct d11rxhdr_le *)p->data;
  6506. rxh = (struct d11rxhdr *)p->data;
  6507. /* fixup rx header endianness */
  6508. rxh->RxFrameSize = le16_to_cpu(rxh_le->RxFrameSize);
  6509. rxh->PhyRxStatus_0 = le16_to_cpu(rxh_le->PhyRxStatus_0);
  6510. rxh->PhyRxStatus_1 = le16_to_cpu(rxh_le->PhyRxStatus_1);
  6511. rxh->PhyRxStatus_2 = le16_to_cpu(rxh_le->PhyRxStatus_2);
  6512. rxh->PhyRxStatus_3 = le16_to_cpu(rxh_le->PhyRxStatus_3);
  6513. rxh->PhyRxStatus_4 = le16_to_cpu(rxh_le->PhyRxStatus_4);
  6514. rxh->PhyRxStatus_5 = le16_to_cpu(rxh_le->PhyRxStatus_5);
  6515. rxh->RxStatus1 = le16_to_cpu(rxh_le->RxStatus1);
  6516. rxh->RxStatus2 = le16_to_cpu(rxh_le->RxStatus2);
  6517. rxh->RxTSFTime = le16_to_cpu(rxh_le->RxTSFTime);
  6518. rxh->RxChan = le16_to_cpu(rxh_le->RxChan);
  6519. brcms_c_recv(wlc_hw->wlc, p);
  6520. }
  6521. return morepending;
  6522. }
  6523. /* second-level interrupt processing
  6524. * Return true if another dpc needs to be re-scheduled. false otherwise.
  6525. * Param 'bounded' indicates if applicable loops should be bounded.
  6526. */
  6527. bool brcms_c_dpc(struct brcms_c_info *wlc, bool bounded)
  6528. {
  6529. u32 macintstatus;
  6530. struct brcms_hardware *wlc_hw = wlc->hw;
  6531. struct bcma_device *core = wlc_hw->d11core;
  6532. if (brcms_deviceremoved(wlc)) {
  6533. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  6534. __func__);
  6535. brcms_down(wlc->wl);
  6536. return false;
  6537. }
  6538. /* grab and clear the saved software intstatus bits */
  6539. macintstatus = wlc->macintstatus;
  6540. wlc->macintstatus = 0;
  6541. brcms_dbg_int(core, "wl%d: macintstatus 0x%x\n",
  6542. wlc_hw->unit, macintstatus);
  6543. WARN_ON(macintstatus & MI_PRQ); /* PRQ Interrupt in non-MBSS */
  6544. /* tx status */
  6545. if (macintstatus & MI_TFS) {
  6546. bool fatal;
  6547. if (brcms_b_txstatus(wlc->hw, bounded, &fatal))
  6548. wlc->macintstatus |= MI_TFS;
  6549. if (fatal) {
  6550. brcms_err(core, "MI_TFS: fatal\n");
  6551. goto fatal;
  6552. }
  6553. }
  6554. if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
  6555. brcms_c_tbtt(wlc);
  6556. /* ATIM window end */
  6557. if (macintstatus & MI_ATIMWINEND) {
  6558. brcms_dbg_info(core, "end of ATIM window\n");
  6559. bcma_set32(core, D11REGOFFS(maccommand), wlc->qvalid);
  6560. wlc->qvalid = 0;
  6561. }
  6562. /*
  6563. * received data or control frame, MI_DMAINT is
  6564. * indication of RX_FIFO interrupt
  6565. */
  6566. if (macintstatus & MI_DMAINT)
  6567. if (brcms_b_recv(wlc_hw, RX_FIFO, bounded))
  6568. wlc->macintstatus |= MI_DMAINT;
  6569. /* noise sample collected */
  6570. if (macintstatus & MI_BG_NOISE)
  6571. wlc_phy_noise_sample_intr(wlc_hw->band->pi);
  6572. if (macintstatus & MI_GP0) {
  6573. brcms_err(core, "wl%d: PSM microcode watchdog fired at %d "
  6574. "(seconds). Resetting.\n", wlc_hw->unit, wlc_hw->now);
  6575. printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
  6576. __func__, ai_get_chip_id(wlc_hw->sih),
  6577. ai_get_chiprev(wlc_hw->sih));
  6578. brcms_fatal_error(wlc_hw->wlc->wl);
  6579. }
  6580. /* gptimer timeout */
  6581. if (macintstatus & MI_TO)
  6582. bcma_write32(core, D11REGOFFS(gptimer), 0);
  6583. if (macintstatus & MI_RFDISABLE) {
  6584. brcms_dbg_info(core, "wl%d: BMAC Detected a change on the"
  6585. " RF Disable Input\n", wlc_hw->unit);
  6586. brcms_rfkill_set_hw_state(wlc->wl);
  6587. }
  6588. /* BCN template is available */
  6589. if (macintstatus & MI_BCNTPL)
  6590. brcms_c_update_beacon(wlc);
  6591. /* it isn't done and needs to be resched if macintstatus is non-zero */
  6592. return wlc->macintstatus != 0;
  6593. fatal:
  6594. brcms_fatal_error(wlc_hw->wlc->wl);
  6595. return wlc->macintstatus != 0;
  6596. }
  6597. void brcms_c_init(struct brcms_c_info *wlc, bool mute_tx)
  6598. {
  6599. struct bcma_device *core = wlc->hw->d11core;
  6600. struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.chandef.chan;
  6601. u16 chanspec;
  6602. brcms_dbg_info(core, "wl%d\n", wlc->pub->unit);
  6603. chanspec = ch20mhz_chspec(ch->hw_value);
  6604. brcms_b_init(wlc->hw, chanspec);
  6605. /* update beacon listen interval */
  6606. brcms_c_bcn_li_upd(wlc);
  6607. /* write ethernet address to core */
  6608. brcms_c_set_mac(wlc->bsscfg);
  6609. brcms_c_set_bssid(wlc->bsscfg);
  6610. /* Update tsf_cfprep if associated and up */
  6611. if (wlc->pub->associated && wlc->pub->up) {
  6612. u32 bi;
  6613. /* get beacon period and convert to uS */
  6614. bi = wlc->bsscfg->current_bss->beacon_period << 10;
  6615. /*
  6616. * update since init path would reset
  6617. * to default value
  6618. */
  6619. bcma_write32(core, D11REGOFFS(tsf_cfprep),
  6620. bi << CFPREP_CBI_SHIFT);
  6621. /* Update maccontrol PM related bits */
  6622. brcms_c_set_ps_ctrl(wlc);
  6623. }
  6624. brcms_c_bandinit_ordered(wlc, chanspec);
  6625. /* init probe response timeout */
  6626. brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
  6627. /* init max burst txop (framebursting) */
  6628. brcms_b_write_shm(wlc->hw, M_MBURST_TXOP,
  6629. (wlc->
  6630. _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
  6631. /* initialize maximum allowed duty cycle */
  6632. brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
  6633. brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
  6634. /*
  6635. * Update some shared memory locations related to
  6636. * max AMPDU size allowed to received
  6637. */
  6638. brcms_c_ampdu_shm_upd(wlc->ampdu);
  6639. /* band-specific inits */
  6640. brcms_c_bsinit(wlc);
  6641. /* Enable EDCF mode (while the MAC is suspended) */
  6642. bcma_set16(core, D11REGOFFS(ifs_ctl), IFS_USEEDCF);
  6643. brcms_c_edcf_setparams(wlc, false);
  6644. /* read the ucode version if we have not yet done so */
  6645. if (wlc->ucode_rev == 0) {
  6646. u16 rev;
  6647. u16 patch;
  6648. rev = brcms_b_read_shm(wlc->hw, M_BOM_REV_MAJOR);
  6649. patch = brcms_b_read_shm(wlc->hw, M_BOM_REV_MINOR);
  6650. wlc->ucode_rev = (rev << NBITS(u16)) | patch;
  6651. snprintf(wlc->wiphy->fw_version,
  6652. sizeof(wlc->wiphy->fw_version), "%u.%u", rev, patch);
  6653. }
  6654. /* ..now really unleash hell (allow the MAC out of suspend) */
  6655. brcms_c_enable_mac(wlc);
  6656. /* suspend the tx fifos and mute the phy for preism cac time */
  6657. if (mute_tx)
  6658. brcms_b_mute(wlc->hw, true);
  6659. /* enable the RF Disable Delay timer */
  6660. bcma_write32(core, D11REGOFFS(rfdisabledly), RFDISABLE_DEFAULT);
  6661. /*
  6662. * Initialize WME parameters; if they haven't been set by some other
  6663. * mechanism (IOVar, etc) then read them from the hardware.
  6664. */
  6665. if (GFIELD(wlc->wme_retries[0], EDCF_SHORT) == 0) {
  6666. /* Uninitialized; read from HW */
  6667. int ac;
  6668. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  6669. wlc->wme_retries[ac] =
  6670. brcms_b_read_shm(wlc->hw, M_AC_TXLMT_ADDR(ac));
  6671. }
  6672. }
  6673. /*
  6674. * The common driver entry routine. Error codes should be unique
  6675. */
  6676. struct brcms_c_info *
  6677. brcms_c_attach(struct brcms_info *wl, struct bcma_device *core, uint unit,
  6678. bool piomode, uint *perr)
  6679. {
  6680. struct brcms_c_info *wlc;
  6681. uint err = 0;
  6682. uint i, j;
  6683. struct brcms_pub *pub;
  6684. /* allocate struct brcms_c_info state and its substructures */
  6685. wlc = brcms_c_attach_malloc(unit, &err, 0);
  6686. if (wlc == NULL)
  6687. goto fail;
  6688. wlc->wiphy = wl->wiphy;
  6689. pub = wlc->pub;
  6690. #if defined(DEBUG)
  6691. wlc_info_dbg = wlc;
  6692. #endif
  6693. wlc->band = wlc->bandstate[0];
  6694. wlc->core = wlc->corestate;
  6695. wlc->wl = wl;
  6696. pub->unit = unit;
  6697. pub->_piomode = piomode;
  6698. wlc->bandinit_pending = false;
  6699. wlc->beacon_template_virgin = true;
  6700. /* populate struct brcms_c_info with default values */
  6701. brcms_c_info_init(wlc, unit);
  6702. /* update sta/ap related parameters */
  6703. brcms_c_ap_upd(wlc);
  6704. /*
  6705. * low level attach steps(all hw accesses go
  6706. * inside, no more in rest of the attach)
  6707. */
  6708. err = brcms_b_attach(wlc, core, unit, piomode);
  6709. if (err)
  6710. goto fail;
  6711. brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, OFF);
  6712. pub->phy_11ncapable = BRCMS_PHY_11N_CAP(wlc->band);
  6713. /* disable allowed duty cycle */
  6714. wlc->tx_duty_cycle_ofdm = 0;
  6715. wlc->tx_duty_cycle_cck = 0;
  6716. brcms_c_stf_phy_chain_calc(wlc);
  6717. /* txchain 1: txant 0, txchain 2: txant 1 */
  6718. if (BRCMS_ISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
  6719. wlc->stf->txant = wlc->stf->hw_txchain - 1;
  6720. /* push to BMAC driver */
  6721. wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
  6722. wlc->stf->hw_rxchain);
  6723. /* pull up some info resulting from the low attach */
  6724. for (i = 0; i < NFIFO; i++)
  6725. wlc->core->txavail[i] = wlc->hw->txavail[i];
  6726. memcpy(&wlc->perm_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
  6727. memcpy(&pub->cur_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
  6728. for (j = 0; j < wlc->pub->_nbands; j++) {
  6729. wlc->band = wlc->bandstate[j];
  6730. if (!brcms_c_attach_stf_ant_init(wlc)) {
  6731. err = 24;
  6732. goto fail;
  6733. }
  6734. /* default contention windows size limits */
  6735. wlc->band->CWmin = APHY_CWMIN;
  6736. wlc->band->CWmax = PHY_CWMAX;
  6737. /* init gmode value */
  6738. if (wlc->band->bandtype == BRCM_BAND_2G) {
  6739. wlc->band->gmode = GMODE_AUTO;
  6740. brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER,
  6741. wlc->band->gmode);
  6742. }
  6743. /* init _n_enab supported mode */
  6744. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  6745. pub->_n_enab = SUPPORT_11N;
  6746. brcms_c_protection_upd(wlc, BRCMS_PROT_N_USER,
  6747. ((pub->_n_enab ==
  6748. SUPPORT_11N) ? WL_11N_2x2 :
  6749. WL_11N_3x3));
  6750. }
  6751. /* init per-band default rateset, depend on band->gmode */
  6752. brcms_default_rateset(wlc, &wlc->band->defrateset);
  6753. /* fill in hw_rateset */
  6754. brcms_c_rateset_filter(&wlc->band->defrateset,
  6755. &wlc->band->hw_rateset, false,
  6756. BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
  6757. (bool) (wlc->pub->_n_enab & SUPPORT_11N));
  6758. }
  6759. /*
  6760. * update antenna config due to
  6761. * wlc->stf->txant/txchain/ant_rx_ovr change
  6762. */
  6763. brcms_c_stf_phy_txant_upd(wlc);
  6764. /* attach each modules */
  6765. err = brcms_c_attach_module(wlc);
  6766. if (err != 0)
  6767. goto fail;
  6768. if (!brcms_c_timers_init(wlc, unit)) {
  6769. wiphy_err(wl->wiphy, "wl%d: %s: init_timer failed\n", unit,
  6770. __func__);
  6771. err = 32;
  6772. goto fail;
  6773. }
  6774. /* depend on rateset, gmode */
  6775. wlc->cmi = brcms_c_channel_mgr_attach(wlc);
  6776. if (!wlc->cmi) {
  6777. wiphy_err(wl->wiphy, "wl%d: %s: channel_mgr_attach failed"
  6778. "\n", unit, __func__);
  6779. err = 33;
  6780. goto fail;
  6781. }
  6782. /* init default when all parameters are ready, i.e. ->rateset */
  6783. brcms_c_bss_default_init(wlc);
  6784. /*
  6785. * Complete the wlc default state initializations..
  6786. */
  6787. wlc->bsscfg->wlc = wlc;
  6788. wlc->mimoft = FT_HT;
  6789. wlc->mimo_40txbw = AUTO;
  6790. wlc->ofdm_40txbw = AUTO;
  6791. wlc->cck_40txbw = AUTO;
  6792. brcms_c_update_mimo_band_bwcap(wlc, BRCMS_N_BW_20IN2G_40IN5G);
  6793. /* Set default values of SGI */
  6794. if (BRCMS_SGI_CAP_PHY(wlc)) {
  6795. brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
  6796. BRCMS_N_SGI_40));
  6797. } else if (BRCMS_ISSSLPNPHY(wlc->band)) {
  6798. brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
  6799. BRCMS_N_SGI_40));
  6800. } else {
  6801. brcms_c_ht_update_sgi_rx(wlc, 0);
  6802. }
  6803. brcms_b_antsel_set(wlc->hw, wlc->asi->antsel_avail);
  6804. if (perr)
  6805. *perr = 0;
  6806. return wlc;
  6807. fail:
  6808. wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
  6809. unit, __func__, err);
  6810. if (wlc)
  6811. brcms_c_detach(wlc);
  6812. if (perr)
  6813. *perr = err;
  6814. return NULL;
  6815. }