iwl3945-base.c 230 KB

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