main.c 225 KB

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