iwl3945-base.c 216 KB

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