main.c 223 KB

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