iwl3945-base.c 233 KB

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