main.c 224 KB

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