main.c 211 KB

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