mwl8k.c 145 KB

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