main.c 224 KB

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