mwl8k.c 153 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352
  1. /*
  2. * drivers/net/wireless/mwl8k.c
  3. * Driver for Marvell TOPDOG 802.11 Wireless cards
  4. *
  5. * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/list.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <linux/completion.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/slab.h>
  22. #include <net/mac80211.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/firmware.h>
  25. #include <linux/workqueue.h>
  26. #define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
  27. #define MWL8K_NAME KBUILD_MODNAME
  28. #define MWL8K_VERSION "0.13"
  29. /* Module parameters */
  30. static bool ap_mode_default;
  31. module_param(ap_mode_default, bool, 0);
  32. MODULE_PARM_DESC(ap_mode_default,
  33. "Set to 1 to make ap mode the default instead of sta mode");
  34. /* Register definitions */
  35. #define MWL8K_HIU_GEN_PTR 0x00000c10
  36. #define MWL8K_MODE_STA 0x0000005a
  37. #define MWL8K_MODE_AP 0x000000a5
  38. #define MWL8K_HIU_INT_CODE 0x00000c14
  39. #define MWL8K_FWSTA_READY 0xf0f1f2f4
  40. #define MWL8K_FWAP_READY 0xf1f2f4a5
  41. #define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
  42. #define MWL8K_HIU_SCRATCH 0x00000c40
  43. /* Host->device communications */
  44. #define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
  45. #define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
  46. #define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
  47. #define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
  48. #define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
  49. #define MWL8K_H2A_INT_DUMMY (1 << 20)
  50. #define MWL8K_H2A_INT_RESET (1 << 15)
  51. #define MWL8K_H2A_INT_DOORBELL (1 << 1)
  52. #define MWL8K_H2A_INT_PPA_READY (1 << 0)
  53. /* Device->host communications */
  54. #define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
  55. #define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
  56. #define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
  57. #define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
  58. #define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
  59. #define MWL8K_A2H_INT_DUMMY (1 << 20)
  60. #define MWL8K_A2H_INT_BA_WATCHDOG (1 << 14)
  61. #define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
  62. #define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
  63. #define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
  64. #define MWL8K_A2H_INT_RADIO_ON (1 << 6)
  65. #define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
  66. #define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
  67. #define MWL8K_A2H_INT_OPC_DONE (1 << 2)
  68. #define MWL8K_A2H_INT_RX_READY (1 << 1)
  69. #define MWL8K_A2H_INT_TX_DONE (1 << 0)
  70. /* HW micro second timer register
  71. * located at offset 0xA600. This
  72. * will be used to timestamp tx
  73. * packets.
  74. */
  75. #define MWL8K_HW_TIMER_REGISTER 0x0000a600
  76. #define BBU_RXRDY_CNT_REG 0x0000a860
  77. #define NOK_CCA_CNT_REG 0x0000a6a0
  78. #define BBU_AVG_NOISE_VAL 0x67
  79. #define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
  80. MWL8K_A2H_INT_CHNL_SWITCHED | \
  81. MWL8K_A2H_INT_QUEUE_EMPTY | \
  82. MWL8K_A2H_INT_RADAR_DETECT | \
  83. MWL8K_A2H_INT_RADIO_ON | \
  84. MWL8K_A2H_INT_RADIO_OFF | \
  85. MWL8K_A2H_INT_MAC_EVENT | \
  86. MWL8K_A2H_INT_OPC_DONE | \
  87. MWL8K_A2H_INT_RX_READY | \
  88. MWL8K_A2H_INT_TX_DONE | \
  89. MWL8K_A2H_INT_BA_WATCHDOG)
  90. #define MWL8K_RX_QUEUES 1
  91. #define MWL8K_TX_WMM_QUEUES 4
  92. #define MWL8K_MAX_AMPDU_QUEUES 8
  93. #define MWL8K_MAX_TX_QUEUES (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
  94. #define mwl8k_tx_queues(priv) (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
  95. /* txpriorities are mapped with hw queues.
  96. * Each hw queue has a txpriority.
  97. */
  98. #define TOTAL_HW_TX_QUEUES 8
  99. /* Each HW queue can have one AMPDU stream.
  100. * But, because one of the hw queue is reserved,
  101. * maximum AMPDU queues that can be created are
  102. * one short of total tx queues.
  103. */
  104. #define MWL8K_NUM_AMPDU_STREAMS (TOTAL_HW_TX_QUEUES - 1)
  105. #define MWL8K_NUM_CHANS 18
  106. struct rxd_ops {
  107. int rxd_size;
  108. void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
  109. void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
  110. int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
  111. __le16 *qos, s8 *noise);
  112. };
  113. struct mwl8k_device_info {
  114. char *part_name;
  115. char *helper_image;
  116. char *fw_image_sta;
  117. char *fw_image_ap;
  118. struct rxd_ops *ap_rxd_ops;
  119. u32 fw_api_ap;
  120. };
  121. struct mwl8k_rx_queue {
  122. int rxd_count;
  123. /* hw receives here */
  124. int head;
  125. /* refill descs here */
  126. int tail;
  127. void *rxd;
  128. dma_addr_t rxd_dma;
  129. struct {
  130. struct sk_buff *skb;
  131. DEFINE_DMA_UNMAP_ADDR(dma);
  132. } *buf;
  133. };
  134. struct mwl8k_tx_queue {
  135. /* hw transmits here */
  136. int head;
  137. /* sw appends here */
  138. int tail;
  139. unsigned int len;
  140. struct mwl8k_tx_desc *txd;
  141. dma_addr_t txd_dma;
  142. struct sk_buff **skb;
  143. };
  144. enum {
  145. AMPDU_NO_STREAM,
  146. AMPDU_STREAM_NEW,
  147. AMPDU_STREAM_IN_PROGRESS,
  148. AMPDU_STREAM_ACTIVE,
  149. };
  150. struct mwl8k_ampdu_stream {
  151. struct ieee80211_sta *sta;
  152. u8 tid;
  153. u8 state;
  154. u8 idx;
  155. };
  156. struct mwl8k_priv {
  157. struct ieee80211_hw *hw;
  158. struct pci_dev *pdev;
  159. int irq;
  160. struct mwl8k_device_info *device_info;
  161. void __iomem *sram;
  162. void __iomem *regs;
  163. /* firmware */
  164. const struct firmware *fw_helper;
  165. const struct firmware *fw_ucode;
  166. /* hardware/firmware parameters */
  167. bool ap_fw;
  168. struct rxd_ops *rxd_ops;
  169. struct ieee80211_supported_band band_24;
  170. struct ieee80211_channel channels_24[14];
  171. struct ieee80211_rate rates_24[13];
  172. struct ieee80211_supported_band band_50;
  173. struct ieee80211_channel channels_50[9];
  174. struct ieee80211_rate rates_50[8];
  175. u32 ap_macids_supported;
  176. u32 sta_macids_supported;
  177. /* Ampdu stream information */
  178. u8 num_ampdu_queues;
  179. spinlock_t stream_lock;
  180. struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
  181. struct work_struct watchdog_ba_handle;
  182. /* firmware access */
  183. struct mutex fw_mutex;
  184. struct task_struct *fw_mutex_owner;
  185. struct task_struct *hw_restart_owner;
  186. int fw_mutex_depth;
  187. struct completion *hostcmd_wait;
  188. atomic_t watchdog_event_pending;
  189. /* lock held over TX and TX reap */
  190. spinlock_t tx_lock;
  191. /* TX quiesce completion, protected by fw_mutex and tx_lock */
  192. struct completion *tx_wait;
  193. /* List of interfaces. */
  194. u32 macids_used;
  195. struct list_head vif_list;
  196. /* power management status cookie from firmware */
  197. u32 *cookie;
  198. dma_addr_t cookie_dma;
  199. u16 num_mcaddrs;
  200. u8 hw_rev;
  201. u32 fw_rev;
  202. u32 caps;
  203. /*
  204. * Running count of TX packets in flight, to avoid
  205. * iterating over the transmit rings each time.
  206. */
  207. int pending_tx_pkts;
  208. struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
  209. struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
  210. u32 txq_offset[MWL8K_MAX_TX_QUEUES];
  211. bool radio_on;
  212. bool radio_short_preamble;
  213. bool sniffer_enabled;
  214. bool wmm_enabled;
  215. /* XXX need to convert this to handle multiple interfaces */
  216. bool capture_beacon;
  217. u8 capture_bssid[ETH_ALEN];
  218. struct sk_buff *beacon_skb;
  219. /*
  220. * This FJ worker has to be global as it is scheduled from the
  221. * RX handler. At this point we don't know which interface it
  222. * belongs to until the list of bssids waiting to complete join
  223. * is checked.
  224. */
  225. struct work_struct finalize_join_worker;
  226. /* Tasklet to perform TX reclaim. */
  227. struct tasklet_struct poll_tx_task;
  228. /* Tasklet to perform RX. */
  229. struct tasklet_struct poll_rx_task;
  230. /* Most recently reported noise in dBm */
  231. s8 noise;
  232. /*
  233. * preserve the queue configurations so they can be restored if/when
  234. * the firmware image is swapped.
  235. */
  236. struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
  237. /* To perform the task of reloading the firmware */
  238. struct work_struct fw_reload;
  239. bool hw_restart_in_progress;
  240. /* async firmware loading state */
  241. unsigned fw_state;
  242. char *fw_pref;
  243. char *fw_alt;
  244. bool is_8764;
  245. struct completion firmware_loading_complete;
  246. /* bitmap of running BSSes */
  247. u32 running_bsses;
  248. /* ACS related */
  249. bool sw_scan_start;
  250. struct ieee80211_channel *acs_chan;
  251. unsigned long channel_time;
  252. struct survey_info survey[MWL8K_NUM_CHANS];
  253. };
  254. #define MAX_WEP_KEY_LEN 13
  255. #define NUM_WEP_KEYS 4
  256. /* Per interface specific private data */
  257. struct mwl8k_vif {
  258. struct list_head list;
  259. struct ieee80211_vif *vif;
  260. /* Firmware macid for this vif. */
  261. int macid;
  262. /* Non AMPDU sequence number assigned by driver. */
  263. u16 seqno;
  264. /* Saved WEP keys */
  265. struct {
  266. u8 enabled;
  267. u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
  268. } wep_key_conf[NUM_WEP_KEYS];
  269. /* BSSID */
  270. u8 bssid[ETH_ALEN];
  271. /* A flag to indicate is HW crypto is enabled for this bssid */
  272. bool is_hw_crypto_enabled;
  273. };
  274. #define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
  275. #define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
  276. struct tx_traffic_info {
  277. u32 start_time;
  278. u32 pkts;
  279. };
  280. #define MWL8K_MAX_TID 8
  281. struct mwl8k_sta {
  282. /* Index into station database. Returned by UPDATE_STADB. */
  283. u8 peer_id;
  284. u8 is_ampdu_allowed;
  285. struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
  286. };
  287. #define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
  288. static const struct ieee80211_channel mwl8k_channels_24[] = {
  289. { .band = NL80211_BAND_2GHZ, .center_freq = 2412, .hw_value = 1, },
  290. { .band = NL80211_BAND_2GHZ, .center_freq = 2417, .hw_value = 2, },
  291. { .band = NL80211_BAND_2GHZ, .center_freq = 2422, .hw_value = 3, },
  292. { .band = NL80211_BAND_2GHZ, .center_freq = 2427, .hw_value = 4, },
  293. { .band = NL80211_BAND_2GHZ, .center_freq = 2432, .hw_value = 5, },
  294. { .band = NL80211_BAND_2GHZ, .center_freq = 2437, .hw_value = 6, },
  295. { .band = NL80211_BAND_2GHZ, .center_freq = 2442, .hw_value = 7, },
  296. { .band = NL80211_BAND_2GHZ, .center_freq = 2447, .hw_value = 8, },
  297. { .band = NL80211_BAND_2GHZ, .center_freq = 2452, .hw_value = 9, },
  298. { .band = NL80211_BAND_2GHZ, .center_freq = 2457, .hw_value = 10, },
  299. { .band = NL80211_BAND_2GHZ, .center_freq = 2462, .hw_value = 11, },
  300. { .band = NL80211_BAND_2GHZ, .center_freq = 2467, .hw_value = 12, },
  301. { .band = NL80211_BAND_2GHZ, .center_freq = 2472, .hw_value = 13, },
  302. { .band = NL80211_BAND_2GHZ, .center_freq = 2484, .hw_value = 14, },
  303. };
  304. static const struct ieee80211_rate mwl8k_rates_24[] = {
  305. { .bitrate = 10, .hw_value = 2, },
  306. { .bitrate = 20, .hw_value = 4, },
  307. { .bitrate = 55, .hw_value = 11, },
  308. { .bitrate = 110, .hw_value = 22, },
  309. { .bitrate = 220, .hw_value = 44, },
  310. { .bitrate = 60, .hw_value = 12, },
  311. { .bitrate = 90, .hw_value = 18, },
  312. { .bitrate = 120, .hw_value = 24, },
  313. { .bitrate = 180, .hw_value = 36, },
  314. { .bitrate = 240, .hw_value = 48, },
  315. { .bitrate = 360, .hw_value = 72, },
  316. { .bitrate = 480, .hw_value = 96, },
  317. { .bitrate = 540, .hw_value = 108, },
  318. };
  319. static const struct ieee80211_channel mwl8k_channels_50[] = {
  320. { .band = NL80211_BAND_5GHZ, .center_freq = 5180, .hw_value = 36, },
  321. { .band = NL80211_BAND_5GHZ, .center_freq = 5200, .hw_value = 40, },
  322. { .band = NL80211_BAND_5GHZ, .center_freq = 5220, .hw_value = 44, },
  323. { .band = NL80211_BAND_5GHZ, .center_freq = 5240, .hw_value = 48, },
  324. { .band = NL80211_BAND_5GHZ, .center_freq = 5745, .hw_value = 149, },
  325. { .band = NL80211_BAND_5GHZ, .center_freq = 5765, .hw_value = 153, },
  326. { .band = NL80211_BAND_5GHZ, .center_freq = 5785, .hw_value = 157, },
  327. { .band = NL80211_BAND_5GHZ, .center_freq = 5805, .hw_value = 161, },
  328. { .band = NL80211_BAND_5GHZ, .center_freq = 5825, .hw_value = 165, },
  329. };
  330. static const struct ieee80211_rate mwl8k_rates_50[] = {
  331. { .bitrate = 60, .hw_value = 12, },
  332. { .bitrate = 90, .hw_value = 18, },
  333. { .bitrate = 120, .hw_value = 24, },
  334. { .bitrate = 180, .hw_value = 36, },
  335. { .bitrate = 240, .hw_value = 48, },
  336. { .bitrate = 360, .hw_value = 72, },
  337. { .bitrate = 480, .hw_value = 96, },
  338. { .bitrate = 540, .hw_value = 108, },
  339. };
  340. /* Set or get info from Firmware */
  341. #define MWL8K_CMD_GET 0x0000
  342. #define MWL8K_CMD_SET 0x0001
  343. #define MWL8K_CMD_SET_LIST 0x0002
  344. /* Firmware command codes */
  345. #define MWL8K_CMD_CODE_DNLD 0x0001
  346. #define MWL8K_CMD_GET_HW_SPEC 0x0003
  347. #define MWL8K_CMD_SET_HW_SPEC 0x0004
  348. #define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
  349. #define MWL8K_CMD_GET_STAT 0x0014
  350. #define MWL8K_CMD_BBP_REG_ACCESS 0x001a
  351. #define MWL8K_CMD_RADIO_CONTROL 0x001c
  352. #define MWL8K_CMD_RF_TX_POWER 0x001e
  353. #define MWL8K_CMD_TX_POWER 0x001f
  354. #define MWL8K_CMD_RF_ANTENNA 0x0020
  355. #define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
  356. #define MWL8K_CMD_SET_PRE_SCAN 0x0107
  357. #define MWL8K_CMD_SET_POST_SCAN 0x0108
  358. #define MWL8K_CMD_SET_RF_CHANNEL 0x010a
  359. #define MWL8K_CMD_SET_AID 0x010d
  360. #define MWL8K_CMD_SET_RATE 0x0110
  361. #define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
  362. #define MWL8K_CMD_RTS_THRESHOLD 0x0113
  363. #define MWL8K_CMD_SET_SLOT 0x0114
  364. #define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
  365. #define MWL8K_CMD_SET_WMM_MODE 0x0123
  366. #define MWL8K_CMD_MIMO_CONFIG 0x0125
  367. #define MWL8K_CMD_USE_FIXED_RATE 0x0126
  368. #define MWL8K_CMD_ENABLE_SNIFFER 0x0150
  369. #define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
  370. #define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
  371. #define MWL8K_CMD_GET_WATCHDOG_BITMAP 0x0205
  372. #define MWL8K_CMD_DEL_MAC_ADDR 0x0206 /* per-vif */
  373. #define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
  374. #define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
  375. #define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
  376. #define MWL8K_CMD_UPDATE_STADB 0x1123
  377. #define MWL8K_CMD_BASTREAM 0x1125
  378. #define MWL8K_LEGACY_5G_RATE_OFFSET \
  379. (ARRAY_SIZE(mwl8k_rates_24) - ARRAY_SIZE(mwl8k_rates_50))
  380. static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
  381. {
  382. u16 command = le16_to_cpu(cmd);
  383. #define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
  384. snprintf(buf, bufsize, "%s", #x);\
  385. return buf;\
  386. } while (0)
  387. switch (command & ~0x8000) {
  388. MWL8K_CMDNAME(CODE_DNLD);
  389. MWL8K_CMDNAME(GET_HW_SPEC);
  390. MWL8K_CMDNAME(SET_HW_SPEC);
  391. MWL8K_CMDNAME(MAC_MULTICAST_ADR);
  392. MWL8K_CMDNAME(GET_STAT);
  393. MWL8K_CMDNAME(RADIO_CONTROL);
  394. MWL8K_CMDNAME(RF_TX_POWER);
  395. MWL8K_CMDNAME(TX_POWER);
  396. MWL8K_CMDNAME(RF_ANTENNA);
  397. MWL8K_CMDNAME(SET_BEACON);
  398. MWL8K_CMDNAME(SET_PRE_SCAN);
  399. MWL8K_CMDNAME(SET_POST_SCAN);
  400. MWL8K_CMDNAME(SET_RF_CHANNEL);
  401. MWL8K_CMDNAME(SET_AID);
  402. MWL8K_CMDNAME(SET_RATE);
  403. MWL8K_CMDNAME(SET_FINALIZE_JOIN);
  404. MWL8K_CMDNAME(RTS_THRESHOLD);
  405. MWL8K_CMDNAME(SET_SLOT);
  406. MWL8K_CMDNAME(SET_EDCA_PARAMS);
  407. MWL8K_CMDNAME(SET_WMM_MODE);
  408. MWL8K_CMDNAME(MIMO_CONFIG);
  409. MWL8K_CMDNAME(USE_FIXED_RATE);
  410. MWL8K_CMDNAME(ENABLE_SNIFFER);
  411. MWL8K_CMDNAME(SET_MAC_ADDR);
  412. MWL8K_CMDNAME(SET_RATEADAPT_MODE);
  413. MWL8K_CMDNAME(BSS_START);
  414. MWL8K_CMDNAME(SET_NEW_STN);
  415. MWL8K_CMDNAME(UPDATE_ENCRYPTION);
  416. MWL8K_CMDNAME(UPDATE_STADB);
  417. MWL8K_CMDNAME(BASTREAM);
  418. MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
  419. default:
  420. snprintf(buf, bufsize, "0x%x", cmd);
  421. }
  422. #undef MWL8K_CMDNAME
  423. return buf;
  424. }
  425. /* Hardware and firmware reset */
  426. static void mwl8k_hw_reset(struct mwl8k_priv *priv)
  427. {
  428. iowrite32(MWL8K_H2A_INT_RESET,
  429. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  430. iowrite32(MWL8K_H2A_INT_RESET,
  431. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  432. msleep(20);
  433. }
  434. /* Release fw image */
  435. static void mwl8k_release_fw(const struct firmware **fw)
  436. {
  437. if (*fw == NULL)
  438. return;
  439. release_firmware(*fw);
  440. *fw = NULL;
  441. }
  442. static void mwl8k_release_firmware(struct mwl8k_priv *priv)
  443. {
  444. mwl8k_release_fw(&priv->fw_ucode);
  445. mwl8k_release_fw(&priv->fw_helper);
  446. }
  447. /* states for asynchronous f/w loading */
  448. static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
  449. enum {
  450. FW_STATE_INIT = 0,
  451. FW_STATE_LOADING_PREF,
  452. FW_STATE_LOADING_ALT,
  453. FW_STATE_ERROR,
  454. };
  455. /* Request fw image */
  456. static int mwl8k_request_fw(struct mwl8k_priv *priv,
  457. const char *fname, const struct firmware **fw,
  458. bool nowait)
  459. {
  460. /* release current image */
  461. if (*fw != NULL)
  462. mwl8k_release_fw(fw);
  463. if (nowait)
  464. return request_firmware_nowait(THIS_MODULE, 1, fname,
  465. &priv->pdev->dev, GFP_KERNEL,
  466. priv, mwl8k_fw_state_machine);
  467. else
  468. return request_firmware(fw, fname, &priv->pdev->dev);
  469. }
  470. static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
  471. bool nowait)
  472. {
  473. struct mwl8k_device_info *di = priv->device_info;
  474. int rc;
  475. if (di->helper_image != NULL) {
  476. if (nowait)
  477. rc = mwl8k_request_fw(priv, di->helper_image,
  478. &priv->fw_helper, true);
  479. else
  480. rc = mwl8k_request_fw(priv, di->helper_image,
  481. &priv->fw_helper, false);
  482. if (rc)
  483. printk(KERN_ERR "%s: Error requesting helper fw %s\n",
  484. pci_name(priv->pdev), di->helper_image);
  485. if (rc || nowait)
  486. return rc;
  487. }
  488. if (nowait) {
  489. /*
  490. * if we get here, no helper image is needed. Skip the
  491. * FW_STATE_INIT state.
  492. */
  493. priv->fw_state = FW_STATE_LOADING_PREF;
  494. rc = mwl8k_request_fw(priv, fw_image,
  495. &priv->fw_ucode,
  496. true);
  497. } else
  498. rc = mwl8k_request_fw(priv, fw_image,
  499. &priv->fw_ucode, false);
  500. if (rc) {
  501. printk(KERN_ERR "%s: Error requesting firmware file %s\n",
  502. pci_name(priv->pdev), fw_image);
  503. mwl8k_release_fw(&priv->fw_helper);
  504. return rc;
  505. }
  506. return 0;
  507. }
  508. struct mwl8k_cmd_pkt {
  509. __le16 code;
  510. __le16 length;
  511. __u8 seq_num;
  512. __u8 macid;
  513. __le16 result;
  514. char payload[0];
  515. } __packed;
  516. /*
  517. * Firmware loading.
  518. */
  519. static int
  520. mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
  521. {
  522. void __iomem *regs = priv->regs;
  523. dma_addr_t dma_addr;
  524. int loops;
  525. dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
  526. if (pci_dma_mapping_error(priv->pdev, dma_addr))
  527. return -ENOMEM;
  528. iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
  529. iowrite32(0, regs + MWL8K_HIU_INT_CODE);
  530. iowrite32(MWL8K_H2A_INT_DOORBELL,
  531. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  532. iowrite32(MWL8K_H2A_INT_DUMMY,
  533. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  534. loops = 1000;
  535. do {
  536. u32 int_code;
  537. if (priv->is_8764) {
  538. int_code = ioread32(regs +
  539. MWL8K_HIU_H2A_INTERRUPT_STATUS);
  540. if (int_code == 0)
  541. break;
  542. } else {
  543. int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
  544. if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
  545. iowrite32(0, regs + MWL8K_HIU_INT_CODE);
  546. break;
  547. }
  548. }
  549. cond_resched();
  550. udelay(1);
  551. } while (--loops);
  552. pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
  553. return loops ? 0 : -ETIMEDOUT;
  554. }
  555. static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
  556. const u8 *data, size_t length)
  557. {
  558. struct mwl8k_cmd_pkt *cmd;
  559. int done;
  560. int rc = 0;
  561. cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
  562. if (cmd == NULL)
  563. return -ENOMEM;
  564. cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
  565. cmd->seq_num = 0;
  566. cmd->macid = 0;
  567. cmd->result = 0;
  568. done = 0;
  569. while (length) {
  570. int block_size = length > 256 ? 256 : length;
  571. memcpy(cmd->payload, data + done, block_size);
  572. cmd->length = cpu_to_le16(block_size);
  573. rc = mwl8k_send_fw_load_cmd(priv, cmd,
  574. sizeof(*cmd) + block_size);
  575. if (rc)
  576. break;
  577. done += block_size;
  578. length -= block_size;
  579. }
  580. if (!rc) {
  581. cmd->length = 0;
  582. rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
  583. }
  584. kfree(cmd);
  585. return rc;
  586. }
  587. static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
  588. const u8 *data, size_t length)
  589. {
  590. unsigned char *buffer;
  591. int may_continue, rc = 0;
  592. u32 done, prev_block_size;
  593. buffer = kmalloc(1024, GFP_KERNEL);
  594. if (buffer == NULL)
  595. return -ENOMEM;
  596. done = 0;
  597. prev_block_size = 0;
  598. may_continue = 1000;
  599. while (may_continue > 0) {
  600. u32 block_size;
  601. block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
  602. if (block_size & 1) {
  603. block_size &= ~1;
  604. may_continue--;
  605. } else {
  606. done += prev_block_size;
  607. length -= prev_block_size;
  608. }
  609. if (block_size > 1024 || block_size > length) {
  610. rc = -EOVERFLOW;
  611. break;
  612. }
  613. if (length == 0) {
  614. rc = 0;
  615. break;
  616. }
  617. if (block_size == 0) {
  618. rc = -EPROTO;
  619. may_continue--;
  620. udelay(1);
  621. continue;
  622. }
  623. prev_block_size = block_size;
  624. memcpy(buffer, data + done, block_size);
  625. rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
  626. if (rc)
  627. break;
  628. }
  629. if (!rc && length != 0)
  630. rc = -EREMOTEIO;
  631. kfree(buffer);
  632. return rc;
  633. }
  634. static int mwl8k_load_firmware(struct ieee80211_hw *hw)
  635. {
  636. struct mwl8k_priv *priv = hw->priv;
  637. const struct firmware *fw = priv->fw_ucode;
  638. int rc;
  639. int loops;
  640. if (!memcmp(fw->data, "\x01\x00\x00\x00", 4) && !priv->is_8764) {
  641. const struct firmware *helper = priv->fw_helper;
  642. if (helper == NULL) {
  643. printk(KERN_ERR "%s: helper image needed but none "
  644. "given\n", pci_name(priv->pdev));
  645. return -EINVAL;
  646. }
  647. rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
  648. if (rc) {
  649. printk(KERN_ERR "%s: unable to load firmware "
  650. "helper image\n", pci_name(priv->pdev));
  651. return rc;
  652. }
  653. msleep(20);
  654. rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
  655. } else {
  656. if (priv->is_8764)
  657. rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
  658. else
  659. rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
  660. }
  661. if (rc) {
  662. printk(KERN_ERR "%s: unable to load firmware image\n",
  663. pci_name(priv->pdev));
  664. return rc;
  665. }
  666. iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
  667. loops = 500000;
  668. do {
  669. u32 ready_code;
  670. ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
  671. if (ready_code == MWL8K_FWAP_READY) {
  672. priv->ap_fw = true;
  673. break;
  674. } else if (ready_code == MWL8K_FWSTA_READY) {
  675. priv->ap_fw = false;
  676. break;
  677. }
  678. cond_resched();
  679. udelay(1);
  680. } while (--loops);
  681. return loops ? 0 : -ETIMEDOUT;
  682. }
  683. /* DMA header used by firmware and hardware. */
  684. struct mwl8k_dma_data {
  685. __le16 fwlen;
  686. struct ieee80211_hdr wh;
  687. char data[0];
  688. } __packed;
  689. /* Routines to add/remove DMA header from skb. */
  690. static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
  691. {
  692. struct mwl8k_dma_data *tr;
  693. int hdrlen;
  694. tr = (struct mwl8k_dma_data *)skb->data;
  695. hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
  696. if (hdrlen != sizeof(tr->wh)) {
  697. if (ieee80211_is_data_qos(tr->wh.frame_control)) {
  698. memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
  699. *((__le16 *)(tr->data - 2)) = qos;
  700. } else {
  701. memmove(tr->data - hdrlen, &tr->wh, hdrlen);
  702. }
  703. }
  704. if (hdrlen != sizeof(*tr))
  705. skb_pull(skb, sizeof(*tr) - hdrlen);
  706. }
  707. #define REDUCED_TX_HEADROOM 8
  708. static void
  709. mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
  710. int head_pad, int tail_pad)
  711. {
  712. struct ieee80211_hdr *wh;
  713. int hdrlen;
  714. int reqd_hdrlen;
  715. struct mwl8k_dma_data *tr;
  716. /*
  717. * Add a firmware DMA header; the firmware requires that we
  718. * present a 2-byte payload length followed by a 4-address
  719. * header (without QoS field), followed (optionally) by any
  720. * WEP/ExtIV header (but only filled in for CCMP).
  721. */
  722. wh = (struct ieee80211_hdr *)skb->data;
  723. hdrlen = ieee80211_hdrlen(wh->frame_control);
  724. /*
  725. * Check if skb_resize is required because of
  726. * tx_headroom adjustment.
  727. */
  728. if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
  729. + REDUCED_TX_HEADROOM))) {
  730. if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
  731. wiphy_err(priv->hw->wiphy,
  732. "Failed to reallocate TX buffer\n");
  733. return;
  734. }
  735. skb->truesize += REDUCED_TX_HEADROOM;
  736. }
  737. reqd_hdrlen = sizeof(*tr) + head_pad;
  738. if (hdrlen != reqd_hdrlen)
  739. skb_push(skb, reqd_hdrlen - hdrlen);
  740. if (ieee80211_is_data_qos(wh->frame_control))
  741. hdrlen -= IEEE80211_QOS_CTL_LEN;
  742. tr = (struct mwl8k_dma_data *)skb->data;
  743. if (wh != &tr->wh)
  744. memmove(&tr->wh, wh, hdrlen);
  745. if (hdrlen != sizeof(tr->wh))
  746. memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
  747. /*
  748. * Firmware length is the length of the fully formed "802.11
  749. * payload". That is, everything except for the 802.11 header.
  750. * This includes all crypto material including the MIC.
  751. */
  752. tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
  753. }
  754. static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
  755. struct sk_buff *skb)
  756. {
  757. struct ieee80211_hdr *wh;
  758. struct ieee80211_tx_info *tx_info;
  759. struct ieee80211_key_conf *key_conf;
  760. int data_pad;
  761. int head_pad = 0;
  762. wh = (struct ieee80211_hdr *)skb->data;
  763. tx_info = IEEE80211_SKB_CB(skb);
  764. key_conf = NULL;
  765. if (ieee80211_is_data(wh->frame_control))
  766. key_conf = tx_info->control.hw_key;
  767. /*
  768. * Make sure the packet header is in the DMA header format (4-address
  769. * without QoS), and add head & tail padding when HW crypto is enabled.
  770. *
  771. * We have the following trailer padding requirements:
  772. * - WEP: 4 trailer bytes (ICV)
  773. * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
  774. * - CCMP: 8 trailer bytes (MIC)
  775. */
  776. data_pad = 0;
  777. if (key_conf != NULL) {
  778. head_pad = key_conf->iv_len;
  779. switch (key_conf->cipher) {
  780. case WLAN_CIPHER_SUITE_WEP40:
  781. case WLAN_CIPHER_SUITE_WEP104:
  782. data_pad = 4;
  783. break;
  784. case WLAN_CIPHER_SUITE_TKIP:
  785. data_pad = 12;
  786. break;
  787. case WLAN_CIPHER_SUITE_CCMP:
  788. data_pad = 8;
  789. break;
  790. }
  791. }
  792. mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
  793. }
  794. /*
  795. * Packet reception for 88w8366/88w8764 AP firmware.
  796. */
  797. struct mwl8k_rxd_ap {
  798. __le16 pkt_len;
  799. __u8 sq2;
  800. __u8 rate;
  801. __le32 pkt_phys_addr;
  802. __le32 next_rxd_phys_addr;
  803. __le16 qos_control;
  804. __le16 htsig2;
  805. __le32 hw_rssi_info;
  806. __le32 hw_noise_floor_info;
  807. __u8 noise_floor;
  808. __u8 pad0[3];
  809. __u8 rssi;
  810. __u8 rx_status;
  811. __u8 channel;
  812. __u8 rx_ctrl;
  813. } __packed;
  814. #define MWL8K_AP_RATE_INFO_MCS_FORMAT 0x80
  815. #define MWL8K_AP_RATE_INFO_40MHZ 0x40
  816. #define MWL8K_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
  817. #define MWL8K_AP_RX_CTRL_OWNED_BY_HOST 0x80
  818. /* 8366/8764 AP rx_status bits */
  819. #define MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
  820. #define MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
  821. #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
  822. #define MWL8K_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
  823. #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
  824. static void mwl8k_rxd_ap_init(void *_rxd, dma_addr_t next_dma_addr)
  825. {
  826. struct mwl8k_rxd_ap *rxd = _rxd;
  827. rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
  828. rxd->rx_ctrl = MWL8K_AP_RX_CTRL_OWNED_BY_HOST;
  829. }
  830. static void mwl8k_rxd_ap_refill(void *_rxd, dma_addr_t addr, int len)
  831. {
  832. struct mwl8k_rxd_ap *rxd = _rxd;
  833. rxd->pkt_len = cpu_to_le16(len);
  834. rxd->pkt_phys_addr = cpu_to_le32(addr);
  835. wmb();
  836. rxd->rx_ctrl = 0;
  837. }
  838. static int
  839. mwl8k_rxd_ap_process(void *_rxd, struct ieee80211_rx_status *status,
  840. __le16 *qos, s8 *noise)
  841. {
  842. struct mwl8k_rxd_ap *rxd = _rxd;
  843. if (!(rxd->rx_ctrl & MWL8K_AP_RX_CTRL_OWNED_BY_HOST))
  844. return -1;
  845. rmb();
  846. memset(status, 0, sizeof(*status));
  847. status->signal = -rxd->rssi;
  848. *noise = -rxd->noise_floor;
  849. if (rxd->rate & MWL8K_AP_RATE_INFO_MCS_FORMAT) {
  850. status->encoding = RX_ENC_HT;
  851. if (rxd->rate & MWL8K_AP_RATE_INFO_40MHZ)
  852. status->bw = RATE_INFO_BW_40;
  853. status->rate_idx = MWL8K_AP_RATE_INFO_RATEID(rxd->rate);
  854. } else {
  855. int i;
  856. for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
  857. if (mwl8k_rates_24[i].hw_value == rxd->rate) {
  858. status->rate_idx = i;
  859. break;
  860. }
  861. }
  862. }
  863. if (rxd->channel > 14) {
  864. status->band = NL80211_BAND_5GHZ;
  865. if (!(status->encoding == RX_ENC_HT) &&
  866. status->rate_idx >= MWL8K_LEGACY_5G_RATE_OFFSET)
  867. status->rate_idx -= MWL8K_LEGACY_5G_RATE_OFFSET;
  868. } else {
  869. status->band = NL80211_BAND_2GHZ;
  870. }
  871. status->freq = ieee80211_channel_to_frequency(rxd->channel,
  872. status->band);
  873. *qos = rxd->qos_control;
  874. if ((rxd->rx_status != MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
  875. (rxd->rx_status & MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK) &&
  876. (rxd->rx_status & MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
  877. status->flag |= RX_FLAG_MMIC_ERROR;
  878. return le16_to_cpu(rxd->pkt_len);
  879. }
  880. static struct rxd_ops rxd_ap_ops = {
  881. .rxd_size = sizeof(struct mwl8k_rxd_ap),
  882. .rxd_init = mwl8k_rxd_ap_init,
  883. .rxd_refill = mwl8k_rxd_ap_refill,
  884. .rxd_process = mwl8k_rxd_ap_process,
  885. };
  886. /*
  887. * Packet reception for STA firmware.
  888. */
  889. struct mwl8k_rxd_sta {
  890. __le16 pkt_len;
  891. __u8 link_quality;
  892. __u8 noise_level;
  893. __le32 pkt_phys_addr;
  894. __le32 next_rxd_phys_addr;
  895. __le16 qos_control;
  896. __le16 rate_info;
  897. __le32 pad0[4];
  898. __u8 rssi;
  899. __u8 channel;
  900. __le16 pad1;
  901. __u8 rx_ctrl;
  902. __u8 rx_status;
  903. __u8 pad2[2];
  904. } __packed;
  905. #define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
  906. #define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
  907. #define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
  908. #define MWL8K_STA_RATE_INFO_40MHZ 0x0004
  909. #define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
  910. #define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
  911. #define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
  912. #define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
  913. /* ICV=0 or MIC=1 */
  914. #define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
  915. /* Key is uploaded only in failure case */
  916. #define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
  917. static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
  918. {
  919. struct mwl8k_rxd_sta *rxd = _rxd;
  920. rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
  921. rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
  922. }
  923. static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
  924. {
  925. struct mwl8k_rxd_sta *rxd = _rxd;
  926. rxd->pkt_len = cpu_to_le16(len);
  927. rxd->pkt_phys_addr = cpu_to_le32(addr);
  928. wmb();
  929. rxd->rx_ctrl = 0;
  930. }
  931. static int
  932. mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
  933. __le16 *qos, s8 *noise)
  934. {
  935. struct mwl8k_rxd_sta *rxd = _rxd;
  936. u16 rate_info;
  937. if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
  938. return -1;
  939. rmb();
  940. rate_info = le16_to_cpu(rxd->rate_info);
  941. memset(status, 0, sizeof(*status));
  942. status->signal = -rxd->rssi;
  943. *noise = -rxd->noise_level;
  944. status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
  945. status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
  946. if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
  947. status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
  948. if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
  949. status->bw = RATE_INFO_BW_40;
  950. if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
  951. status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
  952. if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
  953. status->encoding = RX_ENC_HT;
  954. if (rxd->channel > 14) {
  955. status->band = NL80211_BAND_5GHZ;
  956. if (!(status->encoding == RX_ENC_HT) &&
  957. status->rate_idx >= MWL8K_LEGACY_5G_RATE_OFFSET)
  958. status->rate_idx -= MWL8K_LEGACY_5G_RATE_OFFSET;
  959. } else {
  960. status->band = NL80211_BAND_2GHZ;
  961. }
  962. status->freq = ieee80211_channel_to_frequency(rxd->channel,
  963. status->band);
  964. *qos = rxd->qos_control;
  965. if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
  966. (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
  967. status->flag |= RX_FLAG_MMIC_ERROR;
  968. return le16_to_cpu(rxd->pkt_len);
  969. }
  970. static struct rxd_ops rxd_sta_ops = {
  971. .rxd_size = sizeof(struct mwl8k_rxd_sta),
  972. .rxd_init = mwl8k_rxd_sta_init,
  973. .rxd_refill = mwl8k_rxd_sta_refill,
  974. .rxd_process = mwl8k_rxd_sta_process,
  975. };
  976. #define MWL8K_RX_DESCS 256
  977. #define MWL8K_RX_MAXSZ 3800
  978. static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
  979. {
  980. struct mwl8k_priv *priv = hw->priv;
  981. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  982. int size;
  983. int i;
  984. rxq->rxd_count = 0;
  985. rxq->head = 0;
  986. rxq->tail = 0;
  987. size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
  988. rxq->rxd = pci_zalloc_consistent(priv->pdev, size, &rxq->rxd_dma);
  989. if (rxq->rxd == NULL) {
  990. wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
  991. return -ENOMEM;
  992. }
  993. rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
  994. if (rxq->buf == NULL) {
  995. pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
  996. return -ENOMEM;
  997. }
  998. for (i = 0; i < MWL8K_RX_DESCS; i++) {
  999. int desc_size;
  1000. void *rxd;
  1001. int nexti;
  1002. dma_addr_t next_dma_addr;
  1003. desc_size = priv->rxd_ops->rxd_size;
  1004. rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
  1005. nexti = i + 1;
  1006. if (nexti == MWL8K_RX_DESCS)
  1007. nexti = 0;
  1008. next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
  1009. priv->rxd_ops->rxd_init(rxd, next_dma_addr);
  1010. }
  1011. return 0;
  1012. }
  1013. static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
  1014. {
  1015. struct mwl8k_priv *priv = hw->priv;
  1016. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  1017. int refilled;
  1018. refilled = 0;
  1019. while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
  1020. struct sk_buff *skb;
  1021. dma_addr_t addr;
  1022. int rx;
  1023. void *rxd;
  1024. skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
  1025. if (skb == NULL)
  1026. break;
  1027. addr = pci_map_single(priv->pdev, skb->data,
  1028. MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
  1029. rxq->rxd_count++;
  1030. rx = rxq->tail++;
  1031. if (rxq->tail == MWL8K_RX_DESCS)
  1032. rxq->tail = 0;
  1033. rxq->buf[rx].skb = skb;
  1034. dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
  1035. rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
  1036. priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
  1037. refilled++;
  1038. }
  1039. return refilled;
  1040. }
  1041. /* Must be called only when the card's reception is completely halted */
  1042. static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
  1043. {
  1044. struct mwl8k_priv *priv = hw->priv;
  1045. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  1046. int i;
  1047. if (rxq->rxd == NULL)
  1048. return;
  1049. for (i = 0; i < MWL8K_RX_DESCS; i++) {
  1050. if (rxq->buf[i].skb != NULL) {
  1051. pci_unmap_single(priv->pdev,
  1052. dma_unmap_addr(&rxq->buf[i], dma),
  1053. MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
  1054. dma_unmap_addr_set(&rxq->buf[i], dma, 0);
  1055. kfree_skb(rxq->buf[i].skb);
  1056. rxq->buf[i].skb = NULL;
  1057. }
  1058. }
  1059. kfree(rxq->buf);
  1060. rxq->buf = NULL;
  1061. pci_free_consistent(priv->pdev,
  1062. MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
  1063. rxq->rxd, rxq->rxd_dma);
  1064. rxq->rxd = NULL;
  1065. }
  1066. /*
  1067. * Scan a list of BSSIDs to process for finalize join.
  1068. * Allows for extension to process multiple BSSIDs.
  1069. */
  1070. static inline int
  1071. mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
  1072. {
  1073. return priv->capture_beacon &&
  1074. ieee80211_is_beacon(wh->frame_control) &&
  1075. ether_addr_equal_64bits(wh->addr3, priv->capture_bssid);
  1076. }
  1077. static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
  1078. struct sk_buff *skb)
  1079. {
  1080. struct mwl8k_priv *priv = hw->priv;
  1081. priv->capture_beacon = false;
  1082. eth_zero_addr(priv->capture_bssid);
  1083. /*
  1084. * Use GFP_ATOMIC as rxq_process is called from
  1085. * the primary interrupt handler, memory allocation call
  1086. * must not sleep.
  1087. */
  1088. priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
  1089. if (priv->beacon_skb != NULL)
  1090. ieee80211_queue_work(hw, &priv->finalize_join_worker);
  1091. }
  1092. static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
  1093. u8 *bssid)
  1094. {
  1095. struct mwl8k_vif *mwl8k_vif;
  1096. list_for_each_entry(mwl8k_vif,
  1097. vif_list, list) {
  1098. if (memcmp(bssid, mwl8k_vif->bssid,
  1099. ETH_ALEN) == 0)
  1100. return mwl8k_vif;
  1101. }
  1102. return NULL;
  1103. }
  1104. static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
  1105. {
  1106. struct mwl8k_priv *priv = hw->priv;
  1107. struct mwl8k_vif *mwl8k_vif = NULL;
  1108. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  1109. int processed;
  1110. processed = 0;
  1111. while (rxq->rxd_count && limit--) {
  1112. struct sk_buff *skb;
  1113. void *rxd;
  1114. int pkt_len;
  1115. struct ieee80211_rx_status status;
  1116. struct ieee80211_hdr *wh;
  1117. __le16 qos;
  1118. skb = rxq->buf[rxq->head].skb;
  1119. if (skb == NULL)
  1120. break;
  1121. rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
  1122. pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
  1123. &priv->noise);
  1124. if (pkt_len < 0)
  1125. break;
  1126. rxq->buf[rxq->head].skb = NULL;
  1127. pci_unmap_single(priv->pdev,
  1128. dma_unmap_addr(&rxq->buf[rxq->head], dma),
  1129. MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
  1130. dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
  1131. rxq->head++;
  1132. if (rxq->head == MWL8K_RX_DESCS)
  1133. rxq->head = 0;
  1134. rxq->rxd_count--;
  1135. wh = &((struct mwl8k_dma_data *)skb->data)->wh;
  1136. /*
  1137. * Check for a pending join operation. Save a
  1138. * copy of the beacon and schedule a tasklet to
  1139. * send a FINALIZE_JOIN command to the firmware.
  1140. */
  1141. if (mwl8k_capture_bssid(priv, (void *)skb->data))
  1142. mwl8k_save_beacon(hw, skb);
  1143. if (ieee80211_has_protected(wh->frame_control)) {
  1144. /* Check if hw crypto has been enabled for
  1145. * this bss. If yes, set the status flags
  1146. * accordingly
  1147. */
  1148. mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
  1149. wh->addr1);
  1150. if (mwl8k_vif != NULL &&
  1151. mwl8k_vif->is_hw_crypto_enabled) {
  1152. /*
  1153. * When MMIC ERROR is encountered
  1154. * by the firmware, payload is
  1155. * dropped and only 32 bytes of
  1156. * mwl8k Firmware header is sent
  1157. * to the host.
  1158. *
  1159. * We need to add four bytes of
  1160. * key information. In it
  1161. * MAC80211 expects keyidx set to
  1162. * 0 for triggering Counter
  1163. * Measure of MMIC failure.
  1164. */
  1165. if (status.flag & RX_FLAG_MMIC_ERROR) {
  1166. struct mwl8k_dma_data *tr;
  1167. tr = (struct mwl8k_dma_data *)skb->data;
  1168. memset((void *)&(tr->data), 0, 4);
  1169. pkt_len += 4;
  1170. }
  1171. if (!ieee80211_is_auth(wh->frame_control))
  1172. status.flag |= RX_FLAG_IV_STRIPPED |
  1173. RX_FLAG_DECRYPTED |
  1174. RX_FLAG_MMIC_STRIPPED;
  1175. }
  1176. }
  1177. skb_put(skb, pkt_len);
  1178. mwl8k_remove_dma_header(skb, qos);
  1179. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  1180. ieee80211_rx_irqsafe(hw, skb);
  1181. processed++;
  1182. }
  1183. return processed;
  1184. }
  1185. /*
  1186. * Packet transmission.
  1187. */
  1188. #define MWL8K_TXD_STATUS_OK 0x00000001
  1189. #define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
  1190. #define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
  1191. #define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
  1192. #define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
  1193. #define MWL8K_QOS_QLEN_UNSPEC 0xff00
  1194. #define MWL8K_QOS_ACK_POLICY_MASK 0x0060
  1195. #define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
  1196. #define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
  1197. #define MWL8K_QOS_EOSP 0x0010
  1198. struct mwl8k_tx_desc {
  1199. __le32 status;
  1200. __u8 data_rate;
  1201. __u8 tx_priority;
  1202. __le16 qos_control;
  1203. __le32 pkt_phys_addr;
  1204. __le16 pkt_len;
  1205. __u8 dest_MAC_addr[ETH_ALEN];
  1206. __le32 next_txd_phys_addr;
  1207. __le32 timestamp;
  1208. __le16 rate_info;
  1209. __u8 peer_id;
  1210. __u8 tx_frag_cnt;
  1211. } __packed;
  1212. #define MWL8K_TX_DESCS 128
  1213. static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
  1214. {
  1215. struct mwl8k_priv *priv = hw->priv;
  1216. struct mwl8k_tx_queue *txq = priv->txq + index;
  1217. int size;
  1218. int i;
  1219. txq->len = 0;
  1220. txq->head = 0;
  1221. txq->tail = 0;
  1222. size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
  1223. txq->txd = pci_zalloc_consistent(priv->pdev, size, &txq->txd_dma);
  1224. if (txq->txd == NULL) {
  1225. wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
  1226. return -ENOMEM;
  1227. }
  1228. txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
  1229. if (txq->skb == NULL) {
  1230. pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
  1231. return -ENOMEM;
  1232. }
  1233. for (i = 0; i < MWL8K_TX_DESCS; i++) {
  1234. struct mwl8k_tx_desc *tx_desc;
  1235. int nexti;
  1236. tx_desc = txq->txd + i;
  1237. nexti = (i + 1) % MWL8K_TX_DESCS;
  1238. tx_desc->status = 0;
  1239. tx_desc->next_txd_phys_addr =
  1240. cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
  1241. }
  1242. return 0;
  1243. }
  1244. static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
  1245. {
  1246. iowrite32(MWL8K_H2A_INT_PPA_READY,
  1247. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1248. iowrite32(MWL8K_H2A_INT_DUMMY,
  1249. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1250. ioread32(priv->regs + MWL8K_HIU_INT_CODE);
  1251. }
  1252. static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
  1253. {
  1254. struct mwl8k_priv *priv = hw->priv;
  1255. int i;
  1256. for (i = 0; i < mwl8k_tx_queues(priv); i++) {
  1257. struct mwl8k_tx_queue *txq = priv->txq + i;
  1258. int fw_owned = 0;
  1259. int drv_owned = 0;
  1260. int unused = 0;
  1261. int desc;
  1262. for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
  1263. struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
  1264. u32 status;
  1265. status = le32_to_cpu(tx_desc->status);
  1266. if (status & MWL8K_TXD_STATUS_FW_OWNED)
  1267. fw_owned++;
  1268. else
  1269. drv_owned++;
  1270. if (tx_desc->pkt_len == 0)
  1271. unused++;
  1272. }
  1273. wiphy_err(hw->wiphy,
  1274. "txq[%d] len=%d head=%d tail=%d "
  1275. "fw_owned=%d drv_owned=%d unused=%d\n",
  1276. i,
  1277. txq->len, txq->head, txq->tail,
  1278. fw_owned, drv_owned, unused);
  1279. }
  1280. }
  1281. /*
  1282. * Must be called with priv->fw_mutex held and tx queues stopped.
  1283. */
  1284. #define MWL8K_TX_WAIT_TIMEOUT_MS 5000
  1285. static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
  1286. {
  1287. struct mwl8k_priv *priv = hw->priv;
  1288. DECLARE_COMPLETION_ONSTACK(tx_wait);
  1289. int retry;
  1290. int rc;
  1291. might_sleep();
  1292. /* Since fw restart is in progress, allow only the firmware
  1293. * commands from the restart code and block the other
  1294. * commands since they are going to fail in any case since
  1295. * the firmware has crashed
  1296. */
  1297. if (priv->hw_restart_in_progress) {
  1298. if (priv->hw_restart_owner == current)
  1299. return 0;
  1300. else
  1301. return -EBUSY;
  1302. }
  1303. if (atomic_read(&priv->watchdog_event_pending))
  1304. return 0;
  1305. /*
  1306. * The TX queues are stopped at this point, so this test
  1307. * doesn't need to take ->tx_lock.
  1308. */
  1309. if (!priv->pending_tx_pkts)
  1310. return 0;
  1311. retry = 1;
  1312. rc = 0;
  1313. spin_lock_bh(&priv->tx_lock);
  1314. priv->tx_wait = &tx_wait;
  1315. while (!rc) {
  1316. int oldcount;
  1317. unsigned long timeout;
  1318. oldcount = priv->pending_tx_pkts;
  1319. spin_unlock_bh(&priv->tx_lock);
  1320. timeout = wait_for_completion_timeout(&tx_wait,
  1321. msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
  1322. if (atomic_read(&priv->watchdog_event_pending)) {
  1323. spin_lock_bh(&priv->tx_lock);
  1324. priv->tx_wait = NULL;
  1325. spin_unlock_bh(&priv->tx_lock);
  1326. return 0;
  1327. }
  1328. spin_lock_bh(&priv->tx_lock);
  1329. if (timeout || !priv->pending_tx_pkts) {
  1330. WARN_ON(priv->pending_tx_pkts);
  1331. if (retry)
  1332. wiphy_notice(hw->wiphy, "tx rings drained\n");
  1333. break;
  1334. }
  1335. if (retry) {
  1336. mwl8k_tx_start(priv);
  1337. retry = 0;
  1338. continue;
  1339. }
  1340. if (priv->pending_tx_pkts < oldcount) {
  1341. wiphy_notice(hw->wiphy,
  1342. "waiting for tx rings to drain (%d -> %d pkts)\n",
  1343. oldcount, priv->pending_tx_pkts);
  1344. retry = 1;
  1345. continue;
  1346. }
  1347. priv->tx_wait = NULL;
  1348. wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
  1349. MWL8K_TX_WAIT_TIMEOUT_MS);
  1350. mwl8k_dump_tx_rings(hw);
  1351. priv->hw_restart_in_progress = true;
  1352. ieee80211_queue_work(hw, &priv->fw_reload);
  1353. rc = -ETIMEDOUT;
  1354. }
  1355. priv->tx_wait = NULL;
  1356. spin_unlock_bh(&priv->tx_lock);
  1357. return rc;
  1358. }
  1359. #define MWL8K_TXD_SUCCESS(status) \
  1360. ((status) & (MWL8K_TXD_STATUS_OK | \
  1361. MWL8K_TXD_STATUS_OK_RETRY | \
  1362. MWL8K_TXD_STATUS_OK_MORE_RETRY))
  1363. static int mwl8k_tid_queue_mapping(u8 tid)
  1364. {
  1365. BUG_ON(tid > 7);
  1366. switch (tid) {
  1367. case 0:
  1368. case 3:
  1369. return IEEE80211_AC_BE;
  1370. case 1:
  1371. case 2:
  1372. return IEEE80211_AC_BK;
  1373. case 4:
  1374. case 5:
  1375. return IEEE80211_AC_VI;
  1376. case 6:
  1377. case 7:
  1378. return IEEE80211_AC_VO;
  1379. default:
  1380. return -1;
  1381. }
  1382. }
  1383. /* The firmware will fill in the rate information
  1384. * for each packet that gets queued in the hardware
  1385. * and these macros will interpret that info.
  1386. */
  1387. #define RI_FORMAT(a) (a & 0x0001)
  1388. #define RI_RATE_ID_MCS(a) ((a & 0x01f8) >> 3)
  1389. static int
  1390. mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
  1391. {
  1392. struct mwl8k_priv *priv = hw->priv;
  1393. struct mwl8k_tx_queue *txq = priv->txq + index;
  1394. int processed;
  1395. processed = 0;
  1396. while (txq->len > 0 && limit--) {
  1397. int tx;
  1398. struct mwl8k_tx_desc *tx_desc;
  1399. unsigned long addr;
  1400. int size;
  1401. struct sk_buff *skb;
  1402. struct ieee80211_tx_info *info;
  1403. u32 status;
  1404. struct ieee80211_sta *sta;
  1405. struct mwl8k_sta *sta_info = NULL;
  1406. u16 rate_info;
  1407. struct ieee80211_hdr *wh;
  1408. tx = txq->head;
  1409. tx_desc = txq->txd + tx;
  1410. status = le32_to_cpu(tx_desc->status);
  1411. if (status & MWL8K_TXD_STATUS_FW_OWNED) {
  1412. if (!force)
  1413. break;
  1414. tx_desc->status &=
  1415. ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
  1416. }
  1417. txq->head = (tx + 1) % MWL8K_TX_DESCS;
  1418. BUG_ON(txq->len == 0);
  1419. txq->len--;
  1420. priv->pending_tx_pkts--;
  1421. addr = le32_to_cpu(tx_desc->pkt_phys_addr);
  1422. size = le16_to_cpu(tx_desc->pkt_len);
  1423. skb = txq->skb[tx];
  1424. txq->skb[tx] = NULL;
  1425. BUG_ON(skb == NULL);
  1426. pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
  1427. mwl8k_remove_dma_header(skb, tx_desc->qos_control);
  1428. wh = (struct ieee80211_hdr *) skb->data;
  1429. /* Mark descriptor as unused */
  1430. tx_desc->pkt_phys_addr = 0;
  1431. tx_desc->pkt_len = 0;
  1432. info = IEEE80211_SKB_CB(skb);
  1433. if (ieee80211_is_data(wh->frame_control)) {
  1434. rcu_read_lock();
  1435. sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
  1436. wh->addr2);
  1437. if (sta) {
  1438. sta_info = MWL8K_STA(sta);
  1439. BUG_ON(sta_info == NULL);
  1440. rate_info = le16_to_cpu(tx_desc->rate_info);
  1441. /* If rate is < 6.5 Mpbs for an ht station
  1442. * do not form an ampdu. If the station is a
  1443. * legacy station (format = 0), do not form an
  1444. * ampdu
  1445. */
  1446. if (RI_RATE_ID_MCS(rate_info) < 1 ||
  1447. RI_FORMAT(rate_info) == 0) {
  1448. sta_info->is_ampdu_allowed = false;
  1449. } else {
  1450. sta_info->is_ampdu_allowed = true;
  1451. }
  1452. }
  1453. rcu_read_unlock();
  1454. }
  1455. ieee80211_tx_info_clear_status(info);
  1456. /* Rate control is happening in the firmware.
  1457. * Ensure no tx rate is being reported.
  1458. */
  1459. info->status.rates[0].idx = -1;
  1460. info->status.rates[0].count = 1;
  1461. if (MWL8K_TXD_SUCCESS(status))
  1462. info->flags |= IEEE80211_TX_STAT_ACK;
  1463. ieee80211_tx_status_irqsafe(hw, skb);
  1464. processed++;
  1465. }
  1466. return processed;
  1467. }
  1468. /* must be called only when the card's transmit is completely halted */
  1469. static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
  1470. {
  1471. struct mwl8k_priv *priv = hw->priv;
  1472. struct mwl8k_tx_queue *txq = priv->txq + index;
  1473. if (txq->txd == NULL)
  1474. return;
  1475. mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
  1476. kfree(txq->skb);
  1477. txq->skb = NULL;
  1478. pci_free_consistent(priv->pdev,
  1479. MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
  1480. txq->txd, txq->txd_dma);
  1481. txq->txd = NULL;
  1482. }
  1483. /* caller must hold priv->stream_lock when calling the stream functions */
  1484. static struct mwl8k_ampdu_stream *
  1485. mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
  1486. {
  1487. struct mwl8k_ampdu_stream *stream;
  1488. struct mwl8k_priv *priv = hw->priv;
  1489. int i;
  1490. for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
  1491. stream = &priv->ampdu[i];
  1492. if (stream->state == AMPDU_NO_STREAM) {
  1493. stream->sta = sta;
  1494. stream->state = AMPDU_STREAM_NEW;
  1495. stream->tid = tid;
  1496. stream->idx = i;
  1497. wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
  1498. sta->addr, tid);
  1499. return stream;
  1500. }
  1501. }
  1502. return NULL;
  1503. }
  1504. static int
  1505. mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
  1506. {
  1507. int ret;
  1508. /* if the stream has already been started, don't start it again */
  1509. if (stream->state != AMPDU_STREAM_NEW)
  1510. return 0;
  1511. ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
  1512. if (ret)
  1513. wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
  1514. "%d\n", stream->sta->addr, stream->tid, ret);
  1515. else
  1516. wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
  1517. stream->sta->addr, stream->tid);
  1518. return ret;
  1519. }
  1520. static void
  1521. mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
  1522. {
  1523. wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
  1524. stream->tid);
  1525. memset(stream, 0, sizeof(*stream));
  1526. }
  1527. static struct mwl8k_ampdu_stream *
  1528. mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
  1529. {
  1530. struct mwl8k_priv *priv = hw->priv;
  1531. int i;
  1532. for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
  1533. struct mwl8k_ampdu_stream *stream;
  1534. stream = &priv->ampdu[i];
  1535. if (stream->state == AMPDU_NO_STREAM)
  1536. continue;
  1537. if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
  1538. stream->tid == tid)
  1539. return stream;
  1540. }
  1541. return NULL;
  1542. }
  1543. #define MWL8K_AMPDU_PACKET_THRESHOLD 64
  1544. static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
  1545. {
  1546. struct mwl8k_sta *sta_info = MWL8K_STA(sta);
  1547. struct tx_traffic_info *tx_stats;
  1548. BUG_ON(tid >= MWL8K_MAX_TID);
  1549. tx_stats = &sta_info->tx_stats[tid];
  1550. return sta_info->is_ampdu_allowed &&
  1551. tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
  1552. }
  1553. static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
  1554. {
  1555. struct mwl8k_sta *sta_info = MWL8K_STA(sta);
  1556. struct tx_traffic_info *tx_stats;
  1557. BUG_ON(tid >= MWL8K_MAX_TID);
  1558. tx_stats = &sta_info->tx_stats[tid];
  1559. if (tx_stats->start_time == 0)
  1560. tx_stats->start_time = jiffies;
  1561. /* reset the packet count after each second elapses. If the number of
  1562. * packets ever exceeds the ampdu_min_traffic threshold, we will allow
  1563. * an ampdu stream to be started.
  1564. */
  1565. if (jiffies - tx_stats->start_time > HZ) {
  1566. tx_stats->pkts = 0;
  1567. tx_stats->start_time = 0;
  1568. } else
  1569. tx_stats->pkts++;
  1570. }
  1571. /* The hardware ampdu queues start from 5.
  1572. * txpriorities for ampdu queues are
  1573. * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
  1574. * and queue 3 is lowest (queue 4 is reserved)
  1575. */
  1576. #define BA_QUEUE 5
  1577. static void
  1578. mwl8k_txq_xmit(struct ieee80211_hw *hw,
  1579. int index,
  1580. struct ieee80211_sta *sta,
  1581. struct sk_buff *skb)
  1582. {
  1583. struct mwl8k_priv *priv = hw->priv;
  1584. struct ieee80211_tx_info *tx_info;
  1585. struct mwl8k_vif *mwl8k_vif;
  1586. struct ieee80211_hdr *wh;
  1587. struct mwl8k_tx_queue *txq;
  1588. struct mwl8k_tx_desc *tx;
  1589. dma_addr_t dma;
  1590. u32 txstatus;
  1591. u8 txdatarate;
  1592. u16 qos;
  1593. int txpriority;
  1594. u8 tid = 0;
  1595. struct mwl8k_ampdu_stream *stream = NULL;
  1596. bool start_ba_session = false;
  1597. bool mgmtframe = false;
  1598. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  1599. bool eapol_frame = false;
  1600. wh = (struct ieee80211_hdr *)skb->data;
  1601. if (ieee80211_is_data_qos(wh->frame_control))
  1602. qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
  1603. else
  1604. qos = 0;
  1605. if (skb->protocol == cpu_to_be16(ETH_P_PAE))
  1606. eapol_frame = true;
  1607. if (ieee80211_is_mgmt(wh->frame_control))
  1608. mgmtframe = true;
  1609. if (priv->ap_fw)
  1610. mwl8k_encapsulate_tx_frame(priv, skb);
  1611. else
  1612. mwl8k_add_dma_header(priv, skb, 0, 0);
  1613. wh = &((struct mwl8k_dma_data *)skb->data)->wh;
  1614. tx_info = IEEE80211_SKB_CB(skb);
  1615. mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
  1616. if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1617. wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1618. wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
  1619. mwl8k_vif->seqno += 0x10;
  1620. }
  1621. /* Setup firmware control bit fields for each frame type. */
  1622. txstatus = 0;
  1623. txdatarate = 0;
  1624. if (ieee80211_is_mgmt(wh->frame_control) ||
  1625. ieee80211_is_ctl(wh->frame_control)) {
  1626. txdatarate = 0;
  1627. qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
  1628. } else if (ieee80211_is_data(wh->frame_control)) {
  1629. txdatarate = 1;
  1630. if (is_multicast_ether_addr(wh->addr1))
  1631. txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
  1632. qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
  1633. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
  1634. qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
  1635. else
  1636. qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
  1637. }
  1638. /* Queue ADDBA request in the respective data queue. While setting up
  1639. * the ampdu stream, mac80211 queues further packets for that
  1640. * particular ra/tid pair. However, packets piled up in the hardware
  1641. * for that ra/tid pair will still go out. ADDBA request and the
  1642. * related data packets going out from different queues asynchronously
  1643. * will cause a shift in the receiver window which might result in
  1644. * ampdu packets getting dropped at the receiver after the stream has
  1645. * been setup.
  1646. */
  1647. if (unlikely(ieee80211_is_action(wh->frame_control) &&
  1648. mgmt->u.action.category == WLAN_CATEGORY_BACK &&
  1649. mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
  1650. priv->ap_fw)) {
  1651. u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  1652. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  1653. index = mwl8k_tid_queue_mapping(tid);
  1654. }
  1655. txpriority = index;
  1656. if (priv->ap_fw && sta && sta->ht_cap.ht_supported && !eapol_frame &&
  1657. ieee80211_is_data_qos(wh->frame_control)) {
  1658. tid = qos & 0xf;
  1659. mwl8k_tx_count_packet(sta, tid);
  1660. spin_lock(&priv->stream_lock);
  1661. stream = mwl8k_lookup_stream(hw, sta->addr, tid);
  1662. if (stream != NULL) {
  1663. if (stream->state == AMPDU_STREAM_ACTIVE) {
  1664. WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
  1665. txpriority = (BA_QUEUE + stream->idx) %
  1666. TOTAL_HW_TX_QUEUES;
  1667. if (stream->idx <= 1)
  1668. index = stream->idx +
  1669. MWL8K_TX_WMM_QUEUES;
  1670. } else if (stream->state == AMPDU_STREAM_NEW) {
  1671. /* We get here if the driver sends us packets
  1672. * after we've initiated a stream, but before
  1673. * our ampdu_action routine has been called
  1674. * with IEEE80211_AMPDU_TX_START to get the SSN
  1675. * for the ADDBA request. So this packet can
  1676. * go out with no risk of sequence number
  1677. * mismatch. No special handling is required.
  1678. */
  1679. } else {
  1680. /* Drop packets that would go out after the
  1681. * ADDBA request was sent but before the ADDBA
  1682. * response is received. If we don't do this,
  1683. * the recipient would probably receive it
  1684. * after the ADDBA request with SSN 0. This
  1685. * will cause the recipient's BA receive window
  1686. * to shift, which would cause the subsequent
  1687. * packets in the BA stream to be discarded.
  1688. * mac80211 queues our packets for us in this
  1689. * case, so this is really just a safety check.
  1690. */
  1691. wiphy_warn(hw->wiphy,
  1692. "Cannot send packet while ADDBA "
  1693. "dialog is underway.\n");
  1694. spin_unlock(&priv->stream_lock);
  1695. dev_kfree_skb(skb);
  1696. return;
  1697. }
  1698. } else {
  1699. /* Defer calling mwl8k_start_stream so that the current
  1700. * skb can go out before the ADDBA request. This
  1701. * prevents sequence number mismatch at the recepient
  1702. * as described above.
  1703. */
  1704. if (mwl8k_ampdu_allowed(sta, tid)) {
  1705. stream = mwl8k_add_stream(hw, sta, tid);
  1706. if (stream != NULL)
  1707. start_ba_session = true;
  1708. }
  1709. }
  1710. spin_unlock(&priv->stream_lock);
  1711. } else {
  1712. qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
  1713. qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
  1714. }
  1715. dma = pci_map_single(priv->pdev, skb->data,
  1716. skb->len, PCI_DMA_TODEVICE);
  1717. if (pci_dma_mapping_error(priv->pdev, dma)) {
  1718. wiphy_debug(hw->wiphy,
  1719. "failed to dma map skb, dropping TX frame.\n");
  1720. if (start_ba_session) {
  1721. spin_lock(&priv->stream_lock);
  1722. mwl8k_remove_stream(hw, stream);
  1723. spin_unlock(&priv->stream_lock);
  1724. }
  1725. dev_kfree_skb(skb);
  1726. return;
  1727. }
  1728. spin_lock_bh(&priv->tx_lock);
  1729. txq = priv->txq + index;
  1730. /* Mgmt frames that go out frequently are probe
  1731. * responses. Other mgmt frames got out relatively
  1732. * infrequently. Hence reserve 2 buffers so that
  1733. * other mgmt frames do not get dropped due to an
  1734. * already queued probe response in one of the
  1735. * reserved buffers.
  1736. */
  1737. if (txq->len >= MWL8K_TX_DESCS - 2) {
  1738. if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
  1739. if (start_ba_session) {
  1740. spin_lock(&priv->stream_lock);
  1741. mwl8k_remove_stream(hw, stream);
  1742. spin_unlock(&priv->stream_lock);
  1743. }
  1744. mwl8k_tx_start(priv);
  1745. spin_unlock_bh(&priv->tx_lock);
  1746. pci_unmap_single(priv->pdev, dma, skb->len,
  1747. PCI_DMA_TODEVICE);
  1748. dev_kfree_skb(skb);
  1749. return;
  1750. }
  1751. }
  1752. BUG_ON(txq->skb[txq->tail] != NULL);
  1753. txq->skb[txq->tail] = skb;
  1754. tx = txq->txd + txq->tail;
  1755. tx->data_rate = txdatarate;
  1756. tx->tx_priority = txpriority;
  1757. tx->qos_control = cpu_to_le16(qos);
  1758. tx->pkt_phys_addr = cpu_to_le32(dma);
  1759. tx->pkt_len = cpu_to_le16(skb->len);
  1760. tx->rate_info = 0;
  1761. if (!priv->ap_fw && sta != NULL)
  1762. tx->peer_id = MWL8K_STA(sta)->peer_id;
  1763. else
  1764. tx->peer_id = 0;
  1765. if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
  1766. tx->timestamp = cpu_to_le32(ioread32(priv->regs +
  1767. MWL8K_HW_TIMER_REGISTER));
  1768. else
  1769. tx->timestamp = 0;
  1770. wmb();
  1771. tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
  1772. txq->len++;
  1773. priv->pending_tx_pkts++;
  1774. txq->tail++;
  1775. if (txq->tail == MWL8K_TX_DESCS)
  1776. txq->tail = 0;
  1777. mwl8k_tx_start(priv);
  1778. spin_unlock_bh(&priv->tx_lock);
  1779. /* Initiate the ampdu session here */
  1780. if (start_ba_session) {
  1781. spin_lock(&priv->stream_lock);
  1782. if (mwl8k_start_stream(hw, stream))
  1783. mwl8k_remove_stream(hw, stream);
  1784. spin_unlock(&priv->stream_lock);
  1785. }
  1786. }
  1787. /*
  1788. * Firmware access.
  1789. *
  1790. * We have the following requirements for issuing firmware commands:
  1791. * - Some commands require that the packet transmit path is idle when
  1792. * the command is issued. (For simplicity, we'll just quiesce the
  1793. * transmit path for every command.)
  1794. * - There are certain sequences of commands that need to be issued to
  1795. * the hardware sequentially, with no other intervening commands.
  1796. *
  1797. * This leads to an implementation of a "firmware lock" as a mutex that
  1798. * can be taken recursively, and which is taken by both the low-level
  1799. * command submission function (mwl8k_post_cmd) as well as any users of
  1800. * that function that require issuing of an atomic sequence of commands,
  1801. * and quiesces the transmit path whenever it's taken.
  1802. */
  1803. static int mwl8k_fw_lock(struct ieee80211_hw *hw)
  1804. {
  1805. struct mwl8k_priv *priv = hw->priv;
  1806. if (priv->fw_mutex_owner != current) {
  1807. int rc;
  1808. mutex_lock(&priv->fw_mutex);
  1809. ieee80211_stop_queues(hw);
  1810. rc = mwl8k_tx_wait_empty(hw);
  1811. if (rc) {
  1812. if (!priv->hw_restart_in_progress)
  1813. ieee80211_wake_queues(hw);
  1814. mutex_unlock(&priv->fw_mutex);
  1815. return rc;
  1816. }
  1817. priv->fw_mutex_owner = current;
  1818. }
  1819. priv->fw_mutex_depth++;
  1820. return 0;
  1821. }
  1822. static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
  1823. {
  1824. struct mwl8k_priv *priv = hw->priv;
  1825. if (!--priv->fw_mutex_depth) {
  1826. if (!priv->hw_restart_in_progress)
  1827. ieee80211_wake_queues(hw);
  1828. priv->fw_mutex_owner = NULL;
  1829. mutex_unlock(&priv->fw_mutex);
  1830. }
  1831. }
  1832. static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable,
  1833. u32 bitmap);
  1834. /*
  1835. * Command processing.
  1836. */
  1837. /* Timeout firmware commands after 10s */
  1838. #define MWL8K_CMD_TIMEOUT_MS 10000
  1839. static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
  1840. {
  1841. DECLARE_COMPLETION_ONSTACK(cmd_wait);
  1842. struct mwl8k_priv *priv = hw->priv;
  1843. void __iomem *regs = priv->regs;
  1844. dma_addr_t dma_addr;
  1845. unsigned int dma_size;
  1846. int rc;
  1847. unsigned long timeout = 0;
  1848. u8 buf[32];
  1849. u32 bitmap = 0;
  1850. wiphy_dbg(hw->wiphy, "Posting %s [%d]\n",
  1851. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)), cmd->macid);
  1852. /* Before posting firmware commands that could change the hardware
  1853. * characteristics, make sure that all BSSes are stopped temporary.
  1854. * Enable these stopped BSSes after completion of the commands
  1855. */
  1856. rc = mwl8k_fw_lock(hw);
  1857. if (rc)
  1858. return rc;
  1859. if (priv->ap_fw && priv->running_bsses) {
  1860. switch (le16_to_cpu(cmd->code)) {
  1861. case MWL8K_CMD_SET_RF_CHANNEL:
  1862. case MWL8K_CMD_RADIO_CONTROL:
  1863. case MWL8K_CMD_RF_TX_POWER:
  1864. case MWL8K_CMD_TX_POWER:
  1865. case MWL8K_CMD_RF_ANTENNA:
  1866. case MWL8K_CMD_RTS_THRESHOLD:
  1867. case MWL8K_CMD_MIMO_CONFIG:
  1868. bitmap = priv->running_bsses;
  1869. mwl8k_enable_bsses(hw, false, bitmap);
  1870. break;
  1871. }
  1872. }
  1873. cmd->result = (__force __le16) 0xffff;
  1874. dma_size = le16_to_cpu(cmd->length);
  1875. dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
  1876. PCI_DMA_BIDIRECTIONAL);
  1877. if (pci_dma_mapping_error(priv->pdev, dma_addr))
  1878. return -ENOMEM;
  1879. priv->hostcmd_wait = &cmd_wait;
  1880. iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
  1881. iowrite32(MWL8K_H2A_INT_DOORBELL,
  1882. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1883. iowrite32(MWL8K_H2A_INT_DUMMY,
  1884. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1885. timeout = wait_for_completion_timeout(&cmd_wait,
  1886. msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
  1887. priv->hostcmd_wait = NULL;
  1888. pci_unmap_single(priv->pdev, dma_addr, dma_size,
  1889. PCI_DMA_BIDIRECTIONAL);
  1890. if (!timeout) {
  1891. wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
  1892. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
  1893. MWL8K_CMD_TIMEOUT_MS);
  1894. rc = -ETIMEDOUT;
  1895. } else {
  1896. int ms;
  1897. ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
  1898. rc = cmd->result ? -EINVAL : 0;
  1899. if (rc)
  1900. wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
  1901. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
  1902. le16_to_cpu(cmd->result));
  1903. else if (ms > 2000)
  1904. wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
  1905. mwl8k_cmd_name(cmd->code,
  1906. buf, sizeof(buf)),
  1907. ms);
  1908. }
  1909. if (bitmap)
  1910. mwl8k_enable_bsses(hw, true, bitmap);
  1911. mwl8k_fw_unlock(hw);
  1912. return rc;
  1913. }
  1914. static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
  1915. struct ieee80211_vif *vif,
  1916. struct mwl8k_cmd_pkt *cmd)
  1917. {
  1918. if (vif != NULL)
  1919. cmd->macid = MWL8K_VIF(vif)->macid;
  1920. return mwl8k_post_cmd(hw, cmd);
  1921. }
  1922. /*
  1923. * Setup code shared between STA and AP firmware images.
  1924. */
  1925. static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
  1926. {
  1927. struct mwl8k_priv *priv = hw->priv;
  1928. BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
  1929. memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
  1930. BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
  1931. memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
  1932. priv->band_24.band = NL80211_BAND_2GHZ;
  1933. priv->band_24.channels = priv->channels_24;
  1934. priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
  1935. priv->band_24.bitrates = priv->rates_24;
  1936. priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
  1937. hw->wiphy->bands[NL80211_BAND_2GHZ] = &priv->band_24;
  1938. }
  1939. static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
  1940. {
  1941. struct mwl8k_priv *priv = hw->priv;
  1942. BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
  1943. memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
  1944. BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
  1945. memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
  1946. priv->band_50.band = NL80211_BAND_5GHZ;
  1947. priv->band_50.channels = priv->channels_50;
  1948. priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
  1949. priv->band_50.bitrates = priv->rates_50;
  1950. priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
  1951. hw->wiphy->bands[NL80211_BAND_5GHZ] = &priv->band_50;
  1952. }
  1953. /*
  1954. * CMD_GET_HW_SPEC (STA version).
  1955. */
  1956. struct mwl8k_cmd_get_hw_spec_sta {
  1957. struct mwl8k_cmd_pkt header;
  1958. __u8 hw_rev;
  1959. __u8 host_interface;
  1960. __le16 num_mcaddrs;
  1961. __u8 perm_addr[ETH_ALEN];
  1962. __le16 region_code;
  1963. __le32 fw_rev;
  1964. __le32 ps_cookie;
  1965. __le32 caps;
  1966. __u8 mcs_bitmap[16];
  1967. __le32 rx_queue_ptr;
  1968. __le32 num_tx_queues;
  1969. __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
  1970. __le32 caps2;
  1971. __le32 num_tx_desc_per_queue;
  1972. __le32 total_rxd;
  1973. } __packed;
  1974. #define MWL8K_CAP_MAX_AMSDU 0x20000000
  1975. #define MWL8K_CAP_GREENFIELD 0x08000000
  1976. #define MWL8K_CAP_AMPDU 0x04000000
  1977. #define MWL8K_CAP_RX_STBC 0x01000000
  1978. #define MWL8K_CAP_TX_STBC 0x00800000
  1979. #define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
  1980. #define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
  1981. #define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
  1982. #define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
  1983. #define MWL8K_CAP_DELAY_BA 0x00003000
  1984. #define MWL8K_CAP_MIMO 0x00000200
  1985. #define MWL8K_CAP_40MHZ 0x00000100
  1986. #define MWL8K_CAP_BAND_MASK 0x00000007
  1987. #define MWL8K_CAP_5GHZ 0x00000004
  1988. #define MWL8K_CAP_2GHZ4 0x00000001
  1989. static void
  1990. mwl8k_set_ht_caps(struct ieee80211_hw *hw,
  1991. struct ieee80211_supported_band *band, u32 cap)
  1992. {
  1993. int rx_streams;
  1994. int tx_streams;
  1995. band->ht_cap.ht_supported = 1;
  1996. if (cap & MWL8K_CAP_MAX_AMSDU)
  1997. band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  1998. if (cap & MWL8K_CAP_GREENFIELD)
  1999. band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
  2000. if (cap & MWL8K_CAP_AMPDU) {
  2001. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  2002. band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  2003. band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
  2004. }
  2005. if (cap & MWL8K_CAP_RX_STBC)
  2006. band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
  2007. if (cap & MWL8K_CAP_TX_STBC)
  2008. band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
  2009. if (cap & MWL8K_CAP_SHORTGI_40MHZ)
  2010. band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
  2011. if (cap & MWL8K_CAP_SHORTGI_20MHZ)
  2012. band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
  2013. if (cap & MWL8K_CAP_DELAY_BA)
  2014. band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
  2015. if (cap & MWL8K_CAP_40MHZ)
  2016. band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  2017. rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
  2018. tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
  2019. band->ht_cap.mcs.rx_mask[0] = 0xff;
  2020. if (rx_streams >= 2)
  2021. band->ht_cap.mcs.rx_mask[1] = 0xff;
  2022. if (rx_streams >= 3)
  2023. band->ht_cap.mcs.rx_mask[2] = 0xff;
  2024. band->ht_cap.mcs.rx_mask[4] = 0x01;
  2025. band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  2026. if (rx_streams != tx_streams) {
  2027. band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  2028. band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
  2029. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  2030. }
  2031. }
  2032. static void
  2033. mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
  2034. {
  2035. struct mwl8k_priv *priv = hw->priv;
  2036. if (priv->caps)
  2037. return;
  2038. if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
  2039. mwl8k_setup_2ghz_band(hw);
  2040. if (caps & MWL8K_CAP_MIMO)
  2041. mwl8k_set_ht_caps(hw, &priv->band_24, caps);
  2042. }
  2043. if (caps & MWL8K_CAP_5GHZ) {
  2044. mwl8k_setup_5ghz_band(hw);
  2045. if (caps & MWL8K_CAP_MIMO)
  2046. mwl8k_set_ht_caps(hw, &priv->band_50, caps);
  2047. }
  2048. priv->caps = caps;
  2049. }
  2050. static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
  2051. {
  2052. struct mwl8k_priv *priv = hw->priv;
  2053. struct mwl8k_cmd_get_hw_spec_sta *cmd;
  2054. int rc;
  2055. int i;
  2056. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2057. if (cmd == NULL)
  2058. return -ENOMEM;
  2059. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
  2060. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2061. memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
  2062. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  2063. cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
  2064. cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
  2065. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  2066. cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
  2067. cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
  2068. cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
  2069. rc = mwl8k_post_cmd(hw, &cmd->header);
  2070. if (!rc) {
  2071. SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
  2072. priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
  2073. priv->fw_rev = le32_to_cpu(cmd->fw_rev);
  2074. priv->hw_rev = cmd->hw_rev;
  2075. mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
  2076. priv->ap_macids_supported = 0x00000000;
  2077. priv->sta_macids_supported = 0x00000001;
  2078. }
  2079. kfree(cmd);
  2080. return rc;
  2081. }
  2082. /*
  2083. * CMD_GET_HW_SPEC (AP version).
  2084. */
  2085. struct mwl8k_cmd_get_hw_spec_ap {
  2086. struct mwl8k_cmd_pkt header;
  2087. __u8 hw_rev;
  2088. __u8 host_interface;
  2089. __le16 num_wcb;
  2090. __le16 num_mcaddrs;
  2091. __u8 perm_addr[ETH_ALEN];
  2092. __le16 region_code;
  2093. __le16 num_antenna;
  2094. __le32 fw_rev;
  2095. __le32 wcbbase0;
  2096. __le32 rxwrptr;
  2097. __le32 rxrdptr;
  2098. __le32 ps_cookie;
  2099. __le32 wcbbase1;
  2100. __le32 wcbbase2;
  2101. __le32 wcbbase3;
  2102. __le32 fw_api_version;
  2103. __le32 caps;
  2104. __le32 num_of_ampdu_queues;
  2105. __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
  2106. } __packed;
  2107. static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
  2108. {
  2109. struct mwl8k_priv *priv = hw->priv;
  2110. struct mwl8k_cmd_get_hw_spec_ap *cmd;
  2111. int rc, i;
  2112. u32 api_version;
  2113. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2114. if (cmd == NULL)
  2115. return -ENOMEM;
  2116. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
  2117. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2118. memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
  2119. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  2120. rc = mwl8k_post_cmd(hw, &cmd->header);
  2121. if (!rc) {
  2122. int off;
  2123. api_version = le32_to_cpu(cmd->fw_api_version);
  2124. if (priv->device_info->fw_api_ap != api_version) {
  2125. printk(KERN_ERR "%s: Unsupported fw API version for %s."
  2126. " Expected %d got %d.\n", MWL8K_NAME,
  2127. priv->device_info->part_name,
  2128. priv->device_info->fw_api_ap,
  2129. api_version);
  2130. rc = -EINVAL;
  2131. goto done;
  2132. }
  2133. SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
  2134. priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
  2135. priv->fw_rev = le32_to_cpu(cmd->fw_rev);
  2136. priv->hw_rev = cmd->hw_rev;
  2137. mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
  2138. priv->ap_macids_supported = 0x000000ff;
  2139. priv->sta_macids_supported = 0x00000100;
  2140. priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
  2141. if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
  2142. wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
  2143. " but we only support %d.\n",
  2144. priv->num_ampdu_queues,
  2145. MWL8K_MAX_AMPDU_QUEUES);
  2146. priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
  2147. }
  2148. off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
  2149. iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
  2150. off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
  2151. iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
  2152. priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
  2153. priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
  2154. priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
  2155. priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
  2156. for (i = 0; i < priv->num_ampdu_queues; i++)
  2157. priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
  2158. le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
  2159. }
  2160. done:
  2161. kfree(cmd);
  2162. return rc;
  2163. }
  2164. /*
  2165. * CMD_SET_HW_SPEC.
  2166. */
  2167. struct mwl8k_cmd_set_hw_spec {
  2168. struct mwl8k_cmd_pkt header;
  2169. __u8 hw_rev;
  2170. __u8 host_interface;
  2171. __le16 num_mcaddrs;
  2172. __u8 perm_addr[ETH_ALEN];
  2173. __le16 region_code;
  2174. __le32 fw_rev;
  2175. __le32 ps_cookie;
  2176. __le32 caps;
  2177. __le32 rx_queue_ptr;
  2178. __le32 num_tx_queues;
  2179. __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
  2180. __le32 flags;
  2181. __le32 num_tx_desc_per_queue;
  2182. __le32 total_rxd;
  2183. } __packed;
  2184. /* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
  2185. * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
  2186. * the packets that are queued for more than 500ms, will be dropped in the
  2187. * hardware. This helps minimizing the issues caused due to head-of-line
  2188. * blocking where a slow client can hog the bandwidth and affect traffic to a
  2189. * faster client.
  2190. */
  2191. #define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
  2192. #define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR 0x00000200
  2193. #define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
  2194. #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
  2195. #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
  2196. static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
  2197. {
  2198. struct mwl8k_priv *priv = hw->priv;
  2199. struct mwl8k_cmd_set_hw_spec *cmd;
  2200. int rc;
  2201. int i;
  2202. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2203. if (cmd == NULL)
  2204. return -ENOMEM;
  2205. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
  2206. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2207. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  2208. cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
  2209. cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
  2210. /*
  2211. * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
  2212. * that order. Firmware has Q3 as highest priority and Q0 as lowest
  2213. * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
  2214. * priority is interpreted the right way in firmware.
  2215. */
  2216. for (i = 0; i < mwl8k_tx_queues(priv); i++) {
  2217. int j = mwl8k_tx_queues(priv) - 1 - i;
  2218. cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
  2219. }
  2220. cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
  2221. MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
  2222. MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
  2223. MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
  2224. MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
  2225. cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
  2226. cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
  2227. rc = mwl8k_post_cmd(hw, &cmd->header);
  2228. kfree(cmd);
  2229. return rc;
  2230. }
  2231. /*
  2232. * CMD_MAC_MULTICAST_ADR.
  2233. */
  2234. struct mwl8k_cmd_mac_multicast_adr {
  2235. struct mwl8k_cmd_pkt header;
  2236. __le16 action;
  2237. __le16 numaddr;
  2238. __u8 addr[0][ETH_ALEN];
  2239. };
  2240. #define MWL8K_ENABLE_RX_DIRECTED 0x0001
  2241. #define MWL8K_ENABLE_RX_MULTICAST 0x0002
  2242. #define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
  2243. #define MWL8K_ENABLE_RX_BROADCAST 0x0008
  2244. static struct mwl8k_cmd_pkt *
  2245. __mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
  2246. struct netdev_hw_addr_list *mc_list)
  2247. {
  2248. struct mwl8k_priv *priv = hw->priv;
  2249. struct mwl8k_cmd_mac_multicast_adr *cmd;
  2250. int size;
  2251. int mc_count = 0;
  2252. if (mc_list)
  2253. mc_count = netdev_hw_addr_list_count(mc_list);
  2254. if (allmulti || mc_count > priv->num_mcaddrs) {
  2255. allmulti = 1;
  2256. mc_count = 0;
  2257. }
  2258. size = sizeof(*cmd) + mc_count * ETH_ALEN;
  2259. cmd = kzalloc(size, GFP_ATOMIC);
  2260. if (cmd == NULL)
  2261. return NULL;
  2262. cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
  2263. cmd->header.length = cpu_to_le16(size);
  2264. cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
  2265. MWL8K_ENABLE_RX_BROADCAST);
  2266. if (allmulti) {
  2267. cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
  2268. } else if (mc_count) {
  2269. struct netdev_hw_addr *ha;
  2270. int i = 0;
  2271. cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
  2272. cmd->numaddr = cpu_to_le16(mc_count);
  2273. netdev_hw_addr_list_for_each(ha, mc_list) {
  2274. memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
  2275. }
  2276. }
  2277. return &cmd->header;
  2278. }
  2279. /*
  2280. * CMD_GET_STAT.
  2281. */
  2282. struct mwl8k_cmd_get_stat {
  2283. struct mwl8k_cmd_pkt header;
  2284. __le32 stats[64];
  2285. } __packed;
  2286. #define MWL8K_STAT_ACK_FAILURE 9
  2287. #define MWL8K_STAT_RTS_FAILURE 12
  2288. #define MWL8K_STAT_FCS_ERROR 24
  2289. #define MWL8K_STAT_RTS_SUCCESS 11
  2290. static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
  2291. struct ieee80211_low_level_stats *stats)
  2292. {
  2293. struct mwl8k_cmd_get_stat *cmd;
  2294. int rc;
  2295. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2296. if (cmd == NULL)
  2297. return -ENOMEM;
  2298. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
  2299. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2300. rc = mwl8k_post_cmd(hw, &cmd->header);
  2301. if (!rc) {
  2302. stats->dot11ACKFailureCount =
  2303. le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
  2304. stats->dot11RTSFailureCount =
  2305. le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
  2306. stats->dot11FCSErrorCount =
  2307. le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
  2308. stats->dot11RTSSuccessCount =
  2309. le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
  2310. }
  2311. kfree(cmd);
  2312. return rc;
  2313. }
  2314. /*
  2315. * CMD_RADIO_CONTROL.
  2316. */
  2317. struct mwl8k_cmd_radio_control {
  2318. struct mwl8k_cmd_pkt header;
  2319. __le16 action;
  2320. __le16 control;
  2321. __le16 radio_on;
  2322. } __packed;
  2323. static int
  2324. mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
  2325. {
  2326. struct mwl8k_priv *priv = hw->priv;
  2327. struct mwl8k_cmd_radio_control *cmd;
  2328. int rc;
  2329. if (enable == priv->radio_on && !force)
  2330. return 0;
  2331. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2332. if (cmd == NULL)
  2333. return -ENOMEM;
  2334. cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
  2335. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2336. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2337. cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
  2338. cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
  2339. rc = mwl8k_post_cmd(hw, &cmd->header);
  2340. kfree(cmd);
  2341. if (!rc)
  2342. priv->radio_on = enable;
  2343. return rc;
  2344. }
  2345. static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
  2346. {
  2347. return mwl8k_cmd_radio_control(hw, 0, 0);
  2348. }
  2349. static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
  2350. {
  2351. return mwl8k_cmd_radio_control(hw, 1, 0);
  2352. }
  2353. static int
  2354. mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
  2355. {
  2356. struct mwl8k_priv *priv = hw->priv;
  2357. priv->radio_short_preamble = short_preamble;
  2358. return mwl8k_cmd_radio_control(hw, 1, 1);
  2359. }
  2360. /*
  2361. * CMD_RF_TX_POWER.
  2362. */
  2363. #define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
  2364. struct mwl8k_cmd_rf_tx_power {
  2365. struct mwl8k_cmd_pkt header;
  2366. __le16 action;
  2367. __le16 support_level;
  2368. __le16 current_level;
  2369. __le16 reserved;
  2370. __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
  2371. } __packed;
  2372. static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
  2373. {
  2374. struct mwl8k_cmd_rf_tx_power *cmd;
  2375. int rc;
  2376. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2377. if (cmd == NULL)
  2378. return -ENOMEM;
  2379. cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
  2380. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2381. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2382. cmd->support_level = cpu_to_le16(dBm);
  2383. rc = mwl8k_post_cmd(hw, &cmd->header);
  2384. kfree(cmd);
  2385. return rc;
  2386. }
  2387. /*
  2388. * CMD_TX_POWER.
  2389. */
  2390. #define MWL8K_TX_POWER_LEVEL_TOTAL 12
  2391. struct mwl8k_cmd_tx_power {
  2392. struct mwl8k_cmd_pkt header;
  2393. __le16 action;
  2394. __le16 band;
  2395. __le16 channel;
  2396. __le16 bw;
  2397. __le16 sub_ch;
  2398. __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
  2399. } __packed;
  2400. static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
  2401. struct ieee80211_conf *conf,
  2402. unsigned short pwr)
  2403. {
  2404. struct ieee80211_channel *channel = conf->chandef.chan;
  2405. enum nl80211_channel_type channel_type =
  2406. cfg80211_get_chandef_type(&conf->chandef);
  2407. struct mwl8k_cmd_tx_power *cmd;
  2408. int rc;
  2409. int i;
  2410. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2411. if (cmd == NULL)
  2412. return -ENOMEM;
  2413. cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
  2414. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2415. cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
  2416. if (channel->band == NL80211_BAND_2GHZ)
  2417. cmd->band = cpu_to_le16(0x1);
  2418. else if (channel->band == NL80211_BAND_5GHZ)
  2419. cmd->band = cpu_to_le16(0x4);
  2420. cmd->channel = cpu_to_le16(channel->hw_value);
  2421. if (channel_type == NL80211_CHAN_NO_HT ||
  2422. channel_type == NL80211_CHAN_HT20) {
  2423. cmd->bw = cpu_to_le16(0x2);
  2424. } else {
  2425. cmd->bw = cpu_to_le16(0x4);
  2426. if (channel_type == NL80211_CHAN_HT40MINUS)
  2427. cmd->sub_ch = cpu_to_le16(0x3);
  2428. else if (channel_type == NL80211_CHAN_HT40PLUS)
  2429. cmd->sub_ch = cpu_to_le16(0x1);
  2430. }
  2431. for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
  2432. cmd->power_level_list[i] = cpu_to_le16(pwr);
  2433. rc = mwl8k_post_cmd(hw, &cmd->header);
  2434. kfree(cmd);
  2435. return rc;
  2436. }
  2437. /*
  2438. * CMD_RF_ANTENNA.
  2439. */
  2440. struct mwl8k_cmd_rf_antenna {
  2441. struct mwl8k_cmd_pkt header;
  2442. __le16 antenna;
  2443. __le16 mode;
  2444. } __packed;
  2445. #define MWL8K_RF_ANTENNA_RX 1
  2446. #define MWL8K_RF_ANTENNA_TX 2
  2447. static int
  2448. mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
  2449. {
  2450. struct mwl8k_cmd_rf_antenna *cmd;
  2451. int rc;
  2452. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2453. if (cmd == NULL)
  2454. return -ENOMEM;
  2455. cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
  2456. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2457. cmd->antenna = cpu_to_le16(antenna);
  2458. cmd->mode = cpu_to_le16(mask);
  2459. rc = mwl8k_post_cmd(hw, &cmd->header);
  2460. kfree(cmd);
  2461. return rc;
  2462. }
  2463. /*
  2464. * CMD_SET_BEACON.
  2465. */
  2466. struct mwl8k_cmd_set_beacon {
  2467. struct mwl8k_cmd_pkt header;
  2468. __le16 beacon_len;
  2469. __u8 beacon[0];
  2470. };
  2471. static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
  2472. struct ieee80211_vif *vif, u8 *beacon, int len)
  2473. {
  2474. struct mwl8k_cmd_set_beacon *cmd;
  2475. int rc;
  2476. cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
  2477. if (cmd == NULL)
  2478. return -ENOMEM;
  2479. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
  2480. cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
  2481. cmd->beacon_len = cpu_to_le16(len);
  2482. memcpy(cmd->beacon, beacon, len);
  2483. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  2484. kfree(cmd);
  2485. return rc;
  2486. }
  2487. /*
  2488. * CMD_SET_PRE_SCAN.
  2489. */
  2490. struct mwl8k_cmd_set_pre_scan {
  2491. struct mwl8k_cmd_pkt header;
  2492. } __packed;
  2493. static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
  2494. {
  2495. struct mwl8k_cmd_set_pre_scan *cmd;
  2496. int rc;
  2497. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2498. if (cmd == NULL)
  2499. return -ENOMEM;
  2500. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
  2501. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2502. rc = mwl8k_post_cmd(hw, &cmd->header);
  2503. kfree(cmd);
  2504. return rc;
  2505. }
  2506. /*
  2507. * CMD_BBP_REG_ACCESS.
  2508. */
  2509. struct mwl8k_cmd_bbp_reg_access {
  2510. struct mwl8k_cmd_pkt header;
  2511. __le16 action;
  2512. __le16 offset;
  2513. u8 value;
  2514. u8 rsrv[3];
  2515. } __packed;
  2516. static int
  2517. mwl8k_cmd_bbp_reg_access(struct ieee80211_hw *hw,
  2518. u16 action,
  2519. u16 offset,
  2520. u8 *value)
  2521. {
  2522. struct mwl8k_cmd_bbp_reg_access *cmd;
  2523. int rc;
  2524. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2525. if (cmd == NULL)
  2526. return -ENOMEM;
  2527. cmd->header.code = cpu_to_le16(MWL8K_CMD_BBP_REG_ACCESS);
  2528. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2529. cmd->action = cpu_to_le16(action);
  2530. cmd->offset = cpu_to_le16(offset);
  2531. rc = mwl8k_post_cmd(hw, &cmd->header);
  2532. if (!rc)
  2533. *value = cmd->value;
  2534. else
  2535. *value = 0;
  2536. kfree(cmd);
  2537. return rc;
  2538. }
  2539. /*
  2540. * CMD_SET_POST_SCAN.
  2541. */
  2542. struct mwl8k_cmd_set_post_scan {
  2543. struct mwl8k_cmd_pkt header;
  2544. __le32 isibss;
  2545. __u8 bssid[ETH_ALEN];
  2546. } __packed;
  2547. static int
  2548. mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
  2549. {
  2550. struct mwl8k_cmd_set_post_scan *cmd;
  2551. int rc;
  2552. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2553. if (cmd == NULL)
  2554. return -ENOMEM;
  2555. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
  2556. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2557. cmd->isibss = 0;
  2558. memcpy(cmd->bssid, mac, ETH_ALEN);
  2559. rc = mwl8k_post_cmd(hw, &cmd->header);
  2560. kfree(cmd);
  2561. return rc;
  2562. }
  2563. static int freq_to_idx(struct mwl8k_priv *priv, int freq)
  2564. {
  2565. struct ieee80211_supported_band *sband;
  2566. int band, ch, idx = 0;
  2567. for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
  2568. sband = priv->hw->wiphy->bands[band];
  2569. if (!sband)
  2570. continue;
  2571. for (ch = 0; ch < sband->n_channels; ch++, idx++)
  2572. if (sband->channels[ch].center_freq == freq)
  2573. goto exit;
  2574. }
  2575. exit:
  2576. return idx;
  2577. }
  2578. static void mwl8k_update_survey(struct mwl8k_priv *priv,
  2579. struct ieee80211_channel *channel)
  2580. {
  2581. u32 cca_cnt, rx_rdy;
  2582. s8 nf = 0, idx;
  2583. struct survey_info *survey;
  2584. idx = freq_to_idx(priv, priv->acs_chan->center_freq);
  2585. if (idx >= MWL8K_NUM_CHANS) {
  2586. wiphy_err(priv->hw->wiphy, "Failed to update survey\n");
  2587. return;
  2588. }
  2589. survey = &priv->survey[idx];
  2590. cca_cnt = ioread32(priv->regs + NOK_CCA_CNT_REG);
  2591. cca_cnt /= 1000; /* uSecs to mSecs */
  2592. survey->time_busy = (u64) cca_cnt;
  2593. rx_rdy = ioread32(priv->regs + BBU_RXRDY_CNT_REG);
  2594. rx_rdy /= 1000; /* uSecs to mSecs */
  2595. survey->time_rx = (u64) rx_rdy;
  2596. priv->channel_time = jiffies - priv->channel_time;
  2597. survey->time = jiffies_to_msecs(priv->channel_time);
  2598. survey->channel = channel;
  2599. mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &nf);
  2600. /* Make sure sign is negative else ACS at hostapd fails */
  2601. survey->noise = nf * -1;
  2602. survey->filled = SURVEY_INFO_NOISE_DBM |
  2603. SURVEY_INFO_TIME |
  2604. SURVEY_INFO_TIME_BUSY |
  2605. SURVEY_INFO_TIME_RX;
  2606. }
  2607. /*
  2608. * CMD_SET_RF_CHANNEL.
  2609. */
  2610. struct mwl8k_cmd_set_rf_channel {
  2611. struct mwl8k_cmd_pkt header;
  2612. __le16 action;
  2613. __u8 current_channel;
  2614. __le32 channel_flags;
  2615. } __packed;
  2616. static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
  2617. struct ieee80211_conf *conf)
  2618. {
  2619. struct ieee80211_channel *channel = conf->chandef.chan;
  2620. enum nl80211_channel_type channel_type =
  2621. cfg80211_get_chandef_type(&conf->chandef);
  2622. struct mwl8k_cmd_set_rf_channel *cmd;
  2623. struct mwl8k_priv *priv = hw->priv;
  2624. int rc;
  2625. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2626. if (cmd == NULL)
  2627. return -ENOMEM;
  2628. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
  2629. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2630. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2631. cmd->current_channel = channel->hw_value;
  2632. if (channel->band == NL80211_BAND_2GHZ)
  2633. cmd->channel_flags |= cpu_to_le32(0x00000001);
  2634. else if (channel->band == NL80211_BAND_5GHZ)
  2635. cmd->channel_flags |= cpu_to_le32(0x00000004);
  2636. if (!priv->sw_scan_start) {
  2637. if (channel_type == NL80211_CHAN_NO_HT ||
  2638. channel_type == NL80211_CHAN_HT20)
  2639. cmd->channel_flags |= cpu_to_le32(0x00000080);
  2640. else if (channel_type == NL80211_CHAN_HT40MINUS)
  2641. cmd->channel_flags |= cpu_to_le32(0x000001900);
  2642. else if (channel_type == NL80211_CHAN_HT40PLUS)
  2643. cmd->channel_flags |= cpu_to_le32(0x000000900);
  2644. } else {
  2645. cmd->channel_flags |= cpu_to_le32(0x00000080);
  2646. }
  2647. if (priv->sw_scan_start) {
  2648. /* Store current channel stats
  2649. * before switching to newer one.
  2650. * This will be processed only for AP fw.
  2651. */
  2652. if (priv->channel_time != 0)
  2653. mwl8k_update_survey(priv, priv->acs_chan);
  2654. priv->channel_time = jiffies;
  2655. priv->acs_chan = channel;
  2656. }
  2657. rc = mwl8k_post_cmd(hw, &cmd->header);
  2658. kfree(cmd);
  2659. return rc;
  2660. }
  2661. /*
  2662. * CMD_SET_AID.
  2663. */
  2664. #define MWL8K_FRAME_PROT_DISABLED 0x00
  2665. #define MWL8K_FRAME_PROT_11G 0x07
  2666. #define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
  2667. #define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
  2668. struct mwl8k_cmd_update_set_aid {
  2669. struct mwl8k_cmd_pkt header;
  2670. __le16 aid;
  2671. /* AP's MAC address (BSSID) */
  2672. __u8 bssid[ETH_ALEN];
  2673. __le16 protection_mode;
  2674. __u8 supp_rates[14];
  2675. } __packed;
  2676. static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
  2677. {
  2678. int i;
  2679. int j;
  2680. /*
  2681. * Clear nonstandard rate 4.
  2682. */
  2683. mask &= 0x1fef;
  2684. for (i = 0, j = 0; i < 13; i++) {
  2685. if (mask & (1 << i))
  2686. rates[j++] = mwl8k_rates_24[i].hw_value;
  2687. }
  2688. }
  2689. static int
  2690. mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
  2691. struct ieee80211_vif *vif, u32 legacy_rate_mask)
  2692. {
  2693. struct mwl8k_cmd_update_set_aid *cmd;
  2694. u16 prot_mode;
  2695. int rc;
  2696. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2697. if (cmd == NULL)
  2698. return -ENOMEM;
  2699. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
  2700. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2701. cmd->aid = cpu_to_le16(vif->bss_conf.aid);
  2702. memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
  2703. if (vif->bss_conf.use_cts_prot) {
  2704. prot_mode = MWL8K_FRAME_PROT_11G;
  2705. } else {
  2706. switch (vif->bss_conf.ht_operation_mode &
  2707. IEEE80211_HT_OP_MODE_PROTECTION) {
  2708. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  2709. prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
  2710. break;
  2711. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  2712. prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
  2713. break;
  2714. default:
  2715. prot_mode = MWL8K_FRAME_PROT_DISABLED;
  2716. break;
  2717. }
  2718. }
  2719. cmd->protection_mode = cpu_to_le16(prot_mode);
  2720. legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
  2721. rc = mwl8k_post_cmd(hw, &cmd->header);
  2722. kfree(cmd);
  2723. return rc;
  2724. }
  2725. /*
  2726. * CMD_SET_RATE.
  2727. */
  2728. struct mwl8k_cmd_set_rate {
  2729. struct mwl8k_cmd_pkt header;
  2730. __u8 legacy_rates[14];
  2731. /* Bitmap for supported MCS codes. */
  2732. __u8 mcs_set[16];
  2733. __u8 reserved[16];
  2734. } __packed;
  2735. static int
  2736. mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2737. u32 legacy_rate_mask, u8 *mcs_rates)
  2738. {
  2739. struct mwl8k_cmd_set_rate *cmd;
  2740. int rc;
  2741. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2742. if (cmd == NULL)
  2743. return -ENOMEM;
  2744. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
  2745. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2746. legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
  2747. memcpy(cmd->mcs_set, mcs_rates, 16);
  2748. rc = mwl8k_post_cmd(hw, &cmd->header);
  2749. kfree(cmd);
  2750. return rc;
  2751. }
  2752. /*
  2753. * CMD_FINALIZE_JOIN.
  2754. */
  2755. #define MWL8K_FJ_BEACON_MAXLEN 128
  2756. struct mwl8k_cmd_finalize_join {
  2757. struct mwl8k_cmd_pkt header;
  2758. __le32 sleep_interval; /* Number of beacon periods to sleep */
  2759. __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
  2760. } __packed;
  2761. static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
  2762. int framelen, int dtim)
  2763. {
  2764. struct mwl8k_cmd_finalize_join *cmd;
  2765. struct ieee80211_mgmt *payload = frame;
  2766. int payload_len;
  2767. int rc;
  2768. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2769. if (cmd == NULL)
  2770. return -ENOMEM;
  2771. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
  2772. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2773. cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
  2774. payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
  2775. if (payload_len < 0)
  2776. payload_len = 0;
  2777. else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
  2778. payload_len = MWL8K_FJ_BEACON_MAXLEN;
  2779. memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
  2780. rc = mwl8k_post_cmd(hw, &cmd->header);
  2781. kfree(cmd);
  2782. return rc;
  2783. }
  2784. /*
  2785. * CMD_SET_RTS_THRESHOLD.
  2786. */
  2787. struct mwl8k_cmd_set_rts_threshold {
  2788. struct mwl8k_cmd_pkt header;
  2789. __le16 action;
  2790. __le16 threshold;
  2791. } __packed;
  2792. static int
  2793. mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
  2794. {
  2795. struct mwl8k_cmd_set_rts_threshold *cmd;
  2796. int rc;
  2797. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2798. if (cmd == NULL)
  2799. return -ENOMEM;
  2800. cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
  2801. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2802. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2803. cmd->threshold = cpu_to_le16(rts_thresh);
  2804. rc = mwl8k_post_cmd(hw, &cmd->header);
  2805. kfree(cmd);
  2806. return rc;
  2807. }
  2808. /*
  2809. * CMD_SET_SLOT.
  2810. */
  2811. struct mwl8k_cmd_set_slot {
  2812. struct mwl8k_cmd_pkt header;
  2813. __le16 action;
  2814. __u8 short_slot;
  2815. } __packed;
  2816. static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
  2817. {
  2818. struct mwl8k_cmd_set_slot *cmd;
  2819. int rc;
  2820. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2821. if (cmd == NULL)
  2822. return -ENOMEM;
  2823. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
  2824. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2825. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2826. cmd->short_slot = short_slot_time;
  2827. rc = mwl8k_post_cmd(hw, &cmd->header);
  2828. kfree(cmd);
  2829. return rc;
  2830. }
  2831. /*
  2832. * CMD_SET_EDCA_PARAMS.
  2833. */
  2834. struct mwl8k_cmd_set_edca_params {
  2835. struct mwl8k_cmd_pkt header;
  2836. /* See MWL8K_SET_EDCA_XXX below */
  2837. __le16 action;
  2838. /* TX opportunity in units of 32 us */
  2839. __le16 txop;
  2840. union {
  2841. struct {
  2842. /* Log exponent of max contention period: 0...15 */
  2843. __le32 log_cw_max;
  2844. /* Log exponent of min contention period: 0...15 */
  2845. __le32 log_cw_min;
  2846. /* Adaptive interframe spacing in units of 32us */
  2847. __u8 aifs;
  2848. /* TX queue to configure */
  2849. __u8 txq;
  2850. } ap;
  2851. struct {
  2852. /* Log exponent of max contention period: 0...15 */
  2853. __u8 log_cw_max;
  2854. /* Log exponent of min contention period: 0...15 */
  2855. __u8 log_cw_min;
  2856. /* Adaptive interframe spacing in units of 32us */
  2857. __u8 aifs;
  2858. /* TX queue to configure */
  2859. __u8 txq;
  2860. } sta;
  2861. };
  2862. } __packed;
  2863. #define MWL8K_SET_EDCA_CW 0x01
  2864. #define MWL8K_SET_EDCA_TXOP 0x02
  2865. #define MWL8K_SET_EDCA_AIFS 0x04
  2866. #define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
  2867. MWL8K_SET_EDCA_TXOP | \
  2868. MWL8K_SET_EDCA_AIFS)
  2869. static int
  2870. mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
  2871. __u16 cw_min, __u16 cw_max,
  2872. __u8 aifs, __u16 txop)
  2873. {
  2874. struct mwl8k_priv *priv = hw->priv;
  2875. struct mwl8k_cmd_set_edca_params *cmd;
  2876. int rc;
  2877. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2878. if (cmd == NULL)
  2879. return -ENOMEM;
  2880. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
  2881. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2882. cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
  2883. cmd->txop = cpu_to_le16(txop);
  2884. if (priv->ap_fw) {
  2885. cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
  2886. cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
  2887. cmd->ap.aifs = aifs;
  2888. cmd->ap.txq = qnum;
  2889. } else {
  2890. cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
  2891. cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
  2892. cmd->sta.aifs = aifs;
  2893. cmd->sta.txq = qnum;
  2894. }
  2895. rc = mwl8k_post_cmd(hw, &cmd->header);
  2896. kfree(cmd);
  2897. return rc;
  2898. }
  2899. /*
  2900. * CMD_SET_WMM_MODE.
  2901. */
  2902. struct mwl8k_cmd_set_wmm_mode {
  2903. struct mwl8k_cmd_pkt header;
  2904. __le16 action;
  2905. } __packed;
  2906. static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
  2907. {
  2908. struct mwl8k_priv *priv = hw->priv;
  2909. struct mwl8k_cmd_set_wmm_mode *cmd;
  2910. int rc;
  2911. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2912. if (cmd == NULL)
  2913. return -ENOMEM;
  2914. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
  2915. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2916. cmd->action = cpu_to_le16(!!enable);
  2917. rc = mwl8k_post_cmd(hw, &cmd->header);
  2918. kfree(cmd);
  2919. if (!rc)
  2920. priv->wmm_enabled = enable;
  2921. return rc;
  2922. }
  2923. /*
  2924. * CMD_MIMO_CONFIG.
  2925. */
  2926. struct mwl8k_cmd_mimo_config {
  2927. struct mwl8k_cmd_pkt header;
  2928. __le32 action;
  2929. __u8 rx_antenna_map;
  2930. __u8 tx_antenna_map;
  2931. } __packed;
  2932. static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
  2933. {
  2934. struct mwl8k_cmd_mimo_config *cmd;
  2935. int rc;
  2936. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2937. if (cmd == NULL)
  2938. return -ENOMEM;
  2939. cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
  2940. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2941. cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
  2942. cmd->rx_antenna_map = rx;
  2943. cmd->tx_antenna_map = tx;
  2944. rc = mwl8k_post_cmd(hw, &cmd->header);
  2945. kfree(cmd);
  2946. return rc;
  2947. }
  2948. /*
  2949. * CMD_USE_FIXED_RATE (STA version).
  2950. */
  2951. struct mwl8k_cmd_use_fixed_rate_sta {
  2952. struct mwl8k_cmd_pkt header;
  2953. __le32 action;
  2954. __le32 allow_rate_drop;
  2955. __le32 num_rates;
  2956. struct {
  2957. __le32 is_ht_rate;
  2958. __le32 enable_retry;
  2959. __le32 rate;
  2960. __le32 retry_count;
  2961. } rate_entry[8];
  2962. __le32 rate_type;
  2963. __le32 reserved1;
  2964. __le32 reserved2;
  2965. } __packed;
  2966. #define MWL8K_USE_AUTO_RATE 0x0002
  2967. #define MWL8K_UCAST_RATE 0
  2968. static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
  2969. {
  2970. struct mwl8k_cmd_use_fixed_rate_sta *cmd;
  2971. int rc;
  2972. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2973. if (cmd == NULL)
  2974. return -ENOMEM;
  2975. cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
  2976. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2977. cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
  2978. cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
  2979. rc = mwl8k_post_cmd(hw, &cmd->header);
  2980. kfree(cmd);
  2981. return rc;
  2982. }
  2983. /*
  2984. * CMD_USE_FIXED_RATE (AP version).
  2985. */
  2986. struct mwl8k_cmd_use_fixed_rate_ap {
  2987. struct mwl8k_cmd_pkt header;
  2988. __le32 action;
  2989. __le32 allow_rate_drop;
  2990. __le32 num_rates;
  2991. struct mwl8k_rate_entry_ap {
  2992. __le32 is_ht_rate;
  2993. __le32 enable_retry;
  2994. __le32 rate;
  2995. __le32 retry_count;
  2996. } rate_entry[4];
  2997. u8 multicast_rate;
  2998. u8 multicast_rate_type;
  2999. u8 management_rate;
  3000. } __packed;
  3001. static int
  3002. mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
  3003. {
  3004. struct mwl8k_cmd_use_fixed_rate_ap *cmd;
  3005. int rc;
  3006. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3007. if (cmd == NULL)
  3008. return -ENOMEM;
  3009. cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
  3010. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3011. cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
  3012. cmd->multicast_rate = mcast;
  3013. cmd->management_rate = mgmt;
  3014. rc = mwl8k_post_cmd(hw, &cmd->header);
  3015. kfree(cmd);
  3016. return rc;
  3017. }
  3018. /*
  3019. * CMD_ENABLE_SNIFFER.
  3020. */
  3021. struct mwl8k_cmd_enable_sniffer {
  3022. struct mwl8k_cmd_pkt header;
  3023. __le32 action;
  3024. } __packed;
  3025. static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
  3026. {
  3027. struct mwl8k_cmd_enable_sniffer *cmd;
  3028. int rc;
  3029. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3030. if (cmd == NULL)
  3031. return -ENOMEM;
  3032. cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
  3033. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3034. cmd->action = cpu_to_le32(!!enable);
  3035. rc = mwl8k_post_cmd(hw, &cmd->header);
  3036. kfree(cmd);
  3037. return rc;
  3038. }
  3039. struct mwl8k_cmd_update_mac_addr {
  3040. struct mwl8k_cmd_pkt header;
  3041. union {
  3042. struct {
  3043. __le16 mac_type;
  3044. __u8 mac_addr[ETH_ALEN];
  3045. } mbss;
  3046. __u8 mac_addr[ETH_ALEN];
  3047. };
  3048. } __packed;
  3049. #define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
  3050. #define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
  3051. #define MWL8K_MAC_TYPE_PRIMARY_AP 2
  3052. #define MWL8K_MAC_TYPE_SECONDARY_AP 3
  3053. static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
  3054. struct ieee80211_vif *vif, u8 *mac, bool set)
  3055. {
  3056. struct mwl8k_priv *priv = hw->priv;
  3057. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3058. struct mwl8k_cmd_update_mac_addr *cmd;
  3059. int mac_type;
  3060. int rc;
  3061. mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
  3062. if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
  3063. if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
  3064. if (priv->ap_fw)
  3065. mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
  3066. else
  3067. mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
  3068. else
  3069. mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
  3070. } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
  3071. if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
  3072. mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
  3073. else
  3074. mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
  3075. }
  3076. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3077. if (cmd == NULL)
  3078. return -ENOMEM;
  3079. if (set)
  3080. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
  3081. else
  3082. cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
  3083. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3084. if (priv->ap_fw) {
  3085. cmd->mbss.mac_type = cpu_to_le16(mac_type);
  3086. memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
  3087. } else {
  3088. memcpy(cmd->mac_addr, mac, ETH_ALEN);
  3089. }
  3090. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3091. kfree(cmd);
  3092. return rc;
  3093. }
  3094. /*
  3095. * MWL8K_CMD_SET_MAC_ADDR.
  3096. */
  3097. static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
  3098. struct ieee80211_vif *vif, u8 *mac)
  3099. {
  3100. return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
  3101. }
  3102. /*
  3103. * MWL8K_CMD_DEL_MAC_ADDR.
  3104. */
  3105. static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
  3106. struct ieee80211_vif *vif, u8 *mac)
  3107. {
  3108. return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
  3109. }
  3110. /*
  3111. * CMD_SET_RATEADAPT_MODE.
  3112. */
  3113. struct mwl8k_cmd_set_rate_adapt_mode {
  3114. struct mwl8k_cmd_pkt header;
  3115. __le16 action;
  3116. __le16 mode;
  3117. } __packed;
  3118. static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
  3119. {
  3120. struct mwl8k_cmd_set_rate_adapt_mode *cmd;
  3121. int rc;
  3122. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3123. if (cmd == NULL)
  3124. return -ENOMEM;
  3125. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
  3126. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3127. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  3128. cmd->mode = cpu_to_le16(mode);
  3129. rc = mwl8k_post_cmd(hw, &cmd->header);
  3130. kfree(cmd);
  3131. return rc;
  3132. }
  3133. /*
  3134. * CMD_GET_WATCHDOG_BITMAP.
  3135. */
  3136. struct mwl8k_cmd_get_watchdog_bitmap {
  3137. struct mwl8k_cmd_pkt header;
  3138. u8 bitmap;
  3139. } __packed;
  3140. static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
  3141. {
  3142. struct mwl8k_cmd_get_watchdog_bitmap *cmd;
  3143. int rc;
  3144. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3145. if (cmd == NULL)
  3146. return -ENOMEM;
  3147. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
  3148. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3149. rc = mwl8k_post_cmd(hw, &cmd->header);
  3150. if (!rc)
  3151. *bitmap = cmd->bitmap;
  3152. kfree(cmd);
  3153. return rc;
  3154. }
  3155. #define MWL8K_WMM_QUEUE_NUMBER 3
  3156. static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
  3157. u8 idx);
  3158. static void mwl8k_watchdog_ba_events(struct work_struct *work)
  3159. {
  3160. int rc;
  3161. u8 bitmap = 0, stream_index;
  3162. struct mwl8k_ampdu_stream *streams;
  3163. struct mwl8k_priv *priv =
  3164. container_of(work, struct mwl8k_priv, watchdog_ba_handle);
  3165. struct ieee80211_hw *hw = priv->hw;
  3166. int i;
  3167. u32 status = 0;
  3168. mwl8k_fw_lock(hw);
  3169. rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
  3170. if (rc)
  3171. goto done;
  3172. spin_lock(&priv->stream_lock);
  3173. /* the bitmap is the hw queue number. Map it to the ampdu queue. */
  3174. for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
  3175. if (bitmap & (1 << i)) {
  3176. stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
  3177. TOTAL_HW_TX_QUEUES;
  3178. streams = &priv->ampdu[stream_index];
  3179. if (streams->state == AMPDU_STREAM_ACTIVE) {
  3180. ieee80211_stop_tx_ba_session(streams->sta,
  3181. streams->tid);
  3182. spin_unlock(&priv->stream_lock);
  3183. mwl8k_destroy_ba(hw, stream_index);
  3184. spin_lock(&priv->stream_lock);
  3185. }
  3186. }
  3187. }
  3188. spin_unlock(&priv->stream_lock);
  3189. done:
  3190. atomic_dec(&priv->watchdog_event_pending);
  3191. status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3192. iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
  3193. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3194. mwl8k_fw_unlock(hw);
  3195. return;
  3196. }
  3197. /*
  3198. * CMD_BSS_START.
  3199. */
  3200. struct mwl8k_cmd_bss_start {
  3201. struct mwl8k_cmd_pkt header;
  3202. __le32 enable;
  3203. } __packed;
  3204. static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
  3205. struct ieee80211_vif *vif, int enable)
  3206. {
  3207. struct mwl8k_cmd_bss_start *cmd;
  3208. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3209. struct mwl8k_priv *priv = hw->priv;
  3210. int rc;
  3211. if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
  3212. return 0;
  3213. if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
  3214. return 0;
  3215. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3216. if (cmd == NULL)
  3217. return -ENOMEM;
  3218. cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
  3219. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3220. cmd->enable = cpu_to_le32(enable);
  3221. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3222. kfree(cmd);
  3223. if (!rc) {
  3224. if (enable)
  3225. priv->running_bsses |= (1 << mwl8k_vif->macid);
  3226. else
  3227. priv->running_bsses &= ~(1 << mwl8k_vif->macid);
  3228. }
  3229. return rc;
  3230. }
  3231. static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
  3232. {
  3233. struct mwl8k_priv *priv = hw->priv;
  3234. struct mwl8k_vif *mwl8k_vif, *tmp_vif;
  3235. struct ieee80211_vif *vif;
  3236. list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
  3237. vif = mwl8k_vif->vif;
  3238. if (!(bitmap & (1 << mwl8k_vif->macid)))
  3239. continue;
  3240. if (vif->type == NL80211_IFTYPE_AP)
  3241. mwl8k_cmd_bss_start(hw, vif, enable);
  3242. }
  3243. }
  3244. /*
  3245. * CMD_BASTREAM.
  3246. */
  3247. /*
  3248. * UPSTREAM is tx direction
  3249. */
  3250. #define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
  3251. #define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
  3252. enum ba_stream_action_type {
  3253. MWL8K_BA_CREATE,
  3254. MWL8K_BA_UPDATE,
  3255. MWL8K_BA_DESTROY,
  3256. MWL8K_BA_FLUSH,
  3257. MWL8K_BA_CHECK,
  3258. };
  3259. struct mwl8k_create_ba_stream {
  3260. __le32 flags;
  3261. __le32 idle_thrs;
  3262. __le32 bar_thrs;
  3263. __le32 window_size;
  3264. u8 peer_mac_addr[6];
  3265. u8 dialog_token;
  3266. u8 tid;
  3267. u8 queue_id;
  3268. u8 param_info;
  3269. __le32 ba_context;
  3270. u8 reset_seq_no_flag;
  3271. __le16 curr_seq_no;
  3272. u8 sta_src_mac_addr[6];
  3273. } __packed;
  3274. struct mwl8k_destroy_ba_stream {
  3275. __le32 flags;
  3276. __le32 ba_context;
  3277. } __packed;
  3278. struct mwl8k_cmd_bastream {
  3279. struct mwl8k_cmd_pkt header;
  3280. __le32 action;
  3281. union {
  3282. struct mwl8k_create_ba_stream create_params;
  3283. struct mwl8k_destroy_ba_stream destroy_params;
  3284. };
  3285. } __packed;
  3286. static int
  3287. mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
  3288. struct ieee80211_vif *vif)
  3289. {
  3290. struct mwl8k_cmd_bastream *cmd;
  3291. int rc;
  3292. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3293. if (cmd == NULL)
  3294. return -ENOMEM;
  3295. cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
  3296. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3297. cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
  3298. cmd->create_params.queue_id = stream->idx;
  3299. memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
  3300. ETH_ALEN);
  3301. cmd->create_params.tid = stream->tid;
  3302. cmd->create_params.flags =
  3303. cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
  3304. cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
  3305. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3306. kfree(cmd);
  3307. return rc;
  3308. }
  3309. static int
  3310. mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
  3311. u8 buf_size, struct ieee80211_vif *vif)
  3312. {
  3313. struct mwl8k_cmd_bastream *cmd;
  3314. int rc;
  3315. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3316. if (cmd == NULL)
  3317. return -ENOMEM;
  3318. cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
  3319. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3320. cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
  3321. cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
  3322. cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
  3323. cmd->create_params.queue_id = stream->idx;
  3324. memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
  3325. cmd->create_params.tid = stream->tid;
  3326. cmd->create_params.curr_seq_no = cpu_to_le16(0);
  3327. cmd->create_params.reset_seq_no_flag = 1;
  3328. cmd->create_params.param_info =
  3329. (stream->sta->ht_cap.ampdu_factor &
  3330. IEEE80211_HT_AMPDU_PARM_FACTOR) |
  3331. ((stream->sta->ht_cap.ampdu_density << 2) &
  3332. IEEE80211_HT_AMPDU_PARM_DENSITY);
  3333. cmd->create_params.flags =
  3334. cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
  3335. BASTREAM_FLAG_DIRECTION_UPSTREAM);
  3336. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3337. wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
  3338. stream->sta->addr, stream->tid);
  3339. kfree(cmd);
  3340. return rc;
  3341. }
  3342. static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
  3343. u8 idx)
  3344. {
  3345. struct mwl8k_cmd_bastream *cmd;
  3346. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3347. if (cmd == NULL)
  3348. return;
  3349. cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
  3350. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3351. cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
  3352. cmd->destroy_params.ba_context = cpu_to_le32(idx);
  3353. mwl8k_post_cmd(hw, &cmd->header);
  3354. wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
  3355. kfree(cmd);
  3356. }
  3357. /*
  3358. * CMD_SET_NEW_STN.
  3359. */
  3360. struct mwl8k_cmd_set_new_stn {
  3361. struct mwl8k_cmd_pkt header;
  3362. __le16 aid;
  3363. __u8 mac_addr[6];
  3364. __le16 stn_id;
  3365. __le16 action;
  3366. __le16 rsvd;
  3367. __le32 legacy_rates;
  3368. __u8 ht_rates[4];
  3369. __le16 cap_info;
  3370. __le16 ht_capabilities_info;
  3371. __u8 mac_ht_param_info;
  3372. __u8 rev;
  3373. __u8 control_channel;
  3374. __u8 add_channel;
  3375. __le16 op_mode;
  3376. __le16 stbc;
  3377. __u8 add_qos_info;
  3378. __u8 is_qos_sta;
  3379. __le32 fw_sta_ptr;
  3380. } __packed;
  3381. #define MWL8K_STA_ACTION_ADD 0
  3382. #define MWL8K_STA_ACTION_REMOVE 2
  3383. static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
  3384. struct ieee80211_vif *vif,
  3385. struct ieee80211_sta *sta)
  3386. {
  3387. struct mwl8k_cmd_set_new_stn *cmd;
  3388. u32 rates;
  3389. int rc;
  3390. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3391. if (cmd == NULL)
  3392. return -ENOMEM;
  3393. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
  3394. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3395. cmd->aid = cpu_to_le16(sta->aid);
  3396. memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
  3397. cmd->stn_id = cpu_to_le16(sta->aid);
  3398. cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
  3399. if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
  3400. rates = sta->supp_rates[NL80211_BAND_2GHZ];
  3401. else
  3402. rates = sta->supp_rates[NL80211_BAND_5GHZ] << 5;
  3403. cmd->legacy_rates = cpu_to_le32(rates);
  3404. if (sta->ht_cap.ht_supported) {
  3405. cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
  3406. cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
  3407. cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
  3408. cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
  3409. cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
  3410. cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
  3411. ((sta->ht_cap.ampdu_density & 7) << 2);
  3412. cmd->is_qos_sta = 1;
  3413. }
  3414. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3415. kfree(cmd);
  3416. return rc;
  3417. }
  3418. static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
  3419. struct ieee80211_vif *vif)
  3420. {
  3421. struct mwl8k_cmd_set_new_stn *cmd;
  3422. int rc;
  3423. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3424. if (cmd == NULL)
  3425. return -ENOMEM;
  3426. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
  3427. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3428. memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
  3429. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3430. kfree(cmd);
  3431. return rc;
  3432. }
  3433. static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
  3434. struct ieee80211_vif *vif, u8 *addr)
  3435. {
  3436. struct mwl8k_cmd_set_new_stn *cmd;
  3437. struct mwl8k_priv *priv = hw->priv;
  3438. int rc, i;
  3439. u8 idx;
  3440. spin_lock(&priv->stream_lock);
  3441. /* Destroy any active ampdu streams for this sta */
  3442. for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
  3443. struct mwl8k_ampdu_stream *s;
  3444. s = &priv->ampdu[i];
  3445. if (s->state != AMPDU_NO_STREAM) {
  3446. if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
  3447. if (s->state == AMPDU_STREAM_ACTIVE) {
  3448. idx = s->idx;
  3449. spin_unlock(&priv->stream_lock);
  3450. mwl8k_destroy_ba(hw, idx);
  3451. spin_lock(&priv->stream_lock);
  3452. } else if (s->state == AMPDU_STREAM_NEW) {
  3453. mwl8k_remove_stream(hw, s);
  3454. }
  3455. }
  3456. }
  3457. }
  3458. spin_unlock(&priv->stream_lock);
  3459. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3460. if (cmd == NULL)
  3461. return -ENOMEM;
  3462. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
  3463. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3464. memcpy(cmd->mac_addr, addr, ETH_ALEN);
  3465. cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
  3466. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3467. kfree(cmd);
  3468. return rc;
  3469. }
  3470. /*
  3471. * CMD_UPDATE_ENCRYPTION.
  3472. */
  3473. #define MAX_ENCR_KEY_LENGTH 16
  3474. #define MIC_KEY_LENGTH 8
  3475. struct mwl8k_cmd_update_encryption {
  3476. struct mwl8k_cmd_pkt header;
  3477. __le32 action;
  3478. __le32 reserved;
  3479. __u8 mac_addr[6];
  3480. __u8 encr_type;
  3481. } __packed;
  3482. struct mwl8k_cmd_set_key {
  3483. struct mwl8k_cmd_pkt header;
  3484. __le32 action;
  3485. __le32 reserved;
  3486. __le16 length;
  3487. __le16 key_type_id;
  3488. __le32 key_info;
  3489. __le32 key_id;
  3490. __le16 key_len;
  3491. __u8 key_material[MAX_ENCR_KEY_LENGTH];
  3492. __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
  3493. __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
  3494. __le16 tkip_rsc_low;
  3495. __le32 tkip_rsc_high;
  3496. __le16 tkip_tsc_low;
  3497. __le32 tkip_tsc_high;
  3498. __u8 mac_addr[6];
  3499. } __packed;
  3500. enum {
  3501. MWL8K_ENCR_ENABLE,
  3502. MWL8K_ENCR_SET_KEY,
  3503. MWL8K_ENCR_REMOVE_KEY,
  3504. MWL8K_ENCR_SET_GROUP_KEY,
  3505. };
  3506. #define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
  3507. #define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
  3508. #define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
  3509. #define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
  3510. #define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
  3511. enum {
  3512. MWL8K_ALG_WEP,
  3513. MWL8K_ALG_TKIP,
  3514. MWL8K_ALG_CCMP,
  3515. };
  3516. #define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
  3517. #define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
  3518. #define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
  3519. #define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
  3520. #define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
  3521. static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
  3522. struct ieee80211_vif *vif,
  3523. u8 *addr,
  3524. u8 encr_type)
  3525. {
  3526. struct mwl8k_cmd_update_encryption *cmd;
  3527. int rc;
  3528. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3529. if (cmd == NULL)
  3530. return -ENOMEM;
  3531. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
  3532. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3533. cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
  3534. memcpy(cmd->mac_addr, addr, ETH_ALEN);
  3535. cmd->encr_type = encr_type;
  3536. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3537. kfree(cmd);
  3538. return rc;
  3539. }
  3540. static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
  3541. u8 *addr,
  3542. struct ieee80211_key_conf *key)
  3543. {
  3544. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
  3545. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3546. cmd->length = cpu_to_le16(sizeof(*cmd) -
  3547. offsetof(struct mwl8k_cmd_set_key, length));
  3548. cmd->key_id = cpu_to_le32(key->keyidx);
  3549. cmd->key_len = cpu_to_le16(key->keylen);
  3550. memcpy(cmd->mac_addr, addr, ETH_ALEN);
  3551. switch (key->cipher) {
  3552. case WLAN_CIPHER_SUITE_WEP40:
  3553. case WLAN_CIPHER_SUITE_WEP104:
  3554. cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
  3555. if (key->keyidx == 0)
  3556. cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
  3557. break;
  3558. case WLAN_CIPHER_SUITE_TKIP:
  3559. cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
  3560. cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  3561. ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
  3562. : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
  3563. cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
  3564. | MWL8K_KEY_FLAG_TSC_VALID);
  3565. break;
  3566. case WLAN_CIPHER_SUITE_CCMP:
  3567. cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
  3568. cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  3569. ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
  3570. : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
  3571. break;
  3572. default:
  3573. return -ENOTSUPP;
  3574. }
  3575. return 0;
  3576. }
  3577. static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
  3578. struct ieee80211_vif *vif,
  3579. u8 *addr,
  3580. struct ieee80211_key_conf *key)
  3581. {
  3582. struct mwl8k_cmd_set_key *cmd;
  3583. int rc;
  3584. int keymlen;
  3585. u32 action;
  3586. u8 idx;
  3587. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3588. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3589. if (cmd == NULL)
  3590. return -ENOMEM;
  3591. rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
  3592. if (rc < 0)
  3593. goto done;
  3594. idx = key->keyidx;
  3595. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  3596. action = MWL8K_ENCR_SET_KEY;
  3597. else
  3598. action = MWL8K_ENCR_SET_GROUP_KEY;
  3599. switch (key->cipher) {
  3600. case WLAN_CIPHER_SUITE_WEP40:
  3601. case WLAN_CIPHER_SUITE_WEP104:
  3602. if (!mwl8k_vif->wep_key_conf[idx].enabled) {
  3603. memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
  3604. sizeof(*key) + key->keylen);
  3605. mwl8k_vif->wep_key_conf[idx].enabled = 1;
  3606. }
  3607. keymlen = key->keylen;
  3608. action = MWL8K_ENCR_SET_KEY;
  3609. break;
  3610. case WLAN_CIPHER_SUITE_TKIP:
  3611. keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
  3612. break;
  3613. case WLAN_CIPHER_SUITE_CCMP:
  3614. keymlen = key->keylen;
  3615. break;
  3616. default:
  3617. rc = -ENOTSUPP;
  3618. goto done;
  3619. }
  3620. memcpy(cmd->key_material, key->key, keymlen);
  3621. cmd->action = cpu_to_le32(action);
  3622. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3623. done:
  3624. kfree(cmd);
  3625. return rc;
  3626. }
  3627. static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
  3628. struct ieee80211_vif *vif,
  3629. u8 *addr,
  3630. struct ieee80211_key_conf *key)
  3631. {
  3632. struct mwl8k_cmd_set_key *cmd;
  3633. int rc;
  3634. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3635. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3636. if (cmd == NULL)
  3637. return -ENOMEM;
  3638. rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
  3639. if (rc < 0)
  3640. goto done;
  3641. if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  3642. key->cipher == WLAN_CIPHER_SUITE_WEP104)
  3643. mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
  3644. cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
  3645. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3646. done:
  3647. kfree(cmd);
  3648. return rc;
  3649. }
  3650. static int mwl8k_set_key(struct ieee80211_hw *hw,
  3651. enum set_key_cmd cmd_param,
  3652. struct ieee80211_vif *vif,
  3653. struct ieee80211_sta *sta,
  3654. struct ieee80211_key_conf *key)
  3655. {
  3656. int rc = 0;
  3657. u8 encr_type;
  3658. u8 *addr;
  3659. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3660. struct mwl8k_priv *priv = hw->priv;
  3661. if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
  3662. return -EOPNOTSUPP;
  3663. if (sta == NULL)
  3664. addr = vif->addr;
  3665. else
  3666. addr = sta->addr;
  3667. if (cmd_param == SET_KEY) {
  3668. rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
  3669. if (rc)
  3670. goto out;
  3671. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
  3672. || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
  3673. encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
  3674. else
  3675. encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
  3676. rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
  3677. encr_type);
  3678. if (rc)
  3679. goto out;
  3680. mwl8k_vif->is_hw_crypto_enabled = true;
  3681. } else {
  3682. rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
  3683. if (rc)
  3684. goto out;
  3685. }
  3686. out:
  3687. return rc;
  3688. }
  3689. /*
  3690. * CMD_UPDATE_STADB.
  3691. */
  3692. struct ewc_ht_info {
  3693. __le16 control1;
  3694. __le16 control2;
  3695. __le16 control3;
  3696. } __packed;
  3697. struct peer_capability_info {
  3698. /* Peer type - AP vs. STA. */
  3699. __u8 peer_type;
  3700. /* Basic 802.11 capabilities from assoc resp. */
  3701. __le16 basic_caps;
  3702. /* Set if peer supports 802.11n high throughput (HT). */
  3703. __u8 ht_support;
  3704. /* Valid if HT is supported. */
  3705. __le16 ht_caps;
  3706. __u8 extended_ht_caps;
  3707. struct ewc_ht_info ewc_info;
  3708. /* Legacy rate table. Intersection of our rates and peer rates. */
  3709. __u8 legacy_rates[12];
  3710. /* HT rate table. Intersection of our rates and peer rates. */
  3711. __u8 ht_rates[16];
  3712. __u8 pad[16];
  3713. /* If set, interoperability mode, no proprietary extensions. */
  3714. __u8 interop;
  3715. __u8 pad2;
  3716. __u8 station_id;
  3717. __le16 amsdu_enabled;
  3718. } __packed;
  3719. struct mwl8k_cmd_update_stadb {
  3720. struct mwl8k_cmd_pkt header;
  3721. /* See STADB_ACTION_TYPE */
  3722. __le32 action;
  3723. /* Peer MAC address */
  3724. __u8 peer_addr[ETH_ALEN];
  3725. __le32 reserved;
  3726. /* Peer info - valid during add/update. */
  3727. struct peer_capability_info peer_info;
  3728. } __packed;
  3729. #define MWL8K_STA_DB_MODIFY_ENTRY 1
  3730. #define MWL8K_STA_DB_DEL_ENTRY 2
  3731. /* Peer Entry flags - used to define the type of the peer node */
  3732. #define MWL8K_PEER_TYPE_ACCESSPOINT 2
  3733. static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
  3734. struct ieee80211_vif *vif,
  3735. struct ieee80211_sta *sta)
  3736. {
  3737. struct mwl8k_cmd_update_stadb *cmd;
  3738. struct peer_capability_info *p;
  3739. u32 rates;
  3740. int rc;
  3741. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3742. if (cmd == NULL)
  3743. return -ENOMEM;
  3744. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
  3745. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3746. cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
  3747. memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
  3748. p = &cmd->peer_info;
  3749. p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
  3750. p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
  3751. p->ht_support = sta->ht_cap.ht_supported;
  3752. p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
  3753. p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
  3754. ((sta->ht_cap.ampdu_density & 7) << 2);
  3755. if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
  3756. rates = sta->supp_rates[NL80211_BAND_2GHZ];
  3757. else
  3758. rates = sta->supp_rates[NL80211_BAND_5GHZ] << 5;
  3759. legacy_rate_mask_to_array(p->legacy_rates, rates);
  3760. memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
  3761. p->interop = 1;
  3762. p->amsdu_enabled = 0;
  3763. rc = mwl8k_post_cmd(hw, &cmd->header);
  3764. if (!rc)
  3765. rc = p->station_id;
  3766. kfree(cmd);
  3767. return rc;
  3768. }
  3769. static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
  3770. struct ieee80211_vif *vif, u8 *addr)
  3771. {
  3772. struct mwl8k_cmd_update_stadb *cmd;
  3773. int rc;
  3774. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3775. if (cmd == NULL)
  3776. return -ENOMEM;
  3777. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
  3778. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3779. cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
  3780. memcpy(cmd->peer_addr, addr, ETH_ALEN);
  3781. rc = mwl8k_post_cmd(hw, &cmd->header);
  3782. kfree(cmd);
  3783. return rc;
  3784. }
  3785. /*
  3786. * Interrupt handling.
  3787. */
  3788. static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
  3789. {
  3790. struct ieee80211_hw *hw = dev_id;
  3791. struct mwl8k_priv *priv = hw->priv;
  3792. u32 status;
  3793. status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3794. if (!status)
  3795. return IRQ_NONE;
  3796. if (status & MWL8K_A2H_INT_TX_DONE) {
  3797. status &= ~MWL8K_A2H_INT_TX_DONE;
  3798. tasklet_schedule(&priv->poll_tx_task);
  3799. }
  3800. if (status & MWL8K_A2H_INT_RX_READY) {
  3801. status &= ~MWL8K_A2H_INT_RX_READY;
  3802. tasklet_schedule(&priv->poll_rx_task);
  3803. }
  3804. if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
  3805. iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
  3806. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3807. atomic_inc(&priv->watchdog_event_pending);
  3808. status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
  3809. ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
  3810. }
  3811. if (status)
  3812. iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3813. if (status & MWL8K_A2H_INT_OPC_DONE) {
  3814. if (priv->hostcmd_wait != NULL)
  3815. complete(priv->hostcmd_wait);
  3816. }
  3817. if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
  3818. if (!mutex_is_locked(&priv->fw_mutex) &&
  3819. priv->radio_on && priv->pending_tx_pkts)
  3820. mwl8k_tx_start(priv);
  3821. }
  3822. return IRQ_HANDLED;
  3823. }
  3824. static void mwl8k_tx_poll(unsigned long data)
  3825. {
  3826. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  3827. struct mwl8k_priv *priv = hw->priv;
  3828. int limit;
  3829. int i;
  3830. limit = 32;
  3831. spin_lock_bh(&priv->tx_lock);
  3832. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  3833. limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
  3834. if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
  3835. complete(priv->tx_wait);
  3836. priv->tx_wait = NULL;
  3837. }
  3838. spin_unlock_bh(&priv->tx_lock);
  3839. if (limit) {
  3840. writel(~MWL8K_A2H_INT_TX_DONE,
  3841. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3842. } else {
  3843. tasklet_schedule(&priv->poll_tx_task);
  3844. }
  3845. }
  3846. static void mwl8k_rx_poll(unsigned long data)
  3847. {
  3848. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  3849. struct mwl8k_priv *priv = hw->priv;
  3850. int limit;
  3851. limit = 32;
  3852. limit -= rxq_process(hw, 0, limit);
  3853. limit -= rxq_refill(hw, 0, limit);
  3854. if (limit) {
  3855. writel(~MWL8K_A2H_INT_RX_READY,
  3856. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3857. } else {
  3858. tasklet_schedule(&priv->poll_rx_task);
  3859. }
  3860. }
  3861. /*
  3862. * Core driver operations.
  3863. */
  3864. static void mwl8k_tx(struct ieee80211_hw *hw,
  3865. struct ieee80211_tx_control *control,
  3866. struct sk_buff *skb)
  3867. {
  3868. struct mwl8k_priv *priv = hw->priv;
  3869. int index = skb_get_queue_mapping(skb);
  3870. if (!priv->radio_on) {
  3871. wiphy_debug(hw->wiphy,
  3872. "dropped TX frame since radio disabled\n");
  3873. dev_kfree_skb(skb);
  3874. return;
  3875. }
  3876. mwl8k_txq_xmit(hw, index, control->sta, skb);
  3877. }
  3878. static int mwl8k_start(struct ieee80211_hw *hw)
  3879. {
  3880. struct mwl8k_priv *priv = hw->priv;
  3881. int rc;
  3882. rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
  3883. IRQF_SHARED, MWL8K_NAME, hw);
  3884. if (rc) {
  3885. priv->irq = -1;
  3886. wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
  3887. return -EIO;
  3888. }
  3889. priv->irq = priv->pdev->irq;
  3890. /* Enable TX reclaim and RX tasklets. */
  3891. tasklet_enable(&priv->poll_tx_task);
  3892. tasklet_enable(&priv->poll_rx_task);
  3893. /* Enable interrupts */
  3894. iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  3895. iowrite32(MWL8K_A2H_EVENTS,
  3896. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3897. rc = mwl8k_fw_lock(hw);
  3898. if (!rc) {
  3899. rc = mwl8k_cmd_radio_enable(hw);
  3900. if (!priv->ap_fw) {
  3901. if (!rc)
  3902. rc = mwl8k_cmd_enable_sniffer(hw, 0);
  3903. if (!rc)
  3904. rc = mwl8k_cmd_set_pre_scan(hw);
  3905. if (!rc)
  3906. rc = mwl8k_cmd_set_post_scan(hw,
  3907. "\x00\x00\x00\x00\x00\x00");
  3908. }
  3909. if (!rc)
  3910. rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
  3911. if (!rc)
  3912. rc = mwl8k_cmd_set_wmm_mode(hw, 0);
  3913. mwl8k_fw_unlock(hw);
  3914. }
  3915. if (rc) {
  3916. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  3917. free_irq(priv->pdev->irq, hw);
  3918. priv->irq = -1;
  3919. tasklet_disable(&priv->poll_tx_task);
  3920. tasklet_disable(&priv->poll_rx_task);
  3921. } else {
  3922. ieee80211_wake_queues(hw);
  3923. }
  3924. return rc;
  3925. }
  3926. static void mwl8k_stop(struct ieee80211_hw *hw)
  3927. {
  3928. struct mwl8k_priv *priv = hw->priv;
  3929. int i;
  3930. if (!priv->hw_restart_in_progress)
  3931. mwl8k_cmd_radio_disable(hw);
  3932. ieee80211_stop_queues(hw);
  3933. /* Disable interrupts */
  3934. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  3935. if (priv->irq != -1) {
  3936. free_irq(priv->pdev->irq, hw);
  3937. priv->irq = -1;
  3938. }
  3939. /* Stop finalize join worker */
  3940. cancel_work_sync(&priv->finalize_join_worker);
  3941. cancel_work_sync(&priv->watchdog_ba_handle);
  3942. if (priv->beacon_skb != NULL)
  3943. dev_kfree_skb(priv->beacon_skb);
  3944. /* Stop TX reclaim and RX tasklets. */
  3945. tasklet_disable(&priv->poll_tx_task);
  3946. tasklet_disable(&priv->poll_rx_task);
  3947. /* Return all skbs to mac80211 */
  3948. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  3949. mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
  3950. }
  3951. static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
  3952. static int mwl8k_add_interface(struct ieee80211_hw *hw,
  3953. struct ieee80211_vif *vif)
  3954. {
  3955. struct mwl8k_priv *priv = hw->priv;
  3956. struct mwl8k_vif *mwl8k_vif;
  3957. u32 macids_supported;
  3958. int macid, rc;
  3959. struct mwl8k_device_info *di;
  3960. /*
  3961. * Reject interface creation if sniffer mode is active, as
  3962. * STA operation is mutually exclusive with hardware sniffer
  3963. * mode. (Sniffer mode is only used on STA firmware.)
  3964. */
  3965. if (priv->sniffer_enabled) {
  3966. wiphy_info(hw->wiphy,
  3967. "unable to create STA interface because sniffer mode is enabled\n");
  3968. return -EINVAL;
  3969. }
  3970. di = priv->device_info;
  3971. switch (vif->type) {
  3972. case NL80211_IFTYPE_AP:
  3973. if (!priv->ap_fw && di->fw_image_ap) {
  3974. /* we must load the ap fw to meet this request */
  3975. if (!list_empty(&priv->vif_list))
  3976. return -EBUSY;
  3977. rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
  3978. if (rc)
  3979. return rc;
  3980. }
  3981. macids_supported = priv->ap_macids_supported;
  3982. break;
  3983. case NL80211_IFTYPE_STATION:
  3984. if (priv->ap_fw && di->fw_image_sta) {
  3985. if (!list_empty(&priv->vif_list)) {
  3986. wiphy_warn(hw->wiphy, "AP interface is running.\n"
  3987. "Adding STA interface for WDS");
  3988. } else {
  3989. /* we must load the sta fw to
  3990. * meet this request.
  3991. */
  3992. rc = mwl8k_reload_firmware(hw,
  3993. di->fw_image_sta);
  3994. if (rc)
  3995. return rc;
  3996. }
  3997. }
  3998. macids_supported = priv->sta_macids_supported;
  3999. break;
  4000. default:
  4001. return -EINVAL;
  4002. }
  4003. macid = ffs(macids_supported & ~priv->macids_used);
  4004. if (!macid--)
  4005. return -EBUSY;
  4006. /* Setup driver private area. */
  4007. mwl8k_vif = MWL8K_VIF(vif);
  4008. memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
  4009. mwl8k_vif->vif = vif;
  4010. mwl8k_vif->macid = macid;
  4011. mwl8k_vif->seqno = 0;
  4012. memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
  4013. mwl8k_vif->is_hw_crypto_enabled = false;
  4014. /* Set the mac address. */
  4015. mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
  4016. if (vif->type == NL80211_IFTYPE_AP)
  4017. mwl8k_cmd_set_new_stn_add_self(hw, vif);
  4018. priv->macids_used |= 1 << mwl8k_vif->macid;
  4019. list_add_tail(&mwl8k_vif->list, &priv->vif_list);
  4020. return 0;
  4021. }
  4022. static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
  4023. {
  4024. /* Has ieee80211_restart_hw re-added the removed interfaces? */
  4025. if (!priv->macids_used)
  4026. return;
  4027. priv->macids_used &= ~(1 << vif->macid);
  4028. list_del(&vif->list);
  4029. }
  4030. static void mwl8k_remove_interface(struct ieee80211_hw *hw,
  4031. struct ieee80211_vif *vif)
  4032. {
  4033. struct mwl8k_priv *priv = hw->priv;
  4034. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  4035. if (vif->type == NL80211_IFTYPE_AP)
  4036. mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
  4037. mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
  4038. mwl8k_remove_vif(priv, mwl8k_vif);
  4039. }
  4040. static void mwl8k_hw_restart_work(struct work_struct *work)
  4041. {
  4042. struct mwl8k_priv *priv =
  4043. container_of(work, struct mwl8k_priv, fw_reload);
  4044. struct ieee80211_hw *hw = priv->hw;
  4045. struct mwl8k_device_info *di;
  4046. int rc;
  4047. /* If some command is waiting for a response, clear it */
  4048. if (priv->hostcmd_wait != NULL) {
  4049. complete(priv->hostcmd_wait);
  4050. priv->hostcmd_wait = NULL;
  4051. }
  4052. priv->hw_restart_owner = current;
  4053. di = priv->device_info;
  4054. mwl8k_fw_lock(hw);
  4055. if (priv->ap_fw)
  4056. rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
  4057. else
  4058. rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
  4059. if (rc)
  4060. goto fail;
  4061. priv->hw_restart_owner = NULL;
  4062. priv->hw_restart_in_progress = false;
  4063. /*
  4064. * This unlock will wake up the queues and
  4065. * also opens the command path for other
  4066. * commands
  4067. */
  4068. mwl8k_fw_unlock(hw);
  4069. ieee80211_restart_hw(hw);
  4070. wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
  4071. return;
  4072. fail:
  4073. mwl8k_fw_unlock(hw);
  4074. wiphy_err(hw->wiphy, "Firmware restart failed\n");
  4075. }
  4076. static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
  4077. {
  4078. struct ieee80211_conf *conf = &hw->conf;
  4079. struct mwl8k_priv *priv = hw->priv;
  4080. int rc;
  4081. rc = mwl8k_fw_lock(hw);
  4082. if (rc)
  4083. return rc;
  4084. if (conf->flags & IEEE80211_CONF_IDLE)
  4085. rc = mwl8k_cmd_radio_disable(hw);
  4086. else
  4087. rc = mwl8k_cmd_radio_enable(hw);
  4088. if (rc)
  4089. goto out;
  4090. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  4091. rc = mwl8k_cmd_set_rf_channel(hw, conf);
  4092. if (rc)
  4093. goto out;
  4094. }
  4095. if (conf->power_level > 18)
  4096. conf->power_level = 18;
  4097. if (priv->ap_fw) {
  4098. if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
  4099. rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
  4100. if (rc)
  4101. goto out;
  4102. }
  4103. } else {
  4104. rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
  4105. if (rc)
  4106. goto out;
  4107. rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
  4108. }
  4109. out:
  4110. mwl8k_fw_unlock(hw);
  4111. return rc;
  4112. }
  4113. static void
  4114. mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4115. struct ieee80211_bss_conf *info, u32 changed)
  4116. {
  4117. struct mwl8k_priv *priv = hw->priv;
  4118. u32 ap_legacy_rates = 0;
  4119. u8 ap_mcs_rates[16];
  4120. int rc;
  4121. if (mwl8k_fw_lock(hw))
  4122. return;
  4123. /*
  4124. * No need to capture a beacon if we're no longer associated.
  4125. */
  4126. if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
  4127. priv->capture_beacon = false;
  4128. /*
  4129. * Get the AP's legacy and MCS rates.
  4130. */
  4131. if (vif->bss_conf.assoc) {
  4132. struct ieee80211_sta *ap;
  4133. rcu_read_lock();
  4134. ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
  4135. if (ap == NULL) {
  4136. rcu_read_unlock();
  4137. goto out;
  4138. }
  4139. if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ) {
  4140. ap_legacy_rates = ap->supp_rates[NL80211_BAND_2GHZ];
  4141. } else {
  4142. ap_legacy_rates =
  4143. ap->supp_rates[NL80211_BAND_5GHZ] << 5;
  4144. }
  4145. memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
  4146. rcu_read_unlock();
  4147. if (changed & BSS_CHANGED_ASSOC) {
  4148. if (!priv->ap_fw) {
  4149. rc = mwl8k_cmd_set_rate(hw, vif,
  4150. ap_legacy_rates,
  4151. ap_mcs_rates);
  4152. if (rc)
  4153. goto out;
  4154. rc = mwl8k_cmd_use_fixed_rate_sta(hw);
  4155. if (rc)
  4156. goto out;
  4157. } else {
  4158. int idx;
  4159. int rate;
  4160. /* Use AP firmware specific rate command.
  4161. */
  4162. idx = ffs(vif->bss_conf.basic_rates);
  4163. if (idx)
  4164. idx--;
  4165. if (hw->conf.chandef.chan->band ==
  4166. NL80211_BAND_2GHZ)
  4167. rate = mwl8k_rates_24[idx].hw_value;
  4168. else
  4169. rate = mwl8k_rates_50[idx].hw_value;
  4170. mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
  4171. }
  4172. }
  4173. }
  4174. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  4175. rc = mwl8k_set_radio_preamble(hw,
  4176. vif->bss_conf.use_short_preamble);
  4177. if (rc)
  4178. goto out;
  4179. }
  4180. if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw) {
  4181. rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
  4182. if (rc)
  4183. goto out;
  4184. }
  4185. if (vif->bss_conf.assoc && !priv->ap_fw &&
  4186. (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
  4187. BSS_CHANGED_HT))) {
  4188. rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
  4189. if (rc)
  4190. goto out;
  4191. }
  4192. if (vif->bss_conf.assoc &&
  4193. (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
  4194. /*
  4195. * Finalize the join. Tell rx handler to process
  4196. * next beacon from our BSSID.
  4197. */
  4198. memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
  4199. priv->capture_beacon = true;
  4200. }
  4201. out:
  4202. mwl8k_fw_unlock(hw);
  4203. }
  4204. static void
  4205. mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4206. struct ieee80211_bss_conf *info, u32 changed)
  4207. {
  4208. int rc;
  4209. if (mwl8k_fw_lock(hw))
  4210. return;
  4211. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  4212. rc = mwl8k_set_radio_preamble(hw,
  4213. vif->bss_conf.use_short_preamble);
  4214. if (rc)
  4215. goto out;
  4216. }
  4217. if (changed & BSS_CHANGED_BASIC_RATES) {
  4218. int idx;
  4219. int rate;
  4220. /*
  4221. * Use lowest supported basic rate for multicasts
  4222. * and management frames (such as probe responses --
  4223. * beacons will always go out at 1 Mb/s).
  4224. */
  4225. idx = ffs(vif->bss_conf.basic_rates);
  4226. if (idx)
  4227. idx--;
  4228. if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
  4229. rate = mwl8k_rates_24[idx].hw_value;
  4230. else
  4231. rate = mwl8k_rates_50[idx].hw_value;
  4232. mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
  4233. }
  4234. if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
  4235. struct sk_buff *skb;
  4236. skb = ieee80211_beacon_get(hw, vif);
  4237. if (skb != NULL) {
  4238. mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
  4239. kfree_skb(skb);
  4240. }
  4241. }
  4242. if (changed & BSS_CHANGED_BEACON_ENABLED)
  4243. mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
  4244. out:
  4245. mwl8k_fw_unlock(hw);
  4246. }
  4247. static void
  4248. mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4249. struct ieee80211_bss_conf *info, u32 changed)
  4250. {
  4251. if (vif->type == NL80211_IFTYPE_STATION)
  4252. mwl8k_bss_info_changed_sta(hw, vif, info, changed);
  4253. if (vif->type == NL80211_IFTYPE_AP)
  4254. mwl8k_bss_info_changed_ap(hw, vif, info, changed);
  4255. }
  4256. static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
  4257. struct netdev_hw_addr_list *mc_list)
  4258. {
  4259. struct mwl8k_cmd_pkt *cmd;
  4260. /*
  4261. * Synthesize and return a command packet that programs the
  4262. * hardware multicast address filter. At this point we don't
  4263. * know whether FIF_ALLMULTI is being requested, but if it is,
  4264. * we'll end up throwing this packet away and creating a new
  4265. * one in mwl8k_configure_filter().
  4266. */
  4267. cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
  4268. return (unsigned long)cmd;
  4269. }
  4270. static int
  4271. mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
  4272. unsigned int changed_flags,
  4273. unsigned int *total_flags)
  4274. {
  4275. struct mwl8k_priv *priv = hw->priv;
  4276. /*
  4277. * Hardware sniffer mode is mutually exclusive with STA
  4278. * operation, so refuse to enable sniffer mode if a STA
  4279. * interface is active.
  4280. */
  4281. if (!list_empty(&priv->vif_list)) {
  4282. if (net_ratelimit())
  4283. wiphy_info(hw->wiphy,
  4284. "not enabling sniffer mode because STA interface is active\n");
  4285. return 0;
  4286. }
  4287. if (!priv->sniffer_enabled) {
  4288. if (mwl8k_cmd_enable_sniffer(hw, 1))
  4289. return 0;
  4290. priv->sniffer_enabled = true;
  4291. }
  4292. *total_flags &= FIF_ALLMULTI |
  4293. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
  4294. FIF_OTHER_BSS;
  4295. return 1;
  4296. }
  4297. static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
  4298. {
  4299. if (!list_empty(&priv->vif_list))
  4300. return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
  4301. return NULL;
  4302. }
  4303. static void mwl8k_configure_filter(struct ieee80211_hw *hw,
  4304. unsigned int changed_flags,
  4305. unsigned int *total_flags,
  4306. u64 multicast)
  4307. {
  4308. struct mwl8k_priv *priv = hw->priv;
  4309. struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
  4310. /*
  4311. * AP firmware doesn't allow fine-grained control over
  4312. * the receive filter.
  4313. */
  4314. if (priv->ap_fw) {
  4315. *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
  4316. kfree(cmd);
  4317. return;
  4318. }
  4319. /*
  4320. * Enable hardware sniffer mode if FIF_CONTROL or
  4321. * FIF_OTHER_BSS is requested.
  4322. */
  4323. if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
  4324. mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
  4325. kfree(cmd);
  4326. return;
  4327. }
  4328. /* Clear unsupported feature flags */
  4329. *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
  4330. if (mwl8k_fw_lock(hw)) {
  4331. kfree(cmd);
  4332. return;
  4333. }
  4334. if (priv->sniffer_enabled) {
  4335. mwl8k_cmd_enable_sniffer(hw, 0);
  4336. priv->sniffer_enabled = false;
  4337. }
  4338. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  4339. if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
  4340. /*
  4341. * Disable the BSS filter.
  4342. */
  4343. mwl8k_cmd_set_pre_scan(hw);
  4344. } else {
  4345. struct mwl8k_vif *mwl8k_vif;
  4346. const u8 *bssid;
  4347. /*
  4348. * Enable the BSS filter.
  4349. *
  4350. * If there is an active STA interface, use that
  4351. * interface's BSSID, otherwise use a dummy one
  4352. * (where the OUI part needs to be nonzero for
  4353. * the BSSID to be accepted by POST_SCAN).
  4354. */
  4355. mwl8k_vif = mwl8k_first_vif(priv);
  4356. if (mwl8k_vif != NULL)
  4357. bssid = mwl8k_vif->vif->bss_conf.bssid;
  4358. else
  4359. bssid = "\x01\x00\x00\x00\x00\x00";
  4360. mwl8k_cmd_set_post_scan(hw, bssid);
  4361. }
  4362. }
  4363. /*
  4364. * If FIF_ALLMULTI is being requested, throw away the command
  4365. * packet that ->prepare_multicast() built and replace it with
  4366. * a command packet that enables reception of all multicast
  4367. * packets.
  4368. */
  4369. if (*total_flags & FIF_ALLMULTI) {
  4370. kfree(cmd);
  4371. cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
  4372. }
  4373. if (cmd != NULL) {
  4374. mwl8k_post_cmd(hw, cmd);
  4375. kfree(cmd);
  4376. }
  4377. mwl8k_fw_unlock(hw);
  4378. }
  4379. static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  4380. {
  4381. return mwl8k_cmd_set_rts_threshold(hw, value);
  4382. }
  4383. static int mwl8k_sta_remove(struct ieee80211_hw *hw,
  4384. struct ieee80211_vif *vif,
  4385. struct ieee80211_sta *sta)
  4386. {
  4387. struct mwl8k_priv *priv = hw->priv;
  4388. if (priv->ap_fw)
  4389. return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
  4390. else
  4391. return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
  4392. }
  4393. static int mwl8k_sta_add(struct ieee80211_hw *hw,
  4394. struct ieee80211_vif *vif,
  4395. struct ieee80211_sta *sta)
  4396. {
  4397. struct mwl8k_priv *priv = hw->priv;
  4398. int ret;
  4399. int i;
  4400. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  4401. struct ieee80211_key_conf *key;
  4402. if (!priv->ap_fw) {
  4403. ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
  4404. if (ret >= 0) {
  4405. MWL8K_STA(sta)->peer_id = ret;
  4406. if (sta->ht_cap.ht_supported)
  4407. MWL8K_STA(sta)->is_ampdu_allowed = true;
  4408. ret = 0;
  4409. }
  4410. } else {
  4411. ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
  4412. }
  4413. for (i = 0; i < NUM_WEP_KEYS; i++) {
  4414. key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
  4415. if (mwl8k_vif->wep_key_conf[i].enabled)
  4416. mwl8k_set_key(hw, SET_KEY, vif, sta, key);
  4417. }
  4418. return ret;
  4419. }
  4420. static int mwl8k_conf_tx(struct ieee80211_hw *hw,
  4421. struct ieee80211_vif *vif, u16 queue,
  4422. const struct ieee80211_tx_queue_params *params)
  4423. {
  4424. struct mwl8k_priv *priv = hw->priv;
  4425. int rc;
  4426. rc = mwl8k_fw_lock(hw);
  4427. if (!rc) {
  4428. BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
  4429. memcpy(&priv->wmm_params[queue], params, sizeof(*params));
  4430. if (!priv->wmm_enabled)
  4431. rc = mwl8k_cmd_set_wmm_mode(hw, 1);
  4432. if (!rc) {
  4433. int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
  4434. rc = mwl8k_cmd_set_edca_params(hw, q,
  4435. params->cw_min,
  4436. params->cw_max,
  4437. params->aifs,
  4438. params->txop);
  4439. }
  4440. mwl8k_fw_unlock(hw);
  4441. }
  4442. return rc;
  4443. }
  4444. static int mwl8k_get_stats(struct ieee80211_hw *hw,
  4445. struct ieee80211_low_level_stats *stats)
  4446. {
  4447. return mwl8k_cmd_get_stat(hw, stats);
  4448. }
  4449. static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
  4450. struct survey_info *survey)
  4451. {
  4452. struct mwl8k_priv *priv = hw->priv;
  4453. struct ieee80211_conf *conf = &hw->conf;
  4454. struct ieee80211_supported_band *sband;
  4455. if (priv->ap_fw) {
  4456. sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
  4457. if (sband && idx >= sband->n_channels) {
  4458. idx -= sband->n_channels;
  4459. sband = NULL;
  4460. }
  4461. if (!sband)
  4462. sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
  4463. if (!sband || idx >= sband->n_channels)
  4464. return -ENOENT;
  4465. memcpy(survey, &priv->survey[idx], sizeof(*survey));
  4466. survey->channel = &sband->channels[idx];
  4467. return 0;
  4468. }
  4469. if (idx != 0)
  4470. return -ENOENT;
  4471. survey->channel = conf->chandef.chan;
  4472. survey->filled = SURVEY_INFO_NOISE_DBM;
  4473. survey->noise = priv->noise;
  4474. return 0;
  4475. }
  4476. #define MAX_AMPDU_ATTEMPTS 5
  4477. static int
  4478. mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4479. struct ieee80211_ampdu_params *params)
  4480. {
  4481. struct ieee80211_sta *sta = params->sta;
  4482. enum ieee80211_ampdu_mlme_action action = params->action;
  4483. u16 tid = params->tid;
  4484. u16 *ssn = &params->ssn;
  4485. u8 buf_size = params->buf_size;
  4486. int i, rc = 0;
  4487. struct mwl8k_priv *priv = hw->priv;
  4488. struct mwl8k_ampdu_stream *stream;
  4489. u8 *addr = sta->addr, idx;
  4490. struct mwl8k_sta *sta_info = MWL8K_STA(sta);
  4491. if (!ieee80211_hw_check(hw, AMPDU_AGGREGATION))
  4492. return -ENOTSUPP;
  4493. spin_lock(&priv->stream_lock);
  4494. stream = mwl8k_lookup_stream(hw, addr, tid);
  4495. switch (action) {
  4496. case IEEE80211_AMPDU_RX_START:
  4497. case IEEE80211_AMPDU_RX_STOP:
  4498. break;
  4499. case IEEE80211_AMPDU_TX_START:
  4500. /* By the time we get here the hw queues may contain outgoing
  4501. * packets for this RA/TID that are not part of this BA
  4502. * session. The hw will assign sequence numbers to these
  4503. * packets as they go out. So if we query the hw for its next
  4504. * sequence number and use that for the SSN here, it may end up
  4505. * being wrong, which will lead to sequence number mismatch at
  4506. * the recipient. To avoid this, we reset the sequence number
  4507. * to O for the first MPDU in this BA stream.
  4508. */
  4509. *ssn = 0;
  4510. if (stream == NULL) {
  4511. /* This means that somebody outside this driver called
  4512. * ieee80211_start_tx_ba_session. This is unexpected
  4513. * because we do our own rate control. Just warn and
  4514. * move on.
  4515. */
  4516. wiphy_warn(hw->wiphy, "Unexpected call to %s. "
  4517. "Proceeding anyway.\n", __func__);
  4518. stream = mwl8k_add_stream(hw, sta, tid);
  4519. }
  4520. if (stream == NULL) {
  4521. wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
  4522. rc = -EBUSY;
  4523. break;
  4524. }
  4525. stream->state = AMPDU_STREAM_IN_PROGRESS;
  4526. /* Release the lock before we do the time consuming stuff */
  4527. spin_unlock(&priv->stream_lock);
  4528. for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
  4529. /* Check if link is still valid */
  4530. if (!sta_info->is_ampdu_allowed) {
  4531. spin_lock(&priv->stream_lock);
  4532. mwl8k_remove_stream(hw, stream);
  4533. spin_unlock(&priv->stream_lock);
  4534. return -EBUSY;
  4535. }
  4536. rc = mwl8k_check_ba(hw, stream, vif);
  4537. /* If HW restart is in progress mwl8k_post_cmd will
  4538. * return -EBUSY. Avoid retrying mwl8k_check_ba in
  4539. * such cases
  4540. */
  4541. if (!rc || rc == -EBUSY)
  4542. break;
  4543. /*
  4544. * HW queues take time to be flushed, give them
  4545. * sufficient time
  4546. */
  4547. msleep(1000);
  4548. }
  4549. spin_lock(&priv->stream_lock);
  4550. if (rc) {
  4551. wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
  4552. " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
  4553. mwl8k_remove_stream(hw, stream);
  4554. rc = -EBUSY;
  4555. break;
  4556. }
  4557. ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
  4558. break;
  4559. case IEEE80211_AMPDU_TX_STOP_CONT:
  4560. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  4561. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  4562. if (stream) {
  4563. if (stream->state == AMPDU_STREAM_ACTIVE) {
  4564. idx = stream->idx;
  4565. spin_unlock(&priv->stream_lock);
  4566. mwl8k_destroy_ba(hw, idx);
  4567. spin_lock(&priv->stream_lock);
  4568. }
  4569. mwl8k_remove_stream(hw, stream);
  4570. }
  4571. ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
  4572. break;
  4573. case IEEE80211_AMPDU_TX_OPERATIONAL:
  4574. BUG_ON(stream == NULL);
  4575. BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
  4576. spin_unlock(&priv->stream_lock);
  4577. rc = mwl8k_create_ba(hw, stream, buf_size, vif);
  4578. spin_lock(&priv->stream_lock);
  4579. if (!rc)
  4580. stream->state = AMPDU_STREAM_ACTIVE;
  4581. else {
  4582. idx = stream->idx;
  4583. spin_unlock(&priv->stream_lock);
  4584. mwl8k_destroy_ba(hw, idx);
  4585. spin_lock(&priv->stream_lock);
  4586. wiphy_debug(hw->wiphy,
  4587. "Failed adding stream for sta %pM tid %d\n",
  4588. addr, tid);
  4589. mwl8k_remove_stream(hw, stream);
  4590. }
  4591. break;
  4592. default:
  4593. rc = -ENOTSUPP;
  4594. }
  4595. spin_unlock(&priv->stream_lock);
  4596. return rc;
  4597. }
  4598. static void mwl8k_sw_scan_start(struct ieee80211_hw *hw,
  4599. struct ieee80211_vif *vif,
  4600. const u8 *mac_addr)
  4601. {
  4602. struct mwl8k_priv *priv = hw->priv;
  4603. u8 tmp;
  4604. if (!priv->ap_fw)
  4605. return;
  4606. /* clear all stats */
  4607. priv->channel_time = 0;
  4608. ioread32(priv->regs + BBU_RXRDY_CNT_REG);
  4609. ioread32(priv->regs + NOK_CCA_CNT_REG);
  4610. mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &tmp);
  4611. priv->sw_scan_start = true;
  4612. }
  4613. static void mwl8k_sw_scan_complete(struct ieee80211_hw *hw,
  4614. struct ieee80211_vif *vif)
  4615. {
  4616. struct mwl8k_priv *priv = hw->priv;
  4617. u8 tmp;
  4618. if (!priv->ap_fw)
  4619. return;
  4620. priv->sw_scan_start = false;
  4621. /* clear all stats */
  4622. priv->channel_time = 0;
  4623. ioread32(priv->regs + BBU_RXRDY_CNT_REG);
  4624. ioread32(priv->regs + NOK_CCA_CNT_REG);
  4625. mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &tmp);
  4626. }
  4627. static const struct ieee80211_ops mwl8k_ops = {
  4628. .tx = mwl8k_tx,
  4629. .start = mwl8k_start,
  4630. .stop = mwl8k_stop,
  4631. .add_interface = mwl8k_add_interface,
  4632. .remove_interface = mwl8k_remove_interface,
  4633. .config = mwl8k_config,
  4634. .bss_info_changed = mwl8k_bss_info_changed,
  4635. .prepare_multicast = mwl8k_prepare_multicast,
  4636. .configure_filter = mwl8k_configure_filter,
  4637. .set_key = mwl8k_set_key,
  4638. .set_rts_threshold = mwl8k_set_rts_threshold,
  4639. .sta_add = mwl8k_sta_add,
  4640. .sta_remove = mwl8k_sta_remove,
  4641. .conf_tx = mwl8k_conf_tx,
  4642. .get_stats = mwl8k_get_stats,
  4643. .get_survey = mwl8k_get_survey,
  4644. .ampdu_action = mwl8k_ampdu_action,
  4645. .sw_scan_start = mwl8k_sw_scan_start,
  4646. .sw_scan_complete = mwl8k_sw_scan_complete,
  4647. };
  4648. static void mwl8k_finalize_join_worker(struct work_struct *work)
  4649. {
  4650. struct mwl8k_priv *priv =
  4651. container_of(work, struct mwl8k_priv, finalize_join_worker);
  4652. struct sk_buff *skb = priv->beacon_skb;
  4653. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  4654. int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
  4655. const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
  4656. mgmt->u.beacon.variable, len);
  4657. int dtim_period = 1;
  4658. if (tim && tim[1] >= 2)
  4659. dtim_period = tim[3];
  4660. mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
  4661. dev_kfree_skb(skb);
  4662. priv->beacon_skb = NULL;
  4663. }
  4664. enum {
  4665. MWL8363 = 0,
  4666. MWL8687,
  4667. MWL8366,
  4668. MWL8764,
  4669. };
  4670. #define MWL8K_8366_AP_FW_API 3
  4671. #define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
  4672. #define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
  4673. #define MWL8K_8764_AP_FW_API 1
  4674. #define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
  4675. #define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
  4676. static struct mwl8k_device_info mwl8k_info_tbl[] = {
  4677. [MWL8363] = {
  4678. .part_name = "88w8363",
  4679. .helper_image = "mwl8k/helper_8363.fw",
  4680. .fw_image_sta = "mwl8k/fmimage_8363.fw",
  4681. },
  4682. [MWL8687] = {
  4683. .part_name = "88w8687",
  4684. .helper_image = "mwl8k/helper_8687.fw",
  4685. .fw_image_sta = "mwl8k/fmimage_8687.fw",
  4686. },
  4687. [MWL8366] = {
  4688. .part_name = "88w8366",
  4689. .helper_image = "mwl8k/helper_8366.fw",
  4690. .fw_image_sta = "mwl8k/fmimage_8366.fw",
  4691. .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
  4692. .fw_api_ap = MWL8K_8366_AP_FW_API,
  4693. .ap_rxd_ops = &rxd_ap_ops,
  4694. },
  4695. [MWL8764] = {
  4696. .part_name = "88w8764",
  4697. .fw_image_ap = MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
  4698. .fw_api_ap = MWL8K_8764_AP_FW_API,
  4699. .ap_rxd_ops = &rxd_ap_ops,
  4700. },
  4701. };
  4702. MODULE_FIRMWARE("mwl8k/helper_8363.fw");
  4703. MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
  4704. MODULE_FIRMWARE("mwl8k/helper_8687.fw");
  4705. MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
  4706. MODULE_FIRMWARE("mwl8k/helper_8366.fw");
  4707. MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
  4708. MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
  4709. static const struct pci_device_id mwl8k_pci_id_table[] = {
  4710. { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
  4711. { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
  4712. { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
  4713. { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
  4714. { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
  4715. { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
  4716. { PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
  4717. { PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
  4718. { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
  4719. { PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
  4720. { },
  4721. };
  4722. MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
  4723. static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
  4724. {
  4725. int rc;
  4726. printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
  4727. "Trying alternative firmware %s\n", pci_name(priv->pdev),
  4728. priv->fw_pref, priv->fw_alt);
  4729. rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
  4730. if (rc) {
  4731. printk(KERN_ERR "%s: Error requesting alt fw %s\n",
  4732. pci_name(priv->pdev), priv->fw_alt);
  4733. return rc;
  4734. }
  4735. return 0;
  4736. }
  4737. static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
  4738. static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
  4739. {
  4740. struct mwl8k_priv *priv = context;
  4741. int rc;
  4742. switch (priv->fw_state) {
  4743. case FW_STATE_INIT:
  4744. if (!fw)
  4745. goto fail;
  4746. priv->fw_helper = fw;
  4747. rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
  4748. true);
  4749. if (rc && priv->fw_alt) {
  4750. rc = mwl8k_request_alt_fw(priv);
  4751. if (rc)
  4752. goto fail;
  4753. priv->fw_state = FW_STATE_LOADING_ALT;
  4754. } else if (rc)
  4755. goto fail;
  4756. else
  4757. priv->fw_state = FW_STATE_LOADING_PREF;
  4758. break;
  4759. case FW_STATE_LOADING_PREF:
  4760. if (!fw) {
  4761. if (priv->fw_alt) {
  4762. rc = mwl8k_request_alt_fw(priv);
  4763. if (rc)
  4764. goto fail;
  4765. priv->fw_state = FW_STATE_LOADING_ALT;
  4766. } else
  4767. goto fail;
  4768. } else {
  4769. priv->fw_ucode = fw;
  4770. rc = mwl8k_firmware_load_success(priv);
  4771. if (rc)
  4772. goto fail;
  4773. else
  4774. complete(&priv->firmware_loading_complete);
  4775. }
  4776. break;
  4777. case FW_STATE_LOADING_ALT:
  4778. if (!fw)
  4779. goto fail;
  4780. priv->fw_ucode = fw;
  4781. rc = mwl8k_firmware_load_success(priv);
  4782. if (rc)
  4783. goto fail;
  4784. else
  4785. complete(&priv->firmware_loading_complete);
  4786. break;
  4787. default:
  4788. printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
  4789. MWL8K_NAME, priv->fw_state);
  4790. BUG_ON(1);
  4791. }
  4792. return;
  4793. fail:
  4794. priv->fw_state = FW_STATE_ERROR;
  4795. complete(&priv->firmware_loading_complete);
  4796. device_release_driver(&priv->pdev->dev);
  4797. mwl8k_release_firmware(priv);
  4798. }
  4799. #define MAX_RESTART_ATTEMPTS 1
  4800. static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
  4801. bool nowait)
  4802. {
  4803. struct mwl8k_priv *priv = hw->priv;
  4804. int rc;
  4805. int count = MAX_RESTART_ATTEMPTS;
  4806. retry:
  4807. /* Reset firmware and hardware */
  4808. mwl8k_hw_reset(priv);
  4809. /* Ask userland hotplug daemon for the device firmware */
  4810. rc = mwl8k_request_firmware(priv, fw_image, nowait);
  4811. if (rc)
  4812. return rc;
  4813. if (nowait)
  4814. return rc;
  4815. /* Load firmware into hardware */
  4816. rc = mwl8k_load_firmware(hw);
  4817. if (rc)
  4818. wiphy_err(hw->wiphy, "Cannot start firmware\n");
  4819. /* Reclaim memory once firmware is successfully loaded */
  4820. mwl8k_release_firmware(priv);
  4821. if (rc && count) {
  4822. /* FW did not start successfully;
  4823. * lets try one more time
  4824. */
  4825. count--;
  4826. wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
  4827. msleep(20);
  4828. goto retry;
  4829. }
  4830. return rc;
  4831. }
  4832. static int mwl8k_init_txqs(struct ieee80211_hw *hw)
  4833. {
  4834. struct mwl8k_priv *priv = hw->priv;
  4835. int rc = 0;
  4836. int i;
  4837. for (i = 0; i < mwl8k_tx_queues(priv); i++) {
  4838. rc = mwl8k_txq_init(hw, i);
  4839. if (rc)
  4840. break;
  4841. if (priv->ap_fw)
  4842. iowrite32(priv->txq[i].txd_dma,
  4843. priv->sram + priv->txq_offset[i]);
  4844. }
  4845. return rc;
  4846. }
  4847. /* initialize hw after successfully loading a firmware image */
  4848. static int mwl8k_probe_hw(struct ieee80211_hw *hw)
  4849. {
  4850. struct mwl8k_priv *priv = hw->priv;
  4851. int rc = 0;
  4852. int i;
  4853. if (priv->ap_fw) {
  4854. priv->rxd_ops = priv->device_info->ap_rxd_ops;
  4855. if (priv->rxd_ops == NULL) {
  4856. wiphy_err(hw->wiphy,
  4857. "Driver does not have AP firmware image support for this hardware\n");
  4858. rc = -ENOENT;
  4859. goto err_stop_firmware;
  4860. }
  4861. } else {
  4862. priv->rxd_ops = &rxd_sta_ops;
  4863. }
  4864. priv->sniffer_enabled = false;
  4865. priv->wmm_enabled = false;
  4866. priv->pending_tx_pkts = 0;
  4867. atomic_set(&priv->watchdog_event_pending, 0);
  4868. rc = mwl8k_rxq_init(hw, 0);
  4869. if (rc)
  4870. goto err_stop_firmware;
  4871. rxq_refill(hw, 0, INT_MAX);
  4872. /* For the sta firmware, we need to know the dma addresses of tx queues
  4873. * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
  4874. * prior to issuing this command. But for the AP case, we learn the
  4875. * total number of queues from the result CMD_GET_HW_SPEC, so for this
  4876. * case we must initialize the tx queues after.
  4877. */
  4878. priv->num_ampdu_queues = 0;
  4879. if (!priv->ap_fw) {
  4880. rc = mwl8k_init_txqs(hw);
  4881. if (rc)
  4882. goto err_free_queues;
  4883. }
  4884. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  4885. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4886. iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
  4887. MWL8K_A2H_INT_BA_WATCHDOG,
  4888. priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
  4889. iowrite32(MWL8K_A2H_INT_OPC_DONE,
  4890. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  4891. rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
  4892. IRQF_SHARED, MWL8K_NAME, hw);
  4893. if (rc) {
  4894. wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
  4895. goto err_free_queues;
  4896. }
  4897. /*
  4898. * When hw restart is requested,
  4899. * mac80211 will take care of clearing
  4900. * the ampdu streams, so do not clear
  4901. * the ampdu state here
  4902. */
  4903. if (!priv->hw_restart_in_progress)
  4904. memset(priv->ampdu, 0, sizeof(priv->ampdu));
  4905. /*
  4906. * Temporarily enable interrupts. Initial firmware host
  4907. * commands use interrupts and avoid polling. Disable
  4908. * interrupts when done.
  4909. */
  4910. iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4911. /* Get config data, mac addrs etc */
  4912. if (priv->ap_fw) {
  4913. rc = mwl8k_cmd_get_hw_spec_ap(hw);
  4914. if (!rc)
  4915. rc = mwl8k_init_txqs(hw);
  4916. if (!rc)
  4917. rc = mwl8k_cmd_set_hw_spec(hw);
  4918. } else {
  4919. rc = mwl8k_cmd_get_hw_spec_sta(hw);
  4920. }
  4921. if (rc) {
  4922. wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
  4923. goto err_free_irq;
  4924. }
  4925. /* Turn radio off */
  4926. rc = mwl8k_cmd_radio_disable(hw);
  4927. if (rc) {
  4928. wiphy_err(hw->wiphy, "Cannot disable\n");
  4929. goto err_free_irq;
  4930. }
  4931. /* Clear MAC address */
  4932. rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
  4933. if (rc) {
  4934. wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
  4935. goto err_free_irq;
  4936. }
  4937. /* Configure Antennas */
  4938. rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
  4939. if (rc)
  4940. wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
  4941. rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
  4942. if (rc)
  4943. wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
  4944. /* Disable interrupts */
  4945. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4946. free_irq(priv->pdev->irq, hw);
  4947. wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
  4948. priv->device_info->part_name,
  4949. priv->hw_rev, hw->wiphy->perm_addr,
  4950. priv->ap_fw ? "AP" : "STA",
  4951. (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
  4952. (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
  4953. return 0;
  4954. err_free_irq:
  4955. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4956. free_irq(priv->pdev->irq, hw);
  4957. err_free_queues:
  4958. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4959. mwl8k_txq_deinit(hw, i);
  4960. mwl8k_rxq_deinit(hw, 0);
  4961. err_stop_firmware:
  4962. mwl8k_hw_reset(priv);
  4963. return rc;
  4964. }
  4965. /*
  4966. * invoke mwl8k_reload_firmware to change the firmware image after the device
  4967. * has already been registered
  4968. */
  4969. static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
  4970. {
  4971. int i, rc = 0;
  4972. struct mwl8k_priv *priv = hw->priv;
  4973. struct mwl8k_vif *vif, *tmp_vif;
  4974. mwl8k_stop(hw);
  4975. mwl8k_rxq_deinit(hw, 0);
  4976. /*
  4977. * All the existing interfaces are re-added by the ieee80211_reconfig;
  4978. * which means driver should remove existing interfaces before calling
  4979. * ieee80211_restart_hw
  4980. */
  4981. if (priv->hw_restart_in_progress)
  4982. list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
  4983. mwl8k_remove_vif(priv, vif);
  4984. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4985. mwl8k_txq_deinit(hw, i);
  4986. rc = mwl8k_init_firmware(hw, fw_image, false);
  4987. if (rc)
  4988. goto fail;
  4989. rc = mwl8k_probe_hw(hw);
  4990. if (rc)
  4991. goto fail;
  4992. if (priv->hw_restart_in_progress)
  4993. return rc;
  4994. rc = mwl8k_start(hw);
  4995. if (rc)
  4996. goto fail;
  4997. rc = mwl8k_config(hw, ~0);
  4998. if (rc)
  4999. goto fail;
  5000. for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
  5001. rc = mwl8k_conf_tx(hw, NULL, i, &priv->wmm_params[i]);
  5002. if (rc)
  5003. goto fail;
  5004. }
  5005. return rc;
  5006. fail:
  5007. printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
  5008. return rc;
  5009. }
  5010. static const struct ieee80211_iface_limit ap_if_limits[] = {
  5011. { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
  5012. { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
  5013. };
  5014. static const struct ieee80211_iface_combination ap_if_comb = {
  5015. .limits = ap_if_limits,
  5016. .n_limits = ARRAY_SIZE(ap_if_limits),
  5017. .max_interfaces = 8,
  5018. .num_different_channels = 1,
  5019. };
  5020. static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
  5021. {
  5022. struct ieee80211_hw *hw = priv->hw;
  5023. int i, rc;
  5024. rc = mwl8k_load_firmware(hw);
  5025. mwl8k_release_firmware(priv);
  5026. if (rc) {
  5027. wiphy_err(hw->wiphy, "Cannot start firmware\n");
  5028. return rc;
  5029. }
  5030. /*
  5031. * Extra headroom is the size of the required DMA header
  5032. * minus the size of the smallest 802.11 frame (CTS frame).
  5033. */
  5034. hw->extra_tx_headroom =
  5035. sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
  5036. hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
  5037. hw->queues = MWL8K_TX_WMM_QUEUES;
  5038. /* Set rssi values to dBm */
  5039. ieee80211_hw_set(hw, SIGNAL_DBM);
  5040. ieee80211_hw_set(hw, HAS_RATE_CONTROL);
  5041. /*
  5042. * Ask mac80211 to not to trigger PS mode
  5043. * based on PM bit of incoming frames.
  5044. */
  5045. if (priv->ap_fw)
  5046. ieee80211_hw_set(hw, AP_LINK_PS);
  5047. hw->vif_data_size = sizeof(struct mwl8k_vif);
  5048. hw->sta_data_size = sizeof(struct mwl8k_sta);
  5049. priv->macids_used = 0;
  5050. INIT_LIST_HEAD(&priv->vif_list);
  5051. /* Set default radio state and preamble */
  5052. priv->radio_on = false;
  5053. priv->radio_short_preamble = false;
  5054. /* Finalize join worker */
  5055. INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
  5056. /* Handle watchdog ba events */
  5057. INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
  5058. /* To reload the firmware if it crashes */
  5059. INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
  5060. /* TX reclaim and RX tasklets. */
  5061. tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
  5062. tasklet_disable(&priv->poll_tx_task);
  5063. tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
  5064. tasklet_disable(&priv->poll_rx_task);
  5065. /* Power management cookie */
  5066. priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
  5067. if (priv->cookie == NULL)
  5068. return -ENOMEM;
  5069. mutex_init(&priv->fw_mutex);
  5070. priv->fw_mutex_owner = NULL;
  5071. priv->fw_mutex_depth = 0;
  5072. priv->hostcmd_wait = NULL;
  5073. spin_lock_init(&priv->tx_lock);
  5074. spin_lock_init(&priv->stream_lock);
  5075. priv->tx_wait = NULL;
  5076. rc = mwl8k_probe_hw(hw);
  5077. if (rc)
  5078. goto err_free_cookie;
  5079. hw->wiphy->interface_modes = 0;
  5080. if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
  5081. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
  5082. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
  5083. hw->wiphy->iface_combinations = &ap_if_comb;
  5084. hw->wiphy->n_iface_combinations = 1;
  5085. }
  5086. if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
  5087. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
  5088. wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  5089. rc = ieee80211_register_hw(hw);
  5090. if (rc) {
  5091. wiphy_err(hw->wiphy, "Cannot register device\n");
  5092. goto err_unprobe_hw;
  5093. }
  5094. return 0;
  5095. err_unprobe_hw:
  5096. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  5097. mwl8k_txq_deinit(hw, i);
  5098. mwl8k_rxq_deinit(hw, 0);
  5099. err_free_cookie:
  5100. if (priv->cookie != NULL)
  5101. pci_free_consistent(priv->pdev, 4,
  5102. priv->cookie, priv->cookie_dma);
  5103. return rc;
  5104. }
  5105. static int mwl8k_probe(struct pci_dev *pdev,
  5106. const struct pci_device_id *id)
  5107. {
  5108. static int printed_version;
  5109. struct ieee80211_hw *hw;
  5110. struct mwl8k_priv *priv;
  5111. struct mwl8k_device_info *di;
  5112. int rc;
  5113. if (!printed_version) {
  5114. printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
  5115. printed_version = 1;
  5116. }
  5117. rc = pci_enable_device(pdev);
  5118. if (rc) {
  5119. printk(KERN_ERR "%s: Cannot enable new PCI device\n",
  5120. MWL8K_NAME);
  5121. return rc;
  5122. }
  5123. rc = pci_request_regions(pdev, MWL8K_NAME);
  5124. if (rc) {
  5125. printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
  5126. MWL8K_NAME);
  5127. goto err_disable_device;
  5128. }
  5129. pci_set_master(pdev);
  5130. hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
  5131. if (hw == NULL) {
  5132. printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
  5133. rc = -ENOMEM;
  5134. goto err_free_reg;
  5135. }
  5136. SET_IEEE80211_DEV(hw, &pdev->dev);
  5137. pci_set_drvdata(pdev, hw);
  5138. priv = hw->priv;
  5139. priv->hw = hw;
  5140. priv->pdev = pdev;
  5141. priv->device_info = &mwl8k_info_tbl[id->driver_data];
  5142. if (id->driver_data == MWL8764)
  5143. priv->is_8764 = true;
  5144. priv->sram = pci_iomap(pdev, 0, 0x10000);
  5145. if (priv->sram == NULL) {
  5146. wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
  5147. rc = -EIO;
  5148. goto err_iounmap;
  5149. }
  5150. /*
  5151. * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
  5152. * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
  5153. */
  5154. priv->regs = pci_iomap(pdev, 1, 0x10000);
  5155. if (priv->regs == NULL) {
  5156. priv->regs = pci_iomap(pdev, 2, 0x10000);
  5157. if (priv->regs == NULL) {
  5158. wiphy_err(hw->wiphy, "Cannot map device registers\n");
  5159. rc = -EIO;
  5160. goto err_iounmap;
  5161. }
  5162. }
  5163. /*
  5164. * Choose the initial fw image depending on user input. If a second
  5165. * image is available, make it the alternative image that will be
  5166. * loaded if the first one fails.
  5167. */
  5168. init_completion(&priv->firmware_loading_complete);
  5169. di = priv->device_info;
  5170. if (ap_mode_default && di->fw_image_ap) {
  5171. priv->fw_pref = di->fw_image_ap;
  5172. priv->fw_alt = di->fw_image_sta;
  5173. } else if (!ap_mode_default && di->fw_image_sta) {
  5174. priv->fw_pref = di->fw_image_sta;
  5175. priv->fw_alt = di->fw_image_ap;
  5176. } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
  5177. printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
  5178. priv->fw_pref = di->fw_image_sta;
  5179. } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
  5180. printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
  5181. priv->fw_pref = di->fw_image_ap;
  5182. }
  5183. rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
  5184. if (rc)
  5185. goto err_stop_firmware;
  5186. priv->hw_restart_in_progress = false;
  5187. priv->running_bsses = 0;
  5188. return rc;
  5189. err_stop_firmware:
  5190. mwl8k_hw_reset(priv);
  5191. err_iounmap:
  5192. if (priv->regs != NULL)
  5193. pci_iounmap(pdev, priv->regs);
  5194. if (priv->sram != NULL)
  5195. pci_iounmap(pdev, priv->sram);
  5196. ieee80211_free_hw(hw);
  5197. err_free_reg:
  5198. pci_release_regions(pdev);
  5199. err_disable_device:
  5200. pci_disable_device(pdev);
  5201. return rc;
  5202. }
  5203. static void mwl8k_remove(struct pci_dev *pdev)
  5204. {
  5205. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  5206. struct mwl8k_priv *priv;
  5207. int i;
  5208. if (hw == NULL)
  5209. return;
  5210. priv = hw->priv;
  5211. wait_for_completion(&priv->firmware_loading_complete);
  5212. if (priv->fw_state == FW_STATE_ERROR) {
  5213. mwl8k_hw_reset(priv);
  5214. goto unmap;
  5215. }
  5216. ieee80211_stop_queues(hw);
  5217. ieee80211_unregister_hw(hw);
  5218. /* Remove TX reclaim and RX tasklets. */
  5219. tasklet_kill(&priv->poll_tx_task);
  5220. tasklet_kill(&priv->poll_rx_task);
  5221. /* Stop hardware */
  5222. mwl8k_hw_reset(priv);
  5223. /* Return all skbs to mac80211 */
  5224. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  5225. mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
  5226. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  5227. mwl8k_txq_deinit(hw, i);
  5228. mwl8k_rxq_deinit(hw, 0);
  5229. pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
  5230. unmap:
  5231. pci_iounmap(pdev, priv->regs);
  5232. pci_iounmap(pdev, priv->sram);
  5233. ieee80211_free_hw(hw);
  5234. pci_release_regions(pdev);
  5235. pci_disable_device(pdev);
  5236. }
  5237. static struct pci_driver mwl8k_driver = {
  5238. .name = MWL8K_NAME,
  5239. .id_table = mwl8k_pci_id_table,
  5240. .probe = mwl8k_probe,
  5241. .remove = mwl8k_remove,
  5242. };
  5243. module_pci_driver(mwl8k_driver);
  5244. MODULE_DESCRIPTION(MWL8K_DESC);
  5245. MODULE_VERSION(MWL8K_VERSION);
  5246. MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
  5247. MODULE_LICENSE("GPL");