main.c 223 KB

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