main.c 223 KB

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