main.c 211 KB

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