mwl8k.c 138 KB

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