iwl3945-base.c 234 KB

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