iwl4965-base.c 215 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * James P. Ketrenos <ipw2100-admin@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/version.h>
  32. #include <linux/init.h>
  33. #include <linux/pci.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/delay.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/wireless.h>
  39. #include <linux/firmware.h>
  40. #include <linux/etherdevice.h>
  41. #include <linux/if_arp.h>
  42. #include <net/mac80211.h>
  43. #include <asm/div64.h>
  44. #include "iwl-eeprom.h"
  45. #include "iwl-4965.h"
  46. #include "iwl-core.h"
  47. #include "iwl-io.h"
  48. #include "iwl-helpers.h"
  49. #include "iwl-sta.h"
  50. #include "iwl-calib.h"
  51. static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
  52. struct iwl4965_tx_queue *txq);
  53. /******************************************************************************
  54. *
  55. * module boiler plate
  56. *
  57. ******************************************************************************/
  58. /*
  59. * module name, copyright, version, etc.
  60. * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
  61. */
  62. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
  63. #ifdef CONFIG_IWLWIFI_DEBUG
  64. #define VD "d"
  65. #else
  66. #define VD
  67. #endif
  68. #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
  69. #define VS "s"
  70. #else
  71. #define VS
  72. #endif
  73. #define DRV_VERSION IWLWIFI_VERSION VD VS
  74. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  75. MODULE_VERSION(DRV_VERSION);
  76. MODULE_AUTHOR(DRV_COPYRIGHT);
  77. MODULE_LICENSE("GPL");
  78. __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
  79. {
  80. u16 fc = le16_to_cpu(hdr->frame_control);
  81. int hdr_len = ieee80211_get_hdrlen(fc);
  82. if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
  83. return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
  84. return NULL;
  85. }
  86. static const struct ieee80211_supported_band *iwl_get_hw_mode(
  87. struct iwl_priv *priv, enum ieee80211_band band)
  88. {
  89. return priv->hw->wiphy->bands[band];
  90. }
  91. static int iwl4965_is_empty_essid(const char *essid, int essid_len)
  92. {
  93. /* Single white space is for Linksys APs */
  94. if (essid_len == 1 && essid[0] == ' ')
  95. return 1;
  96. /* Otherwise, if the entire essid is 0, we assume it is hidden */
  97. while (essid_len) {
  98. essid_len--;
  99. if (essid[essid_len] != '\0')
  100. return 0;
  101. }
  102. return 1;
  103. }
  104. static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
  105. {
  106. static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
  107. const char *s = essid;
  108. char *d = escaped;
  109. if (iwl4965_is_empty_essid(essid, essid_len)) {
  110. memcpy(escaped, "<hidden>", sizeof("<hidden>"));
  111. return escaped;
  112. }
  113. essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
  114. while (essid_len--) {
  115. if (*s == '\0') {
  116. *d++ = '\\';
  117. *d++ = '0';
  118. s++;
  119. } else
  120. *d++ = *s++;
  121. }
  122. *d = '\0';
  123. return escaped;
  124. }
  125. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  126. * DMA services
  127. *
  128. * Theory of operation
  129. *
  130. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  131. * of buffer descriptors, each of which points to one or more data buffers for
  132. * the device to read from or fill. Driver and device exchange status of each
  133. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  134. * entries in each circular buffer, to protect against confusing empty and full
  135. * queue states.
  136. *
  137. * The device reads or writes the data in the queues via the device's several
  138. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  139. *
  140. * For Tx queue, there are low mark and high mark limits. If, after queuing
  141. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  142. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  143. * Tx queue resumed.
  144. *
  145. * The 4965 operates with up to 17 queues: One receive queue, one transmit
  146. * queue (#4) for sending commands to the device firmware, and 15 other
  147. * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
  148. *
  149. * See more detailed info in iwl-4965-hw.h.
  150. ***************************************************/
  151. int iwl4965_queue_space(const struct iwl4965_queue *q)
  152. {
  153. int s = q->read_ptr - q->write_ptr;
  154. if (q->read_ptr > q->write_ptr)
  155. s -= q->n_bd;
  156. if (s <= 0)
  157. s += q->n_window;
  158. /* keep some reserve to not confuse empty and full situations */
  159. s -= 2;
  160. if (s < 0)
  161. s = 0;
  162. return s;
  163. }
  164. static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
  165. {
  166. return q->write_ptr > q->read_ptr ?
  167. (i >= q->read_ptr && i < q->write_ptr) :
  168. !(i < q->read_ptr && i >= q->write_ptr);
  169. }
  170. static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
  171. {
  172. /* This is for scan command, the big buffer at end of command array */
  173. if (is_huge)
  174. return q->n_window; /* must be power of 2 */
  175. /* Otherwise, use normal size buffers */
  176. return index & (q->n_window - 1);
  177. }
  178. /**
  179. * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
  180. */
  181. static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
  182. int count, int slots_num, u32 id)
  183. {
  184. q->n_bd = count;
  185. q->n_window = slots_num;
  186. q->id = id;
  187. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  188. * and iwl_queue_dec_wrap are broken. */
  189. BUG_ON(!is_power_of_2(count));
  190. /* slots_num must be power-of-two size, otherwise
  191. * get_cmd_index is broken. */
  192. BUG_ON(!is_power_of_2(slots_num));
  193. q->low_mark = q->n_window / 4;
  194. if (q->low_mark < 4)
  195. q->low_mark = 4;
  196. q->high_mark = q->n_window / 8;
  197. if (q->high_mark < 2)
  198. q->high_mark = 2;
  199. q->write_ptr = q->read_ptr = 0;
  200. return 0;
  201. }
  202. /**
  203. * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  204. */
  205. static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
  206. struct iwl4965_tx_queue *txq, u32 id)
  207. {
  208. struct pci_dev *dev = priv->pci_dev;
  209. /* Driver private data, only for Tx (not command) queues,
  210. * not shared with device. */
  211. if (id != IWL_CMD_QUEUE_NUM) {
  212. txq->txb = kmalloc(sizeof(txq->txb[0]) *
  213. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  214. if (!txq->txb) {
  215. IWL_ERROR("kmalloc for auxiliary BD "
  216. "structures failed\n");
  217. goto error;
  218. }
  219. } else
  220. txq->txb = NULL;
  221. /* Circular buffer of transmit frame descriptors (TFDs),
  222. * shared with device */
  223. txq->bd = pci_alloc_consistent(dev,
  224. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
  225. &txq->q.dma_addr);
  226. if (!txq->bd) {
  227. IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
  228. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
  229. goto error;
  230. }
  231. txq->q.id = id;
  232. return 0;
  233. error:
  234. if (txq->txb) {
  235. kfree(txq->txb);
  236. txq->txb = NULL;
  237. }
  238. return -ENOMEM;
  239. }
  240. /**
  241. * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
  242. */
  243. int iwl4965_tx_queue_init(struct iwl_priv *priv,
  244. struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
  245. {
  246. struct pci_dev *dev = priv->pci_dev;
  247. int len;
  248. int rc = 0;
  249. /*
  250. * Alloc buffer array for commands (Tx or other types of commands).
  251. * For the command queue (#4), allocate command space + one big
  252. * command for scan, since scan command is very huge; the system will
  253. * not have two scans at the same time, so only one is needed.
  254. * For normal Tx queues (all other queues), no super-size command
  255. * space is needed.
  256. */
  257. len = sizeof(struct iwl_cmd) * slots_num;
  258. if (txq_id == IWL_CMD_QUEUE_NUM)
  259. len += IWL_MAX_SCAN_SIZE;
  260. txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
  261. if (!txq->cmd)
  262. return -ENOMEM;
  263. /* Alloc driver data array and TFD circular buffer */
  264. rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
  265. if (rc) {
  266. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  267. return -ENOMEM;
  268. }
  269. txq->need_update = 0;
  270. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  271. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  272. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  273. /* Initialize queue's high/low-water marks, and head/tail indexes */
  274. iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  275. /* Tell device where to find queue */
  276. iwl4965_hw_tx_queue_init(priv, txq);
  277. return 0;
  278. }
  279. /**
  280. * iwl4965_tx_queue_free - Deallocate DMA queue.
  281. * @txq: Transmit queue to deallocate.
  282. *
  283. * Empty queue by removing and destroying all BD's.
  284. * Free all buffers.
  285. * 0-fill, but do not free "txq" descriptor structure.
  286. */
  287. void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
  288. {
  289. struct iwl4965_queue *q = &txq->q;
  290. struct pci_dev *dev = priv->pci_dev;
  291. int len;
  292. if (q->n_bd == 0)
  293. return;
  294. /* first, empty all BD's */
  295. for (; q->write_ptr != q->read_ptr;
  296. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
  297. iwl4965_hw_txq_free_tfd(priv, txq);
  298. len = sizeof(struct iwl_cmd) * q->n_window;
  299. if (q->id == IWL_CMD_QUEUE_NUM)
  300. len += IWL_MAX_SCAN_SIZE;
  301. /* De-alloc array of command/tx buffers */
  302. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  303. /* De-alloc circular buffer of TFDs */
  304. if (txq->q.n_bd)
  305. pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
  306. txq->q.n_bd, txq->bd, txq->q.dma_addr);
  307. /* De-alloc array of per-TFD driver data */
  308. if (txq->txb) {
  309. kfree(txq->txb);
  310. txq->txb = NULL;
  311. }
  312. /* 0-fill queue descriptor structure */
  313. memset(txq, 0, sizeof(*txq));
  314. }
  315. const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  316. /*************** STATION TABLE MANAGEMENT ****
  317. * mac80211 should be examined to determine if sta_info is duplicating
  318. * the functionality provided here
  319. */
  320. /**************************************************************/
  321. #if 0 /* temporary disable till we add real remove station */
  322. /**
  323. * iwl4965_remove_station - Remove driver's knowledge of station.
  324. *
  325. * NOTE: This does not remove station from device's station table.
  326. */
  327. static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  328. {
  329. int index = IWL_INVALID_STATION;
  330. int i;
  331. unsigned long flags;
  332. spin_lock_irqsave(&priv->sta_lock, flags);
  333. if (is_ap)
  334. index = IWL_AP_ID;
  335. else if (is_broadcast_ether_addr(addr))
  336. index = priv->hw_params.bcast_sta_id;
  337. else
  338. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  339. if (priv->stations[i].used &&
  340. !compare_ether_addr(priv->stations[i].sta.sta.addr,
  341. addr)) {
  342. index = i;
  343. break;
  344. }
  345. if (unlikely(index == IWL_INVALID_STATION))
  346. goto out;
  347. if (priv->stations[index].used) {
  348. priv->stations[index].used = 0;
  349. priv->num_stations--;
  350. }
  351. BUG_ON(priv->num_stations < 0);
  352. out:
  353. spin_unlock_irqrestore(&priv->sta_lock, flags);
  354. return 0;
  355. }
  356. #endif
  357. /**
  358. * iwl4965_add_station_flags - Add station to tables in driver and device
  359. */
  360. u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
  361. int is_ap, u8 flags, void *ht_data)
  362. {
  363. int i;
  364. int index = IWL_INVALID_STATION;
  365. struct iwl4965_station_entry *station;
  366. unsigned long flags_spin;
  367. DECLARE_MAC_BUF(mac);
  368. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  369. if (is_ap)
  370. index = IWL_AP_ID;
  371. else if (is_broadcast_ether_addr(addr))
  372. index = priv->hw_params.bcast_sta_id;
  373. else
  374. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  375. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  376. addr)) {
  377. index = i;
  378. break;
  379. }
  380. if (!priv->stations[i].used &&
  381. index == IWL_INVALID_STATION)
  382. index = i;
  383. }
  384. /* These two conditions have the same outcome, but keep them separate
  385. since they have different meanings */
  386. if (unlikely(index == IWL_INVALID_STATION)) {
  387. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  388. return index;
  389. }
  390. if (priv->stations[index].used &&
  391. !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
  392. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  393. return index;
  394. }
  395. IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
  396. station = &priv->stations[index];
  397. station->used = 1;
  398. priv->num_stations++;
  399. /* Set up the REPLY_ADD_STA command to send to device */
  400. memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
  401. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  402. station->sta.mode = 0;
  403. station->sta.sta.sta_id = index;
  404. station->sta.station_flags = 0;
  405. #ifdef CONFIG_IWL4965_HT
  406. /* BCAST station and IBSS stations do not work in HT mode */
  407. if (index != priv->hw_params.bcast_sta_id &&
  408. priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
  409. iwl4965_set_ht_add_station(priv, index,
  410. (struct ieee80211_ht_info *) ht_data);
  411. #endif /*CONFIG_IWL4965_HT*/
  412. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  413. /* Add station to device's station table */
  414. iwl4965_send_add_station(priv, &station->sta, flags);
  415. return index;
  416. }
  417. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  418. /**
  419. * iwl4965_enqueue_hcmd - enqueue a uCode command
  420. * @priv: device private data point
  421. * @cmd: a point to the ucode command structure
  422. *
  423. * The function returns < 0 values to indicate the operation is
  424. * failed. On success, it turns the index (> 0) of command in the
  425. * command queue.
  426. */
  427. int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  428. {
  429. struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  430. struct iwl4965_queue *q = &txq->q;
  431. struct iwl4965_tfd_frame *tfd;
  432. u32 *control_flags;
  433. struct iwl_cmd *out_cmd;
  434. u32 idx;
  435. u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  436. dma_addr_t phys_addr;
  437. int ret;
  438. unsigned long flags;
  439. /* If any of the command structures end up being larger than
  440. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  441. * we will need to increase the size of the TFD entries */
  442. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  443. !(cmd->meta.flags & CMD_SIZE_HUGE));
  444. if (iwl_is_rfkill(priv)) {
  445. IWL_DEBUG_INFO("Not sending command - RF KILL");
  446. return -EIO;
  447. }
  448. if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
  449. IWL_ERROR("No space for Tx\n");
  450. return -ENOSPC;
  451. }
  452. spin_lock_irqsave(&priv->hcmd_lock, flags);
  453. tfd = &txq->bd[q->write_ptr];
  454. memset(tfd, 0, sizeof(*tfd));
  455. control_flags = (u32 *) tfd;
  456. idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
  457. out_cmd = &txq->cmd[idx];
  458. out_cmd->hdr.cmd = cmd->id;
  459. memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
  460. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  461. /* At this point, the out_cmd now has all of the incoming cmd
  462. * information */
  463. out_cmd->hdr.flags = 0;
  464. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
  465. INDEX_TO_SEQ(q->write_ptr));
  466. if (out_cmd->meta.flags & CMD_SIZE_HUGE)
  467. out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
  468. phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
  469. offsetof(struct iwl_cmd, hdr);
  470. iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
  471. IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
  472. "%d bytes at %d[%d]:%d\n",
  473. get_cmd_string(out_cmd->hdr.cmd),
  474. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  475. fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  476. txq->need_update = 1;
  477. /* Set up entry in queue's byte count circular buffer */
  478. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
  479. /* Increment and update queue's write index */
  480. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  481. ret = iwl4965_tx_queue_update_write_ptr(priv, txq);
  482. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  483. return ret ? ret : idx;
  484. }
  485. static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
  486. {
  487. struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
  488. if (hw_decrypt)
  489. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  490. else
  491. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  492. }
  493. /**
  494. * iwl4965_rxon_add_station - add station into station table.
  495. *
  496. * there is only one AP station with id= IWL_AP_ID
  497. * NOTE: mutex must be held before calling this fnction
  498. */
  499. static int iwl4965_rxon_add_station(struct iwl_priv *priv,
  500. const u8 *addr, int is_ap)
  501. {
  502. u8 sta_id;
  503. /* Add station to device's station table */
  504. #ifdef CONFIG_IWL4965_HT
  505. struct ieee80211_conf *conf = &priv->hw->conf;
  506. struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
  507. if ((is_ap) &&
  508. (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
  509. (priv->iw_mode == IEEE80211_IF_TYPE_STA))
  510. sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
  511. 0, cur_ht_config);
  512. else
  513. #endif /* CONFIG_IWL4965_HT */
  514. sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
  515. 0, NULL);
  516. /* Set up default rate scaling table in device's station table */
  517. iwl4965_add_station(priv, addr, is_ap);
  518. return sta_id;
  519. }
  520. /**
  521. * iwl4965_check_rxon_cmd - validate RXON structure is valid
  522. *
  523. * NOTE: This is really only useful during development and can eventually
  524. * be #ifdef'd out once the driver is stable and folks aren't actively
  525. * making changes
  526. */
  527. static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
  528. {
  529. int error = 0;
  530. int counter = 1;
  531. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  532. error |= le32_to_cpu(rxon->flags &
  533. (RXON_FLG_TGJ_NARROW_BAND_MSK |
  534. RXON_FLG_RADAR_DETECT_MSK));
  535. if (error)
  536. IWL_WARNING("check 24G fields %d | %d\n",
  537. counter++, error);
  538. } else {
  539. error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
  540. 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
  541. if (error)
  542. IWL_WARNING("check 52 fields %d | %d\n",
  543. counter++, error);
  544. error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
  545. if (error)
  546. IWL_WARNING("check 52 CCK %d | %d\n",
  547. counter++, error);
  548. }
  549. error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
  550. if (error)
  551. IWL_WARNING("check mac addr %d | %d\n", counter++, error);
  552. /* make sure basic rates 6Mbps and 1Mbps are supported */
  553. error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
  554. ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
  555. if (error)
  556. IWL_WARNING("check basic rate %d | %d\n", counter++, error);
  557. error |= (le16_to_cpu(rxon->assoc_id) > 2007);
  558. if (error)
  559. IWL_WARNING("check assoc id %d | %d\n", counter++, error);
  560. error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
  561. == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
  562. if (error)
  563. IWL_WARNING("check CCK and short slot %d | %d\n",
  564. counter++, error);
  565. error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
  566. == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
  567. if (error)
  568. IWL_WARNING("check CCK & auto detect %d | %d\n",
  569. counter++, error);
  570. error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
  571. RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
  572. if (error)
  573. IWL_WARNING("check TGG and auto detect %d | %d\n",
  574. counter++, error);
  575. if (error)
  576. IWL_WARNING("Tuning to channel %d\n",
  577. le16_to_cpu(rxon->channel));
  578. if (error) {
  579. IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
  580. return -1;
  581. }
  582. return 0;
  583. }
  584. /**
  585. * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  586. * @priv: staging_rxon is compared to active_rxon
  587. *
  588. * If the RXON structure is changing enough to require a new tune,
  589. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  590. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  591. */
  592. static int iwl4965_full_rxon_required(struct iwl_priv *priv)
  593. {
  594. /* These items are only settable from the full RXON command */
  595. if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
  596. compare_ether_addr(priv->staging_rxon.bssid_addr,
  597. priv->active_rxon.bssid_addr) ||
  598. compare_ether_addr(priv->staging_rxon.node_addr,
  599. priv->active_rxon.node_addr) ||
  600. compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
  601. priv->active_rxon.wlap_bssid_addr) ||
  602. (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
  603. (priv->staging_rxon.channel != priv->active_rxon.channel) ||
  604. (priv->staging_rxon.air_propagation !=
  605. priv->active_rxon.air_propagation) ||
  606. (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
  607. priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
  608. (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
  609. priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
  610. (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
  611. (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
  612. return 1;
  613. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  614. * be updated with the RXON_ASSOC command -- however only some
  615. * flag transitions are allowed using RXON_ASSOC */
  616. /* Check if we are not switching bands */
  617. if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
  618. (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
  619. return 1;
  620. /* Check if we are switching association toggle */
  621. if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
  622. (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
  623. return 1;
  624. return 0;
  625. }
  626. /**
  627. * iwl4965_commit_rxon - commit staging_rxon to hardware
  628. *
  629. * The RXON command in staging_rxon is committed to the hardware and
  630. * the active_rxon structure is updated with the new data. This
  631. * function correctly transitions out of the RXON_ASSOC_MSK state if
  632. * a HW tune is required based on the RXON structure changes.
  633. */
  634. static int iwl4965_commit_rxon(struct iwl_priv *priv)
  635. {
  636. /* cast away the const for active_rxon in this function */
  637. struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
  638. DECLARE_MAC_BUF(mac);
  639. int rc = 0;
  640. if (!iwl_is_alive(priv))
  641. return -1;
  642. /* always get timestamp with Rx frame */
  643. priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
  644. rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
  645. if (rc) {
  646. IWL_ERROR("Invalid RXON configuration. Not committing.\n");
  647. return -EINVAL;
  648. }
  649. /* If we don't need to send a full RXON, we can use
  650. * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
  651. * and other flags for the current radio configuration. */
  652. if (!iwl4965_full_rxon_required(priv)) {
  653. rc = iwl_send_rxon_assoc(priv);
  654. if (rc) {
  655. IWL_ERROR("Error setting RXON_ASSOC "
  656. "configuration (%d).\n", rc);
  657. return rc;
  658. }
  659. memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
  660. return 0;
  661. }
  662. /* station table will be cleared */
  663. priv->assoc_station_added = 0;
  664. /* If we are currently associated and the new config requires
  665. * an RXON_ASSOC and the new config wants the associated mask enabled,
  666. * we must clear the associated from the active configuration
  667. * before we apply the new config */
  668. if (iwl_is_associated(priv) &&
  669. (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
  670. IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
  671. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  672. rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
  673. sizeof(struct iwl4965_rxon_cmd),
  674. &priv->active_rxon);
  675. /* If the mask clearing failed then we set
  676. * active_rxon back to what it was previously */
  677. if (rc) {
  678. active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
  679. IWL_ERROR("Error clearing ASSOC_MSK on current "
  680. "configuration (%d).\n", rc);
  681. return rc;
  682. }
  683. }
  684. IWL_DEBUG_INFO("Sending RXON\n"
  685. "* with%s RXON_FILTER_ASSOC_MSK\n"
  686. "* channel = %d\n"
  687. "* bssid = %s\n",
  688. ((priv->staging_rxon.filter_flags &
  689. RXON_FILTER_ASSOC_MSK) ? "" : "out"),
  690. le16_to_cpu(priv->staging_rxon.channel),
  691. print_mac(mac, priv->staging_rxon.bssid_addr));
  692. iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
  693. /* Apply the new configuration */
  694. rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
  695. sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
  696. if (rc) {
  697. IWL_ERROR("Error setting new configuration (%d).\n", rc);
  698. return rc;
  699. }
  700. iwlcore_clear_stations_table(priv);
  701. if (!priv->error_recovering)
  702. priv->start_calib = 0;
  703. iwl_init_sensitivity(priv);
  704. memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
  705. /* If we issue a new RXON command which required a tune then we must
  706. * send a new TXPOWER command or we won't be able to Tx any frames */
  707. rc = iwl4965_hw_reg_send_txpower(priv);
  708. if (rc) {
  709. IWL_ERROR("Error setting Tx power (%d).\n", rc);
  710. return rc;
  711. }
  712. /* Add the broadcast address so we can send broadcast frames */
  713. if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
  714. IWL_INVALID_STATION) {
  715. IWL_ERROR("Error adding BROADCAST address for transmit.\n");
  716. return -EIO;
  717. }
  718. /* If we have set the ASSOC_MSK and we are in BSS mode then
  719. * add the IWL_AP_ID to the station rate table */
  720. if (iwl_is_associated(priv) &&
  721. (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
  722. if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
  723. == IWL_INVALID_STATION) {
  724. IWL_ERROR("Error adding AP address for transmit.\n");
  725. return -EIO;
  726. }
  727. priv->assoc_station_added = 1;
  728. if (priv->default_wep_key &&
  729. iwl_send_static_wepkey_cmd(priv, 0))
  730. IWL_ERROR("Could not send WEP static key.\n");
  731. }
  732. return 0;
  733. }
  734. void iwl4965_update_chain_flags(struct iwl_priv *priv)
  735. {
  736. iwl4965_set_rxon_chain(priv);
  737. iwl4965_commit_rxon(priv);
  738. }
  739. static int iwl4965_send_bt_config(struct iwl_priv *priv)
  740. {
  741. struct iwl4965_bt_cmd bt_cmd = {
  742. .flags = 3,
  743. .lead_time = 0xAA,
  744. .max_kill = 1,
  745. .kill_ack_mask = 0,
  746. .kill_cts_mask = 0,
  747. };
  748. return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  749. sizeof(struct iwl4965_bt_cmd), &bt_cmd);
  750. }
  751. static int iwl4965_send_scan_abort(struct iwl_priv *priv)
  752. {
  753. int rc = 0;
  754. struct iwl4965_rx_packet *res;
  755. struct iwl_host_cmd cmd = {
  756. .id = REPLY_SCAN_ABORT_CMD,
  757. .meta.flags = CMD_WANT_SKB,
  758. };
  759. /* If there isn't a scan actively going on in the hardware
  760. * then we are in between scan bands and not actually
  761. * actively scanning, so don't send the abort command */
  762. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  763. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  764. return 0;
  765. }
  766. rc = iwl_send_cmd_sync(priv, &cmd);
  767. if (rc) {
  768. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  769. return rc;
  770. }
  771. res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
  772. if (res->u.status != CAN_ABORT_STATUS) {
  773. /* The scan abort will return 1 for success or
  774. * 2 for "failure". A failure condition can be
  775. * due to simply not being in an active scan which
  776. * can occur if we send the scan abort before we
  777. * the microcode has notified us that a scan is
  778. * completed. */
  779. IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
  780. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  781. clear_bit(STATUS_SCAN_HW, &priv->status);
  782. }
  783. dev_kfree_skb_any(cmd.meta.u.skb);
  784. return rc;
  785. }
  786. /*
  787. * CARD_STATE_CMD
  788. *
  789. * Use: Sets the device's internal card state to enable, disable, or halt
  790. *
  791. * When in the 'enable' state the card operates as normal.
  792. * When in the 'disable' state, the card enters into a low power mode.
  793. * When in the 'halt' state, the card is shut down and must be fully
  794. * restarted to come back on.
  795. */
  796. static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
  797. {
  798. struct iwl_host_cmd cmd = {
  799. .id = REPLY_CARD_STATE_CMD,
  800. .len = sizeof(u32),
  801. .data = &flags,
  802. .meta.flags = meta_flag,
  803. };
  804. return iwl_send_cmd(priv, &cmd);
  805. }
  806. int iwl4965_send_add_station(struct iwl_priv *priv,
  807. struct iwl4965_addsta_cmd *sta, u8 flags)
  808. {
  809. struct iwl4965_rx_packet *res = NULL;
  810. int rc = 0;
  811. struct iwl_host_cmd cmd = {
  812. .id = REPLY_ADD_STA,
  813. .len = sizeof(struct iwl4965_addsta_cmd),
  814. .meta.flags = flags,
  815. .data = sta,
  816. };
  817. if (!(flags & CMD_ASYNC))
  818. cmd.meta.flags |= CMD_WANT_SKB;
  819. rc = iwl_send_cmd(priv, &cmd);
  820. if (rc || (flags & CMD_ASYNC))
  821. return rc;
  822. res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
  823. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  824. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  825. res->hdr.flags);
  826. rc = -EIO;
  827. }
  828. if (rc == 0) {
  829. switch (res->u.add_sta.status) {
  830. case ADD_STA_SUCCESS_MSK:
  831. IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
  832. break;
  833. default:
  834. rc = -EIO;
  835. IWL_WARNING("REPLY_ADD_STA failed\n");
  836. break;
  837. }
  838. }
  839. priv->alloc_rxb_skb--;
  840. dev_kfree_skb_any(cmd.meta.u.skb);
  841. return rc;
  842. }
  843. static void iwl4965_clear_free_frames(struct iwl_priv *priv)
  844. {
  845. struct list_head *element;
  846. IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
  847. priv->frames_count);
  848. while (!list_empty(&priv->free_frames)) {
  849. element = priv->free_frames.next;
  850. list_del(element);
  851. kfree(list_entry(element, struct iwl4965_frame, list));
  852. priv->frames_count--;
  853. }
  854. if (priv->frames_count) {
  855. IWL_WARNING("%d frames still in use. Did we lose one?\n",
  856. priv->frames_count);
  857. priv->frames_count = 0;
  858. }
  859. }
  860. static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
  861. {
  862. struct iwl4965_frame *frame;
  863. struct list_head *element;
  864. if (list_empty(&priv->free_frames)) {
  865. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  866. if (!frame) {
  867. IWL_ERROR("Could not allocate frame!\n");
  868. return NULL;
  869. }
  870. priv->frames_count++;
  871. return frame;
  872. }
  873. element = priv->free_frames.next;
  874. list_del(element);
  875. return list_entry(element, struct iwl4965_frame, list);
  876. }
  877. static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
  878. {
  879. memset(frame, 0, sizeof(*frame));
  880. list_add(&frame->list, &priv->free_frames);
  881. }
  882. unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
  883. struct ieee80211_hdr *hdr,
  884. const u8 *dest, int left)
  885. {
  886. if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
  887. ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
  888. (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
  889. return 0;
  890. if (priv->ibss_beacon->len > left)
  891. return 0;
  892. memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
  893. return priv->ibss_beacon->len;
  894. }
  895. static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
  896. {
  897. u8 i;
  898. for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
  899. i = iwl4965_rates[i].next_ieee) {
  900. if (rate_mask & (1 << i))
  901. return iwl4965_rates[i].plcp;
  902. }
  903. return IWL_RATE_INVALID;
  904. }
  905. static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
  906. {
  907. struct iwl4965_frame *frame;
  908. unsigned int frame_size;
  909. int rc;
  910. u8 rate;
  911. frame = iwl4965_get_free_frame(priv);
  912. if (!frame) {
  913. IWL_ERROR("Could not obtain free frame buffer for beacon "
  914. "command.\n");
  915. return -ENOMEM;
  916. }
  917. if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
  918. rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
  919. 0xFF0);
  920. if (rate == IWL_INVALID_RATE)
  921. rate = IWL_RATE_6M_PLCP;
  922. } else {
  923. rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
  924. if (rate == IWL_INVALID_RATE)
  925. rate = IWL_RATE_1M_PLCP;
  926. }
  927. frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
  928. rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  929. &frame->u.cmd[0]);
  930. iwl4965_free_frame(priv, frame);
  931. return rc;
  932. }
  933. /******************************************************************************
  934. *
  935. * Misc. internal state and helper functions
  936. *
  937. ******************************************************************************/
  938. /**
  939. * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
  940. *
  941. * return : set the bit for each supported rate insert in ie
  942. */
  943. static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
  944. u16 basic_rate, int *left)
  945. {
  946. u16 ret_rates = 0, bit;
  947. int i;
  948. u8 *cnt = ie;
  949. u8 *rates = ie + 1;
  950. for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
  951. if (bit & supported_rate) {
  952. ret_rates |= bit;
  953. rates[*cnt] = iwl4965_rates[i].ieee |
  954. ((bit & basic_rate) ? 0x80 : 0x00);
  955. (*cnt)++;
  956. (*left)--;
  957. if ((*left <= 0) ||
  958. (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
  959. break;
  960. }
  961. }
  962. return ret_rates;
  963. }
  964. #ifdef CONFIG_IWL4965_HT
  965. static void iwl4965_ht_conf(struct iwl_priv *priv,
  966. struct ieee80211_bss_conf *bss_conf)
  967. {
  968. struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
  969. struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
  970. struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
  971. IWL_DEBUG_MAC80211("enter: \n");
  972. iwl_conf->is_ht = bss_conf->assoc_ht;
  973. if (!iwl_conf->is_ht)
  974. return;
  975. priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
  976. if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
  977. iwl_conf->sgf |= 0x1;
  978. if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
  979. iwl_conf->sgf |= 0x2;
  980. iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
  981. iwl_conf->max_amsdu_size =
  982. !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
  983. iwl_conf->supported_chan_width =
  984. !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
  985. iwl_conf->extension_chan_offset =
  986. ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
  987. /* If no above or below channel supplied disable FAT channel */
  988. if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
  989. iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
  990. iwl_conf->supported_chan_width = 0;
  991. iwl_conf->tx_mimo_ps_mode =
  992. (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
  993. memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
  994. iwl_conf->control_channel = ht_bss_conf->primary_channel;
  995. iwl_conf->tx_chan_width =
  996. !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
  997. iwl_conf->ht_protection =
  998. ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
  999. iwl_conf->non_GF_STA_present =
  1000. !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
  1001. IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
  1002. IWL_DEBUG_MAC80211("leave\n");
  1003. }
  1004. static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
  1005. u8 *pos, int *left)
  1006. {
  1007. struct ieee80211_ht_cap *ht_cap;
  1008. if (!sband || !sband->ht_info.ht_supported)
  1009. return;
  1010. if (*left < sizeof(struct ieee80211_ht_cap))
  1011. return;
  1012. *pos++ = sizeof(struct ieee80211_ht_cap);
  1013. ht_cap = (struct ieee80211_ht_cap *) pos;
  1014. ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
  1015. memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
  1016. ht_cap->ampdu_params_info =
  1017. (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
  1018. ((sband->ht_info.ampdu_density << 2) &
  1019. IEEE80211_HT_CAP_AMPDU_DENSITY);
  1020. *left -= sizeof(struct ieee80211_ht_cap);
  1021. }
  1022. #else
  1023. static inline void iwl4965_ht_conf(struct iwl_priv *priv,
  1024. struct ieee80211_bss_conf *bss_conf)
  1025. {
  1026. }
  1027. static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
  1028. u8 *pos, int *left)
  1029. {
  1030. }
  1031. #endif
  1032. /**
  1033. * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
  1034. */
  1035. static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
  1036. enum ieee80211_band band,
  1037. struct ieee80211_mgmt *frame,
  1038. int left, int is_direct)
  1039. {
  1040. int len = 0;
  1041. u8 *pos = NULL;
  1042. u16 active_rates, ret_rates, cck_rates, active_rate_basic;
  1043. const struct ieee80211_supported_band *sband =
  1044. iwl_get_hw_mode(priv, band);
  1045. /* Make sure there is enough space for the probe request,
  1046. * two mandatory IEs and the data */
  1047. left -= 24;
  1048. if (left < 0)
  1049. return 0;
  1050. len += 24;
  1051. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1052. memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
  1053. memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
  1054. memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
  1055. frame->seq_ctrl = 0;
  1056. /* fill in our indirect SSID IE */
  1057. /* ...next IE... */
  1058. left -= 2;
  1059. if (left < 0)
  1060. return 0;
  1061. len += 2;
  1062. pos = &(frame->u.probe_req.variable[0]);
  1063. *pos++ = WLAN_EID_SSID;
  1064. *pos++ = 0;
  1065. /* fill in our direct SSID IE... */
  1066. if (is_direct) {
  1067. /* ...next IE... */
  1068. left -= 2 + priv->essid_len;
  1069. if (left < 0)
  1070. return 0;
  1071. /* ... fill it in... */
  1072. *pos++ = WLAN_EID_SSID;
  1073. *pos++ = priv->essid_len;
  1074. memcpy(pos, priv->essid, priv->essid_len);
  1075. pos += priv->essid_len;
  1076. len += 2 + priv->essid_len;
  1077. }
  1078. /* fill in supported rate */
  1079. /* ...next IE... */
  1080. left -= 2;
  1081. if (left < 0)
  1082. return 0;
  1083. /* ... fill it in... */
  1084. *pos++ = WLAN_EID_SUPP_RATES;
  1085. *pos = 0;
  1086. /* exclude 60M rate */
  1087. active_rates = priv->rates_mask;
  1088. active_rates &= ~IWL_RATE_60M_MASK;
  1089. active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
  1090. cck_rates = IWL_CCK_RATES_MASK & active_rates;
  1091. ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
  1092. active_rate_basic, &left);
  1093. active_rates &= ~ret_rates;
  1094. ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
  1095. active_rate_basic, &left);
  1096. active_rates &= ~ret_rates;
  1097. len += 2 + *pos;
  1098. pos += (*pos) + 1;
  1099. if (active_rates == 0)
  1100. goto fill_end;
  1101. /* fill in supported extended rate */
  1102. /* ...next IE... */
  1103. left -= 2;
  1104. if (left < 0)
  1105. return 0;
  1106. /* ... fill it in... */
  1107. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1108. *pos = 0;
  1109. iwl4965_supported_rate_to_ie(pos, active_rates,
  1110. active_rate_basic, &left);
  1111. if (*pos > 0)
  1112. len += 2 + *pos;
  1113. fill_end:
  1114. /* fill in HT IE */
  1115. left -= 2;
  1116. if (left < 0)
  1117. return 0;
  1118. *pos++ = WLAN_EID_HT_CAPABILITY;
  1119. *pos = 0;
  1120. iwl_ht_cap_to_ie(sband, pos, &left);
  1121. if (*pos > 0)
  1122. len += 2 + *pos;
  1123. return (u16)len;
  1124. }
  1125. /*
  1126. * QoS support
  1127. */
  1128. static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
  1129. struct iwl4965_qosparam_cmd *qos)
  1130. {
  1131. return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
  1132. sizeof(struct iwl4965_qosparam_cmd), qos);
  1133. }
  1134. static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
  1135. {
  1136. unsigned long flags;
  1137. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1138. return;
  1139. if (!priv->qos_data.qos_enable)
  1140. return;
  1141. spin_lock_irqsave(&priv->lock, flags);
  1142. priv->qos_data.def_qos_parm.qos_flags = 0;
  1143. if (priv->qos_data.qos_cap.q_AP.queue_request &&
  1144. !priv->qos_data.qos_cap.q_AP.txop_request)
  1145. priv->qos_data.def_qos_parm.qos_flags |=
  1146. QOS_PARAM_FLG_TXOP_TYPE_MSK;
  1147. if (priv->qos_data.qos_active)
  1148. priv->qos_data.def_qos_parm.qos_flags |=
  1149. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  1150. #ifdef CONFIG_IWL4965_HT
  1151. if (priv->current_ht_config.is_ht)
  1152. priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
  1153. #endif /* CONFIG_IWL4965_HT */
  1154. spin_unlock_irqrestore(&priv->lock, flags);
  1155. if (force || iwl_is_associated(priv)) {
  1156. IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
  1157. priv->qos_data.qos_active,
  1158. priv->qos_data.def_qos_parm.qos_flags);
  1159. iwl4965_send_qos_params_command(priv,
  1160. &(priv->qos_data.def_qos_parm));
  1161. }
  1162. }
  1163. int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
  1164. {
  1165. /* Filter incoming packets to determine if they are targeted toward
  1166. * this network, discarding packets coming from ourselves */
  1167. switch (priv->iw_mode) {
  1168. case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
  1169. /* packets from our adapter are dropped (echo) */
  1170. if (!compare_ether_addr(header->addr2, priv->mac_addr))
  1171. return 0;
  1172. /* {broad,multi}cast packets to our IBSS go through */
  1173. if (is_multicast_ether_addr(header->addr1))
  1174. return !compare_ether_addr(header->addr3, priv->bssid);
  1175. /* packets to our adapter go through */
  1176. return !compare_ether_addr(header->addr1, priv->mac_addr);
  1177. case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
  1178. /* packets from our adapter are dropped (echo) */
  1179. if (!compare_ether_addr(header->addr3, priv->mac_addr))
  1180. return 0;
  1181. /* {broad,multi}cast packets to our BSS go through */
  1182. if (is_multicast_ether_addr(header->addr1))
  1183. return !compare_ether_addr(header->addr2, priv->bssid);
  1184. /* packets to our adapter go through */
  1185. return !compare_ether_addr(header->addr1, priv->mac_addr);
  1186. default:
  1187. break;
  1188. }
  1189. return 1;
  1190. }
  1191. #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
  1192. static const char *iwl4965_get_tx_fail_reason(u32 status)
  1193. {
  1194. switch (status & TX_STATUS_MSK) {
  1195. case TX_STATUS_SUCCESS:
  1196. return "SUCCESS";
  1197. TX_STATUS_ENTRY(SHORT_LIMIT);
  1198. TX_STATUS_ENTRY(LONG_LIMIT);
  1199. TX_STATUS_ENTRY(FIFO_UNDERRUN);
  1200. TX_STATUS_ENTRY(MGMNT_ABORT);
  1201. TX_STATUS_ENTRY(NEXT_FRAG);
  1202. TX_STATUS_ENTRY(LIFE_EXPIRE);
  1203. TX_STATUS_ENTRY(DEST_PS);
  1204. TX_STATUS_ENTRY(ABORTED);
  1205. TX_STATUS_ENTRY(BT_RETRY);
  1206. TX_STATUS_ENTRY(STA_INVALID);
  1207. TX_STATUS_ENTRY(FRAG_DROPPED);
  1208. TX_STATUS_ENTRY(TID_DISABLE);
  1209. TX_STATUS_ENTRY(FRAME_FLUSHED);
  1210. TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
  1211. TX_STATUS_ENTRY(TX_LOCKED);
  1212. TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
  1213. }
  1214. return "UNKNOWN";
  1215. }
  1216. /**
  1217. * iwl4965_scan_cancel - Cancel any currently executing HW scan
  1218. *
  1219. * NOTE: priv->mutex is not required before calling this function
  1220. */
  1221. static int iwl4965_scan_cancel(struct iwl_priv *priv)
  1222. {
  1223. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  1224. clear_bit(STATUS_SCANNING, &priv->status);
  1225. return 0;
  1226. }
  1227. if (test_bit(STATUS_SCANNING, &priv->status)) {
  1228. if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1229. IWL_DEBUG_SCAN("Queuing scan abort.\n");
  1230. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  1231. queue_work(priv->workqueue, &priv->abort_scan);
  1232. } else
  1233. IWL_DEBUG_SCAN("Scan abort already in progress.\n");
  1234. return test_bit(STATUS_SCANNING, &priv->status);
  1235. }
  1236. return 0;
  1237. }
  1238. /**
  1239. * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
  1240. * @ms: amount of time to wait (in milliseconds) for scan to abort
  1241. *
  1242. * NOTE: priv->mutex must be held before calling this function
  1243. */
  1244. static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
  1245. {
  1246. unsigned long now = jiffies;
  1247. int ret;
  1248. ret = iwl4965_scan_cancel(priv);
  1249. if (ret && ms) {
  1250. mutex_unlock(&priv->mutex);
  1251. while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
  1252. test_bit(STATUS_SCANNING, &priv->status))
  1253. msleep(1);
  1254. mutex_lock(&priv->mutex);
  1255. return test_bit(STATUS_SCANNING, &priv->status);
  1256. }
  1257. return ret;
  1258. }
  1259. static void iwl4965_sequence_reset(struct iwl_priv *priv)
  1260. {
  1261. /* Reset ieee stats */
  1262. /* We don't reset the net_device_stats (ieee->stats) on
  1263. * re-association */
  1264. priv->last_seq_num = -1;
  1265. priv->last_frag_num = -1;
  1266. priv->last_packet_time = 0;
  1267. iwl4965_scan_cancel(priv);
  1268. }
  1269. #define MAX_UCODE_BEACON_INTERVAL 4096
  1270. #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
  1271. static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
  1272. {
  1273. u16 new_val = 0;
  1274. u16 beacon_factor = 0;
  1275. beacon_factor =
  1276. (beacon_val + MAX_UCODE_BEACON_INTERVAL)
  1277. / MAX_UCODE_BEACON_INTERVAL;
  1278. new_val = beacon_val / beacon_factor;
  1279. return cpu_to_le16(new_val);
  1280. }
  1281. static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
  1282. {
  1283. u64 interval_tm_unit;
  1284. u64 tsf, result;
  1285. unsigned long flags;
  1286. struct ieee80211_conf *conf = NULL;
  1287. u16 beacon_int = 0;
  1288. conf = ieee80211_get_hw_conf(priv->hw);
  1289. spin_lock_irqsave(&priv->lock, flags);
  1290. priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
  1291. priv->rxon_timing.timestamp.dw[0] =
  1292. cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
  1293. priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
  1294. tsf = priv->timestamp;
  1295. beacon_int = priv->beacon_int;
  1296. spin_unlock_irqrestore(&priv->lock, flags);
  1297. if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
  1298. if (beacon_int == 0) {
  1299. priv->rxon_timing.beacon_interval = cpu_to_le16(100);
  1300. priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
  1301. } else {
  1302. priv->rxon_timing.beacon_interval =
  1303. cpu_to_le16(beacon_int);
  1304. priv->rxon_timing.beacon_interval =
  1305. iwl4965_adjust_beacon_interval(
  1306. le16_to_cpu(priv->rxon_timing.beacon_interval));
  1307. }
  1308. priv->rxon_timing.atim_window = 0;
  1309. } else {
  1310. priv->rxon_timing.beacon_interval =
  1311. iwl4965_adjust_beacon_interval(conf->beacon_int);
  1312. /* TODO: we need to get atim_window from upper stack
  1313. * for now we set to 0 */
  1314. priv->rxon_timing.atim_window = 0;
  1315. }
  1316. interval_tm_unit =
  1317. (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
  1318. result = do_div(tsf, interval_tm_unit);
  1319. priv->rxon_timing.beacon_init_val =
  1320. cpu_to_le32((u32) ((u64) interval_tm_unit - result));
  1321. IWL_DEBUG_ASSOC
  1322. ("beacon interval %d beacon timer %d beacon tim %d\n",
  1323. le16_to_cpu(priv->rxon_timing.beacon_interval),
  1324. le32_to_cpu(priv->rxon_timing.beacon_init_val),
  1325. le16_to_cpu(priv->rxon_timing.atim_window));
  1326. }
  1327. static int iwl4965_scan_initiate(struct iwl_priv *priv)
  1328. {
  1329. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  1330. IWL_ERROR("APs don't scan.\n");
  1331. return 0;
  1332. }
  1333. if (!iwl_is_ready_rf(priv)) {
  1334. IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
  1335. return -EIO;
  1336. }
  1337. if (test_bit(STATUS_SCANNING, &priv->status)) {
  1338. IWL_DEBUG_SCAN("Scan already in progress.\n");
  1339. return -EAGAIN;
  1340. }
  1341. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1342. IWL_DEBUG_SCAN("Scan request while abort pending. "
  1343. "Queuing.\n");
  1344. return -EAGAIN;
  1345. }
  1346. IWL_DEBUG_INFO("Starting scan...\n");
  1347. priv->scan_bands = 2;
  1348. set_bit(STATUS_SCANNING, &priv->status);
  1349. priv->scan_start = jiffies;
  1350. priv->scan_pass_start = priv->scan_start;
  1351. queue_work(priv->workqueue, &priv->request_scan);
  1352. return 0;
  1353. }
  1354. static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
  1355. enum ieee80211_band band)
  1356. {
  1357. if (band == IEEE80211_BAND_5GHZ) {
  1358. priv->staging_rxon.flags &=
  1359. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  1360. | RXON_FLG_CCK_MSK);
  1361. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  1362. } else {
  1363. /* Copied from iwl4965_post_associate() */
  1364. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  1365. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  1366. else
  1367. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  1368. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  1369. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  1370. priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
  1371. priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
  1372. priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
  1373. }
  1374. }
  1375. /*
  1376. * initialize rxon structure with default values from eeprom
  1377. */
  1378. static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
  1379. {
  1380. const struct iwl_channel_info *ch_info;
  1381. memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
  1382. switch (priv->iw_mode) {
  1383. case IEEE80211_IF_TYPE_AP:
  1384. priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
  1385. break;
  1386. case IEEE80211_IF_TYPE_STA:
  1387. priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
  1388. priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  1389. break;
  1390. case IEEE80211_IF_TYPE_IBSS:
  1391. priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
  1392. priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  1393. priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  1394. RXON_FILTER_ACCEPT_GRP_MSK;
  1395. break;
  1396. case IEEE80211_IF_TYPE_MNTR:
  1397. priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
  1398. priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
  1399. RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
  1400. break;
  1401. default:
  1402. IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
  1403. break;
  1404. }
  1405. #if 0
  1406. /* TODO: Figure out when short_preamble would be set and cache from
  1407. * that */
  1408. if (!hw_to_local(priv->hw)->short_preamble)
  1409. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  1410. else
  1411. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  1412. #endif
  1413. ch_info = iwl_get_channel_info(priv, priv->band,
  1414. le16_to_cpu(priv->staging_rxon.channel));
  1415. if (!ch_info)
  1416. ch_info = &priv->channel_info[0];
  1417. /*
  1418. * in some case A channels are all non IBSS
  1419. * in this case force B/G channel
  1420. */
  1421. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  1422. !(is_channel_ibss(ch_info)))
  1423. ch_info = &priv->channel_info[0];
  1424. priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
  1425. priv->band = ch_info->band;
  1426. iwl4965_set_flags_for_phymode(priv, priv->band);
  1427. priv->staging_rxon.ofdm_basic_rates =
  1428. (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  1429. priv->staging_rxon.cck_basic_rates =
  1430. (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  1431. priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
  1432. RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
  1433. memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
  1434. memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
  1435. priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
  1436. priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
  1437. iwl4965_set_rxon_chain(priv);
  1438. }
  1439. static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
  1440. {
  1441. if (mode == IEEE80211_IF_TYPE_IBSS) {
  1442. const struct iwl_channel_info *ch_info;
  1443. ch_info = iwl_get_channel_info(priv,
  1444. priv->band,
  1445. le16_to_cpu(priv->staging_rxon.channel));
  1446. if (!ch_info || !is_channel_ibss(ch_info)) {
  1447. IWL_ERROR("channel %d not IBSS channel\n",
  1448. le16_to_cpu(priv->staging_rxon.channel));
  1449. return -EINVAL;
  1450. }
  1451. }
  1452. priv->iw_mode = mode;
  1453. iwl4965_connection_init_rx_config(priv);
  1454. memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
  1455. iwlcore_clear_stations_table(priv);
  1456. /* dont commit rxon if rf-kill is on*/
  1457. if (!iwl_is_ready_rf(priv))
  1458. return -EAGAIN;
  1459. cancel_delayed_work(&priv->scan_check);
  1460. if (iwl4965_scan_cancel_timeout(priv, 100)) {
  1461. IWL_WARNING("Aborted scan still in progress after 100ms\n");
  1462. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  1463. return -EAGAIN;
  1464. }
  1465. iwl4965_commit_rxon(priv);
  1466. return 0;
  1467. }
  1468. static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
  1469. struct ieee80211_tx_control *ctl,
  1470. struct iwl_cmd *cmd,
  1471. struct sk_buff *skb_frag,
  1472. int sta_id)
  1473. {
  1474. struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
  1475. struct iwl_wep_key *wepkey;
  1476. int keyidx = 0;
  1477. BUG_ON(ctl->hw_key->hw_key_idx > 3);
  1478. switch (keyinfo->alg) {
  1479. case ALG_CCMP:
  1480. cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
  1481. memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
  1482. if (ctl->flags & IEEE80211_TXCTL_AMPDU)
  1483. cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  1484. IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
  1485. break;
  1486. case ALG_TKIP:
  1487. cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
  1488. ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
  1489. IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
  1490. IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
  1491. break;
  1492. case ALG_WEP:
  1493. wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
  1494. cmd->cmd.tx.sec_ctl = 0;
  1495. if (priv->default_wep_key) {
  1496. /* the WEP key was sent as static */
  1497. keyidx = ctl->hw_key->hw_key_idx;
  1498. memcpy(&cmd->cmd.tx.key[3], wepkey->key,
  1499. wepkey->key_size);
  1500. if (wepkey->key_size == WEP_KEY_LEN_128)
  1501. cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
  1502. } else {
  1503. /* the WEP key was sent as dynamic */
  1504. keyidx = keyinfo->keyidx;
  1505. memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
  1506. keyinfo->keylen);
  1507. if (keyinfo->keylen == WEP_KEY_LEN_128)
  1508. cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
  1509. }
  1510. cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
  1511. (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  1512. IWL_DEBUG_TX("Configuring packet for WEP encryption "
  1513. "with key %d\n", keyidx);
  1514. break;
  1515. default:
  1516. printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
  1517. break;
  1518. }
  1519. }
  1520. /*
  1521. * handle build REPLY_TX command notification.
  1522. */
  1523. static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
  1524. struct iwl_cmd *cmd,
  1525. struct ieee80211_tx_control *ctrl,
  1526. struct ieee80211_hdr *hdr,
  1527. int is_unicast, u8 std_id)
  1528. {
  1529. __le16 *qc;
  1530. u16 fc = le16_to_cpu(hdr->frame_control);
  1531. __le32 tx_flags = cmd->cmd.tx.tx_flags;
  1532. cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1533. if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
  1534. tx_flags |= TX_CMD_FLG_ACK_MSK;
  1535. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
  1536. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1537. if (ieee80211_is_probe_response(fc) &&
  1538. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  1539. tx_flags |= TX_CMD_FLG_TSF_MSK;
  1540. } else {
  1541. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  1542. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1543. }
  1544. if (ieee80211_is_back_request(fc))
  1545. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  1546. cmd->cmd.tx.sta_id = std_id;
  1547. if (ieee80211_get_morefrag(hdr))
  1548. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  1549. qc = ieee80211_get_qos_ctrl(hdr);
  1550. if (qc) {
  1551. cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
  1552. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  1553. } else
  1554. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1555. if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  1556. tx_flags |= TX_CMD_FLG_RTS_MSK;
  1557. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  1558. } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
  1559. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  1560. tx_flags |= TX_CMD_FLG_CTS_MSK;
  1561. }
  1562. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  1563. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  1564. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  1565. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
  1566. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
  1567. (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
  1568. cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
  1569. else
  1570. cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
  1571. } else {
  1572. cmd->cmd.tx.timeout.pm_frame_timeout = 0;
  1573. }
  1574. cmd->cmd.tx.driver_txop = 0;
  1575. cmd->cmd.tx.tx_flags = tx_flags;
  1576. cmd->cmd.tx.next_frame_len = 0;
  1577. }
  1578. static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  1579. {
  1580. /* 0 - mgmt, 1 - cnt, 2 - data */
  1581. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  1582. priv->tx_stats[idx].cnt++;
  1583. priv->tx_stats[idx].bytes += len;
  1584. }
  1585. /**
  1586. * iwl4965_get_sta_id - Find station's index within station table
  1587. *
  1588. * If new IBSS station, create new entry in station table
  1589. */
  1590. static int iwl4965_get_sta_id(struct iwl_priv *priv,
  1591. struct ieee80211_hdr *hdr)
  1592. {
  1593. int sta_id;
  1594. u16 fc = le16_to_cpu(hdr->frame_control);
  1595. DECLARE_MAC_BUF(mac);
  1596. /* If this frame is broadcast or management, use broadcast station id */
  1597. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  1598. is_multicast_ether_addr(hdr->addr1))
  1599. return priv->hw_params.bcast_sta_id;
  1600. switch (priv->iw_mode) {
  1601. /* If we are a client station in a BSS network, use the special
  1602. * AP station entry (that's the only station we communicate with) */
  1603. case IEEE80211_IF_TYPE_STA:
  1604. return IWL_AP_ID;
  1605. /* If we are an AP, then find the station, or use BCAST */
  1606. case IEEE80211_IF_TYPE_AP:
  1607. sta_id = iwl_find_station(priv, hdr->addr1);
  1608. if (sta_id != IWL_INVALID_STATION)
  1609. return sta_id;
  1610. return priv->hw_params.bcast_sta_id;
  1611. /* If this frame is going out to an IBSS network, find the station,
  1612. * or create a new station table entry */
  1613. case IEEE80211_IF_TYPE_IBSS:
  1614. sta_id = iwl_find_station(priv, hdr->addr1);
  1615. if (sta_id != IWL_INVALID_STATION)
  1616. return sta_id;
  1617. /* Create new station table entry */
  1618. sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
  1619. 0, CMD_ASYNC, NULL);
  1620. if (sta_id != IWL_INVALID_STATION)
  1621. return sta_id;
  1622. IWL_DEBUG_DROP("Station %s not in station map. "
  1623. "Defaulting to broadcast...\n",
  1624. print_mac(mac, hdr->addr1));
  1625. iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  1626. return priv->hw_params.bcast_sta_id;
  1627. default:
  1628. IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
  1629. return priv->hw_params.bcast_sta_id;
  1630. }
  1631. }
  1632. /*
  1633. * start REPLY_TX command process
  1634. */
  1635. static int iwl4965_tx_skb(struct iwl_priv *priv,
  1636. struct sk_buff *skb, struct ieee80211_tx_control *ctl)
  1637. {
  1638. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1639. struct iwl4965_tfd_frame *tfd;
  1640. u32 *control_flags;
  1641. int txq_id = ctl->queue;
  1642. struct iwl4965_tx_queue *txq = NULL;
  1643. struct iwl4965_queue *q = NULL;
  1644. dma_addr_t phys_addr;
  1645. dma_addr_t txcmd_phys;
  1646. dma_addr_t scratch_phys;
  1647. struct iwl_cmd *out_cmd = NULL;
  1648. u16 len, idx, len_org;
  1649. u8 id, hdr_len, unicast;
  1650. u8 sta_id;
  1651. u16 seq_number = 0;
  1652. u16 fc;
  1653. __le16 *qc;
  1654. u8 wait_write_ptr = 0;
  1655. unsigned long flags;
  1656. int rc;
  1657. spin_lock_irqsave(&priv->lock, flags);
  1658. if (iwl_is_rfkill(priv)) {
  1659. IWL_DEBUG_DROP("Dropping - RF KILL\n");
  1660. goto drop_unlock;
  1661. }
  1662. if (!priv->vif) {
  1663. IWL_DEBUG_DROP("Dropping - !priv->vif\n");
  1664. goto drop_unlock;
  1665. }
  1666. if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
  1667. IWL_ERROR("ERROR: No TX rate available.\n");
  1668. goto drop_unlock;
  1669. }
  1670. unicast = !is_multicast_ether_addr(hdr->addr1);
  1671. id = 0;
  1672. fc = le16_to_cpu(hdr->frame_control);
  1673. #ifdef CONFIG_IWLWIFI_DEBUG
  1674. if (ieee80211_is_auth(fc))
  1675. IWL_DEBUG_TX("Sending AUTH frame\n");
  1676. else if (ieee80211_is_assoc_request(fc))
  1677. IWL_DEBUG_TX("Sending ASSOC frame\n");
  1678. else if (ieee80211_is_reassoc_request(fc))
  1679. IWL_DEBUG_TX("Sending REASSOC frame\n");
  1680. #endif
  1681. /* drop all data frame if we are not associated */
  1682. if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  1683. (!iwl_is_associated(priv) ||
  1684. ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
  1685. !priv->assoc_station_added)) {
  1686. IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
  1687. goto drop_unlock;
  1688. }
  1689. spin_unlock_irqrestore(&priv->lock, flags);
  1690. hdr_len = ieee80211_get_hdrlen(fc);
  1691. /* Find (or create) index into station table for destination station */
  1692. sta_id = iwl4965_get_sta_id(priv, hdr);
  1693. if (sta_id == IWL_INVALID_STATION) {
  1694. DECLARE_MAC_BUF(mac);
  1695. IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
  1696. print_mac(mac, hdr->addr1));
  1697. goto drop;
  1698. }
  1699. IWL_DEBUG_RATE("station Id %d\n", sta_id);
  1700. qc = ieee80211_get_qos_ctrl(hdr);
  1701. if (qc) {
  1702. u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
  1703. seq_number = priv->stations[sta_id].tid[tid].seq_number &
  1704. IEEE80211_SCTL_SEQ;
  1705. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  1706. (hdr->seq_ctrl &
  1707. __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
  1708. seq_number += 0x10;
  1709. #ifdef CONFIG_IWL4965_HT
  1710. /* aggregation is on for this <sta,tid> */
  1711. if (ctl->flags & IEEE80211_TXCTL_AMPDU)
  1712. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  1713. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  1714. #endif /* CONFIG_IWL4965_HT */
  1715. }
  1716. /* Descriptor for chosen Tx queue */
  1717. txq = &priv->txq[txq_id];
  1718. q = &txq->q;
  1719. spin_lock_irqsave(&priv->lock, flags);
  1720. /* Set up first empty TFD within this queue's circular TFD buffer */
  1721. tfd = &txq->bd[q->write_ptr];
  1722. memset(tfd, 0, sizeof(*tfd));
  1723. control_flags = (u32 *) tfd;
  1724. idx = get_cmd_index(q, q->write_ptr, 0);
  1725. /* Set up driver data for this TFD */
  1726. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
  1727. txq->txb[q->write_ptr].skb[0] = skb;
  1728. memcpy(&(txq->txb[q->write_ptr].status.control),
  1729. ctl, sizeof(struct ieee80211_tx_control));
  1730. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1731. out_cmd = &txq->cmd[idx];
  1732. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  1733. memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
  1734. /*
  1735. * Set up the Tx-command (not MAC!) header.
  1736. * Store the chosen Tx queue and TFD index within the sequence field;
  1737. * after Tx, uCode's Tx response will return this value so driver can
  1738. * locate the frame within the tx queue and do post-tx processing.
  1739. */
  1740. out_cmd->hdr.cmd = REPLY_TX;
  1741. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1742. INDEX_TO_SEQ(q->write_ptr)));
  1743. /* Copy MAC header from skb into command buffer */
  1744. memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
  1745. /*
  1746. * Use the first empty entry in this queue's command buffer array
  1747. * to contain the Tx command and MAC header concatenated together
  1748. * (payload data will be in another buffer).
  1749. * Size of this varies, due to varying MAC header length.
  1750. * If end is not dword aligned, we'll have 2 extra bytes at the end
  1751. * of the MAC header (device reads on dword boundaries).
  1752. * We'll tell device about this padding later.
  1753. */
  1754. len = priv->hw_params.tx_cmd_len +
  1755. sizeof(struct iwl_cmd_header) + hdr_len;
  1756. len_org = len;
  1757. len = (len + 3) & ~3;
  1758. if (len_org != len)
  1759. len_org = 1;
  1760. else
  1761. len_org = 0;
  1762. /* Physical address of this Tx command's header (not MAC header!),
  1763. * within command buffer array. */
  1764. txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
  1765. offsetof(struct iwl_cmd, hdr);
  1766. /* Add buffer containing Tx command and MAC(!) header to TFD's
  1767. * first entry */
  1768. iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
  1769. if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
  1770. iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
  1771. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  1772. * if any (802.11 null frames have no payload). */
  1773. len = skb->len - hdr_len;
  1774. if (len) {
  1775. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  1776. len, PCI_DMA_TODEVICE);
  1777. iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
  1778. }
  1779. /* Tell 4965 about any 2-byte padding after MAC header */
  1780. if (len_org)
  1781. out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1782. /* Total # bytes to be transmitted */
  1783. len = (u16)skb->len;
  1784. out_cmd->cmd.tx.len = cpu_to_le16(len);
  1785. /* TODO need this for burst mode later on */
  1786. iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
  1787. /* set is_hcca to 0; it probably will never be implemented */
  1788. iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
  1789. iwl_update_tx_stats(priv, fc, len);
  1790. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  1791. offsetof(struct iwl4965_tx_cmd, scratch);
  1792. out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1793. out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
  1794. if (!ieee80211_get_morefrag(hdr)) {
  1795. txq->need_update = 1;
  1796. if (qc) {
  1797. u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
  1798. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  1799. }
  1800. } else {
  1801. wait_write_ptr = 1;
  1802. txq->need_update = 0;
  1803. }
  1804. iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
  1805. sizeof(out_cmd->cmd.tx));
  1806. iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
  1807. ieee80211_get_hdrlen(fc));
  1808. /* Set up entry for this TFD in Tx byte-count array */
  1809. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
  1810. /* Tell device the write index *just past* this latest filled TFD */
  1811. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1812. rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
  1813. spin_unlock_irqrestore(&priv->lock, flags);
  1814. if (rc)
  1815. return rc;
  1816. if ((iwl4965_queue_space(q) < q->high_mark)
  1817. && priv->mac80211_registered) {
  1818. if (wait_write_ptr) {
  1819. spin_lock_irqsave(&priv->lock, flags);
  1820. txq->need_update = 1;
  1821. iwl4965_tx_queue_update_write_ptr(priv, txq);
  1822. spin_unlock_irqrestore(&priv->lock, flags);
  1823. }
  1824. ieee80211_stop_queue(priv->hw, ctl->queue);
  1825. }
  1826. return 0;
  1827. drop_unlock:
  1828. spin_unlock_irqrestore(&priv->lock, flags);
  1829. drop:
  1830. return -1;
  1831. }
  1832. static void iwl4965_set_rate(struct iwl_priv *priv)
  1833. {
  1834. const struct ieee80211_supported_band *hw = NULL;
  1835. struct ieee80211_rate *rate;
  1836. int i;
  1837. hw = iwl_get_hw_mode(priv, priv->band);
  1838. if (!hw) {
  1839. IWL_ERROR("Failed to set rate: unable to get hw mode\n");
  1840. return;
  1841. }
  1842. priv->active_rate = 0;
  1843. priv->active_rate_basic = 0;
  1844. for (i = 0; i < hw->n_bitrates; i++) {
  1845. rate = &(hw->bitrates[i]);
  1846. if (rate->hw_value < IWL_RATE_COUNT)
  1847. priv->active_rate |= (1 << rate->hw_value);
  1848. }
  1849. IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
  1850. priv->active_rate, priv->active_rate_basic);
  1851. /*
  1852. * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
  1853. * otherwise set it to the default of all CCK rates and 6, 12, 24 for
  1854. * OFDM
  1855. */
  1856. if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
  1857. priv->staging_rxon.cck_basic_rates =
  1858. ((priv->active_rate_basic &
  1859. IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
  1860. else
  1861. priv->staging_rxon.cck_basic_rates =
  1862. (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  1863. if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
  1864. priv->staging_rxon.ofdm_basic_rates =
  1865. ((priv->active_rate_basic &
  1866. (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
  1867. IWL_FIRST_OFDM_RATE) & 0xFF;
  1868. else
  1869. priv->staging_rxon.ofdm_basic_rates =
  1870. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  1871. }
  1872. void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
  1873. {
  1874. unsigned long flags;
  1875. if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
  1876. return;
  1877. IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
  1878. disable_radio ? "OFF" : "ON");
  1879. if (disable_radio) {
  1880. iwl4965_scan_cancel(priv);
  1881. /* FIXME: This is a workaround for AP */
  1882. if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
  1883. spin_lock_irqsave(&priv->lock, flags);
  1884. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  1885. CSR_UCODE_SW_BIT_RFKILL);
  1886. spin_unlock_irqrestore(&priv->lock, flags);
  1887. /* call the host command only if no hw rf-kill set */
  1888. if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
  1889. iwl_is_ready(priv))
  1890. iwl4965_send_card_state(priv,
  1891. CARD_STATE_CMD_DISABLE,
  1892. 0);
  1893. set_bit(STATUS_RF_KILL_SW, &priv->status);
  1894. /* make sure mac80211 stop sending Tx frame */
  1895. if (priv->mac80211_registered)
  1896. ieee80211_stop_queues(priv->hw);
  1897. }
  1898. return;
  1899. }
  1900. spin_lock_irqsave(&priv->lock, flags);
  1901. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1902. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  1903. spin_unlock_irqrestore(&priv->lock, flags);
  1904. /* wake up ucode */
  1905. msleep(10);
  1906. spin_lock_irqsave(&priv->lock, flags);
  1907. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  1908. if (!iwl_grab_nic_access(priv))
  1909. iwl_release_nic_access(priv);
  1910. spin_unlock_irqrestore(&priv->lock, flags);
  1911. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  1912. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  1913. "disabled by HW switch\n");
  1914. return;
  1915. }
  1916. queue_work(priv->workqueue, &priv->restart);
  1917. return;
  1918. }
  1919. #define IWL_PACKET_RETRY_TIME HZ
  1920. int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
  1921. {
  1922. u16 sc = le16_to_cpu(header->seq_ctrl);
  1923. u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1924. u16 frag = sc & IEEE80211_SCTL_FRAG;
  1925. u16 *last_seq, *last_frag;
  1926. unsigned long *last_time;
  1927. switch (priv->iw_mode) {
  1928. case IEEE80211_IF_TYPE_IBSS:{
  1929. struct list_head *p;
  1930. struct iwl4965_ibss_seq *entry = NULL;
  1931. u8 *mac = header->addr2;
  1932. int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
  1933. __list_for_each(p, &priv->ibss_mac_hash[index]) {
  1934. entry = list_entry(p, struct iwl4965_ibss_seq, list);
  1935. if (!compare_ether_addr(entry->mac, mac))
  1936. break;
  1937. }
  1938. if (p == &priv->ibss_mac_hash[index]) {
  1939. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  1940. if (!entry) {
  1941. IWL_ERROR("Cannot malloc new mac entry\n");
  1942. return 0;
  1943. }
  1944. memcpy(entry->mac, mac, ETH_ALEN);
  1945. entry->seq_num = seq;
  1946. entry->frag_num = frag;
  1947. entry->packet_time = jiffies;
  1948. list_add(&entry->list, &priv->ibss_mac_hash[index]);
  1949. return 0;
  1950. }
  1951. last_seq = &entry->seq_num;
  1952. last_frag = &entry->frag_num;
  1953. last_time = &entry->packet_time;
  1954. break;
  1955. }
  1956. case IEEE80211_IF_TYPE_STA:
  1957. last_seq = &priv->last_seq_num;
  1958. last_frag = &priv->last_frag_num;
  1959. last_time = &priv->last_packet_time;
  1960. break;
  1961. default:
  1962. return 0;
  1963. }
  1964. if ((*last_seq == seq) &&
  1965. time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
  1966. if (*last_frag == frag)
  1967. goto drop;
  1968. if (*last_frag + 1 != frag)
  1969. /* out-of-order fragment */
  1970. goto drop;
  1971. } else
  1972. *last_seq = seq;
  1973. *last_frag = frag;
  1974. *last_time = jiffies;
  1975. return 0;
  1976. drop:
  1977. return 1;
  1978. }
  1979. #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
  1980. #include "iwl-spectrum.h"
  1981. #define BEACON_TIME_MASK_LOW 0x00FFFFFF
  1982. #define BEACON_TIME_MASK_HIGH 0xFF000000
  1983. #define TIME_UNIT 1024
  1984. /*
  1985. * extended beacon time format
  1986. * time in usec will be changed into a 32-bit value in 8:24 format
  1987. * the high 1 byte is the beacon counts
  1988. * the lower 3 bytes is the time in usec within one beacon interval
  1989. */
  1990. static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
  1991. {
  1992. u32 quot;
  1993. u32 rem;
  1994. u32 interval = beacon_interval * 1024;
  1995. if (!interval || !usec)
  1996. return 0;
  1997. quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
  1998. rem = (usec % interval) & BEACON_TIME_MASK_LOW;
  1999. return (quot << 24) + rem;
  2000. }
  2001. /* base is usually what we get from ucode with each received frame,
  2002. * the same as HW timer counter counting down
  2003. */
  2004. static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
  2005. {
  2006. u32 base_low = base & BEACON_TIME_MASK_LOW;
  2007. u32 addon_low = addon & BEACON_TIME_MASK_LOW;
  2008. u32 interval = beacon_interval * TIME_UNIT;
  2009. u32 res = (base & BEACON_TIME_MASK_HIGH) +
  2010. (addon & BEACON_TIME_MASK_HIGH);
  2011. if (base_low > addon_low)
  2012. res += base_low - addon_low;
  2013. else if (base_low < addon_low) {
  2014. res += interval + base_low - addon_low;
  2015. res += (1 << 24);
  2016. } else
  2017. res += (1 << 24);
  2018. return cpu_to_le32(res);
  2019. }
  2020. static int iwl4965_get_measurement(struct iwl_priv *priv,
  2021. struct ieee80211_measurement_params *params,
  2022. u8 type)
  2023. {
  2024. struct iwl4965_spectrum_cmd spectrum;
  2025. struct iwl4965_rx_packet *res;
  2026. struct iwl_host_cmd cmd = {
  2027. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  2028. .data = (void *)&spectrum,
  2029. .meta.flags = CMD_WANT_SKB,
  2030. };
  2031. u32 add_time = le64_to_cpu(params->start_time);
  2032. int rc;
  2033. int spectrum_resp_status;
  2034. int duration = le16_to_cpu(params->duration);
  2035. if (iwl_is_associated(priv))
  2036. add_time =
  2037. iwl4965_usecs_to_beacons(
  2038. le64_to_cpu(params->start_time) - priv->last_tsf,
  2039. le16_to_cpu(priv->rxon_timing.beacon_interval));
  2040. memset(&spectrum, 0, sizeof(spectrum));
  2041. spectrum.channel_count = cpu_to_le16(1);
  2042. spectrum.flags =
  2043. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  2044. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  2045. cmd.len = sizeof(spectrum);
  2046. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  2047. if (iwl_is_associated(priv))
  2048. spectrum.start_time =
  2049. iwl4965_add_beacon_time(priv->last_beacon_time,
  2050. add_time,
  2051. le16_to_cpu(priv->rxon_timing.beacon_interval));
  2052. else
  2053. spectrum.start_time = 0;
  2054. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  2055. spectrum.channels[0].channel = params->channel;
  2056. spectrum.channels[0].type = type;
  2057. if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
  2058. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  2059. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  2060. rc = iwl_send_cmd_sync(priv, &cmd);
  2061. if (rc)
  2062. return rc;
  2063. res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
  2064. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  2065. IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
  2066. rc = -EIO;
  2067. }
  2068. spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
  2069. switch (spectrum_resp_status) {
  2070. case 0: /* Command will be handled */
  2071. if (res->u.spectrum.id != 0xff) {
  2072. IWL_DEBUG_INFO
  2073. ("Replaced existing measurement: %d\n",
  2074. res->u.spectrum.id);
  2075. priv->measurement_status &= ~MEASUREMENT_READY;
  2076. }
  2077. priv->measurement_status |= MEASUREMENT_ACTIVE;
  2078. rc = 0;
  2079. break;
  2080. case 1: /* Command will not be handled */
  2081. rc = -EAGAIN;
  2082. break;
  2083. }
  2084. dev_kfree_skb_any(cmd.meta.u.skb);
  2085. return rc;
  2086. }
  2087. #endif
  2088. static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
  2089. struct iwl4965_tx_info *tx_sta)
  2090. {
  2091. tx_sta->status.ack_signal = 0;
  2092. tx_sta->status.excessive_retries = 0;
  2093. tx_sta->status.queue_length = 0;
  2094. tx_sta->status.queue_number = 0;
  2095. if (in_interrupt())
  2096. ieee80211_tx_status_irqsafe(priv->hw,
  2097. tx_sta->skb[0], &(tx_sta->status));
  2098. else
  2099. ieee80211_tx_status(priv->hw,
  2100. tx_sta->skb[0], &(tx_sta->status));
  2101. tx_sta->skb[0] = NULL;
  2102. }
  2103. /**
  2104. * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
  2105. *
  2106. * When FW advances 'R' index, all entries between old and new 'R' index
  2107. * need to be reclaimed. As result, some free space forms. If there is
  2108. * enough free space (> low mark), wake the stack that feeds us.
  2109. */
  2110. int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  2111. {
  2112. struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
  2113. struct iwl4965_queue *q = &txq->q;
  2114. int nfreed = 0;
  2115. if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
  2116. IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
  2117. "is out of range [0-%d] %d %d.\n", txq_id,
  2118. index, q->n_bd, q->write_ptr, q->read_ptr);
  2119. return 0;
  2120. }
  2121. for (index = iwl_queue_inc_wrap(index, q->n_bd);
  2122. q->read_ptr != index;
  2123. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  2124. if (txq_id != IWL_CMD_QUEUE_NUM) {
  2125. iwl4965_txstatus_to_ieee(priv,
  2126. &(txq->txb[txq->q.read_ptr]));
  2127. iwl4965_hw_txq_free_tfd(priv, txq);
  2128. } else if (nfreed > 1) {
  2129. IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
  2130. q->write_ptr, q->read_ptr);
  2131. queue_work(priv->workqueue, &priv->restart);
  2132. }
  2133. nfreed++;
  2134. }
  2135. /* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
  2136. (txq_id != IWL_CMD_QUEUE_NUM) &&
  2137. priv->mac80211_registered)
  2138. ieee80211_wake_queue(priv->hw, txq_id); */
  2139. return nfreed;
  2140. }
  2141. static int iwl4965_is_tx_success(u32 status)
  2142. {
  2143. status &= TX_STATUS_MSK;
  2144. return (status == TX_STATUS_SUCCESS)
  2145. || (status == TX_STATUS_DIRECT_DONE);
  2146. }
  2147. /******************************************************************************
  2148. *
  2149. * Generic RX handler implementations
  2150. *
  2151. ******************************************************************************/
  2152. #ifdef CONFIG_IWL4965_HT
  2153. static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
  2154. struct ieee80211_hdr *hdr)
  2155. {
  2156. if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
  2157. return IWL_AP_ID;
  2158. else {
  2159. u8 *da = ieee80211_get_DA(hdr);
  2160. return iwl_find_station(priv, da);
  2161. }
  2162. }
  2163. static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
  2164. struct iwl_priv *priv, int txq_id, int idx)
  2165. {
  2166. if (priv->txq[txq_id].txb[idx].skb[0])
  2167. return (struct ieee80211_hdr *)priv->txq[txq_id].
  2168. txb[idx].skb[0]->data;
  2169. return NULL;
  2170. }
  2171. static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
  2172. {
  2173. __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
  2174. tx_resp->frame_count);
  2175. return le32_to_cpu(*scd_ssn) & MAX_SN;
  2176. }
  2177. /**
  2178. * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
  2179. */
  2180. static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
  2181. struct iwl4965_ht_agg *agg,
  2182. struct iwl4965_tx_resp_agg *tx_resp,
  2183. u16 start_idx)
  2184. {
  2185. u16 status;
  2186. struct agg_tx_status *frame_status = &tx_resp->status;
  2187. struct ieee80211_tx_status *tx_status = NULL;
  2188. struct ieee80211_hdr *hdr = NULL;
  2189. int i, sh;
  2190. int txq_id, idx;
  2191. u16 seq;
  2192. if (agg->wait_for_ba)
  2193. IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
  2194. agg->frame_count = tx_resp->frame_count;
  2195. agg->start_idx = start_idx;
  2196. agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  2197. agg->bitmap = 0;
  2198. /* # frames attempted by Tx command */
  2199. if (agg->frame_count == 1) {
  2200. /* Only one frame was attempted; no block-ack will arrive */
  2201. status = le16_to_cpu(frame_status[0].status);
  2202. seq = le16_to_cpu(frame_status[0].sequence);
  2203. idx = SEQ_TO_INDEX(seq);
  2204. txq_id = SEQ_TO_QUEUE(seq);
  2205. /* FIXME: code repetition */
  2206. IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
  2207. agg->frame_count, agg->start_idx, idx);
  2208. tx_status = &(priv->txq[txq_id].txb[idx].status);
  2209. tx_status->retry_count = tx_resp->failure_frame;
  2210. tx_status->queue_number = status & 0xff;
  2211. tx_status->queue_length = tx_resp->failure_rts;
  2212. tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
  2213. tx_status->flags = iwl4965_is_tx_success(status)?
  2214. IEEE80211_TX_STATUS_ACK : 0;
  2215. iwl4965_hwrate_to_tx_control(priv,
  2216. le32_to_cpu(tx_resp->rate_n_flags),
  2217. &tx_status->control);
  2218. /* FIXME: code repetition end */
  2219. IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
  2220. status & 0xff, tx_resp->failure_frame);
  2221. IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
  2222. iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
  2223. agg->wait_for_ba = 0;
  2224. } else {
  2225. /* Two or more frames were attempted; expect block-ack */
  2226. u64 bitmap = 0;
  2227. int start = agg->start_idx;
  2228. /* Construct bit-map of pending frames within Tx window */
  2229. for (i = 0; i < agg->frame_count; i++) {
  2230. u16 sc;
  2231. status = le16_to_cpu(frame_status[i].status);
  2232. seq = le16_to_cpu(frame_status[i].sequence);
  2233. idx = SEQ_TO_INDEX(seq);
  2234. txq_id = SEQ_TO_QUEUE(seq);
  2235. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  2236. AGG_TX_STATE_ABORT_MSK))
  2237. continue;
  2238. IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
  2239. agg->frame_count, txq_id, idx);
  2240. hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
  2241. sc = le16_to_cpu(hdr->seq_ctrl);
  2242. if (idx != (SEQ_TO_SN(sc) & 0xff)) {
  2243. IWL_ERROR("BUG_ON idx doesn't match seq control"
  2244. " idx=%d, seq_idx=%d, seq=%d\n",
  2245. idx, SEQ_TO_SN(sc),
  2246. hdr->seq_ctrl);
  2247. return -1;
  2248. }
  2249. IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
  2250. i, idx, SEQ_TO_SN(sc));
  2251. sh = idx - start;
  2252. if (sh > 64) {
  2253. sh = (start - idx) + 0xff;
  2254. bitmap = bitmap << sh;
  2255. sh = 0;
  2256. start = idx;
  2257. } else if (sh < -64)
  2258. sh = 0xff - (start - idx);
  2259. else if (sh < 0) {
  2260. sh = start - idx;
  2261. start = idx;
  2262. bitmap = bitmap << sh;
  2263. sh = 0;
  2264. }
  2265. bitmap |= (1 << sh);
  2266. IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
  2267. start, (u32)(bitmap & 0xFFFFFFFF));
  2268. }
  2269. agg->bitmap = bitmap;
  2270. agg->start_idx = start;
  2271. agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  2272. IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
  2273. agg->frame_count, agg->start_idx,
  2274. (unsigned long long)agg->bitmap);
  2275. if (bitmap)
  2276. agg->wait_for_ba = 1;
  2277. }
  2278. return 0;
  2279. }
  2280. #endif
  2281. /**
  2282. * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
  2283. */
  2284. static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
  2285. struct iwl4965_rx_mem_buffer *rxb)
  2286. {
  2287. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2288. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2289. int txq_id = SEQ_TO_QUEUE(sequence);
  2290. int index = SEQ_TO_INDEX(sequence);
  2291. struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
  2292. struct ieee80211_tx_status *tx_status;
  2293. struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  2294. u32 status = le32_to_cpu(tx_resp->status);
  2295. #ifdef CONFIG_IWL4965_HT
  2296. int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
  2297. struct ieee80211_hdr *hdr;
  2298. __le16 *qc;
  2299. #endif
  2300. if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
  2301. IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
  2302. "is out of range [0-%d] %d %d\n", txq_id,
  2303. index, txq->q.n_bd, txq->q.write_ptr,
  2304. txq->q.read_ptr);
  2305. return;
  2306. }
  2307. #ifdef CONFIG_IWL4965_HT
  2308. hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
  2309. qc = ieee80211_get_qos_ctrl(hdr);
  2310. if (qc)
  2311. tid = le16_to_cpu(*qc) & 0xf;
  2312. sta_id = iwl4965_get_ra_sta_id(priv, hdr);
  2313. if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
  2314. IWL_ERROR("Station not known\n");
  2315. return;
  2316. }
  2317. if (txq->sched_retry) {
  2318. const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
  2319. struct iwl4965_ht_agg *agg = NULL;
  2320. if (!qc)
  2321. return;
  2322. agg = &priv->stations[sta_id].tid[tid].agg;
  2323. iwl4965_tx_status_reply_tx(priv, agg,
  2324. (struct iwl4965_tx_resp_agg *)tx_resp, index);
  2325. if ((tx_resp->frame_count == 1) &&
  2326. !iwl4965_is_tx_success(status)) {
  2327. /* TODO: send BAR */
  2328. }
  2329. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  2330. int freed;
  2331. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  2332. IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
  2333. "%d index %d\n", scd_ssn , index);
  2334. freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
  2335. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  2336. if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
  2337. txq_id >= 0 && priv->mac80211_registered &&
  2338. agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
  2339. ieee80211_wake_queue(priv->hw, txq_id);
  2340. iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
  2341. }
  2342. } else {
  2343. #endif /* CONFIG_IWL4965_HT */
  2344. tx_status = &(txq->txb[txq->q.read_ptr].status);
  2345. tx_status->retry_count = tx_resp->failure_frame;
  2346. tx_status->queue_number = status;
  2347. tx_status->queue_length = tx_resp->bt_kill_count;
  2348. tx_status->queue_length |= tx_resp->failure_rts;
  2349. tx_status->flags =
  2350. iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
  2351. iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
  2352. &tx_status->control);
  2353. IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
  2354. "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
  2355. status, le32_to_cpu(tx_resp->rate_n_flags),
  2356. tx_resp->failure_frame);
  2357. IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
  2358. if (index != -1) {
  2359. int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
  2360. #ifdef CONFIG_IWL4965_HT
  2361. if (tid != MAX_TID_COUNT)
  2362. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  2363. if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
  2364. (txq_id >= 0) &&
  2365. priv->mac80211_registered)
  2366. ieee80211_wake_queue(priv->hw, txq_id);
  2367. if (tid != MAX_TID_COUNT)
  2368. iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
  2369. #endif
  2370. }
  2371. #ifdef CONFIG_IWL4965_HT
  2372. }
  2373. #endif /* CONFIG_IWL4965_HT */
  2374. if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
  2375. IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
  2376. }
  2377. static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
  2378. struct iwl4965_rx_mem_buffer *rxb)
  2379. {
  2380. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2381. struct iwl4965_alive_resp *palive;
  2382. struct delayed_work *pwork;
  2383. palive = &pkt->u.alive_frame;
  2384. IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
  2385. "0x%01X 0x%01X\n",
  2386. palive->is_valid, palive->ver_type,
  2387. palive->ver_subtype);
  2388. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  2389. IWL_DEBUG_INFO("Initialization Alive received.\n");
  2390. memcpy(&priv->card_alive_init,
  2391. &pkt->u.alive_frame,
  2392. sizeof(struct iwl4965_init_alive_resp));
  2393. pwork = &priv->init_alive_start;
  2394. } else {
  2395. IWL_DEBUG_INFO("Runtime Alive received.\n");
  2396. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  2397. sizeof(struct iwl4965_alive_resp));
  2398. pwork = &priv->alive_start;
  2399. }
  2400. /* We delay the ALIVE response by 5ms to
  2401. * give the HW RF Kill time to activate... */
  2402. if (palive->is_valid == UCODE_VALID_OK)
  2403. queue_delayed_work(priv->workqueue, pwork,
  2404. msecs_to_jiffies(5));
  2405. else
  2406. IWL_WARNING("uCode did not respond OK.\n");
  2407. }
  2408. static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
  2409. struct iwl4965_rx_mem_buffer *rxb)
  2410. {
  2411. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2412. IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  2413. return;
  2414. }
  2415. static void iwl4965_rx_reply_error(struct iwl_priv *priv,
  2416. struct iwl4965_rx_mem_buffer *rxb)
  2417. {
  2418. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2419. IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
  2420. "seq 0x%04X ser 0x%08X\n",
  2421. le32_to_cpu(pkt->u.err_resp.error_type),
  2422. get_cmd_string(pkt->u.err_resp.cmd_id),
  2423. pkt->u.err_resp.cmd_id,
  2424. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  2425. le32_to_cpu(pkt->u.err_resp.error_info));
  2426. }
  2427. #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
  2428. static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
  2429. {
  2430. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2431. struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
  2432. struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
  2433. IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
  2434. le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
  2435. rxon->channel = csa->channel;
  2436. priv->staging_rxon.channel = csa->channel;
  2437. }
  2438. static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
  2439. struct iwl4965_rx_mem_buffer *rxb)
  2440. {
  2441. #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
  2442. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2443. struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
  2444. if (!report->state) {
  2445. IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
  2446. "Spectrum Measure Notification: Start\n");
  2447. return;
  2448. }
  2449. memcpy(&priv->measure_report, report, sizeof(*report));
  2450. priv->measurement_status |= MEASUREMENT_READY;
  2451. #endif
  2452. }
  2453. static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
  2454. struct iwl4965_rx_mem_buffer *rxb)
  2455. {
  2456. #ifdef CONFIG_IWLWIFI_DEBUG
  2457. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2458. struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
  2459. IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
  2460. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  2461. #endif
  2462. }
  2463. static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
  2464. struct iwl4965_rx_mem_buffer *rxb)
  2465. {
  2466. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2467. IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
  2468. "notification for %s:\n",
  2469. le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
  2470. iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
  2471. }
  2472. static void iwl4965_bg_beacon_update(struct work_struct *work)
  2473. {
  2474. struct iwl_priv *priv =
  2475. container_of(work, struct iwl_priv, beacon_update);
  2476. struct sk_buff *beacon;
  2477. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  2478. beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
  2479. if (!beacon) {
  2480. IWL_ERROR("update beacon failed\n");
  2481. return;
  2482. }
  2483. mutex_lock(&priv->mutex);
  2484. /* new beacon skb is allocated every time; dispose previous.*/
  2485. if (priv->ibss_beacon)
  2486. dev_kfree_skb(priv->ibss_beacon);
  2487. priv->ibss_beacon = beacon;
  2488. mutex_unlock(&priv->mutex);
  2489. iwl4965_send_beacon_cmd(priv);
  2490. }
  2491. static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
  2492. struct iwl4965_rx_mem_buffer *rxb)
  2493. {
  2494. #ifdef CONFIG_IWLWIFI_DEBUG
  2495. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2496. struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
  2497. u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
  2498. IWL_DEBUG_RX("beacon status %x retries %d iss %d "
  2499. "tsf %d %d rate %d\n",
  2500. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  2501. beacon->beacon_notify_hdr.failure_frame,
  2502. le32_to_cpu(beacon->ibss_mgr_status),
  2503. le32_to_cpu(beacon->high_tsf),
  2504. le32_to_cpu(beacon->low_tsf), rate);
  2505. #endif
  2506. if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
  2507. (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
  2508. queue_work(priv->workqueue, &priv->beacon_update);
  2509. }
  2510. /* Service response to REPLY_SCAN_CMD (0x80) */
  2511. static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
  2512. struct iwl4965_rx_mem_buffer *rxb)
  2513. {
  2514. #ifdef CONFIG_IWLWIFI_DEBUG
  2515. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2516. struct iwl4965_scanreq_notification *notif =
  2517. (struct iwl4965_scanreq_notification *)pkt->u.raw;
  2518. IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
  2519. #endif
  2520. }
  2521. /* Service SCAN_START_NOTIFICATION (0x82) */
  2522. static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
  2523. struct iwl4965_rx_mem_buffer *rxb)
  2524. {
  2525. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2526. struct iwl4965_scanstart_notification *notif =
  2527. (struct iwl4965_scanstart_notification *)pkt->u.raw;
  2528. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  2529. IWL_DEBUG_SCAN("Scan start: "
  2530. "%d [802.11%s] "
  2531. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  2532. notif->channel,
  2533. notif->band ? "bg" : "a",
  2534. notif->tsf_high,
  2535. notif->tsf_low, notif->status, notif->beacon_timer);
  2536. }
  2537. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  2538. static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
  2539. struct iwl4965_rx_mem_buffer *rxb)
  2540. {
  2541. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2542. struct iwl4965_scanresults_notification *notif =
  2543. (struct iwl4965_scanresults_notification *)pkt->u.raw;
  2544. IWL_DEBUG_SCAN("Scan ch.res: "
  2545. "%d [802.11%s] "
  2546. "(TSF: 0x%08X:%08X) - %d "
  2547. "elapsed=%lu usec (%dms since last)\n",
  2548. notif->channel,
  2549. notif->band ? "bg" : "a",
  2550. le32_to_cpu(notif->tsf_high),
  2551. le32_to_cpu(notif->tsf_low),
  2552. le32_to_cpu(notif->statistics[0]),
  2553. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
  2554. jiffies_to_msecs(elapsed_jiffies
  2555. (priv->last_scan_jiffies, jiffies)));
  2556. priv->last_scan_jiffies = jiffies;
  2557. priv->next_scan_jiffies = 0;
  2558. }
  2559. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  2560. static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
  2561. struct iwl4965_rx_mem_buffer *rxb)
  2562. {
  2563. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2564. struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  2565. IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  2566. scan_notif->scanned_channels,
  2567. scan_notif->tsf_low,
  2568. scan_notif->tsf_high, scan_notif->status);
  2569. /* The HW is no longer scanning */
  2570. clear_bit(STATUS_SCAN_HW, &priv->status);
  2571. /* The scan completion notification came in, so kill that timer... */
  2572. cancel_delayed_work(&priv->scan_check);
  2573. IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
  2574. (priv->scan_bands == 2) ? "2.4" : "5.2",
  2575. jiffies_to_msecs(elapsed_jiffies
  2576. (priv->scan_pass_start, jiffies)));
  2577. /* Remove this scanned band from the list
  2578. * of pending bands to scan */
  2579. priv->scan_bands--;
  2580. /* If a request to abort was given, or the scan did not succeed
  2581. * then we reset the scan state machine and terminate,
  2582. * re-queuing another scan if one has been requested */
  2583. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  2584. IWL_DEBUG_INFO("Aborted scan completed.\n");
  2585. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  2586. } else {
  2587. /* If there are more bands on this scan pass reschedule */
  2588. if (priv->scan_bands > 0)
  2589. goto reschedule;
  2590. }
  2591. priv->last_scan_jiffies = jiffies;
  2592. priv->next_scan_jiffies = 0;
  2593. IWL_DEBUG_INFO("Setting scan to off\n");
  2594. clear_bit(STATUS_SCANNING, &priv->status);
  2595. IWL_DEBUG_INFO("Scan took %dms\n",
  2596. jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
  2597. queue_work(priv->workqueue, &priv->scan_completed);
  2598. return;
  2599. reschedule:
  2600. priv->scan_pass_start = jiffies;
  2601. queue_work(priv->workqueue, &priv->request_scan);
  2602. }
  2603. /* Handle notification from uCode that card's power state is changing
  2604. * due to software, hardware, or critical temperature RFKILL */
  2605. static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
  2606. struct iwl4965_rx_mem_buffer *rxb)
  2607. {
  2608. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2609. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  2610. unsigned long status = priv->status;
  2611. IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
  2612. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  2613. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  2614. if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
  2615. RF_CARD_DISABLED)) {
  2616. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  2617. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2618. if (!iwl_grab_nic_access(priv)) {
  2619. iwl_write_direct32(
  2620. priv, HBUS_TARG_MBX_C,
  2621. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  2622. iwl_release_nic_access(priv);
  2623. }
  2624. if (!(flags & RXON_CARD_DISABLED)) {
  2625. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  2626. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2627. if (!iwl_grab_nic_access(priv)) {
  2628. iwl_write_direct32(
  2629. priv, HBUS_TARG_MBX_C,
  2630. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  2631. iwl_release_nic_access(priv);
  2632. }
  2633. }
  2634. if (flags & RF_CARD_DISABLED) {
  2635. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  2636. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  2637. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  2638. if (!iwl_grab_nic_access(priv))
  2639. iwl_release_nic_access(priv);
  2640. }
  2641. }
  2642. if (flags & HW_CARD_DISABLED)
  2643. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2644. else
  2645. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2646. if (flags & SW_CARD_DISABLED)
  2647. set_bit(STATUS_RF_KILL_SW, &priv->status);
  2648. else
  2649. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  2650. if (!(flags & RXON_CARD_DISABLED))
  2651. iwl4965_scan_cancel(priv);
  2652. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  2653. test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
  2654. (test_bit(STATUS_RF_KILL_SW, &status) !=
  2655. test_bit(STATUS_RF_KILL_SW, &priv->status)))
  2656. queue_work(priv->workqueue, &priv->rf_kill);
  2657. else
  2658. wake_up_interruptible(&priv->wait_command_queue);
  2659. }
  2660. /**
  2661. * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
  2662. *
  2663. * Setup the RX handlers for each of the reply types sent from the uCode
  2664. * to the host.
  2665. *
  2666. * This function chains into the hardware specific files for them to setup
  2667. * any hardware specific handlers as well.
  2668. */
  2669. static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
  2670. {
  2671. priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
  2672. priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
  2673. priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
  2674. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
  2675. priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  2676. iwl4965_rx_spectrum_measure_notif;
  2677. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
  2678. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  2679. iwl4965_rx_pm_debug_statistics_notif;
  2680. priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
  2681. /*
  2682. * The same handler is used for both the REPLY to a discrete
  2683. * statistics request from the host as well as for the periodic
  2684. * statistics notifications (after received beacons) from the uCode.
  2685. */
  2686. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
  2687. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
  2688. priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
  2689. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
  2690. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  2691. iwl4965_rx_scan_results_notif;
  2692. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  2693. iwl4965_rx_scan_complete_notif;
  2694. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
  2695. priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
  2696. /* Set up hardware specific Rx handlers */
  2697. iwl4965_hw_rx_handler_setup(priv);
  2698. }
  2699. /**
  2700. * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  2701. * @rxb: Rx buffer to reclaim
  2702. *
  2703. * If an Rx buffer has an async callback associated with it the callback
  2704. * will be executed. The attached skb (if present) will only be freed
  2705. * if the callback returns 1
  2706. */
  2707. static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
  2708. struct iwl4965_rx_mem_buffer *rxb)
  2709. {
  2710. struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
  2711. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2712. int txq_id = SEQ_TO_QUEUE(sequence);
  2713. int index = SEQ_TO_INDEX(sequence);
  2714. int huge = sequence & SEQ_HUGE_FRAME;
  2715. int cmd_index;
  2716. struct iwl_cmd *cmd;
  2717. /* If a Tx command is being handled and it isn't in the actual
  2718. * command queue then there a command routing bug has been introduced
  2719. * in the queue management code. */
  2720. if (txq_id != IWL_CMD_QUEUE_NUM)
  2721. IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
  2722. txq_id, pkt->hdr.cmd);
  2723. BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
  2724. cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
  2725. cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
  2726. /* Input error checking is done when commands are added to queue. */
  2727. if (cmd->meta.flags & CMD_WANT_SKB) {
  2728. cmd->meta.source->u.skb = rxb->skb;
  2729. rxb->skb = NULL;
  2730. } else if (cmd->meta.u.callback &&
  2731. !cmd->meta.u.callback(priv, cmd, rxb->skb))
  2732. rxb->skb = NULL;
  2733. iwl4965_tx_queue_reclaim(priv, txq_id, index);
  2734. if (!(cmd->meta.flags & CMD_ASYNC)) {
  2735. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  2736. wake_up_interruptible(&priv->wait_command_queue);
  2737. }
  2738. }
  2739. /************************** RX-FUNCTIONS ****************************/
  2740. /*
  2741. * Rx theory of operation
  2742. *
  2743. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  2744. * each of which point to Receive Buffers to be filled by 4965. These get
  2745. * used not only for Rx frames, but for any command response or notification
  2746. * from the 4965. The driver and 4965 manage the Rx buffers by means
  2747. * of indexes into the circular buffer.
  2748. *
  2749. * Rx Queue Indexes
  2750. * The host/firmware share two index registers for managing the Rx buffers.
  2751. *
  2752. * The READ index maps to the first position that the firmware may be writing
  2753. * to -- the driver can read up to (but not including) this position and get
  2754. * good data.
  2755. * The READ index is managed by the firmware once the card is enabled.
  2756. *
  2757. * The WRITE index maps to the last position the driver has read from -- the
  2758. * position preceding WRITE is the last slot the firmware can place a packet.
  2759. *
  2760. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  2761. * WRITE = READ.
  2762. *
  2763. * During initialization, the host sets up the READ queue position to the first
  2764. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  2765. *
  2766. * When the firmware places a packet in a buffer, it will advance the READ index
  2767. * and fire the RX interrupt. The driver can then query the READ index and
  2768. * process as many packets as possible, moving the WRITE index forward as it
  2769. * resets the Rx queue buffers with new memory.
  2770. *
  2771. * The management in the driver is as follows:
  2772. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  2773. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  2774. * to replenish the iwl->rxq->rx_free.
  2775. * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
  2776. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  2777. * 'processed' and 'read' driver indexes as well)
  2778. * + A received packet is processed and handed to the kernel network stack,
  2779. * detached from the iwl->rxq. The driver 'processed' index is updated.
  2780. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  2781. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  2782. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  2783. * were enough free buffers and RX_STALLED is set it is cleared.
  2784. *
  2785. *
  2786. * Driver sequence:
  2787. *
  2788. * iwl4965_rx_queue_alloc() Allocates rx_free
  2789. * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
  2790. * iwl4965_rx_queue_restock
  2791. * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
  2792. * queue, updates firmware pointers, and updates
  2793. * the WRITE index. If insufficient rx_free buffers
  2794. * are available, schedules iwl4965_rx_replenish
  2795. *
  2796. * -- enable interrupts --
  2797. * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
  2798. * READ INDEX, detaching the SKB from the pool.
  2799. * Moves the packet buffer from queue to rx_used.
  2800. * Calls iwl4965_rx_queue_restock to refill any empty
  2801. * slots.
  2802. * ...
  2803. *
  2804. */
  2805. /**
  2806. * iwl4965_rx_queue_space - Return number of free slots available in queue.
  2807. */
  2808. static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
  2809. {
  2810. int s = q->read - q->write;
  2811. if (s <= 0)
  2812. s += RX_QUEUE_SIZE;
  2813. /* keep some buffer to not confuse full and empty queue */
  2814. s -= 2;
  2815. if (s < 0)
  2816. s = 0;
  2817. return s;
  2818. }
  2819. /**
  2820. * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  2821. */
  2822. int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
  2823. {
  2824. u32 reg = 0;
  2825. int rc = 0;
  2826. unsigned long flags;
  2827. spin_lock_irqsave(&q->lock, flags);
  2828. if (q->need_update == 0)
  2829. goto exit_unlock;
  2830. /* If power-saving is in use, make sure device is awake */
  2831. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  2832. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  2833. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  2834. iwl_set_bit(priv, CSR_GP_CNTRL,
  2835. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2836. goto exit_unlock;
  2837. }
  2838. rc = iwl_grab_nic_access(priv);
  2839. if (rc)
  2840. goto exit_unlock;
  2841. /* Device expects a multiple of 8 */
  2842. iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
  2843. q->write & ~0x7);
  2844. iwl_release_nic_access(priv);
  2845. /* Else device is assumed to be awake */
  2846. } else
  2847. /* Device expects a multiple of 8 */
  2848. iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
  2849. q->need_update = 0;
  2850. exit_unlock:
  2851. spin_unlock_irqrestore(&q->lock, flags);
  2852. return rc;
  2853. }
  2854. /**
  2855. * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  2856. */
  2857. static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
  2858. dma_addr_t dma_addr)
  2859. {
  2860. return cpu_to_le32((u32)(dma_addr >> 8));
  2861. }
  2862. /**
  2863. * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
  2864. *
  2865. * If there are slots in the RX queue that need to be restocked,
  2866. * and we have free pre-allocated buffers, fill the ranks as much
  2867. * as we can, pulling from rx_free.
  2868. *
  2869. * This moves the 'write' index forward to catch up with 'processed', and
  2870. * also updates the memory address in the firmware to reference the new
  2871. * target buffer.
  2872. */
  2873. static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
  2874. {
  2875. struct iwl4965_rx_queue *rxq = &priv->rxq;
  2876. struct list_head *element;
  2877. struct iwl4965_rx_mem_buffer *rxb;
  2878. unsigned long flags;
  2879. int write, rc;
  2880. spin_lock_irqsave(&rxq->lock, flags);
  2881. write = rxq->write & ~0x7;
  2882. while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  2883. /* Get next free Rx buffer, remove from free list */
  2884. element = rxq->rx_free.next;
  2885. rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
  2886. list_del(element);
  2887. /* Point to Rx buffer via next RBD in circular buffer */
  2888. rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
  2889. rxq->queue[rxq->write] = rxb;
  2890. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  2891. rxq->free_count--;
  2892. }
  2893. spin_unlock_irqrestore(&rxq->lock, flags);
  2894. /* If the pre-allocated buffer pool is dropping low, schedule to
  2895. * refill it */
  2896. if (rxq->free_count <= RX_LOW_WATERMARK)
  2897. queue_work(priv->workqueue, &priv->rx_replenish);
  2898. /* If we've added more space for the firmware to place data, tell it.
  2899. * Increment device's write pointer in multiples of 8. */
  2900. if ((write != (rxq->write & ~0x7))
  2901. || (abs(rxq->write - rxq->read) > 7)) {
  2902. spin_lock_irqsave(&rxq->lock, flags);
  2903. rxq->need_update = 1;
  2904. spin_unlock_irqrestore(&rxq->lock, flags);
  2905. rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
  2906. if (rc)
  2907. return rc;
  2908. }
  2909. return 0;
  2910. }
  2911. /**
  2912. * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
  2913. *
  2914. * When moving to rx_free an SKB is allocated for the slot.
  2915. *
  2916. * Also restock the Rx queue via iwl4965_rx_queue_restock.
  2917. * This is called as a scheduled work item (except for during initialization)
  2918. */
  2919. static void iwl4965_rx_allocate(struct iwl_priv *priv)
  2920. {
  2921. struct iwl4965_rx_queue *rxq = &priv->rxq;
  2922. struct list_head *element;
  2923. struct iwl4965_rx_mem_buffer *rxb;
  2924. unsigned long flags;
  2925. spin_lock_irqsave(&rxq->lock, flags);
  2926. while (!list_empty(&rxq->rx_used)) {
  2927. element = rxq->rx_used.next;
  2928. rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
  2929. /* Alloc a new receive buffer */
  2930. rxb->skb =
  2931. alloc_skb(priv->hw_params.rx_buf_size,
  2932. __GFP_NOWARN | GFP_ATOMIC);
  2933. if (!rxb->skb) {
  2934. if (net_ratelimit())
  2935. printk(KERN_CRIT DRV_NAME
  2936. ": Can not allocate SKB buffers\n");
  2937. /* We don't reschedule replenish work here -- we will
  2938. * call the restock method and if it still needs
  2939. * more buffers it will schedule replenish */
  2940. break;
  2941. }
  2942. priv->alloc_rxb_skb++;
  2943. list_del(element);
  2944. /* Get physical address of RB/SKB */
  2945. rxb->dma_addr =
  2946. pci_map_single(priv->pci_dev, rxb->skb->data,
  2947. priv->hw_params.rx_buf_size, PCI_DMA_FROMDEVICE);
  2948. list_add_tail(&rxb->list, &rxq->rx_free);
  2949. rxq->free_count++;
  2950. }
  2951. spin_unlock_irqrestore(&rxq->lock, flags);
  2952. }
  2953. /*
  2954. * this should be called while priv->lock is locked
  2955. */
  2956. static void __iwl4965_rx_replenish(void *data)
  2957. {
  2958. struct iwl_priv *priv = data;
  2959. iwl4965_rx_allocate(priv);
  2960. iwl4965_rx_queue_restock(priv);
  2961. }
  2962. void iwl4965_rx_replenish(void *data)
  2963. {
  2964. struct iwl_priv *priv = data;
  2965. unsigned long flags;
  2966. iwl4965_rx_allocate(priv);
  2967. spin_lock_irqsave(&priv->lock, flags);
  2968. iwl4965_rx_queue_restock(priv);
  2969. spin_unlock_irqrestore(&priv->lock, flags);
  2970. }
  2971. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  2972. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  2973. * This free routine walks the list of POOL entries and if SKB is set to
  2974. * non NULL it is unmapped and freed
  2975. */
  2976. static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
  2977. {
  2978. int i;
  2979. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  2980. if (rxq->pool[i].skb != NULL) {
  2981. pci_unmap_single(priv->pci_dev,
  2982. rxq->pool[i].dma_addr,
  2983. priv->hw_params.rx_buf_size,
  2984. PCI_DMA_FROMDEVICE);
  2985. dev_kfree_skb(rxq->pool[i].skb);
  2986. }
  2987. }
  2988. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  2989. rxq->dma_addr);
  2990. rxq->bd = NULL;
  2991. }
  2992. int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
  2993. {
  2994. struct iwl4965_rx_queue *rxq = &priv->rxq;
  2995. struct pci_dev *dev = priv->pci_dev;
  2996. int i;
  2997. spin_lock_init(&rxq->lock);
  2998. INIT_LIST_HEAD(&rxq->rx_free);
  2999. INIT_LIST_HEAD(&rxq->rx_used);
  3000. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  3001. rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
  3002. if (!rxq->bd)
  3003. return -ENOMEM;
  3004. /* Fill the rx_used queue with _all_ of the Rx buffers */
  3005. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  3006. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  3007. /* Set us so that we have processed and used all buffers, but have
  3008. * not restocked the Rx queue with fresh buffers */
  3009. rxq->read = rxq->write = 0;
  3010. rxq->free_count = 0;
  3011. rxq->need_update = 0;
  3012. return 0;
  3013. }
  3014. void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
  3015. {
  3016. unsigned long flags;
  3017. int i;
  3018. spin_lock_irqsave(&rxq->lock, flags);
  3019. INIT_LIST_HEAD(&rxq->rx_free);
  3020. INIT_LIST_HEAD(&rxq->rx_used);
  3021. /* Fill the rx_used queue with _all_ of the Rx buffers */
  3022. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  3023. /* In the reset function, these buffers may have been allocated
  3024. * to an SKB, so we need to unmap and free potential storage */
  3025. if (rxq->pool[i].skb != NULL) {
  3026. pci_unmap_single(priv->pci_dev,
  3027. rxq->pool[i].dma_addr,
  3028. priv->hw_params.rx_buf_size,
  3029. PCI_DMA_FROMDEVICE);
  3030. priv->alloc_rxb_skb--;
  3031. dev_kfree_skb(rxq->pool[i].skb);
  3032. rxq->pool[i].skb = NULL;
  3033. }
  3034. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  3035. }
  3036. /* Set us so that we have processed and used all buffers, but have
  3037. * not restocked the Rx queue with fresh buffers */
  3038. rxq->read = rxq->write = 0;
  3039. rxq->free_count = 0;
  3040. spin_unlock_irqrestore(&rxq->lock, flags);
  3041. }
  3042. /* Convert linear signal-to-noise ratio into dB */
  3043. static u8 ratio2dB[100] = {
  3044. /* 0 1 2 3 4 5 6 7 8 9 */
  3045. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  3046. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  3047. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  3048. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  3049. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  3050. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  3051. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  3052. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  3053. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  3054. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  3055. };
  3056. /* Calculates a relative dB value from a ratio of linear
  3057. * (i.e. not dB) signal levels.
  3058. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  3059. int iwl4965_calc_db_from_ratio(int sig_ratio)
  3060. {
  3061. /* 1000:1 or higher just report as 60 dB */
  3062. if (sig_ratio >= 1000)
  3063. return 60;
  3064. /* 100:1 or higher, divide by 10 and use table,
  3065. * add 20 dB to make up for divide by 10 */
  3066. if (sig_ratio >= 100)
  3067. return (20 + (int)ratio2dB[sig_ratio/10]);
  3068. /* We shouldn't see this */
  3069. if (sig_ratio < 1)
  3070. return 0;
  3071. /* Use table for ratios 1:1 - 99:1 */
  3072. return (int)ratio2dB[sig_ratio];
  3073. }
  3074. #define PERFECT_RSSI (-20) /* dBm */
  3075. #define WORST_RSSI (-95) /* dBm */
  3076. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  3077. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  3078. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  3079. * about formulas used below. */
  3080. int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
  3081. {
  3082. int sig_qual;
  3083. int degradation = PERFECT_RSSI - rssi_dbm;
  3084. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  3085. * as indicator; formula is (signal dbm - noise dbm).
  3086. * SNR at or above 40 is a great signal (100%).
  3087. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  3088. * Weakest usable signal is usually 10 - 15 dB SNR. */
  3089. if (noise_dbm) {
  3090. if (rssi_dbm - noise_dbm >= 40)
  3091. return 100;
  3092. else if (rssi_dbm < noise_dbm)
  3093. return 0;
  3094. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  3095. /* Else use just the signal level.
  3096. * This formula is a least squares fit of data points collected and
  3097. * compared with a reference system that had a percentage (%) display
  3098. * for signal quality. */
  3099. } else
  3100. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  3101. (15 * RSSI_RANGE + 62 * degradation)) /
  3102. (RSSI_RANGE * RSSI_RANGE);
  3103. if (sig_qual > 100)
  3104. sig_qual = 100;
  3105. else if (sig_qual < 1)
  3106. sig_qual = 0;
  3107. return sig_qual;
  3108. }
  3109. /**
  3110. * iwl4965_rx_handle - Main entry function for receiving responses from uCode
  3111. *
  3112. * Uses the priv->rx_handlers callback function array to invoke
  3113. * the appropriate handlers, including command responses,
  3114. * frame-received notifications, and other notifications.
  3115. */
  3116. static void iwl4965_rx_handle(struct iwl_priv *priv)
  3117. {
  3118. struct iwl4965_rx_mem_buffer *rxb;
  3119. struct iwl4965_rx_packet *pkt;
  3120. struct iwl4965_rx_queue *rxq = &priv->rxq;
  3121. u32 r, i;
  3122. int reclaim;
  3123. unsigned long flags;
  3124. u8 fill_rx = 0;
  3125. u32 count = 8;
  3126. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  3127. * buffer that the driver may process (last buffer filled by ucode). */
  3128. r = iwl4965_hw_get_rx_read(priv);
  3129. i = rxq->read;
  3130. /* Rx interrupt, but nothing sent from uCode */
  3131. if (i == r)
  3132. IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
  3133. if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
  3134. fill_rx = 1;
  3135. while (i != r) {
  3136. rxb = rxq->queue[i];
  3137. /* If an RXB doesn't have a Rx queue slot associated with it,
  3138. * then a bug has been introduced in the queue refilling
  3139. * routines -- catch it here */
  3140. BUG_ON(rxb == NULL);
  3141. rxq->queue[i] = NULL;
  3142. pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
  3143. priv->hw_params.rx_buf_size,
  3144. PCI_DMA_FROMDEVICE);
  3145. pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
  3146. /* Reclaim a command buffer only if this packet is a response
  3147. * to a (driver-originated) command.
  3148. * If the packet (e.g. Rx frame) originated from uCode,
  3149. * there is no command buffer to reclaim.
  3150. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  3151. * but apparently a few don't get set; catch them here. */
  3152. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  3153. (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
  3154. (pkt->hdr.cmd != REPLY_RX) &&
  3155. (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
  3156. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  3157. (pkt->hdr.cmd != REPLY_TX);
  3158. /* Based on type of command response or notification,
  3159. * handle those that need handling via function in
  3160. * rx_handlers table. See iwl4965_setup_rx_handlers() */
  3161. if (priv->rx_handlers[pkt->hdr.cmd]) {
  3162. IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
  3163. "r = %d, i = %d, %s, 0x%02x\n", r, i,
  3164. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  3165. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  3166. } else {
  3167. /* No handling needed */
  3168. IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
  3169. "r %d i %d No handler needed for %s, 0x%02x\n",
  3170. r, i, get_cmd_string(pkt->hdr.cmd),
  3171. pkt->hdr.cmd);
  3172. }
  3173. if (reclaim) {
  3174. /* Invoke any callbacks, transfer the skb to caller, and
  3175. * fire off the (possibly) blocking iwl_send_cmd()
  3176. * as we reclaim the driver command queue */
  3177. if (rxb && rxb->skb)
  3178. iwl4965_tx_cmd_complete(priv, rxb);
  3179. else
  3180. IWL_WARNING("Claim null rxb?\n");
  3181. }
  3182. /* For now we just don't re-use anything. We can tweak this
  3183. * later to try and re-use notification packets and SKBs that
  3184. * fail to Rx correctly */
  3185. if (rxb->skb != NULL) {
  3186. priv->alloc_rxb_skb--;
  3187. dev_kfree_skb_any(rxb->skb);
  3188. rxb->skb = NULL;
  3189. }
  3190. pci_unmap_single(priv->pci_dev, rxb->dma_addr,
  3191. priv->hw_params.rx_buf_size,
  3192. PCI_DMA_FROMDEVICE);
  3193. spin_lock_irqsave(&rxq->lock, flags);
  3194. list_add_tail(&rxb->list, &priv->rxq.rx_used);
  3195. spin_unlock_irqrestore(&rxq->lock, flags);
  3196. i = (i + 1) & RX_QUEUE_MASK;
  3197. /* If there are a lot of unused frames,
  3198. * restock the Rx queue so ucode wont assert. */
  3199. if (fill_rx) {
  3200. count++;
  3201. if (count >= 8) {
  3202. priv->rxq.read = i;
  3203. __iwl4965_rx_replenish(priv);
  3204. count = 0;
  3205. }
  3206. }
  3207. }
  3208. /* Backtrack one entry */
  3209. priv->rxq.read = i;
  3210. iwl4965_rx_queue_restock(priv);
  3211. }
  3212. /**
  3213. * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
  3214. */
  3215. static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
  3216. struct iwl4965_tx_queue *txq)
  3217. {
  3218. u32 reg = 0;
  3219. int rc = 0;
  3220. int txq_id = txq->q.id;
  3221. if (txq->need_update == 0)
  3222. return rc;
  3223. /* if we're trying to save power */
  3224. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  3225. /* wake up nic if it's powered down ...
  3226. * uCode will wake up, and interrupt us again, so next
  3227. * time we'll skip this part. */
  3228. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  3229. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  3230. IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
  3231. iwl_set_bit(priv, CSR_GP_CNTRL,
  3232. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  3233. return rc;
  3234. }
  3235. /* restore this queue's parameters in nic hardware. */
  3236. rc = iwl_grab_nic_access(priv);
  3237. if (rc)
  3238. return rc;
  3239. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  3240. txq->q.write_ptr | (txq_id << 8));
  3241. iwl_release_nic_access(priv);
  3242. /* else not in power-save mode, uCode will never sleep when we're
  3243. * trying to tx (during RFKILL, we're not trying to tx). */
  3244. } else
  3245. iwl_write32(priv, HBUS_TARG_WRPTR,
  3246. txq->q.write_ptr | (txq_id << 8));
  3247. txq->need_update = 0;
  3248. return rc;
  3249. }
  3250. #ifdef CONFIG_IWLWIFI_DEBUG
  3251. static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
  3252. {
  3253. DECLARE_MAC_BUF(mac);
  3254. IWL_DEBUG_RADIO("RX CONFIG:\n");
  3255. iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  3256. IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  3257. IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  3258. IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
  3259. le32_to_cpu(rxon->filter_flags));
  3260. IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
  3261. IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
  3262. rxon->ofdm_basic_rates);
  3263. IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  3264. IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
  3265. print_mac(mac, rxon->node_addr));
  3266. IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
  3267. print_mac(mac, rxon->bssid_addr));
  3268. IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  3269. }
  3270. #endif
  3271. static void iwl4965_enable_interrupts(struct iwl_priv *priv)
  3272. {
  3273. IWL_DEBUG_ISR("Enabling interrupts\n");
  3274. set_bit(STATUS_INT_ENABLED, &priv->status);
  3275. iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
  3276. }
  3277. /* call this function to flush any scheduled tasklet */
  3278. static inline void iwl_synchronize_irq(struct iwl_priv *priv)
  3279. {
  3280. /* wait to make sure we flush pedding tasklet*/
  3281. synchronize_irq(priv->pci_dev->irq);
  3282. tasklet_kill(&priv->irq_tasklet);
  3283. }
  3284. static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
  3285. {
  3286. clear_bit(STATUS_INT_ENABLED, &priv->status);
  3287. /* disable interrupts from uCode/NIC to host */
  3288. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  3289. /* acknowledge/clear/reset any interrupts still pending
  3290. * from uCode or flow handler (Rx/Tx DMA) */
  3291. iwl_write32(priv, CSR_INT, 0xffffffff);
  3292. iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
  3293. IWL_DEBUG_ISR("Disabled interrupts\n");
  3294. }
  3295. static const char *desc_lookup(int i)
  3296. {
  3297. switch (i) {
  3298. case 1:
  3299. return "FAIL";
  3300. case 2:
  3301. return "BAD_PARAM";
  3302. case 3:
  3303. return "BAD_CHECKSUM";
  3304. case 4:
  3305. return "NMI_INTERRUPT";
  3306. case 5:
  3307. return "SYSASSERT";
  3308. case 6:
  3309. return "FATAL_ERROR";
  3310. }
  3311. return "UNKNOWN";
  3312. }
  3313. #define ERROR_START_OFFSET (1 * sizeof(u32))
  3314. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  3315. static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
  3316. {
  3317. u32 data2, line;
  3318. u32 desc, time, count, base, data1;
  3319. u32 blink1, blink2, ilink1, ilink2;
  3320. int rc;
  3321. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  3322. if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
  3323. IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
  3324. return;
  3325. }
  3326. rc = iwl_grab_nic_access(priv);
  3327. if (rc) {
  3328. IWL_WARNING("Can not read from adapter at this time.\n");
  3329. return;
  3330. }
  3331. count = iwl_read_targ_mem(priv, base);
  3332. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  3333. IWL_ERROR("Start IWL Error Log Dump:\n");
  3334. IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
  3335. }
  3336. desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
  3337. blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
  3338. blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
  3339. ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
  3340. ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
  3341. data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
  3342. data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
  3343. line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
  3344. time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
  3345. IWL_ERROR("Desc Time "
  3346. "data1 data2 line\n");
  3347. IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
  3348. desc_lookup(desc), desc, time, data1, data2, line);
  3349. IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
  3350. IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
  3351. ilink1, ilink2);
  3352. iwl_release_nic_access(priv);
  3353. }
  3354. #define EVENT_START_OFFSET (4 * sizeof(u32))
  3355. /**
  3356. * iwl4965_print_event_log - Dump error event log to syslog
  3357. *
  3358. * NOTE: Must be called with iwl_grab_nic_access() already obtained!
  3359. */
  3360. static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
  3361. u32 num_events, u32 mode)
  3362. {
  3363. u32 i;
  3364. u32 base; /* SRAM byte address of event log header */
  3365. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  3366. u32 ptr; /* SRAM byte address of log data */
  3367. u32 ev, time, data; /* event log data */
  3368. if (num_events == 0)
  3369. return;
  3370. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  3371. if (mode == 0)
  3372. event_size = 2 * sizeof(u32);
  3373. else
  3374. event_size = 3 * sizeof(u32);
  3375. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  3376. /* "time" is actually "data" for mode 0 (no timestamp).
  3377. * place event id # at far right for easier visual parsing. */
  3378. for (i = 0; i < num_events; i++) {
  3379. ev = iwl_read_targ_mem(priv, ptr);
  3380. ptr += sizeof(u32);
  3381. time = iwl_read_targ_mem(priv, ptr);
  3382. ptr += sizeof(u32);
  3383. if (mode == 0)
  3384. IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
  3385. else {
  3386. data = iwl_read_targ_mem(priv, ptr);
  3387. ptr += sizeof(u32);
  3388. IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
  3389. }
  3390. }
  3391. }
  3392. static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
  3393. {
  3394. int rc;
  3395. u32 base; /* SRAM byte address of event log header */
  3396. u32 capacity; /* event log capacity in # entries */
  3397. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  3398. u32 num_wraps; /* # times uCode wrapped to top of log */
  3399. u32 next_entry; /* index of next entry to be written by uCode */
  3400. u32 size; /* # entries that we'll print */
  3401. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  3402. if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
  3403. IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
  3404. return;
  3405. }
  3406. rc = iwl_grab_nic_access(priv);
  3407. if (rc) {
  3408. IWL_WARNING("Can not read from adapter at this time.\n");
  3409. return;
  3410. }
  3411. /* event log header */
  3412. capacity = iwl_read_targ_mem(priv, base);
  3413. mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
  3414. num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
  3415. next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
  3416. size = num_wraps ? capacity : next_entry;
  3417. /* bail out if nothing in log */
  3418. if (size == 0) {
  3419. IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
  3420. iwl_release_nic_access(priv);
  3421. return;
  3422. }
  3423. IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
  3424. size, num_wraps);
  3425. /* if uCode has wrapped back to top of log, start at the oldest entry,
  3426. * i.e the next one that uCode would fill. */
  3427. if (num_wraps)
  3428. iwl4965_print_event_log(priv, next_entry,
  3429. capacity - next_entry, mode);
  3430. /* (then/else) start at top of log */
  3431. iwl4965_print_event_log(priv, 0, next_entry, mode);
  3432. iwl_release_nic_access(priv);
  3433. }
  3434. /**
  3435. * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
  3436. */
  3437. static void iwl4965_irq_handle_error(struct iwl_priv *priv)
  3438. {
  3439. /* Set the FW error flag -- cleared on iwl4965_down */
  3440. set_bit(STATUS_FW_ERROR, &priv->status);
  3441. /* Cancel currently queued command. */
  3442. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  3443. #ifdef CONFIG_IWLWIFI_DEBUG
  3444. if (iwl_debug_level & IWL_DL_FW_ERRORS) {
  3445. iwl4965_dump_nic_error_log(priv);
  3446. iwl4965_dump_nic_event_log(priv);
  3447. iwl4965_print_rx_config_cmd(&priv->staging_rxon);
  3448. }
  3449. #endif
  3450. wake_up_interruptible(&priv->wait_command_queue);
  3451. /* Keep the restart process from trying to send host
  3452. * commands by clearing the INIT status bit */
  3453. clear_bit(STATUS_READY, &priv->status);
  3454. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  3455. IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
  3456. "Restarting adapter due to uCode error.\n");
  3457. if (iwl_is_associated(priv)) {
  3458. memcpy(&priv->recovery_rxon, &priv->active_rxon,
  3459. sizeof(priv->recovery_rxon));
  3460. priv->error_recovering = 1;
  3461. }
  3462. queue_work(priv->workqueue, &priv->restart);
  3463. }
  3464. }
  3465. static void iwl4965_error_recovery(struct iwl_priv *priv)
  3466. {
  3467. unsigned long flags;
  3468. memcpy(&priv->staging_rxon, &priv->recovery_rxon,
  3469. sizeof(priv->staging_rxon));
  3470. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3471. iwl4965_commit_rxon(priv);
  3472. iwl4965_rxon_add_station(priv, priv->bssid, 1);
  3473. spin_lock_irqsave(&priv->lock, flags);
  3474. priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
  3475. priv->error_recovering = 0;
  3476. spin_unlock_irqrestore(&priv->lock, flags);
  3477. }
  3478. static void iwl4965_irq_tasklet(struct iwl_priv *priv)
  3479. {
  3480. u32 inta, handled = 0;
  3481. u32 inta_fh;
  3482. unsigned long flags;
  3483. #ifdef CONFIG_IWLWIFI_DEBUG
  3484. u32 inta_mask;
  3485. #endif
  3486. spin_lock_irqsave(&priv->lock, flags);
  3487. /* Ack/clear/reset pending uCode interrupts.
  3488. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  3489. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  3490. inta = iwl_read32(priv, CSR_INT);
  3491. iwl_write32(priv, CSR_INT, inta);
  3492. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  3493. * Any new interrupts that happen after this, either while we're
  3494. * in this tasklet, or later, will show up in next ISR/tasklet. */
  3495. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  3496. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  3497. #ifdef CONFIG_IWLWIFI_DEBUG
  3498. if (iwl_debug_level & IWL_DL_ISR) {
  3499. /* just for debug */
  3500. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  3501. IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  3502. inta, inta_mask, inta_fh);
  3503. }
  3504. #endif
  3505. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  3506. * atomic, make sure that inta covers all the interrupts that
  3507. * we've discovered, even if FH interrupt came in just after
  3508. * reading CSR_INT. */
  3509. if (inta_fh & CSR49_FH_INT_RX_MASK)
  3510. inta |= CSR_INT_BIT_FH_RX;
  3511. if (inta_fh & CSR49_FH_INT_TX_MASK)
  3512. inta |= CSR_INT_BIT_FH_TX;
  3513. /* Now service all interrupt bits discovered above. */
  3514. if (inta & CSR_INT_BIT_HW_ERR) {
  3515. IWL_ERROR("Microcode HW error detected. Restarting.\n");
  3516. /* Tell the device to stop sending interrupts */
  3517. iwl4965_disable_interrupts(priv);
  3518. iwl4965_irq_handle_error(priv);
  3519. handled |= CSR_INT_BIT_HW_ERR;
  3520. spin_unlock_irqrestore(&priv->lock, flags);
  3521. return;
  3522. }
  3523. #ifdef CONFIG_IWLWIFI_DEBUG
  3524. if (iwl_debug_level & (IWL_DL_ISR)) {
  3525. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  3526. if (inta & CSR_INT_BIT_SCD)
  3527. IWL_DEBUG_ISR("Scheduler finished to transmit "
  3528. "the frame/frames.\n");
  3529. /* Alive notification via Rx interrupt will do the real work */
  3530. if (inta & CSR_INT_BIT_ALIVE)
  3531. IWL_DEBUG_ISR("Alive interrupt\n");
  3532. }
  3533. #endif
  3534. /* Safely ignore these bits for debug checks below */
  3535. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  3536. /* HW RF KILL switch toggled */
  3537. if (inta & CSR_INT_BIT_RF_KILL) {
  3538. int hw_rf_kill = 0;
  3539. if (!(iwl_read32(priv, CSR_GP_CNTRL) &
  3540. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  3541. hw_rf_kill = 1;
  3542. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
  3543. "RF_KILL bit toggled to %s.\n",
  3544. hw_rf_kill ? "disable radio":"enable radio");
  3545. /* Queue restart only if RF_KILL switch was set to "kill"
  3546. * when we loaded driver, and is now set to "enable".
  3547. * After we're Alive, RF_KILL gets handled by
  3548. * iwl4965_rx_card_state_notif() */
  3549. if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
  3550. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  3551. queue_work(priv->workqueue, &priv->restart);
  3552. }
  3553. handled |= CSR_INT_BIT_RF_KILL;
  3554. }
  3555. /* Chip got too hot and stopped itself */
  3556. if (inta & CSR_INT_BIT_CT_KILL) {
  3557. IWL_ERROR("Microcode CT kill error detected.\n");
  3558. handled |= CSR_INT_BIT_CT_KILL;
  3559. }
  3560. /* Error detected by uCode */
  3561. if (inta & CSR_INT_BIT_SW_ERR) {
  3562. IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
  3563. inta);
  3564. iwl4965_irq_handle_error(priv);
  3565. handled |= CSR_INT_BIT_SW_ERR;
  3566. }
  3567. /* uCode wakes up after power-down sleep */
  3568. if (inta & CSR_INT_BIT_WAKEUP) {
  3569. IWL_DEBUG_ISR("Wakeup interrupt\n");
  3570. iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
  3571. iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
  3572. iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
  3573. iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
  3574. iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
  3575. iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
  3576. iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
  3577. handled |= CSR_INT_BIT_WAKEUP;
  3578. }
  3579. /* All uCode command responses, including Tx command responses,
  3580. * Rx "responses" (frame-received notification), and other
  3581. * notifications from uCode come through here*/
  3582. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  3583. iwl4965_rx_handle(priv);
  3584. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  3585. }
  3586. if (inta & CSR_INT_BIT_FH_TX) {
  3587. IWL_DEBUG_ISR("Tx interrupt\n");
  3588. handled |= CSR_INT_BIT_FH_TX;
  3589. }
  3590. if (inta & ~handled)
  3591. IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
  3592. if (inta & ~CSR_INI_SET_MASK) {
  3593. IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
  3594. inta & ~CSR_INI_SET_MASK);
  3595. IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
  3596. }
  3597. /* Re-enable all interrupts */
  3598. /* only Re-enable if diabled by irq */
  3599. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  3600. iwl4965_enable_interrupts(priv);
  3601. #ifdef CONFIG_IWLWIFI_DEBUG
  3602. if (iwl_debug_level & (IWL_DL_ISR)) {
  3603. inta = iwl_read32(priv, CSR_INT);
  3604. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  3605. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  3606. IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  3607. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  3608. }
  3609. #endif
  3610. spin_unlock_irqrestore(&priv->lock, flags);
  3611. }
  3612. static irqreturn_t iwl4965_isr(int irq, void *data)
  3613. {
  3614. struct iwl_priv *priv = data;
  3615. u32 inta, inta_mask;
  3616. u32 inta_fh;
  3617. if (!priv)
  3618. return IRQ_NONE;
  3619. spin_lock(&priv->lock);
  3620. /* Disable (but don't clear!) interrupts here to avoid
  3621. * back-to-back ISRs and sporadic interrupts from our NIC.
  3622. * If we have something to service, the tasklet will re-enable ints.
  3623. * If we *don't* have something, we'll re-enable before leaving here. */
  3624. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  3625. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  3626. /* Discover which interrupts are active/pending */
  3627. inta = iwl_read32(priv, CSR_INT);
  3628. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  3629. /* Ignore interrupt if there's nothing in NIC to service.
  3630. * This may be due to IRQ shared with another device,
  3631. * or due to sporadic interrupts thrown from our NIC. */
  3632. if (!inta && !inta_fh) {
  3633. IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
  3634. goto none;
  3635. }
  3636. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  3637. /* Hardware disappeared. It might have already raised
  3638. * an interrupt */
  3639. IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
  3640. goto unplugged;
  3641. }
  3642. IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  3643. inta, inta_mask, inta_fh);
  3644. inta &= ~CSR_INT_BIT_SCD;
  3645. /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
  3646. if (likely(inta || inta_fh))
  3647. tasklet_schedule(&priv->irq_tasklet);
  3648. unplugged:
  3649. spin_unlock(&priv->lock);
  3650. return IRQ_HANDLED;
  3651. none:
  3652. /* re-enable interrupts here since we don't have anything to service. */
  3653. /* only Re-enable if diabled by irq */
  3654. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  3655. iwl4965_enable_interrupts(priv);
  3656. spin_unlock(&priv->lock);
  3657. return IRQ_NONE;
  3658. }
  3659. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  3660. * sending probe req. This should be set long enough to hear probe responses
  3661. * from more than one AP. */
  3662. #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
  3663. #define IWL_ACTIVE_DWELL_TIME_52 (10)
  3664. /* For faster active scanning, scan will move to the next channel if fewer than
  3665. * PLCP_QUIET_THRESH packets are heard on this channel within
  3666. * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
  3667. * time if it's a quiet channel (nothing responded to our probe, and there's
  3668. * no other traffic).
  3669. * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
  3670. #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
  3671. #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
  3672. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  3673. * Must be set longer than active dwell time.
  3674. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  3675. #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  3676. #define IWL_PASSIVE_DWELL_TIME_52 (10)
  3677. #define IWL_PASSIVE_DWELL_BASE (100)
  3678. #define IWL_CHANNEL_TUNE_TIME 5
  3679. static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
  3680. enum ieee80211_band band)
  3681. {
  3682. if (band == IEEE80211_BAND_5GHZ)
  3683. return IWL_ACTIVE_DWELL_TIME_52;
  3684. else
  3685. return IWL_ACTIVE_DWELL_TIME_24;
  3686. }
  3687. static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
  3688. enum ieee80211_band band)
  3689. {
  3690. u16 active = iwl4965_get_active_dwell_time(priv, band);
  3691. u16 passive = (band != IEEE80211_BAND_5GHZ) ?
  3692. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  3693. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  3694. if (iwl_is_associated(priv)) {
  3695. /* If we're associated, we clamp the maximum passive
  3696. * dwell time to be 98% of the beacon interval (minus
  3697. * 2 * channel tune time) */
  3698. passive = priv->beacon_int;
  3699. if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
  3700. passive = IWL_PASSIVE_DWELL_BASE;
  3701. passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  3702. }
  3703. if (passive <= active)
  3704. passive = active + 1;
  3705. return passive;
  3706. }
  3707. static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
  3708. enum ieee80211_band band,
  3709. u8 is_active, u8 direct_mask,
  3710. struct iwl4965_scan_channel *scan_ch)
  3711. {
  3712. const struct ieee80211_channel *channels = NULL;
  3713. const struct ieee80211_supported_band *sband;
  3714. const struct iwl_channel_info *ch_info;
  3715. u16 passive_dwell = 0;
  3716. u16 active_dwell = 0;
  3717. int added, i;
  3718. sband = iwl_get_hw_mode(priv, band);
  3719. if (!sband)
  3720. return 0;
  3721. channels = sband->channels;
  3722. active_dwell = iwl4965_get_active_dwell_time(priv, band);
  3723. passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
  3724. for (i = 0, added = 0; i < sband->n_channels; i++) {
  3725. if (channels[i].flags & IEEE80211_CHAN_DISABLED)
  3726. continue;
  3727. scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
  3728. ch_info = iwl_get_channel_info(priv, band,
  3729. scan_ch->channel);
  3730. if (!is_channel_valid(ch_info)) {
  3731. IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
  3732. scan_ch->channel);
  3733. continue;
  3734. }
  3735. if (!is_active || is_channel_passive(ch_info) ||
  3736. (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
  3737. scan_ch->type = 0; /* passive */
  3738. else
  3739. scan_ch->type = 1; /* active */
  3740. if (scan_ch->type & 1)
  3741. scan_ch->type |= (direct_mask << 1);
  3742. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  3743. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  3744. /* Set txpower levels to defaults */
  3745. scan_ch->tpc.dsp_atten = 110;
  3746. /* scan_pwr_info->tpc.dsp_atten; */
  3747. /*scan_pwr_info->tpc.tx_gain; */
  3748. if (band == IEEE80211_BAND_5GHZ)
  3749. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  3750. else {
  3751. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  3752. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  3753. * power level:
  3754. * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
  3755. */
  3756. }
  3757. IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
  3758. scan_ch->channel,
  3759. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  3760. (scan_ch->type & 1) ?
  3761. active_dwell : passive_dwell);
  3762. scan_ch++;
  3763. added++;
  3764. }
  3765. IWL_DEBUG_SCAN("total channels to scan %d \n", added);
  3766. return added;
  3767. }
  3768. static void iwl4965_init_hw_rates(struct iwl_priv *priv,
  3769. struct ieee80211_rate *rates)
  3770. {
  3771. int i;
  3772. for (i = 0; i < IWL_RATE_COUNT; i++) {
  3773. rates[i].bitrate = iwl4965_rates[i].ieee * 5;
  3774. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  3775. rates[i].hw_value_short = i;
  3776. rates[i].flags = 0;
  3777. if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  3778. /*
  3779. * If CCK != 1M then set short preamble rate flag.
  3780. */
  3781. rates[i].flags |=
  3782. (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
  3783. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  3784. }
  3785. }
  3786. }
  3787. /**
  3788. * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
  3789. */
  3790. int iwl4965_init_geos(struct iwl_priv *priv)
  3791. {
  3792. struct iwl_channel_info *ch;
  3793. struct ieee80211_supported_band *sband;
  3794. struct ieee80211_channel *channels;
  3795. struct ieee80211_channel *geo_ch;
  3796. struct ieee80211_rate *rates;
  3797. int i = 0;
  3798. if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  3799. priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  3800. IWL_DEBUG_INFO("Geography modes already initialized.\n");
  3801. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  3802. return 0;
  3803. }
  3804. channels = kzalloc(sizeof(struct ieee80211_channel) *
  3805. priv->channel_count, GFP_KERNEL);
  3806. if (!channels)
  3807. return -ENOMEM;
  3808. rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
  3809. GFP_KERNEL);
  3810. if (!rates) {
  3811. kfree(channels);
  3812. return -ENOMEM;
  3813. }
  3814. /* 5.2GHz channels start after the 2.4GHz channels */
  3815. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  3816. sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
  3817. /* just OFDM */
  3818. sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
  3819. sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
  3820. iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
  3821. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  3822. sband->channels = channels;
  3823. /* OFDM & CCK */
  3824. sband->bitrates = rates;
  3825. sband->n_bitrates = IWL_RATE_COUNT;
  3826. iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
  3827. priv->ieee_channels = channels;
  3828. priv->ieee_rates = rates;
  3829. iwl4965_init_hw_rates(priv, rates);
  3830. for (i = 0; i < priv->channel_count; i++) {
  3831. ch = &priv->channel_info[i];
  3832. /* FIXME: might be removed if scan is OK */
  3833. if (!is_channel_valid(ch))
  3834. continue;
  3835. if (is_channel_a_band(ch))
  3836. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  3837. else
  3838. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  3839. geo_ch = &sband->channels[sband->n_channels++];
  3840. geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
  3841. geo_ch->max_power = ch->max_power_avg;
  3842. geo_ch->max_antenna_gain = 0xff;
  3843. geo_ch->hw_value = ch->channel;
  3844. if (is_channel_valid(ch)) {
  3845. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  3846. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  3847. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  3848. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  3849. if (ch->flags & EEPROM_CHANNEL_RADAR)
  3850. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  3851. if (ch->max_power_avg > priv->max_channel_txpower_limit)
  3852. priv->max_channel_txpower_limit =
  3853. ch->max_power_avg;
  3854. } else {
  3855. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  3856. }
  3857. /* Save flags for reg domain usage */
  3858. geo_ch->orig_flags = geo_ch->flags;
  3859. IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
  3860. ch->channel, geo_ch->center_freq,
  3861. is_channel_a_band(ch) ? "5.2" : "2.4",
  3862. geo_ch->flags & IEEE80211_CHAN_DISABLED ?
  3863. "restricted" : "valid",
  3864. geo_ch->flags);
  3865. }
  3866. if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
  3867. priv->cfg->sku & IWL_SKU_A) {
  3868. printk(KERN_INFO DRV_NAME
  3869. ": Incorrectly detected BG card as ABG. Please send "
  3870. "your PCI ID 0x%04X:0x%04X to maintainer.\n",
  3871. priv->pci_dev->device, priv->pci_dev->subsystem_device);
  3872. priv->cfg->sku &= ~IWL_SKU_A;
  3873. }
  3874. printk(KERN_INFO DRV_NAME
  3875. ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  3876. priv->bands[IEEE80211_BAND_2GHZ].n_channels,
  3877. priv->bands[IEEE80211_BAND_5GHZ].n_channels);
  3878. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  3879. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3880. &priv->bands[IEEE80211_BAND_2GHZ];
  3881. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  3882. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  3883. &priv->bands[IEEE80211_BAND_5GHZ];
  3884. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  3885. return 0;
  3886. }
  3887. /*
  3888. * iwl4965_free_geos - undo allocations in iwl4965_init_geos
  3889. */
  3890. void iwl4965_free_geos(struct iwl_priv *priv)
  3891. {
  3892. kfree(priv->ieee_channels);
  3893. kfree(priv->ieee_rates);
  3894. clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
  3895. }
  3896. /******************************************************************************
  3897. *
  3898. * uCode download functions
  3899. *
  3900. ******************************************************************************/
  3901. static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
  3902. {
  3903. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
  3904. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
  3905. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  3906. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
  3907. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  3908. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
  3909. }
  3910. /**
  3911. * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
  3912. * looking at all data.
  3913. */
  3914. static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
  3915. u32 len)
  3916. {
  3917. u32 val;
  3918. u32 save_len = len;
  3919. int rc = 0;
  3920. u32 errcnt;
  3921. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  3922. rc = iwl_grab_nic_access(priv);
  3923. if (rc)
  3924. return rc;
  3925. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
  3926. errcnt = 0;
  3927. for (; len > 0; len -= sizeof(u32), image++) {
  3928. /* read data comes through single port, auto-incr addr */
  3929. /* NOTE: Use the debugless read so we don't flood kernel log
  3930. * if IWL_DL_IO is set */
  3931. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  3932. if (val != le32_to_cpu(*image)) {
  3933. IWL_ERROR("uCode INST section is invalid at "
  3934. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  3935. save_len - len, val, le32_to_cpu(*image));
  3936. rc = -EIO;
  3937. errcnt++;
  3938. if (errcnt >= 20)
  3939. break;
  3940. }
  3941. }
  3942. iwl_release_nic_access(priv);
  3943. if (!errcnt)
  3944. IWL_DEBUG_INFO
  3945. ("ucode image in INSTRUCTION memory is good\n");
  3946. return rc;
  3947. }
  3948. /**
  3949. * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
  3950. * using sample data 100 bytes apart. If these sample points are good,
  3951. * it's a pretty good bet that everything between them is good, too.
  3952. */
  3953. static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  3954. {
  3955. u32 val;
  3956. int rc = 0;
  3957. u32 errcnt = 0;
  3958. u32 i;
  3959. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  3960. rc = iwl_grab_nic_access(priv);
  3961. if (rc)
  3962. return rc;
  3963. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  3964. /* read data comes through single port, auto-incr addr */
  3965. /* NOTE: Use the debugless read so we don't flood kernel log
  3966. * if IWL_DL_IO is set */
  3967. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  3968. i + RTC_INST_LOWER_BOUND);
  3969. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  3970. if (val != le32_to_cpu(*image)) {
  3971. #if 0 /* Enable this if you want to see details */
  3972. IWL_ERROR("uCode INST section is invalid at "
  3973. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  3974. i, val, *image);
  3975. #endif
  3976. rc = -EIO;
  3977. errcnt++;
  3978. if (errcnt >= 3)
  3979. break;
  3980. }
  3981. }
  3982. iwl_release_nic_access(priv);
  3983. return rc;
  3984. }
  3985. /**
  3986. * iwl4965_verify_ucode - determine which instruction image is in SRAM,
  3987. * and verify its contents
  3988. */
  3989. static int iwl4965_verify_ucode(struct iwl_priv *priv)
  3990. {
  3991. __le32 *image;
  3992. u32 len;
  3993. int rc = 0;
  3994. /* Try bootstrap */
  3995. image = (__le32 *)priv->ucode_boot.v_addr;
  3996. len = priv->ucode_boot.len;
  3997. rc = iwl4965_verify_inst_sparse(priv, image, len);
  3998. if (rc == 0) {
  3999. IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
  4000. return 0;
  4001. }
  4002. /* Try initialize */
  4003. image = (__le32 *)priv->ucode_init.v_addr;
  4004. len = priv->ucode_init.len;
  4005. rc = iwl4965_verify_inst_sparse(priv, image, len);
  4006. if (rc == 0) {
  4007. IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
  4008. return 0;
  4009. }
  4010. /* Try runtime/protocol */
  4011. image = (__le32 *)priv->ucode_code.v_addr;
  4012. len = priv->ucode_code.len;
  4013. rc = iwl4965_verify_inst_sparse(priv, image, len);
  4014. if (rc == 0) {
  4015. IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
  4016. return 0;
  4017. }
  4018. IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  4019. /* Since nothing seems to match, show first several data entries in
  4020. * instruction SRAM, so maybe visual inspection will give a clue.
  4021. * Selection of bootstrap image (vs. other images) is arbitrary. */
  4022. image = (__le32 *)priv->ucode_boot.v_addr;
  4023. len = priv->ucode_boot.len;
  4024. rc = iwl4965_verify_inst_full(priv, image, len);
  4025. return rc;
  4026. }
  4027. static void iwl4965_nic_start(struct iwl_priv *priv)
  4028. {
  4029. /* Remove all resets to allow NIC to operate */
  4030. iwl_write32(priv, CSR_RESET, 0);
  4031. }
  4032. /**
  4033. * iwl4965_read_ucode - Read uCode images from disk file.
  4034. *
  4035. * Copy into buffers for card to fetch via bus-mastering
  4036. */
  4037. static int iwl4965_read_ucode(struct iwl_priv *priv)
  4038. {
  4039. struct iwl4965_ucode *ucode;
  4040. int ret;
  4041. const struct firmware *ucode_raw;
  4042. const char *name = priv->cfg->fw_name;
  4043. u8 *src;
  4044. size_t len;
  4045. u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
  4046. /* Ask kernel firmware_class module to get the boot firmware off disk.
  4047. * request_firmware() is synchronous, file is in memory on return. */
  4048. ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
  4049. if (ret < 0) {
  4050. IWL_ERROR("%s firmware file req failed: Reason %d\n",
  4051. name, ret);
  4052. goto error;
  4053. }
  4054. IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
  4055. name, ucode_raw->size);
  4056. /* Make sure that we got at least our header! */
  4057. if (ucode_raw->size < sizeof(*ucode)) {
  4058. IWL_ERROR("File size way too small!\n");
  4059. ret = -EINVAL;
  4060. goto err_release;
  4061. }
  4062. /* Data from ucode file: header followed by uCode images */
  4063. ucode = (void *)ucode_raw->data;
  4064. ver = le32_to_cpu(ucode->ver);
  4065. inst_size = le32_to_cpu(ucode->inst_size);
  4066. data_size = le32_to_cpu(ucode->data_size);
  4067. init_size = le32_to_cpu(ucode->init_size);
  4068. init_data_size = le32_to_cpu(ucode->init_data_size);
  4069. boot_size = le32_to_cpu(ucode->boot_size);
  4070. IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
  4071. IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
  4072. inst_size);
  4073. IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
  4074. data_size);
  4075. IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
  4076. init_size);
  4077. IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
  4078. init_data_size);
  4079. IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
  4080. boot_size);
  4081. /* Verify size of file vs. image size info in file's header */
  4082. if (ucode_raw->size < sizeof(*ucode) +
  4083. inst_size + data_size + init_size +
  4084. init_data_size + boot_size) {
  4085. IWL_DEBUG_INFO("uCode file size %d too small\n",
  4086. (int)ucode_raw->size);
  4087. ret = -EINVAL;
  4088. goto err_release;
  4089. }
  4090. /* Verify that uCode images will fit in card's SRAM */
  4091. if (inst_size > priv->hw_params.max_inst_size) {
  4092. IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
  4093. inst_size);
  4094. ret = -EINVAL;
  4095. goto err_release;
  4096. }
  4097. if (data_size > priv->hw_params.max_data_size) {
  4098. IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
  4099. data_size);
  4100. ret = -EINVAL;
  4101. goto err_release;
  4102. }
  4103. if (init_size > priv->hw_params.max_inst_size) {
  4104. IWL_DEBUG_INFO
  4105. ("uCode init instr len %d too large to fit in\n",
  4106. init_size);
  4107. ret = -EINVAL;
  4108. goto err_release;
  4109. }
  4110. if (init_data_size > priv->hw_params.max_data_size) {
  4111. IWL_DEBUG_INFO
  4112. ("uCode init data len %d too large to fit in\n",
  4113. init_data_size);
  4114. ret = -EINVAL;
  4115. goto err_release;
  4116. }
  4117. if (boot_size > priv->hw_params.max_bsm_size) {
  4118. IWL_DEBUG_INFO
  4119. ("uCode boot instr len %d too large to fit in\n",
  4120. boot_size);
  4121. ret = -EINVAL;
  4122. goto err_release;
  4123. }
  4124. /* Allocate ucode buffers for card's bus-master loading ... */
  4125. /* Runtime instructions and 2 copies of data:
  4126. * 1) unmodified from disk
  4127. * 2) backup cache for save/restore during power-downs */
  4128. priv->ucode_code.len = inst_size;
  4129. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
  4130. priv->ucode_data.len = data_size;
  4131. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
  4132. priv->ucode_data_backup.len = data_size;
  4133. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  4134. /* Initialization instructions and data */
  4135. if (init_size && init_data_size) {
  4136. priv->ucode_init.len = init_size;
  4137. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
  4138. priv->ucode_init_data.len = init_data_size;
  4139. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  4140. if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
  4141. goto err_pci_alloc;
  4142. }
  4143. /* Bootstrap (instructions only, no data) */
  4144. if (boot_size) {
  4145. priv->ucode_boot.len = boot_size;
  4146. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
  4147. if (!priv->ucode_boot.v_addr)
  4148. goto err_pci_alloc;
  4149. }
  4150. /* Copy images into buffers for card's bus-master reads ... */
  4151. /* Runtime instructions (first block of data in file) */
  4152. src = &ucode->data[0];
  4153. len = priv->ucode_code.len;
  4154. IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
  4155. memcpy(priv->ucode_code.v_addr, src, len);
  4156. IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  4157. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  4158. /* Runtime data (2nd block)
  4159. * NOTE: Copy into backup buffer will be done in iwl4965_up() */
  4160. src = &ucode->data[inst_size];
  4161. len = priv->ucode_data.len;
  4162. IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
  4163. memcpy(priv->ucode_data.v_addr, src, len);
  4164. memcpy(priv->ucode_data_backup.v_addr, src, len);
  4165. /* Initialization instructions (3rd block) */
  4166. if (init_size) {
  4167. src = &ucode->data[inst_size + data_size];
  4168. len = priv->ucode_init.len;
  4169. IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
  4170. len);
  4171. memcpy(priv->ucode_init.v_addr, src, len);
  4172. }
  4173. /* Initialization data (4th block) */
  4174. if (init_data_size) {
  4175. src = &ucode->data[inst_size + data_size + init_size];
  4176. len = priv->ucode_init_data.len;
  4177. IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
  4178. len);
  4179. memcpy(priv->ucode_init_data.v_addr, src, len);
  4180. }
  4181. /* Bootstrap instructions (5th block) */
  4182. src = &ucode->data[inst_size + data_size + init_size + init_data_size];
  4183. len = priv->ucode_boot.len;
  4184. IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
  4185. memcpy(priv->ucode_boot.v_addr, src, len);
  4186. /* We have our copies now, allow OS release its copies */
  4187. release_firmware(ucode_raw);
  4188. return 0;
  4189. err_pci_alloc:
  4190. IWL_ERROR("failed to allocate pci memory\n");
  4191. ret = -ENOMEM;
  4192. iwl4965_dealloc_ucode_pci(priv);
  4193. err_release:
  4194. release_firmware(ucode_raw);
  4195. error:
  4196. return ret;
  4197. }
  4198. /**
  4199. * iwl4965_set_ucode_ptrs - Set uCode address location
  4200. *
  4201. * Tell initialization uCode where to find runtime uCode.
  4202. *
  4203. * BSM registers initially contain pointers to initialization uCode.
  4204. * We need to replace them to load runtime uCode inst and data,
  4205. * and to save runtime data when powering down.
  4206. */
  4207. static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
  4208. {
  4209. dma_addr_t pinst;
  4210. dma_addr_t pdata;
  4211. int rc = 0;
  4212. unsigned long flags;
  4213. /* bits 35:4 for 4965 */
  4214. pinst = priv->ucode_code.p_addr >> 4;
  4215. pdata = priv->ucode_data_backup.p_addr >> 4;
  4216. spin_lock_irqsave(&priv->lock, flags);
  4217. rc = iwl_grab_nic_access(priv);
  4218. if (rc) {
  4219. spin_unlock_irqrestore(&priv->lock, flags);
  4220. return rc;
  4221. }
  4222. /* Tell bootstrap uCode where to find image to load */
  4223. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  4224. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  4225. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  4226. priv->ucode_data.len);
  4227. /* Inst bytecount must be last to set up, bit 31 signals uCode
  4228. * that all new ptr/size info is in place */
  4229. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  4230. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  4231. iwl_release_nic_access(priv);
  4232. spin_unlock_irqrestore(&priv->lock, flags);
  4233. IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
  4234. return rc;
  4235. }
  4236. /**
  4237. * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
  4238. *
  4239. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  4240. *
  4241. * The 4965 "initialize" ALIVE reply contains calibration data for:
  4242. * Voltage, temperature, and MIMO tx gain correction, now stored in priv
  4243. * (3945 does not contain this data).
  4244. *
  4245. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  4246. */
  4247. static void iwl4965_init_alive_start(struct iwl_priv *priv)
  4248. {
  4249. /* Check alive response for "valid" sign from uCode */
  4250. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  4251. /* We had an error bringing up the hardware, so take it
  4252. * all the way back down so we can try again */
  4253. IWL_DEBUG_INFO("Initialize Alive failed.\n");
  4254. goto restart;
  4255. }
  4256. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  4257. * This is a paranoid check, because we would not have gotten the
  4258. * "initialize" alive if code weren't properly loaded. */
  4259. if (iwl4965_verify_ucode(priv)) {
  4260. /* Runtime instruction load was bad;
  4261. * take it all the way back down so we can try again */
  4262. IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
  4263. goto restart;
  4264. }
  4265. /* Calculate temperature */
  4266. priv->temperature = iwl4965_get_temperature(priv);
  4267. /* Send pointers to protocol/runtime uCode image ... init code will
  4268. * load and launch runtime uCode, which will send us another "Alive"
  4269. * notification. */
  4270. IWL_DEBUG_INFO("Initialization Alive received.\n");
  4271. if (iwl4965_set_ucode_ptrs(priv)) {
  4272. /* Runtime instruction load won't happen;
  4273. * take it all the way back down so we can try again */
  4274. IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
  4275. goto restart;
  4276. }
  4277. return;
  4278. restart:
  4279. queue_work(priv->workqueue, &priv->restart);
  4280. }
  4281. /**
  4282. * iwl4965_alive_start - called after REPLY_ALIVE notification received
  4283. * from protocol/runtime uCode (initialization uCode's
  4284. * Alive gets handled by iwl4965_init_alive_start()).
  4285. */
  4286. static void iwl4965_alive_start(struct iwl_priv *priv)
  4287. {
  4288. int ret = 0;
  4289. IWL_DEBUG_INFO("Runtime Alive received.\n");
  4290. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  4291. /* We had an error bringing up the hardware, so take it
  4292. * all the way back down so we can try again */
  4293. IWL_DEBUG_INFO("Alive failed.\n");
  4294. goto restart;
  4295. }
  4296. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  4297. * This is a paranoid check, because we would not have gotten the
  4298. * "runtime" alive if code weren't properly loaded. */
  4299. if (iwl4965_verify_ucode(priv)) {
  4300. /* Runtime instruction load was bad;
  4301. * take it all the way back down so we can try again */
  4302. IWL_DEBUG_INFO("Bad runtime uCode load.\n");
  4303. goto restart;
  4304. }
  4305. iwlcore_clear_stations_table(priv);
  4306. ret = priv->cfg->ops->lib->alive_notify(priv);
  4307. if (ret) {
  4308. IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
  4309. ret);
  4310. goto restart;
  4311. }
  4312. /* After the ALIVE response, we can send host commands to 4965 uCode */
  4313. set_bit(STATUS_ALIVE, &priv->status);
  4314. /* Clear out the uCode error bit if it is set */
  4315. clear_bit(STATUS_FW_ERROR, &priv->status);
  4316. if (iwl_is_rfkill(priv))
  4317. return;
  4318. ieee80211_start_queues(priv->hw);
  4319. priv->active_rate = priv->rates_mask;
  4320. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  4321. if (iwl_is_associated(priv)) {
  4322. struct iwl4965_rxon_cmd *active_rxon =
  4323. (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
  4324. memcpy(&priv->staging_rxon, &priv->active_rxon,
  4325. sizeof(priv->staging_rxon));
  4326. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4327. } else {
  4328. /* Initialize our rx_config data */
  4329. iwl4965_connection_init_rx_config(priv);
  4330. memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
  4331. }
  4332. /* Configure Bluetooth device coexistence support */
  4333. iwl4965_send_bt_config(priv);
  4334. /* Configure the adapter for unassociated operation */
  4335. iwl4965_commit_rxon(priv);
  4336. /* At this point, the NIC is initialized and operational */
  4337. priv->notif_missed_beacons = 0;
  4338. iwl4965_rf_kill_ct_config(priv);
  4339. iwl_leds_register(priv);
  4340. IWL_DEBUG_INFO("ALIVE processing complete.\n");
  4341. set_bit(STATUS_READY, &priv->status);
  4342. wake_up_interruptible(&priv->wait_command_queue);
  4343. if (priv->error_recovering)
  4344. iwl4965_error_recovery(priv);
  4345. iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
  4346. ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
  4347. return;
  4348. restart:
  4349. queue_work(priv->workqueue, &priv->restart);
  4350. }
  4351. static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
  4352. static void __iwl4965_down(struct iwl_priv *priv)
  4353. {
  4354. unsigned long flags;
  4355. int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
  4356. struct ieee80211_conf *conf = NULL;
  4357. IWL_DEBUG_INFO(DRV_NAME " is going down\n");
  4358. conf = ieee80211_get_hw_conf(priv->hw);
  4359. if (!exit_pending)
  4360. set_bit(STATUS_EXIT_PENDING, &priv->status);
  4361. iwl_leds_unregister(priv);
  4362. iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
  4363. iwlcore_clear_stations_table(priv);
  4364. /* Unblock any waiting calls */
  4365. wake_up_interruptible_all(&priv->wait_command_queue);
  4366. /* Wipe out the EXIT_PENDING status bit if we are not actually
  4367. * exiting the module */
  4368. if (!exit_pending)
  4369. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  4370. /* stop and reset the on-board processor */
  4371. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  4372. /* tell the device to stop sending interrupts */
  4373. spin_lock_irqsave(&priv->lock, flags);
  4374. iwl4965_disable_interrupts(priv);
  4375. spin_unlock_irqrestore(&priv->lock, flags);
  4376. iwl_synchronize_irq(priv);
  4377. if (priv->mac80211_registered)
  4378. ieee80211_stop_queues(priv->hw);
  4379. /* If we have not previously called iwl4965_init() then
  4380. * clear all bits but the RF Kill and SUSPEND bits and return */
  4381. if (!iwl_is_init(priv)) {
  4382. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  4383. STATUS_RF_KILL_HW |
  4384. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  4385. STATUS_RF_KILL_SW |
  4386. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  4387. STATUS_GEO_CONFIGURED |
  4388. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  4389. STATUS_IN_SUSPEND;
  4390. goto exit;
  4391. }
  4392. /* ...otherwise clear out all the status bits but the RF Kill and
  4393. * SUSPEND bits and continue taking the NIC down. */
  4394. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  4395. STATUS_RF_KILL_HW |
  4396. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  4397. STATUS_RF_KILL_SW |
  4398. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  4399. STATUS_GEO_CONFIGURED |
  4400. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  4401. STATUS_IN_SUSPEND |
  4402. test_bit(STATUS_FW_ERROR, &priv->status) <<
  4403. STATUS_FW_ERROR;
  4404. spin_lock_irqsave(&priv->lock, flags);
  4405. iwl_clear_bit(priv, CSR_GP_CNTRL,
  4406. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  4407. spin_unlock_irqrestore(&priv->lock, flags);
  4408. iwl4965_hw_txq_ctx_stop(priv);
  4409. iwl4965_hw_rxq_stop(priv);
  4410. spin_lock_irqsave(&priv->lock, flags);
  4411. if (!iwl_grab_nic_access(priv)) {
  4412. iwl_write_prph(priv, APMG_CLK_DIS_REG,
  4413. APMG_CLK_VAL_DMA_CLK_RQT);
  4414. iwl_release_nic_access(priv);
  4415. }
  4416. spin_unlock_irqrestore(&priv->lock, flags);
  4417. udelay(5);
  4418. iwl4965_hw_nic_stop_master(priv);
  4419. iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  4420. iwl4965_hw_nic_reset(priv);
  4421. priv->cfg->ops->lib->free_shared_mem(priv);
  4422. exit:
  4423. memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
  4424. if (priv->ibss_beacon)
  4425. dev_kfree_skb(priv->ibss_beacon);
  4426. priv->ibss_beacon = NULL;
  4427. /* clear out any free frames */
  4428. iwl4965_clear_free_frames(priv);
  4429. }
  4430. static void iwl4965_down(struct iwl_priv *priv)
  4431. {
  4432. mutex_lock(&priv->mutex);
  4433. __iwl4965_down(priv);
  4434. mutex_unlock(&priv->mutex);
  4435. iwl4965_cancel_deferred_work(priv);
  4436. }
  4437. #define MAX_HW_RESTARTS 5
  4438. static int __iwl4965_up(struct iwl_priv *priv)
  4439. {
  4440. int i;
  4441. int ret;
  4442. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  4443. IWL_WARNING("Exit pending; will not bring the NIC up\n");
  4444. return -EIO;
  4445. }
  4446. if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  4447. IWL_WARNING("Radio disabled by SW RF kill (module "
  4448. "parameter)\n");
  4449. iwl_rfkill_set_hw_state(priv);
  4450. return -ENODEV;
  4451. }
  4452. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  4453. IWL_ERROR("ucode not available for device bringup\n");
  4454. return -EIO;
  4455. }
  4456. /* If platform's RF_KILL switch is NOT set to KILL */
  4457. if (iwl_read32(priv, CSR_GP_CNTRL) &
  4458. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  4459. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  4460. else {
  4461. set_bit(STATUS_RF_KILL_HW, &priv->status);
  4462. if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
  4463. iwl_rfkill_set_hw_state(priv);
  4464. IWL_WARNING("Radio disabled by HW RF Kill switch\n");
  4465. return -ENODEV;
  4466. }
  4467. }
  4468. iwl_rfkill_set_hw_state(priv);
  4469. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  4470. ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
  4471. if (ret) {
  4472. IWL_ERROR("Unable to allocate shared memory\n");
  4473. return ret;
  4474. }
  4475. ret = priv->cfg->ops->lib->hw_nic_init(priv);
  4476. if (ret) {
  4477. IWL_ERROR("Unable to init nic\n");
  4478. return ret;
  4479. }
  4480. /* make sure rfkill handshake bits are cleared */
  4481. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4482. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  4483. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  4484. /* clear (again), then enable host interrupts */
  4485. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  4486. iwl4965_enable_interrupts(priv);
  4487. /* really make sure rfkill handshake bits are cleared */
  4488. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4489. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4490. /* Copy original ucode data image from disk into backup cache.
  4491. * This will be used to initialize the on-board processor's
  4492. * data SRAM for a clean start when the runtime program first loads. */
  4493. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  4494. priv->ucode_data.len);
  4495. /* We return success when we resume from suspend and rf_kill is on. */
  4496. if (test_bit(STATUS_RF_KILL_HW, &priv->status))
  4497. return 0;
  4498. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  4499. iwlcore_clear_stations_table(priv);
  4500. /* load bootstrap state machine,
  4501. * load bootstrap program into processor's memory,
  4502. * prepare to load the "initialize" uCode */
  4503. ret = priv->cfg->ops->lib->load_ucode(priv);
  4504. if (ret) {
  4505. IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
  4506. continue;
  4507. }
  4508. /* start card; "initialize" will load runtime ucode */
  4509. iwl4965_nic_start(priv);
  4510. IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
  4511. return 0;
  4512. }
  4513. set_bit(STATUS_EXIT_PENDING, &priv->status);
  4514. __iwl4965_down(priv);
  4515. /* tried to restart and config the device for as long as our
  4516. * patience could withstand */
  4517. IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
  4518. return -EIO;
  4519. }
  4520. /*****************************************************************************
  4521. *
  4522. * Workqueue callbacks
  4523. *
  4524. *****************************************************************************/
  4525. static void iwl4965_bg_init_alive_start(struct work_struct *data)
  4526. {
  4527. struct iwl_priv *priv =
  4528. container_of(data, struct iwl_priv, init_alive_start.work);
  4529. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4530. return;
  4531. mutex_lock(&priv->mutex);
  4532. iwl4965_init_alive_start(priv);
  4533. mutex_unlock(&priv->mutex);
  4534. }
  4535. static void iwl4965_bg_alive_start(struct work_struct *data)
  4536. {
  4537. struct iwl_priv *priv =
  4538. container_of(data, struct iwl_priv, alive_start.work);
  4539. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4540. return;
  4541. mutex_lock(&priv->mutex);
  4542. iwl4965_alive_start(priv);
  4543. mutex_unlock(&priv->mutex);
  4544. }
  4545. static void iwl4965_bg_rf_kill(struct work_struct *work)
  4546. {
  4547. struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
  4548. wake_up_interruptible(&priv->wait_command_queue);
  4549. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4550. return;
  4551. mutex_lock(&priv->mutex);
  4552. if (!iwl_is_rfkill(priv)) {
  4553. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
  4554. "HW and/or SW RF Kill no longer active, restarting "
  4555. "device\n");
  4556. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  4557. queue_work(priv->workqueue, &priv->restart);
  4558. } else {
  4559. /* make sure mac80211 stop sending Tx frame */
  4560. if (priv->mac80211_registered)
  4561. ieee80211_stop_queues(priv->hw);
  4562. if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
  4563. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  4564. "disabled by SW switch\n");
  4565. else
  4566. IWL_WARNING("Radio Frequency Kill Switch is On:\n"
  4567. "Kill switch must be turned off for "
  4568. "wireless networking to work.\n");
  4569. }
  4570. iwl_rfkill_set_hw_state(priv);
  4571. mutex_unlock(&priv->mutex);
  4572. }
  4573. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  4574. static void iwl4965_bg_scan_check(struct work_struct *data)
  4575. {
  4576. struct iwl_priv *priv =
  4577. container_of(data, struct iwl_priv, scan_check.work);
  4578. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4579. return;
  4580. mutex_lock(&priv->mutex);
  4581. if (test_bit(STATUS_SCANNING, &priv->status) ||
  4582. test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  4583. IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
  4584. "Scan completion watchdog resetting adapter (%dms)\n",
  4585. jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
  4586. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  4587. iwl4965_send_scan_abort(priv);
  4588. }
  4589. mutex_unlock(&priv->mutex);
  4590. }
  4591. static void iwl4965_bg_request_scan(struct work_struct *data)
  4592. {
  4593. struct iwl_priv *priv =
  4594. container_of(data, struct iwl_priv, request_scan);
  4595. struct iwl_host_cmd cmd = {
  4596. .id = REPLY_SCAN_CMD,
  4597. .len = sizeof(struct iwl4965_scan_cmd),
  4598. .meta.flags = CMD_SIZE_HUGE,
  4599. };
  4600. struct iwl4965_scan_cmd *scan;
  4601. struct ieee80211_conf *conf = NULL;
  4602. u16 cmd_len;
  4603. enum ieee80211_band band;
  4604. u8 direct_mask;
  4605. int ret = 0;
  4606. conf = ieee80211_get_hw_conf(priv->hw);
  4607. mutex_lock(&priv->mutex);
  4608. if (!iwl_is_ready(priv)) {
  4609. IWL_WARNING("request scan called when driver not ready.\n");
  4610. goto done;
  4611. }
  4612. /* Make sure the scan wasn't cancelled before this queued work
  4613. * was given the chance to run... */
  4614. if (!test_bit(STATUS_SCANNING, &priv->status))
  4615. goto done;
  4616. /* This should never be called or scheduled if there is currently
  4617. * a scan active in the hardware. */
  4618. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  4619. IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
  4620. "Ignoring second request.\n");
  4621. ret = -EIO;
  4622. goto done;
  4623. }
  4624. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  4625. IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
  4626. goto done;
  4627. }
  4628. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  4629. IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
  4630. goto done;
  4631. }
  4632. if (iwl_is_rfkill(priv)) {
  4633. IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
  4634. goto done;
  4635. }
  4636. if (!test_bit(STATUS_READY, &priv->status)) {
  4637. IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
  4638. goto done;
  4639. }
  4640. if (!priv->scan_bands) {
  4641. IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
  4642. goto done;
  4643. }
  4644. if (!priv->scan) {
  4645. priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
  4646. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  4647. if (!priv->scan) {
  4648. ret = -ENOMEM;
  4649. goto done;
  4650. }
  4651. }
  4652. scan = priv->scan;
  4653. memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
  4654. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  4655. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  4656. if (iwl_is_associated(priv)) {
  4657. u16 interval = 0;
  4658. u32 extra;
  4659. u32 suspend_time = 100;
  4660. u32 scan_suspend_time = 100;
  4661. unsigned long flags;
  4662. IWL_DEBUG_INFO("Scanning while associated...\n");
  4663. spin_lock_irqsave(&priv->lock, flags);
  4664. interval = priv->beacon_int;
  4665. spin_unlock_irqrestore(&priv->lock, flags);
  4666. scan->suspend_time = 0;
  4667. scan->max_out_time = cpu_to_le32(200 * 1024);
  4668. if (!interval)
  4669. interval = suspend_time;
  4670. extra = (suspend_time / interval) << 22;
  4671. scan_suspend_time = (extra |
  4672. ((suspend_time % interval) * 1024));
  4673. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  4674. IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
  4675. scan_suspend_time, interval);
  4676. }
  4677. /* We should add the ability for user to lock to PASSIVE ONLY */
  4678. if (priv->one_direct_scan) {
  4679. IWL_DEBUG_SCAN
  4680. ("Kicking off one direct scan for '%s'\n",
  4681. iwl4965_escape_essid(priv->direct_ssid,
  4682. priv->direct_ssid_len));
  4683. scan->direct_scan[0].id = WLAN_EID_SSID;
  4684. scan->direct_scan[0].len = priv->direct_ssid_len;
  4685. memcpy(scan->direct_scan[0].ssid,
  4686. priv->direct_ssid, priv->direct_ssid_len);
  4687. direct_mask = 1;
  4688. } else if (!iwl_is_associated(priv) && priv->essid_len) {
  4689. IWL_DEBUG_SCAN
  4690. ("Kicking off one direct scan for '%s' when not associated\n",
  4691. iwl4965_escape_essid(priv->essid, priv->essid_len));
  4692. scan->direct_scan[0].id = WLAN_EID_SSID;
  4693. scan->direct_scan[0].len = priv->essid_len;
  4694. memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
  4695. direct_mask = 1;
  4696. } else {
  4697. IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
  4698. direct_mask = 0;
  4699. }
  4700. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  4701. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  4702. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  4703. switch (priv->scan_bands) {
  4704. case 2:
  4705. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  4706. scan->tx_cmd.rate_n_flags =
  4707. iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
  4708. RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
  4709. scan->good_CRC_th = 0;
  4710. band = IEEE80211_BAND_2GHZ;
  4711. break;
  4712. case 1:
  4713. scan->tx_cmd.rate_n_flags =
  4714. iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
  4715. RATE_MCS_ANT_B_MSK);
  4716. scan->good_CRC_th = IWL_GOOD_CRC_TH;
  4717. band = IEEE80211_BAND_5GHZ;
  4718. break;
  4719. default:
  4720. IWL_WARNING("Invalid scan band count\n");
  4721. goto done;
  4722. }
  4723. /* We don't build a direct scan probe request; the uCode will do
  4724. * that based on the direct_mask added to each channel entry */
  4725. cmd_len = iwl4965_fill_probe_req(priv, band,
  4726. (struct ieee80211_mgmt *)scan->data,
  4727. IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
  4728. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  4729. /* select Rx chains */
  4730. /* Force use of chains B and C (0x6) for scan Rx.
  4731. * Avoid A (0x1) because of its off-channel reception on A-band.
  4732. * MIMO is not used here, but value is required to make uCode happy. */
  4733. scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
  4734. cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
  4735. (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
  4736. (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
  4737. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
  4738. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  4739. if (direct_mask)
  4740. scan->channel_count =
  4741. iwl4965_get_channels_for_scan(
  4742. priv, band, 1, /* active */
  4743. direct_mask,
  4744. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  4745. else
  4746. scan->channel_count =
  4747. iwl4965_get_channels_for_scan(
  4748. priv, band, 0, /* passive */
  4749. direct_mask,
  4750. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  4751. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  4752. RXON_FILTER_BCON_AWARE_MSK);
  4753. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  4754. scan->channel_count * sizeof(struct iwl4965_scan_channel);
  4755. cmd.data = scan;
  4756. scan->len = cpu_to_le16(cmd.len);
  4757. set_bit(STATUS_SCAN_HW, &priv->status);
  4758. ret = iwl_send_cmd_sync(priv, &cmd);
  4759. if (ret)
  4760. goto done;
  4761. queue_delayed_work(priv->workqueue, &priv->scan_check,
  4762. IWL_SCAN_CHECK_WATCHDOG);
  4763. mutex_unlock(&priv->mutex);
  4764. return;
  4765. done:
  4766. /* inform mac80211 scan aborted */
  4767. queue_work(priv->workqueue, &priv->scan_completed);
  4768. mutex_unlock(&priv->mutex);
  4769. }
  4770. static void iwl4965_bg_up(struct work_struct *data)
  4771. {
  4772. struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
  4773. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4774. return;
  4775. mutex_lock(&priv->mutex);
  4776. __iwl4965_up(priv);
  4777. mutex_unlock(&priv->mutex);
  4778. }
  4779. static void iwl4965_bg_restart(struct work_struct *data)
  4780. {
  4781. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  4782. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4783. return;
  4784. iwl4965_down(priv);
  4785. queue_work(priv->workqueue, &priv->up);
  4786. }
  4787. static void iwl4965_bg_rx_replenish(struct work_struct *data)
  4788. {
  4789. struct iwl_priv *priv =
  4790. container_of(data, struct iwl_priv, rx_replenish);
  4791. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4792. return;
  4793. mutex_lock(&priv->mutex);
  4794. iwl4965_rx_replenish(priv);
  4795. mutex_unlock(&priv->mutex);
  4796. }
  4797. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  4798. static void iwl4965_post_associate(struct iwl_priv *priv)
  4799. {
  4800. struct ieee80211_conf *conf = NULL;
  4801. int ret = 0;
  4802. DECLARE_MAC_BUF(mac);
  4803. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  4804. IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
  4805. return;
  4806. }
  4807. IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
  4808. priv->assoc_id,
  4809. print_mac(mac, priv->active_rxon.bssid_addr));
  4810. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4811. return;
  4812. if (!priv->vif || !priv->is_open)
  4813. return;
  4814. iwl4965_scan_cancel_timeout(priv, 200);
  4815. conf = ieee80211_get_hw_conf(priv->hw);
  4816. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4817. iwl4965_commit_rxon(priv);
  4818. memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
  4819. iwl4965_setup_rxon_timing(priv);
  4820. ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  4821. sizeof(priv->rxon_timing), &priv->rxon_timing);
  4822. if (ret)
  4823. IWL_WARNING("REPLY_RXON_TIMING failed - "
  4824. "Attempting to continue.\n");
  4825. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  4826. #ifdef CONFIG_IWL4965_HT
  4827. if (priv->current_ht_config.is_ht)
  4828. iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
  4829. #endif /* CONFIG_IWL4965_HT*/
  4830. iwl4965_set_rxon_chain(priv);
  4831. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  4832. IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
  4833. priv->assoc_id, priv->beacon_int);
  4834. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  4835. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  4836. else
  4837. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  4838. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  4839. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  4840. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  4841. else
  4842. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  4843. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  4844. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  4845. }
  4846. iwl4965_commit_rxon(priv);
  4847. switch (priv->iw_mode) {
  4848. case IEEE80211_IF_TYPE_STA:
  4849. iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
  4850. break;
  4851. case IEEE80211_IF_TYPE_IBSS:
  4852. /* clear out the station table */
  4853. iwlcore_clear_stations_table(priv);
  4854. iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
  4855. iwl4965_rxon_add_station(priv, priv->bssid, 0);
  4856. iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
  4857. iwl4965_send_beacon_cmd(priv);
  4858. break;
  4859. default:
  4860. IWL_ERROR("%s Should not be called in %d mode\n",
  4861. __FUNCTION__, priv->iw_mode);
  4862. break;
  4863. }
  4864. iwl4965_sequence_reset(priv);
  4865. /* Enable Rx differential gain and sensitivity calibrations */
  4866. iwl_chain_noise_reset(priv);
  4867. priv->start_calib = 1;
  4868. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  4869. priv->assoc_station_added = 1;
  4870. iwl4965_activate_qos(priv, 0);
  4871. iwl_power_update_mode(priv, 0);
  4872. /* we have just associated, don't start scan too early */
  4873. priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
  4874. }
  4875. static void iwl4965_bg_post_associate(struct work_struct *data)
  4876. {
  4877. struct iwl_priv *priv = container_of(data, struct iwl_priv,
  4878. post_associate.work);
  4879. mutex_lock(&priv->mutex);
  4880. iwl4965_post_associate(priv);
  4881. mutex_unlock(&priv->mutex);
  4882. }
  4883. static void iwl4965_bg_abort_scan(struct work_struct *work)
  4884. {
  4885. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  4886. if (!iwl_is_ready(priv))
  4887. return;
  4888. mutex_lock(&priv->mutex);
  4889. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  4890. iwl4965_send_scan_abort(priv);
  4891. mutex_unlock(&priv->mutex);
  4892. }
  4893. static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
  4894. static void iwl4965_bg_scan_completed(struct work_struct *work)
  4895. {
  4896. struct iwl_priv *priv =
  4897. container_of(work, struct iwl_priv, scan_completed);
  4898. IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
  4899. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4900. return;
  4901. if (test_bit(STATUS_CONF_PENDING, &priv->status))
  4902. iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
  4903. ieee80211_scan_completed(priv->hw);
  4904. /* Since setting the TXPOWER may have been deferred while
  4905. * performing the scan, fire one off */
  4906. mutex_lock(&priv->mutex);
  4907. iwl4965_hw_reg_send_txpower(priv);
  4908. mutex_unlock(&priv->mutex);
  4909. }
  4910. /*****************************************************************************
  4911. *
  4912. * mac80211 entry point functions
  4913. *
  4914. *****************************************************************************/
  4915. #define UCODE_READY_TIMEOUT (2 * HZ)
  4916. static int iwl4965_mac_start(struct ieee80211_hw *hw)
  4917. {
  4918. struct iwl_priv *priv = hw->priv;
  4919. int ret;
  4920. IWL_DEBUG_MAC80211("enter\n");
  4921. if (pci_enable_device(priv->pci_dev)) {
  4922. IWL_ERROR("Fail to pci_enable_device\n");
  4923. return -ENODEV;
  4924. }
  4925. pci_restore_state(priv->pci_dev);
  4926. pci_enable_msi(priv->pci_dev);
  4927. ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
  4928. DRV_NAME, priv);
  4929. if (ret) {
  4930. IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
  4931. goto out_disable_msi;
  4932. }
  4933. /* we should be verifying the device is ready to be opened */
  4934. mutex_lock(&priv->mutex);
  4935. memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
  4936. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  4937. * ucode filename and max sizes are card-specific. */
  4938. if (!priv->ucode_code.len) {
  4939. ret = iwl4965_read_ucode(priv);
  4940. if (ret) {
  4941. IWL_ERROR("Could not read microcode: %d\n", ret);
  4942. mutex_unlock(&priv->mutex);
  4943. goto out_release_irq;
  4944. }
  4945. }
  4946. ret = __iwl4965_up(priv);
  4947. mutex_unlock(&priv->mutex);
  4948. if (ret)
  4949. goto out_release_irq;
  4950. IWL_DEBUG_INFO("Start UP work done.\n");
  4951. if (test_bit(STATUS_IN_SUSPEND, &priv->status))
  4952. return 0;
  4953. /* Wait for START_ALIVE from ucode. Otherwise callbacks from
  4954. * mac80211 will not be run successfully. */
  4955. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  4956. test_bit(STATUS_READY, &priv->status),
  4957. UCODE_READY_TIMEOUT);
  4958. if (!ret) {
  4959. if (!test_bit(STATUS_READY, &priv->status)) {
  4960. IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
  4961. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  4962. ret = -ETIMEDOUT;
  4963. goto out_release_irq;
  4964. }
  4965. }
  4966. priv->is_open = 1;
  4967. IWL_DEBUG_MAC80211("leave\n");
  4968. return 0;
  4969. out_release_irq:
  4970. free_irq(priv->pci_dev->irq, priv);
  4971. out_disable_msi:
  4972. pci_disable_msi(priv->pci_dev);
  4973. pci_disable_device(priv->pci_dev);
  4974. priv->is_open = 0;
  4975. IWL_DEBUG_MAC80211("leave - failed\n");
  4976. return ret;
  4977. }
  4978. static void iwl4965_mac_stop(struct ieee80211_hw *hw)
  4979. {
  4980. struct iwl_priv *priv = hw->priv;
  4981. IWL_DEBUG_MAC80211("enter\n");
  4982. if (!priv->is_open) {
  4983. IWL_DEBUG_MAC80211("leave - skip\n");
  4984. return;
  4985. }
  4986. priv->is_open = 0;
  4987. if (iwl_is_ready_rf(priv)) {
  4988. /* stop mac, cancel any scan request and clear
  4989. * RXON_FILTER_ASSOC_MSK BIT
  4990. */
  4991. mutex_lock(&priv->mutex);
  4992. iwl4965_scan_cancel_timeout(priv, 100);
  4993. cancel_delayed_work(&priv->post_associate);
  4994. mutex_unlock(&priv->mutex);
  4995. }
  4996. iwl4965_down(priv);
  4997. flush_workqueue(priv->workqueue);
  4998. free_irq(priv->pci_dev->irq, priv);
  4999. pci_disable_msi(priv->pci_dev);
  5000. pci_save_state(priv->pci_dev);
  5001. pci_disable_device(priv->pci_dev);
  5002. IWL_DEBUG_MAC80211("leave\n");
  5003. }
  5004. static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  5005. struct ieee80211_tx_control *ctl)
  5006. {
  5007. struct iwl_priv *priv = hw->priv;
  5008. IWL_DEBUG_MAC80211("enter\n");
  5009. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
  5010. IWL_DEBUG_MAC80211("leave - monitor\n");
  5011. return -1;
  5012. }
  5013. IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  5014. ctl->tx_rate->bitrate);
  5015. if (iwl4965_tx_skb(priv, skb, ctl))
  5016. dev_kfree_skb_any(skb);
  5017. IWL_DEBUG_MAC80211("leave\n");
  5018. return 0;
  5019. }
  5020. static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
  5021. struct ieee80211_if_init_conf *conf)
  5022. {
  5023. struct iwl_priv *priv = hw->priv;
  5024. unsigned long flags;
  5025. DECLARE_MAC_BUF(mac);
  5026. IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
  5027. if (priv->vif) {
  5028. IWL_DEBUG_MAC80211("leave - vif != NULL\n");
  5029. return -EOPNOTSUPP;
  5030. }
  5031. spin_lock_irqsave(&priv->lock, flags);
  5032. priv->vif = conf->vif;
  5033. spin_unlock_irqrestore(&priv->lock, flags);
  5034. mutex_lock(&priv->mutex);
  5035. if (conf->mac_addr) {
  5036. IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
  5037. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  5038. }
  5039. if (iwl_is_ready(priv))
  5040. iwl4965_set_mode(priv, conf->type);
  5041. mutex_unlock(&priv->mutex);
  5042. IWL_DEBUG_MAC80211("leave\n");
  5043. return 0;
  5044. }
  5045. /**
  5046. * iwl4965_mac_config - mac80211 config callback
  5047. *
  5048. * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
  5049. * be set inappropriately and the driver currently sets the hardware up to
  5050. * use it whenever needed.
  5051. */
  5052. static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  5053. {
  5054. struct iwl_priv *priv = hw->priv;
  5055. const struct iwl_channel_info *ch_info;
  5056. unsigned long flags;
  5057. int ret = 0;
  5058. mutex_lock(&priv->mutex);
  5059. IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
  5060. priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
  5061. if (!iwl_is_ready(priv)) {
  5062. IWL_DEBUG_MAC80211("leave - not ready\n");
  5063. ret = -EIO;
  5064. goto out;
  5065. }
  5066. if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
  5067. test_bit(STATUS_SCANNING, &priv->status))) {
  5068. IWL_DEBUG_MAC80211("leave - scanning\n");
  5069. set_bit(STATUS_CONF_PENDING, &priv->status);
  5070. mutex_unlock(&priv->mutex);
  5071. return 0;
  5072. }
  5073. spin_lock_irqsave(&priv->lock, flags);
  5074. ch_info = iwl_get_channel_info(priv, conf->channel->band,
  5075. ieee80211_frequency_to_channel(conf->channel->center_freq));
  5076. if (!is_channel_valid(ch_info)) {
  5077. IWL_DEBUG_MAC80211("leave - invalid channel\n");
  5078. spin_unlock_irqrestore(&priv->lock, flags);
  5079. ret = -EINVAL;
  5080. goto out;
  5081. }
  5082. #ifdef CONFIG_IWL4965_HT
  5083. /* if we are switching from ht to 2.4 clear flags
  5084. * from any ht related info since 2.4 does not
  5085. * support ht */
  5086. if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
  5087. #ifdef IEEE80211_CONF_CHANNEL_SWITCH
  5088. && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
  5089. #endif
  5090. )
  5091. priv->staging_rxon.flags = 0;
  5092. #endif /* CONFIG_IWL4965_HT */
  5093. iwlcore_set_rxon_channel(priv, conf->channel->band,
  5094. ieee80211_frequency_to_channel(conf->channel->center_freq));
  5095. iwl4965_set_flags_for_phymode(priv, conf->channel->band);
  5096. /* The list of supported rates and rate mask can be different
  5097. * for each band; since the band may have changed, reset
  5098. * the rate mask to what mac80211 lists */
  5099. iwl4965_set_rate(priv);
  5100. spin_unlock_irqrestore(&priv->lock, flags);
  5101. #ifdef IEEE80211_CONF_CHANNEL_SWITCH
  5102. if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
  5103. iwl4965_hw_channel_switch(priv, conf->channel);
  5104. goto out;
  5105. }
  5106. #endif
  5107. if (priv->cfg->ops->lib->radio_kill_sw)
  5108. priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
  5109. if (!conf->radio_enabled) {
  5110. IWL_DEBUG_MAC80211("leave - radio disabled\n");
  5111. goto out;
  5112. }
  5113. if (iwl_is_rfkill(priv)) {
  5114. IWL_DEBUG_MAC80211("leave - RF kill\n");
  5115. ret = -EIO;
  5116. goto out;
  5117. }
  5118. iwl4965_set_rate(priv);
  5119. if (memcmp(&priv->active_rxon,
  5120. &priv->staging_rxon, sizeof(priv->staging_rxon)))
  5121. iwl4965_commit_rxon(priv);
  5122. else
  5123. IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
  5124. IWL_DEBUG_MAC80211("leave\n");
  5125. out:
  5126. clear_bit(STATUS_CONF_PENDING, &priv->status);
  5127. mutex_unlock(&priv->mutex);
  5128. return ret;
  5129. }
  5130. static void iwl4965_config_ap(struct iwl_priv *priv)
  5131. {
  5132. int ret = 0;
  5133. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5134. return;
  5135. /* The following should be done only at AP bring up */
  5136. if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
  5137. /* RXON - unassoc (to set timing command) */
  5138. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5139. iwl4965_commit_rxon(priv);
  5140. /* RXON Timing */
  5141. memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
  5142. iwl4965_setup_rxon_timing(priv);
  5143. ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  5144. sizeof(priv->rxon_timing), &priv->rxon_timing);
  5145. if (ret)
  5146. IWL_WARNING("REPLY_RXON_TIMING failed - "
  5147. "Attempting to continue.\n");
  5148. iwl4965_set_rxon_chain(priv);
  5149. /* FIXME: what should be the assoc_id for AP? */
  5150. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  5151. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  5152. priv->staging_rxon.flags |=
  5153. RXON_FLG_SHORT_PREAMBLE_MSK;
  5154. else
  5155. priv->staging_rxon.flags &=
  5156. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  5157. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  5158. if (priv->assoc_capability &
  5159. WLAN_CAPABILITY_SHORT_SLOT_TIME)
  5160. priv->staging_rxon.flags |=
  5161. RXON_FLG_SHORT_SLOT_MSK;
  5162. else
  5163. priv->staging_rxon.flags &=
  5164. ~RXON_FLG_SHORT_SLOT_MSK;
  5165. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  5166. priv->staging_rxon.flags &=
  5167. ~RXON_FLG_SHORT_SLOT_MSK;
  5168. }
  5169. /* restore RXON assoc */
  5170. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  5171. iwl4965_commit_rxon(priv);
  5172. iwl4965_activate_qos(priv, 1);
  5173. iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
  5174. }
  5175. iwl4965_send_beacon_cmd(priv);
  5176. /* FIXME - we need to add code here to detect a totally new
  5177. * configuration, reset the AP, unassoc, rxon timing, assoc,
  5178. * clear sta table, add BCAST sta... */
  5179. }
  5180. static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
  5181. struct ieee80211_vif *vif,
  5182. struct ieee80211_if_conf *conf)
  5183. {
  5184. struct iwl_priv *priv = hw->priv;
  5185. DECLARE_MAC_BUF(mac);
  5186. unsigned long flags;
  5187. int rc;
  5188. if (conf == NULL)
  5189. return -EIO;
  5190. if (priv->vif != vif) {
  5191. IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
  5192. return 0;
  5193. }
  5194. if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
  5195. (!conf->beacon || !conf->ssid_len)) {
  5196. IWL_DEBUG_MAC80211
  5197. ("Leaving in AP mode because HostAPD is not ready.\n");
  5198. return 0;
  5199. }
  5200. if (!iwl_is_alive(priv))
  5201. return -EAGAIN;
  5202. mutex_lock(&priv->mutex);
  5203. if (conf->bssid)
  5204. IWL_DEBUG_MAC80211("bssid: %s\n",
  5205. print_mac(mac, conf->bssid));
  5206. /*
  5207. * very dubious code was here; the probe filtering flag is never set:
  5208. *
  5209. if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
  5210. !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
  5211. */
  5212. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  5213. if (!conf->bssid) {
  5214. conf->bssid = priv->mac_addr;
  5215. memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
  5216. IWL_DEBUG_MAC80211("bssid was set to: %s\n",
  5217. print_mac(mac, conf->bssid));
  5218. }
  5219. if (priv->ibss_beacon)
  5220. dev_kfree_skb(priv->ibss_beacon);
  5221. priv->ibss_beacon = conf->beacon;
  5222. }
  5223. if (iwl_is_rfkill(priv))
  5224. goto done;
  5225. if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
  5226. !is_multicast_ether_addr(conf->bssid)) {
  5227. /* If there is currently a HW scan going on in the background
  5228. * then we need to cancel it else the RXON below will fail. */
  5229. if (iwl4965_scan_cancel_timeout(priv, 100)) {
  5230. IWL_WARNING("Aborted scan still in progress "
  5231. "after 100ms\n");
  5232. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  5233. mutex_unlock(&priv->mutex);
  5234. return -EAGAIN;
  5235. }
  5236. memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
  5237. /* TODO: Audit driver for usage of these members and see
  5238. * if mac80211 deprecates them (priv->bssid looks like it
  5239. * shouldn't be there, but I haven't scanned the IBSS code
  5240. * to verify) - jpk */
  5241. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  5242. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  5243. iwl4965_config_ap(priv);
  5244. else {
  5245. rc = iwl4965_commit_rxon(priv);
  5246. if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
  5247. iwl4965_rxon_add_station(
  5248. priv, priv->active_rxon.bssid_addr, 1);
  5249. }
  5250. } else {
  5251. iwl4965_scan_cancel_timeout(priv, 100);
  5252. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5253. iwl4965_commit_rxon(priv);
  5254. }
  5255. done:
  5256. spin_lock_irqsave(&priv->lock, flags);
  5257. if (!conf->ssid_len)
  5258. memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
  5259. else
  5260. memcpy(priv->essid, conf->ssid, conf->ssid_len);
  5261. priv->essid_len = conf->ssid_len;
  5262. spin_unlock_irqrestore(&priv->lock, flags);
  5263. IWL_DEBUG_MAC80211("leave\n");
  5264. mutex_unlock(&priv->mutex);
  5265. return 0;
  5266. }
  5267. static void iwl4965_configure_filter(struct ieee80211_hw *hw,
  5268. unsigned int changed_flags,
  5269. unsigned int *total_flags,
  5270. int mc_count, struct dev_addr_list *mc_list)
  5271. {
  5272. /*
  5273. * XXX: dummy
  5274. * see also iwl4965_connection_init_rx_config
  5275. */
  5276. *total_flags = 0;
  5277. }
  5278. static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
  5279. struct ieee80211_if_init_conf *conf)
  5280. {
  5281. struct iwl_priv *priv = hw->priv;
  5282. IWL_DEBUG_MAC80211("enter\n");
  5283. mutex_lock(&priv->mutex);
  5284. if (iwl_is_ready_rf(priv)) {
  5285. iwl4965_scan_cancel_timeout(priv, 100);
  5286. cancel_delayed_work(&priv->post_associate);
  5287. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5288. iwl4965_commit_rxon(priv);
  5289. }
  5290. if (priv->vif == conf->vif) {
  5291. priv->vif = NULL;
  5292. memset(priv->bssid, 0, ETH_ALEN);
  5293. memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
  5294. priv->essid_len = 0;
  5295. }
  5296. mutex_unlock(&priv->mutex);
  5297. IWL_DEBUG_MAC80211("leave\n");
  5298. }
  5299. #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
  5300. static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
  5301. struct ieee80211_vif *vif,
  5302. struct ieee80211_bss_conf *bss_conf,
  5303. u32 changes)
  5304. {
  5305. struct iwl_priv *priv = hw->priv;
  5306. IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
  5307. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  5308. IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
  5309. bss_conf->use_short_preamble);
  5310. if (bss_conf->use_short_preamble)
  5311. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  5312. else
  5313. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  5314. }
  5315. if (changes & BSS_CHANGED_ERP_CTS_PROT) {
  5316. IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
  5317. if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
  5318. priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
  5319. else
  5320. priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  5321. }
  5322. if (changes & BSS_CHANGED_HT) {
  5323. IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
  5324. iwl4965_ht_conf(priv, bss_conf);
  5325. iwl4965_set_rxon_chain(priv);
  5326. }
  5327. if (changes & BSS_CHANGED_ASSOC) {
  5328. IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
  5329. /* This should never happen as this function should
  5330. * never be called from interrupt context. */
  5331. if (WARN_ON_ONCE(in_interrupt()))
  5332. return;
  5333. if (bss_conf->assoc) {
  5334. priv->assoc_id = bss_conf->aid;
  5335. priv->beacon_int = bss_conf->beacon_int;
  5336. priv->timestamp = bss_conf->timestamp;
  5337. priv->assoc_capability = bss_conf->assoc_capability;
  5338. priv->next_scan_jiffies = jiffies +
  5339. IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
  5340. mutex_lock(&priv->mutex);
  5341. iwl4965_post_associate(priv);
  5342. mutex_unlock(&priv->mutex);
  5343. } else {
  5344. priv->assoc_id = 0;
  5345. IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
  5346. }
  5347. } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
  5348. IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
  5349. iwl_send_rxon_assoc(priv);
  5350. }
  5351. }
  5352. static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
  5353. {
  5354. int rc = 0;
  5355. unsigned long flags;
  5356. struct iwl_priv *priv = hw->priv;
  5357. IWL_DEBUG_MAC80211("enter\n");
  5358. mutex_lock(&priv->mutex);
  5359. spin_lock_irqsave(&priv->lock, flags);
  5360. if (!iwl_is_ready_rf(priv)) {
  5361. rc = -EIO;
  5362. IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
  5363. goto out_unlock;
  5364. }
  5365. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
  5366. rc = -EIO;
  5367. IWL_ERROR("ERROR: APs don't scan\n");
  5368. goto out_unlock;
  5369. }
  5370. /* we don't schedule scan within next_scan_jiffies period */
  5371. if (priv->next_scan_jiffies &&
  5372. time_after(priv->next_scan_jiffies, jiffies)) {
  5373. rc = -EAGAIN;
  5374. goto out_unlock;
  5375. }
  5376. /* if we just finished scan ask for delay */
  5377. if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
  5378. IWL_DELAY_NEXT_SCAN, jiffies)) {
  5379. rc = -EAGAIN;
  5380. goto out_unlock;
  5381. }
  5382. if (len) {
  5383. IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
  5384. iwl4965_escape_essid(ssid, len), (int)len);
  5385. priv->one_direct_scan = 1;
  5386. priv->direct_ssid_len = (u8)
  5387. min((u8) len, (u8) IW_ESSID_MAX_SIZE);
  5388. memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
  5389. } else
  5390. priv->one_direct_scan = 0;
  5391. rc = iwl4965_scan_initiate(priv);
  5392. IWL_DEBUG_MAC80211("leave\n");
  5393. out_unlock:
  5394. spin_unlock_irqrestore(&priv->lock, flags);
  5395. mutex_unlock(&priv->mutex);
  5396. return rc;
  5397. }
  5398. static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
  5399. struct ieee80211_key_conf *keyconf, const u8 *addr,
  5400. u32 iv32, u16 *phase1key)
  5401. {
  5402. struct iwl_priv *priv = hw->priv;
  5403. u8 sta_id = IWL_INVALID_STATION;
  5404. unsigned long flags;
  5405. __le16 key_flags = 0;
  5406. int i;
  5407. DECLARE_MAC_BUF(mac);
  5408. IWL_DEBUG_MAC80211("enter\n");
  5409. sta_id = iwl_find_station(priv, addr);
  5410. if (sta_id == IWL_INVALID_STATION) {
  5411. IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
  5412. print_mac(mac, addr));
  5413. return;
  5414. }
  5415. iwl4965_scan_cancel_timeout(priv, 100);
  5416. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  5417. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  5418. key_flags &= ~STA_KEY_FLG_INVALID;
  5419. if (sta_id == priv->hw_params.bcast_sta_id)
  5420. key_flags |= STA_KEY_MULTICAST_MSK;
  5421. spin_lock_irqsave(&priv->sta_lock, flags);
  5422. priv->stations[sta_id].sta.key.key_flags = key_flags;
  5423. priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  5424. for (i = 0; i < 5; i++)
  5425. priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  5426. cpu_to_le16(phase1key[i]);
  5427. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  5428. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  5429. iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  5430. spin_unlock_irqrestore(&priv->sta_lock, flags);
  5431. IWL_DEBUG_MAC80211("leave\n");
  5432. }
  5433. static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  5434. const u8 *local_addr, const u8 *addr,
  5435. struct ieee80211_key_conf *key)
  5436. {
  5437. struct iwl_priv *priv = hw->priv;
  5438. DECLARE_MAC_BUF(mac);
  5439. int ret = 0;
  5440. u8 sta_id = IWL_INVALID_STATION;
  5441. u8 is_default_wep_key = 0;
  5442. IWL_DEBUG_MAC80211("enter\n");
  5443. if (priv->hw_params.sw_crypto) {
  5444. IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
  5445. return -EOPNOTSUPP;
  5446. }
  5447. if (is_zero_ether_addr(addr))
  5448. /* only support pairwise keys */
  5449. return -EOPNOTSUPP;
  5450. sta_id = iwl_find_station(priv, addr);
  5451. if (sta_id == IWL_INVALID_STATION) {
  5452. IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
  5453. print_mac(mac, addr));
  5454. return -EINVAL;
  5455. }
  5456. mutex_lock(&priv->mutex);
  5457. iwl4965_scan_cancel_timeout(priv, 100);
  5458. mutex_unlock(&priv->mutex);
  5459. /* If we are getting WEP group key and we didn't receive any key mapping
  5460. * so far, we are in legacy wep mode (group key only), otherwise we are
  5461. * in 1X mode.
  5462. * In legacy wep mode, we use another host command to the uCode */
  5463. if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
  5464. priv->iw_mode != IEEE80211_IF_TYPE_AP) {
  5465. if (cmd == SET_KEY)
  5466. is_default_wep_key = !priv->key_mapping_key;
  5467. else
  5468. is_default_wep_key = priv->default_wep_key;
  5469. }
  5470. switch (cmd) {
  5471. case SET_KEY:
  5472. if (is_default_wep_key)
  5473. ret = iwl_set_default_wep_key(priv, key);
  5474. else
  5475. ret = iwl_set_dynamic_key(priv, key, sta_id);
  5476. IWL_DEBUG_MAC80211("enable hwcrypto key\n");
  5477. break;
  5478. case DISABLE_KEY:
  5479. if (is_default_wep_key)
  5480. ret = iwl_remove_default_wep_key(priv, key);
  5481. else
  5482. ret = iwl_remove_dynamic_key(priv, key, sta_id);
  5483. IWL_DEBUG_MAC80211("disable hwcrypto key\n");
  5484. break;
  5485. default:
  5486. ret = -EINVAL;
  5487. }
  5488. IWL_DEBUG_MAC80211("leave\n");
  5489. return ret;
  5490. }
  5491. static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
  5492. const struct ieee80211_tx_queue_params *params)
  5493. {
  5494. struct iwl_priv *priv = hw->priv;
  5495. unsigned long flags;
  5496. int q;
  5497. IWL_DEBUG_MAC80211("enter\n");
  5498. if (!iwl_is_ready_rf(priv)) {
  5499. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  5500. return -EIO;
  5501. }
  5502. if (queue >= AC_NUM) {
  5503. IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
  5504. return 0;
  5505. }
  5506. if (!priv->qos_data.qos_enable) {
  5507. priv->qos_data.qos_active = 0;
  5508. IWL_DEBUG_MAC80211("leave - qos not enabled\n");
  5509. return 0;
  5510. }
  5511. q = AC_NUM - 1 - queue;
  5512. spin_lock_irqsave(&priv->lock, flags);
  5513. priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
  5514. priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
  5515. priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  5516. priv->qos_data.def_qos_parm.ac[q].edca_txop =
  5517. cpu_to_le16((params->txop * 32));
  5518. priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  5519. priv->qos_data.qos_active = 1;
  5520. spin_unlock_irqrestore(&priv->lock, flags);
  5521. mutex_lock(&priv->mutex);
  5522. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  5523. iwl4965_activate_qos(priv, 1);
  5524. else if (priv->assoc_id && iwl_is_associated(priv))
  5525. iwl4965_activate_qos(priv, 0);
  5526. mutex_unlock(&priv->mutex);
  5527. IWL_DEBUG_MAC80211("leave\n");
  5528. return 0;
  5529. }
  5530. static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
  5531. struct ieee80211_tx_queue_stats *stats)
  5532. {
  5533. struct iwl_priv *priv = hw->priv;
  5534. int i, avail;
  5535. struct iwl4965_tx_queue *txq;
  5536. struct iwl4965_queue *q;
  5537. unsigned long flags;
  5538. IWL_DEBUG_MAC80211("enter\n");
  5539. if (!iwl_is_ready_rf(priv)) {
  5540. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  5541. return -EIO;
  5542. }
  5543. spin_lock_irqsave(&priv->lock, flags);
  5544. for (i = 0; i < AC_NUM; i++) {
  5545. txq = &priv->txq[i];
  5546. q = &txq->q;
  5547. avail = iwl4965_queue_space(q);
  5548. stats->data[i].len = q->n_window - avail;
  5549. stats->data[i].limit = q->n_window - q->high_mark;
  5550. stats->data[i].count = q->n_window;
  5551. }
  5552. spin_unlock_irqrestore(&priv->lock, flags);
  5553. IWL_DEBUG_MAC80211("leave\n");
  5554. return 0;
  5555. }
  5556. static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
  5557. struct ieee80211_low_level_stats *stats)
  5558. {
  5559. IWL_DEBUG_MAC80211("enter\n");
  5560. IWL_DEBUG_MAC80211("leave\n");
  5561. return 0;
  5562. }
  5563. static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
  5564. {
  5565. IWL_DEBUG_MAC80211("enter\n");
  5566. IWL_DEBUG_MAC80211("leave\n");
  5567. return 0;
  5568. }
  5569. static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
  5570. {
  5571. struct iwl_priv *priv = hw->priv;
  5572. unsigned long flags;
  5573. mutex_lock(&priv->mutex);
  5574. IWL_DEBUG_MAC80211("enter\n");
  5575. priv->lq_mngr.lq_ready = 0;
  5576. #ifdef CONFIG_IWL4965_HT
  5577. spin_lock_irqsave(&priv->lock, flags);
  5578. memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
  5579. spin_unlock_irqrestore(&priv->lock, flags);
  5580. #endif /* CONFIG_IWL4965_HT */
  5581. iwlcore_reset_qos(priv);
  5582. cancel_delayed_work(&priv->post_associate);
  5583. spin_lock_irqsave(&priv->lock, flags);
  5584. priv->assoc_id = 0;
  5585. priv->assoc_capability = 0;
  5586. priv->assoc_station_added = 0;
  5587. /* new association get rid of ibss beacon skb */
  5588. if (priv->ibss_beacon)
  5589. dev_kfree_skb(priv->ibss_beacon);
  5590. priv->ibss_beacon = NULL;
  5591. priv->beacon_int = priv->hw->conf.beacon_int;
  5592. priv->timestamp = 0;
  5593. if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
  5594. priv->beacon_int = 0;
  5595. spin_unlock_irqrestore(&priv->lock, flags);
  5596. if (!iwl_is_ready_rf(priv)) {
  5597. IWL_DEBUG_MAC80211("leave - not ready\n");
  5598. mutex_unlock(&priv->mutex);
  5599. return;
  5600. }
  5601. /* we are restarting association process
  5602. * clear RXON_FILTER_ASSOC_MSK bit
  5603. */
  5604. if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
  5605. iwl4965_scan_cancel_timeout(priv, 100);
  5606. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5607. iwl4965_commit_rxon(priv);
  5608. }
  5609. iwl_power_update_mode(priv, 0);
  5610. /* Per mac80211.h: This is only used in IBSS mode... */
  5611. if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
  5612. IWL_DEBUG_MAC80211("leave - not in IBSS\n");
  5613. mutex_unlock(&priv->mutex);
  5614. return;
  5615. }
  5616. iwl4965_set_rate(priv);
  5617. mutex_unlock(&priv->mutex);
  5618. IWL_DEBUG_MAC80211("leave\n");
  5619. }
  5620. static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
  5621. struct ieee80211_tx_control *control)
  5622. {
  5623. struct iwl_priv *priv = hw->priv;
  5624. unsigned long flags;
  5625. mutex_lock(&priv->mutex);
  5626. IWL_DEBUG_MAC80211("enter\n");
  5627. if (!iwl_is_ready_rf(priv)) {
  5628. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  5629. mutex_unlock(&priv->mutex);
  5630. return -EIO;
  5631. }
  5632. if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
  5633. IWL_DEBUG_MAC80211("leave - not IBSS\n");
  5634. mutex_unlock(&priv->mutex);
  5635. return -EIO;
  5636. }
  5637. spin_lock_irqsave(&priv->lock, flags);
  5638. if (priv->ibss_beacon)
  5639. dev_kfree_skb(priv->ibss_beacon);
  5640. priv->ibss_beacon = skb;
  5641. priv->assoc_id = 0;
  5642. IWL_DEBUG_MAC80211("leave\n");
  5643. spin_unlock_irqrestore(&priv->lock, flags);
  5644. iwlcore_reset_qos(priv);
  5645. queue_work(priv->workqueue, &priv->post_associate.work);
  5646. mutex_unlock(&priv->mutex);
  5647. return 0;
  5648. }
  5649. /*****************************************************************************
  5650. *
  5651. * sysfs attributes
  5652. *
  5653. *****************************************************************************/
  5654. #ifdef CONFIG_IWLWIFI_DEBUG
  5655. /*
  5656. * The following adds a new attribute to the sysfs representation
  5657. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  5658. * used for controlling the debug level.
  5659. *
  5660. * See the level definitions in iwl for details.
  5661. */
  5662. static ssize_t show_debug_level(struct device_driver *d, char *buf)
  5663. {
  5664. return sprintf(buf, "0x%08X\n", iwl_debug_level);
  5665. }
  5666. static ssize_t store_debug_level(struct device_driver *d,
  5667. const char *buf, size_t count)
  5668. {
  5669. char *p = (char *)buf;
  5670. u32 val;
  5671. val = simple_strtoul(p, &p, 0);
  5672. if (p == buf)
  5673. printk(KERN_INFO DRV_NAME
  5674. ": %s is not in hex or decimal form.\n", buf);
  5675. else
  5676. iwl_debug_level = val;
  5677. return strnlen(buf, count);
  5678. }
  5679. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  5680. show_debug_level, store_debug_level);
  5681. #endif /* CONFIG_IWLWIFI_DEBUG */
  5682. static ssize_t show_temperature(struct device *d,
  5683. struct device_attribute *attr, char *buf)
  5684. {
  5685. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5686. if (!iwl_is_alive(priv))
  5687. return -EAGAIN;
  5688. return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
  5689. }
  5690. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  5691. static ssize_t show_rs_window(struct device *d,
  5692. struct device_attribute *attr,
  5693. char *buf)
  5694. {
  5695. struct iwl_priv *priv = d->driver_data;
  5696. return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
  5697. }
  5698. static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
  5699. static ssize_t show_tx_power(struct device *d,
  5700. struct device_attribute *attr, char *buf)
  5701. {
  5702. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5703. return sprintf(buf, "%d\n", priv->user_txpower_limit);
  5704. }
  5705. static ssize_t store_tx_power(struct device *d,
  5706. struct device_attribute *attr,
  5707. const char *buf, size_t count)
  5708. {
  5709. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5710. char *p = (char *)buf;
  5711. u32 val;
  5712. val = simple_strtoul(p, &p, 10);
  5713. if (p == buf)
  5714. printk(KERN_INFO DRV_NAME
  5715. ": %s is not in decimal form.\n", buf);
  5716. else
  5717. iwl4965_hw_reg_set_txpower(priv, val);
  5718. return count;
  5719. }
  5720. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  5721. static ssize_t show_flags(struct device *d,
  5722. struct device_attribute *attr, char *buf)
  5723. {
  5724. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5725. return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
  5726. }
  5727. static ssize_t store_flags(struct device *d,
  5728. struct device_attribute *attr,
  5729. const char *buf, size_t count)
  5730. {
  5731. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5732. u32 flags = simple_strtoul(buf, NULL, 0);
  5733. mutex_lock(&priv->mutex);
  5734. if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
  5735. /* Cancel any currently running scans... */
  5736. if (iwl4965_scan_cancel_timeout(priv, 100))
  5737. IWL_WARNING("Could not cancel scan.\n");
  5738. else {
  5739. IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
  5740. flags);
  5741. priv->staging_rxon.flags = cpu_to_le32(flags);
  5742. iwl4965_commit_rxon(priv);
  5743. }
  5744. }
  5745. mutex_unlock(&priv->mutex);
  5746. return count;
  5747. }
  5748. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
  5749. static ssize_t show_filter_flags(struct device *d,
  5750. struct device_attribute *attr, char *buf)
  5751. {
  5752. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5753. return sprintf(buf, "0x%04X\n",
  5754. le32_to_cpu(priv->active_rxon.filter_flags));
  5755. }
  5756. static ssize_t store_filter_flags(struct device *d,
  5757. struct device_attribute *attr,
  5758. const char *buf, size_t count)
  5759. {
  5760. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5761. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  5762. mutex_lock(&priv->mutex);
  5763. if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
  5764. /* Cancel any currently running scans... */
  5765. if (iwl4965_scan_cancel_timeout(priv, 100))
  5766. IWL_WARNING("Could not cancel scan.\n");
  5767. else {
  5768. IWL_DEBUG_INFO("Committing rxon.filter_flags = "
  5769. "0x%04X\n", filter_flags);
  5770. priv->staging_rxon.filter_flags =
  5771. cpu_to_le32(filter_flags);
  5772. iwl4965_commit_rxon(priv);
  5773. }
  5774. }
  5775. mutex_unlock(&priv->mutex);
  5776. return count;
  5777. }
  5778. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
  5779. store_filter_flags);
  5780. #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
  5781. static ssize_t show_measurement(struct device *d,
  5782. struct device_attribute *attr, char *buf)
  5783. {
  5784. struct iwl_priv *priv = dev_get_drvdata(d);
  5785. struct iwl4965_spectrum_notification measure_report;
  5786. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  5787. u8 *data = (u8 *) & measure_report;
  5788. unsigned long flags;
  5789. spin_lock_irqsave(&priv->lock, flags);
  5790. if (!(priv->measurement_status & MEASUREMENT_READY)) {
  5791. spin_unlock_irqrestore(&priv->lock, flags);
  5792. return 0;
  5793. }
  5794. memcpy(&measure_report, &priv->measure_report, size);
  5795. priv->measurement_status = 0;
  5796. spin_unlock_irqrestore(&priv->lock, flags);
  5797. while (size && (PAGE_SIZE - len)) {
  5798. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  5799. PAGE_SIZE - len, 1);
  5800. len = strlen(buf);
  5801. if (PAGE_SIZE - len)
  5802. buf[len++] = '\n';
  5803. ofs += 16;
  5804. size -= min(size, 16U);
  5805. }
  5806. return len;
  5807. }
  5808. static ssize_t store_measurement(struct device *d,
  5809. struct device_attribute *attr,
  5810. const char *buf, size_t count)
  5811. {
  5812. struct iwl_priv *priv = dev_get_drvdata(d);
  5813. struct ieee80211_measurement_params params = {
  5814. .channel = le16_to_cpu(priv->active_rxon.channel),
  5815. .start_time = cpu_to_le64(priv->last_tsf),
  5816. .duration = cpu_to_le16(1),
  5817. };
  5818. u8 type = IWL_MEASURE_BASIC;
  5819. u8 buffer[32];
  5820. u8 channel;
  5821. if (count) {
  5822. char *p = buffer;
  5823. strncpy(buffer, buf, min(sizeof(buffer), count));
  5824. channel = simple_strtoul(p, NULL, 0);
  5825. if (channel)
  5826. params.channel = channel;
  5827. p = buffer;
  5828. while (*p && *p != ' ')
  5829. p++;
  5830. if (*p)
  5831. type = simple_strtoul(p + 1, NULL, 0);
  5832. }
  5833. IWL_DEBUG_INFO("Invoking measurement of type %d on "
  5834. "channel %d (for '%s')\n", type, params.channel, buf);
  5835. iwl4965_get_measurement(priv, &params, type);
  5836. return count;
  5837. }
  5838. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
  5839. show_measurement, store_measurement);
  5840. #endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
  5841. static ssize_t store_retry_rate(struct device *d,
  5842. struct device_attribute *attr,
  5843. const char *buf, size_t count)
  5844. {
  5845. struct iwl_priv *priv = dev_get_drvdata(d);
  5846. priv->retry_rate = simple_strtoul(buf, NULL, 0);
  5847. if (priv->retry_rate <= 0)
  5848. priv->retry_rate = 1;
  5849. return count;
  5850. }
  5851. static ssize_t show_retry_rate(struct device *d,
  5852. struct device_attribute *attr, char *buf)
  5853. {
  5854. struct iwl_priv *priv = dev_get_drvdata(d);
  5855. return sprintf(buf, "%d", priv->retry_rate);
  5856. }
  5857. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
  5858. store_retry_rate);
  5859. static ssize_t store_power_level(struct device *d,
  5860. struct device_attribute *attr,
  5861. const char *buf, size_t count)
  5862. {
  5863. struct iwl_priv *priv = dev_get_drvdata(d);
  5864. int rc;
  5865. int mode;
  5866. mode = simple_strtoul(buf, NULL, 0);
  5867. mutex_lock(&priv->mutex);
  5868. if (!iwl_is_ready(priv)) {
  5869. rc = -EAGAIN;
  5870. goto out;
  5871. }
  5872. rc = iwl_power_set_user_mode(priv, mode);
  5873. if (rc) {
  5874. IWL_DEBUG_MAC80211("failed setting power mode.\n");
  5875. goto out;
  5876. }
  5877. rc = count;
  5878. out:
  5879. mutex_unlock(&priv->mutex);
  5880. return rc;
  5881. }
  5882. #define MAX_WX_STRING 80
  5883. /* Values are in microsecond */
  5884. static const s32 timeout_duration[] = {
  5885. 350000,
  5886. 250000,
  5887. 75000,
  5888. 37000,
  5889. 25000,
  5890. };
  5891. static const s32 period_duration[] = {
  5892. 400000,
  5893. 700000,
  5894. 1000000,
  5895. 1000000,
  5896. 1000000
  5897. };
  5898. static ssize_t show_power_level(struct device *d,
  5899. struct device_attribute *attr, char *buf)
  5900. {
  5901. struct iwl_priv *priv = dev_get_drvdata(d);
  5902. int level = priv->power_data.power_mode;
  5903. char *p = buf;
  5904. p += sprintf(p, "%d ", level);
  5905. switch (level) {
  5906. case IWL_POWER_MODE_CAM:
  5907. case IWL_POWER_AC:
  5908. p += sprintf(p, "(AC)");
  5909. break;
  5910. case IWL_POWER_BATTERY:
  5911. p += sprintf(p, "(BATTERY)");
  5912. break;
  5913. default:
  5914. p += sprintf(p,
  5915. "(Timeout %dms, Period %dms)",
  5916. timeout_duration[level - 1] / 1000,
  5917. period_duration[level - 1] / 1000);
  5918. }
  5919. /*
  5920. if (!(priv->power_mode & IWL_POWER_ENABLED))
  5921. p += sprintf(p, " OFF\n");
  5922. else
  5923. p += sprintf(p, " \n");
  5924. */
  5925. p += sprintf(p, " \n");
  5926. return (p - buf + 1);
  5927. }
  5928. static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
  5929. store_power_level);
  5930. static ssize_t show_channels(struct device *d,
  5931. struct device_attribute *attr, char *buf)
  5932. {
  5933. /* all this shit doesn't belong into sysfs anyway */
  5934. return 0;
  5935. }
  5936. static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
  5937. static ssize_t show_statistics(struct device *d,
  5938. struct device_attribute *attr, char *buf)
  5939. {
  5940. struct iwl_priv *priv = dev_get_drvdata(d);
  5941. u32 size = sizeof(struct iwl4965_notif_statistics);
  5942. u32 len = 0, ofs = 0;
  5943. u8 *data = (u8 *) & priv->statistics;
  5944. int rc = 0;
  5945. if (!iwl_is_alive(priv))
  5946. return -EAGAIN;
  5947. mutex_lock(&priv->mutex);
  5948. rc = iwl_send_statistics_request(priv, 0);
  5949. mutex_unlock(&priv->mutex);
  5950. if (rc) {
  5951. len = sprintf(buf,
  5952. "Error sending statistics request: 0x%08X\n", rc);
  5953. return len;
  5954. }
  5955. while (size && (PAGE_SIZE - len)) {
  5956. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  5957. PAGE_SIZE - len, 1);
  5958. len = strlen(buf);
  5959. if (PAGE_SIZE - len)
  5960. buf[len++] = '\n';
  5961. ofs += 16;
  5962. size -= min(size, 16U);
  5963. }
  5964. return len;
  5965. }
  5966. static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
  5967. static ssize_t show_status(struct device *d,
  5968. struct device_attribute *attr, char *buf)
  5969. {
  5970. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5971. if (!iwl_is_alive(priv))
  5972. return -EAGAIN;
  5973. return sprintf(buf, "0x%08x\n", (int)priv->status);
  5974. }
  5975. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  5976. static ssize_t dump_error_log(struct device *d,
  5977. struct device_attribute *attr,
  5978. const char *buf, size_t count)
  5979. {
  5980. char *p = (char *)buf;
  5981. if (p[0] == '1')
  5982. iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
  5983. return strnlen(buf, count);
  5984. }
  5985. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
  5986. static ssize_t dump_event_log(struct device *d,
  5987. struct device_attribute *attr,
  5988. const char *buf, size_t count)
  5989. {
  5990. char *p = (char *)buf;
  5991. if (p[0] == '1')
  5992. iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
  5993. return strnlen(buf, count);
  5994. }
  5995. static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
  5996. /*****************************************************************************
  5997. *
  5998. * driver setup and teardown
  5999. *
  6000. *****************************************************************************/
  6001. static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
  6002. {
  6003. priv->workqueue = create_workqueue(DRV_NAME);
  6004. init_waitqueue_head(&priv->wait_command_queue);
  6005. INIT_WORK(&priv->up, iwl4965_bg_up);
  6006. INIT_WORK(&priv->restart, iwl4965_bg_restart);
  6007. INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
  6008. INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
  6009. INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
  6010. INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
  6011. INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
  6012. INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
  6013. INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
  6014. INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
  6015. INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
  6016. INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
  6017. iwl4965_hw_setup_deferred_work(priv);
  6018. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  6019. iwl4965_irq_tasklet, (unsigned long)priv);
  6020. }
  6021. static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
  6022. {
  6023. iwl4965_hw_cancel_deferred_work(priv);
  6024. cancel_delayed_work_sync(&priv->init_alive_start);
  6025. cancel_delayed_work(&priv->scan_check);
  6026. cancel_delayed_work(&priv->alive_start);
  6027. cancel_delayed_work(&priv->post_associate);
  6028. cancel_work_sync(&priv->beacon_update);
  6029. }
  6030. static struct attribute *iwl4965_sysfs_entries[] = {
  6031. &dev_attr_channels.attr,
  6032. &dev_attr_dump_errors.attr,
  6033. &dev_attr_dump_events.attr,
  6034. &dev_attr_flags.attr,
  6035. &dev_attr_filter_flags.attr,
  6036. #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
  6037. &dev_attr_measurement.attr,
  6038. #endif
  6039. &dev_attr_power_level.attr,
  6040. &dev_attr_retry_rate.attr,
  6041. &dev_attr_rs_window.attr,
  6042. &dev_attr_statistics.attr,
  6043. &dev_attr_status.attr,
  6044. &dev_attr_temperature.attr,
  6045. &dev_attr_tx_power.attr,
  6046. NULL
  6047. };
  6048. static struct attribute_group iwl4965_attribute_group = {
  6049. .name = NULL, /* put in device directory */
  6050. .attrs = iwl4965_sysfs_entries,
  6051. };
  6052. static struct ieee80211_ops iwl4965_hw_ops = {
  6053. .tx = iwl4965_mac_tx,
  6054. .start = iwl4965_mac_start,
  6055. .stop = iwl4965_mac_stop,
  6056. .add_interface = iwl4965_mac_add_interface,
  6057. .remove_interface = iwl4965_mac_remove_interface,
  6058. .config = iwl4965_mac_config,
  6059. .config_interface = iwl4965_mac_config_interface,
  6060. .configure_filter = iwl4965_configure_filter,
  6061. .set_key = iwl4965_mac_set_key,
  6062. .update_tkip_key = iwl4965_mac_update_tkip_key,
  6063. .get_stats = iwl4965_mac_get_stats,
  6064. .get_tx_stats = iwl4965_mac_get_tx_stats,
  6065. .conf_tx = iwl4965_mac_conf_tx,
  6066. .get_tsf = iwl4965_mac_get_tsf,
  6067. .reset_tsf = iwl4965_mac_reset_tsf,
  6068. .beacon_update = iwl4965_mac_beacon_update,
  6069. .bss_info_changed = iwl4965_bss_info_changed,
  6070. #ifdef CONFIG_IWL4965_HT
  6071. .ampdu_action = iwl4965_mac_ampdu_action,
  6072. #endif /* CONFIG_IWL4965_HT */
  6073. .hw_scan = iwl4965_mac_hw_scan
  6074. };
  6075. static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  6076. {
  6077. int err = 0;
  6078. struct iwl_priv *priv;
  6079. struct ieee80211_hw *hw;
  6080. struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
  6081. unsigned long flags;
  6082. DECLARE_MAC_BUF(mac);
  6083. /************************
  6084. * 1. Allocating HW data
  6085. ************************/
  6086. /* Disabling hardware scan means that mac80211 will perform scans
  6087. * "the hard way", rather than using device's scan. */
  6088. if (cfg->mod_params->disable_hw_scan) {
  6089. IWL_DEBUG_INFO("Disabling hw_scan\n");
  6090. iwl4965_hw_ops.hw_scan = NULL;
  6091. }
  6092. hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
  6093. if (!hw) {
  6094. err = -ENOMEM;
  6095. goto out;
  6096. }
  6097. priv = hw->priv;
  6098. /* At this point both hw and priv are allocated. */
  6099. SET_IEEE80211_DEV(hw, &pdev->dev);
  6100. IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
  6101. priv->cfg = cfg;
  6102. priv->pci_dev = pdev;
  6103. #ifdef CONFIG_IWLWIFI_DEBUG
  6104. iwl_debug_level = priv->cfg->mod_params->debug;
  6105. atomic_set(&priv->restrict_refcnt, 0);
  6106. #endif
  6107. /**************************
  6108. * 2. Initializing PCI bus
  6109. **************************/
  6110. if (pci_enable_device(pdev)) {
  6111. err = -ENODEV;
  6112. goto out_ieee80211_free_hw;
  6113. }
  6114. pci_set_master(pdev);
  6115. err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  6116. if (!err)
  6117. err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  6118. if (err) {
  6119. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  6120. if (!err)
  6121. err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  6122. /* both attempts failed: */
  6123. if (err) {
  6124. printk(KERN_WARNING "%s: No suitable DMA available.\n",
  6125. DRV_NAME);
  6126. goto out_pci_disable_device;
  6127. }
  6128. }
  6129. err = pci_request_regions(pdev, DRV_NAME);
  6130. if (err)
  6131. goto out_pci_disable_device;
  6132. pci_set_drvdata(pdev, priv);
  6133. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  6134. * PCI Tx retries from interfering with C3 CPU state */
  6135. pci_write_config_byte(pdev, 0x41, 0x00);
  6136. /***********************
  6137. * 3. Read REV register
  6138. ***********************/
  6139. priv->hw_base = pci_iomap(pdev, 0, 0);
  6140. if (!priv->hw_base) {
  6141. err = -ENODEV;
  6142. goto out_pci_release_regions;
  6143. }
  6144. IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
  6145. (unsigned long long) pci_resource_len(pdev, 0));
  6146. IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
  6147. iwl_hw_detect(priv);
  6148. printk(KERN_INFO DRV_NAME
  6149. ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
  6150. priv->cfg->name, priv->hw_rev);
  6151. /* amp init */
  6152. err = priv->cfg->ops->lib->apm_ops.init(priv);
  6153. if (err < 0) {
  6154. IWL_DEBUG_INFO("Failed to init APMG\n");
  6155. goto out_iounmap;
  6156. }
  6157. /*****************
  6158. * 4. Read EEPROM
  6159. *****************/
  6160. /* Read the EEPROM */
  6161. err = iwl_eeprom_init(priv);
  6162. if (err) {
  6163. IWL_ERROR("Unable to init EEPROM\n");
  6164. goto out_iounmap;
  6165. }
  6166. err = iwl_eeprom_check_version(priv);
  6167. if (err)
  6168. goto out_iounmap;
  6169. /* MAC Address location in EEPROM same for 3945/4965 */
  6170. iwl_eeprom_get_mac(priv, priv->mac_addr);
  6171. IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
  6172. SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
  6173. /************************
  6174. * 5. Setup HW constants
  6175. ************************/
  6176. /* Device-specific setup */
  6177. if (priv->cfg->ops->lib->set_hw_params(priv)) {
  6178. IWL_ERROR("failed to set hw parameters\n");
  6179. goto out_free_eeprom;
  6180. }
  6181. /*******************
  6182. * 6. Setup hw/priv
  6183. *******************/
  6184. err = iwl_setup(priv);
  6185. if (err)
  6186. goto out_free_eeprom;
  6187. /* At this point both hw and priv are initialized. */
  6188. /**********************************
  6189. * 7. Initialize module parameters
  6190. **********************************/
  6191. /* Disable radio (SW RF KILL) via parameter when loading driver */
  6192. if (priv->cfg->mod_params->disable) {
  6193. set_bit(STATUS_RF_KILL_SW, &priv->status);
  6194. IWL_DEBUG_INFO("Radio disabled.\n");
  6195. }
  6196. if (priv->cfg->mod_params->enable_qos)
  6197. priv->qos_data.qos_enable = 1;
  6198. /********************
  6199. * 8. Setup services
  6200. ********************/
  6201. spin_lock_irqsave(&priv->lock, flags);
  6202. iwl4965_disable_interrupts(priv);
  6203. spin_unlock_irqrestore(&priv->lock, flags);
  6204. err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
  6205. if (err) {
  6206. IWL_ERROR("failed to create sysfs device attributes\n");
  6207. goto out_free_eeprom;
  6208. }
  6209. err = iwl_dbgfs_register(priv, DRV_NAME);
  6210. if (err) {
  6211. IWL_ERROR("failed to create debugfs files\n");
  6212. goto out_remove_sysfs;
  6213. }
  6214. iwl4965_setup_deferred_work(priv);
  6215. iwl4965_setup_rx_handlers(priv);
  6216. /********************
  6217. * 9. Conclude
  6218. ********************/
  6219. pci_save_state(pdev);
  6220. pci_disable_device(pdev);
  6221. /* notify iwlcore to init */
  6222. iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
  6223. return 0;
  6224. out_remove_sysfs:
  6225. sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
  6226. out_free_eeprom:
  6227. iwl_eeprom_free(priv);
  6228. out_iounmap:
  6229. pci_iounmap(pdev, priv->hw_base);
  6230. out_pci_release_regions:
  6231. pci_release_regions(pdev);
  6232. pci_set_drvdata(pdev, NULL);
  6233. out_pci_disable_device:
  6234. pci_disable_device(pdev);
  6235. out_ieee80211_free_hw:
  6236. ieee80211_free_hw(priv->hw);
  6237. out:
  6238. return err;
  6239. }
  6240. static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
  6241. {
  6242. struct iwl_priv *priv = pci_get_drvdata(pdev);
  6243. struct list_head *p, *q;
  6244. int i;
  6245. unsigned long flags;
  6246. if (!priv)
  6247. return;
  6248. IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
  6249. if (priv->mac80211_registered) {
  6250. ieee80211_unregister_hw(priv->hw);
  6251. priv->mac80211_registered = 0;
  6252. }
  6253. set_bit(STATUS_EXIT_PENDING, &priv->status);
  6254. iwl4965_down(priv);
  6255. /* make sure we flush any pending irq or
  6256. * tasklet for the driver
  6257. */
  6258. spin_lock_irqsave(&priv->lock, flags);
  6259. iwl4965_disable_interrupts(priv);
  6260. spin_unlock_irqrestore(&priv->lock, flags);
  6261. iwl_synchronize_irq(priv);
  6262. /* Free MAC hash list for ADHOC */
  6263. for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
  6264. list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
  6265. list_del(p);
  6266. kfree(list_entry(p, struct iwl4965_ibss_seq, list));
  6267. }
  6268. }
  6269. iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
  6270. iwl_dbgfs_unregister(priv);
  6271. sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
  6272. iwl4965_dealloc_ucode_pci(priv);
  6273. if (priv->rxq.bd)
  6274. iwl4965_rx_queue_free(priv, &priv->rxq);
  6275. iwl4965_hw_txq_ctx_free(priv);
  6276. iwlcore_clear_stations_table(priv);
  6277. iwl_eeprom_free(priv);
  6278. /*netif_stop_queue(dev); */
  6279. flush_workqueue(priv->workqueue);
  6280. /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
  6281. * priv->workqueue... so we can't take down the workqueue
  6282. * until now... */
  6283. destroy_workqueue(priv->workqueue);
  6284. priv->workqueue = NULL;
  6285. pci_iounmap(pdev, priv->hw_base);
  6286. pci_release_regions(pdev);
  6287. pci_disable_device(pdev);
  6288. pci_set_drvdata(pdev, NULL);
  6289. iwl_free_channel_map(priv);
  6290. iwl4965_free_geos(priv);
  6291. if (priv->ibss_beacon)
  6292. dev_kfree_skb(priv->ibss_beacon);
  6293. ieee80211_free_hw(priv->hw);
  6294. }
  6295. #ifdef CONFIG_PM
  6296. static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  6297. {
  6298. struct iwl_priv *priv = pci_get_drvdata(pdev);
  6299. if (priv->is_open) {
  6300. set_bit(STATUS_IN_SUSPEND, &priv->status);
  6301. iwl4965_mac_stop(priv->hw);
  6302. priv->is_open = 1;
  6303. }
  6304. pci_set_power_state(pdev, PCI_D3hot);
  6305. return 0;
  6306. }
  6307. static int iwl4965_pci_resume(struct pci_dev *pdev)
  6308. {
  6309. struct iwl_priv *priv = pci_get_drvdata(pdev);
  6310. pci_set_power_state(pdev, PCI_D0);
  6311. if (priv->is_open)
  6312. iwl4965_mac_start(priv->hw);
  6313. clear_bit(STATUS_IN_SUSPEND, &priv->status);
  6314. return 0;
  6315. }
  6316. #endif /* CONFIG_PM */
  6317. /*****************************************************************************
  6318. *
  6319. * driver and module entry point
  6320. *
  6321. *****************************************************************************/
  6322. /* Hardware specific file defines the PCI IDs table for that hardware module */
  6323. static struct pci_device_id iwl_hw_card_ids[] = {
  6324. {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
  6325. {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
  6326. {0}
  6327. };
  6328. MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
  6329. static struct pci_driver iwl_driver = {
  6330. .name = DRV_NAME,
  6331. .id_table = iwl_hw_card_ids,
  6332. .probe = iwl4965_pci_probe,
  6333. .remove = __devexit_p(iwl4965_pci_remove),
  6334. #ifdef CONFIG_PM
  6335. .suspend = iwl4965_pci_suspend,
  6336. .resume = iwl4965_pci_resume,
  6337. #endif
  6338. };
  6339. static int __init iwl4965_init(void)
  6340. {
  6341. int ret;
  6342. printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
  6343. printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
  6344. ret = iwl4965_rate_control_register();
  6345. if (ret) {
  6346. IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
  6347. return ret;
  6348. }
  6349. ret = pci_register_driver(&iwl_driver);
  6350. if (ret) {
  6351. IWL_ERROR("Unable to initialize PCI module\n");
  6352. goto error_register;
  6353. }
  6354. #ifdef CONFIG_IWLWIFI_DEBUG
  6355. ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
  6356. if (ret) {
  6357. IWL_ERROR("Unable to create driver sysfs file\n");
  6358. goto error_debug;
  6359. }
  6360. #endif
  6361. return ret;
  6362. #ifdef CONFIG_IWLWIFI_DEBUG
  6363. error_debug:
  6364. pci_unregister_driver(&iwl_driver);
  6365. #endif
  6366. error_register:
  6367. iwl4965_rate_control_unregister();
  6368. return ret;
  6369. }
  6370. static void __exit iwl4965_exit(void)
  6371. {
  6372. #ifdef CONFIG_IWLWIFI_DEBUG
  6373. driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
  6374. #endif
  6375. pci_unregister_driver(&iwl_driver);
  6376. iwl4965_rate_control_unregister();
  6377. }
  6378. module_exit(iwl4965_exit);
  6379. module_init(iwl4965_init);