main.c 223 KB

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