main.c 212 KB

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