iwl3945-base.c 162 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912
  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-sta.h"
  50. #include "iwl-3945.h"
  51. #include "iwl-helpers.h"
  52. #include "iwl-core.h"
  53. #include "iwl-dev.h"
  54. /*
  55. * module name, copyright, version, etc.
  56. */
  57. #define DRV_DESCRIPTION \
  58. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  59. #ifdef CONFIG_IWLWIFI_DEBUG
  60. #define VD "d"
  61. #else
  62. #define VD
  63. #endif
  64. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  65. #define VS "s"
  66. #else
  67. #define VS
  68. #endif
  69. #define IWL39_VERSION "1.2.26k" VD VS
  70. #define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
  71. #define DRV_AUTHOR "<ilw@linux.intel.com>"
  72. #define DRV_VERSION IWL39_VERSION
  73. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  74. MODULE_VERSION(DRV_VERSION);
  75. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  76. MODULE_LICENSE("GPL");
  77. /* module parameters */
  78. struct iwl_mod_params iwl3945_mod_params = {
  79. .num_of_queues = IWL39_MAX_NUM_QUEUES,
  80. .sw_crypto = 1,
  81. .restart_fw = 1,
  82. /* the rest are 0 by default */
  83. };
  84. /*************** STATION TABLE MANAGEMENT ****
  85. * mac80211 should be examined to determine if sta_info is duplicating
  86. * the functionality provided here
  87. */
  88. /**************************************************************/
  89. #if 0 /* temporary disable till we add real remove station */
  90. /**
  91. * iwl3945_remove_station - Remove driver's knowledge of station.
  92. *
  93. * NOTE: This does not remove station from device's station table.
  94. */
  95. static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  96. {
  97. int index = IWL_INVALID_STATION;
  98. int i;
  99. unsigned long flags;
  100. spin_lock_irqsave(&priv->sta_lock, flags);
  101. if (is_ap)
  102. index = IWL_AP_ID;
  103. else if (is_broadcast_ether_addr(addr))
  104. index = priv->hw_params.bcast_sta_id;
  105. else
  106. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  107. if (priv->stations_39[i].used &&
  108. !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  109. addr)) {
  110. index = i;
  111. break;
  112. }
  113. if (unlikely(index == IWL_INVALID_STATION))
  114. goto out;
  115. if (priv->stations_39[index].used) {
  116. priv->stations_39[index].used = 0;
  117. priv->num_stations--;
  118. }
  119. BUG_ON(priv->num_stations < 0);
  120. out:
  121. spin_unlock_irqrestore(&priv->sta_lock, flags);
  122. return 0;
  123. }
  124. #endif
  125. /**
  126. * iwl3945_clear_stations_table - Clear the driver's station table
  127. *
  128. * NOTE: This does not clear or otherwise alter the device's station table.
  129. */
  130. static void iwl3945_clear_stations_table(struct iwl_priv *priv)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&priv->sta_lock, flags);
  134. priv->num_stations = 0;
  135. memset(priv->stations_39, 0, sizeof(priv->stations_39));
  136. spin_unlock_irqrestore(&priv->sta_lock, flags);
  137. }
  138. /**
  139. * iwl3945_add_station - Add station to station tables in driver and device
  140. */
  141. u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
  142. {
  143. int i;
  144. int index = IWL_INVALID_STATION;
  145. struct iwl3945_station_entry *station;
  146. unsigned long flags_spin;
  147. u8 rate;
  148. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  149. if (is_ap)
  150. index = IWL_AP_ID;
  151. else if (is_broadcast_ether_addr(addr))
  152. index = priv->hw_params.bcast_sta_id;
  153. else
  154. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  155. if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  156. addr)) {
  157. index = i;
  158. break;
  159. }
  160. if (!priv->stations_39[i].used &&
  161. index == IWL_INVALID_STATION)
  162. index = i;
  163. }
  164. /* These two conditions has the same outcome but keep them separate
  165. since they have different meaning */
  166. if (unlikely(index == IWL_INVALID_STATION)) {
  167. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  168. return index;
  169. }
  170. if (priv->stations_39[index].used &&
  171. !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
  172. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  173. return index;
  174. }
  175. IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
  176. station = &priv->stations_39[index];
  177. station->used = 1;
  178. priv->num_stations++;
  179. /* Set up the REPLY_ADD_STA command to send to device */
  180. memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
  181. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  182. station->sta.mode = 0;
  183. station->sta.sta.sta_id = index;
  184. station->sta.station_flags = 0;
  185. if (priv->band == IEEE80211_BAND_5GHZ)
  186. rate = IWL_RATE_6M_PLCP;
  187. else
  188. rate = IWL_RATE_1M_PLCP;
  189. /* Turn on both antennas for the station... */
  190. station->sta.rate_n_flags =
  191. iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
  192. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  193. /* Add station to device's station table */
  194. iwl_send_add_sta(priv,
  195. (struct iwl_addsta_cmd *)&station->sta, flags);
  196. return index;
  197. }
  198. static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
  199. {
  200. int rc = 0;
  201. struct iwl_rx_packet *res = NULL;
  202. struct iwl3945_rxon_assoc_cmd rxon_assoc;
  203. struct iwl_host_cmd cmd = {
  204. .id = REPLY_RXON_ASSOC,
  205. .len = sizeof(rxon_assoc),
  206. .meta.flags = CMD_WANT_SKB,
  207. .data = &rxon_assoc,
  208. };
  209. const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
  210. const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
  211. if ((rxon1->flags == rxon2->flags) &&
  212. (rxon1->filter_flags == rxon2->filter_flags) &&
  213. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  214. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  215. IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
  216. return 0;
  217. }
  218. rxon_assoc.flags = priv->staging_rxon.flags;
  219. rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
  220. rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
  221. rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
  222. rxon_assoc.reserved = 0;
  223. rc = iwl_send_cmd_sync(priv, &cmd);
  224. if (rc)
  225. return rc;
  226. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  227. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  228. IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
  229. rc = -EIO;
  230. }
  231. priv->alloc_rxb_skb--;
  232. dev_kfree_skb_any(cmd.meta.u.skb);
  233. return rc;
  234. }
  235. /**
  236. * iwl3945_get_antenna_flags - Get antenna flags for RXON command
  237. * @priv: eeprom and antenna fields are used to determine antenna flags
  238. *
  239. * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
  240. * iwl3945_mod_params.antenna specifies the antenna diversity mode:
  241. *
  242. * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
  243. * IWL_ANTENNA_MAIN - Force MAIN antenna
  244. * IWL_ANTENNA_AUX - Force AUX antenna
  245. */
  246. __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
  247. {
  248. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  249. switch (iwl3945_mod_params.antenna) {
  250. case IWL_ANTENNA_DIVERSITY:
  251. return 0;
  252. case IWL_ANTENNA_MAIN:
  253. if (eeprom->antenna_switch_type)
  254. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  255. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  256. case IWL_ANTENNA_AUX:
  257. if (eeprom->antenna_switch_type)
  258. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  259. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  260. }
  261. /* bad antenna selector value */
  262. IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
  263. iwl3945_mod_params.antenna);
  264. return 0; /* "diversity" is default if error */
  265. }
  266. /**
  267. * iwl3945_commit_rxon - commit staging_rxon to hardware
  268. *
  269. * The RXON command in staging_rxon is committed to the hardware and
  270. * the active_rxon structure is updated with the new data. This
  271. * function correctly transitions out of the RXON_ASSOC_MSK state if
  272. * a HW tune is required based on the RXON structure changes.
  273. */
  274. static int iwl3945_commit_rxon(struct iwl_priv *priv)
  275. {
  276. /* cast away the const for active_rxon in this function */
  277. struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
  278. struct iwl3945_rxon_cmd *staging_rxon = (void *)&priv->staging_rxon;
  279. int rc = 0;
  280. if (!iwl_is_alive(priv))
  281. return -1;
  282. /* always get timestamp with Rx frame */
  283. staging_rxon->flags |= RXON_FLG_TSF2HOST_MSK;
  284. /* select antenna */
  285. staging_rxon->flags &=
  286. ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
  287. staging_rxon->flags |= iwl3945_get_antenna_flags(priv);
  288. rc = iwl_check_rxon_cmd(priv);
  289. if (rc) {
  290. IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
  291. return -EINVAL;
  292. }
  293. /* If we don't need to send a full RXON, we can use
  294. * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
  295. * and other flags for the current radio configuration. */
  296. if (!iwl_full_rxon_required(priv)) {
  297. rc = iwl3945_send_rxon_assoc(priv);
  298. if (rc) {
  299. IWL_ERR(priv, "Error setting RXON_ASSOC "
  300. "configuration (%d).\n", rc);
  301. return rc;
  302. }
  303. memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
  304. return 0;
  305. }
  306. /* If we are currently associated and the new config requires
  307. * an RXON_ASSOC and the new config wants the associated mask enabled,
  308. * we must clear the associated from the active configuration
  309. * before we apply the new config */
  310. if (iwl_is_associated(priv) &&
  311. (staging_rxon->filter_flags & RXON_FILTER_ASSOC_MSK)) {
  312. IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
  313. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  314. /*
  315. * reserved4 and 5 could have been filled by the iwlcore code.
  316. * Let's clear them before pushing to the 3945.
  317. */
  318. active_rxon->reserved4 = 0;
  319. active_rxon->reserved5 = 0;
  320. rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
  321. sizeof(struct iwl3945_rxon_cmd),
  322. &priv->active_rxon);
  323. /* If the mask clearing failed then we set
  324. * active_rxon back to what it was previously */
  325. if (rc) {
  326. active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
  327. IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
  328. "configuration (%d).\n", rc);
  329. return rc;
  330. }
  331. }
  332. IWL_DEBUG_INFO("Sending RXON\n"
  333. "* with%s RXON_FILTER_ASSOC_MSK\n"
  334. "* channel = %d\n"
  335. "* bssid = %pM\n",
  336. ((priv->staging_rxon.filter_flags &
  337. RXON_FILTER_ASSOC_MSK) ? "" : "out"),
  338. le16_to_cpu(staging_rxon->channel),
  339. staging_rxon->bssid_addr);
  340. /*
  341. * reserved4 and 5 could have been filled by the iwlcore code.
  342. * Let's clear them before pushing to the 3945.
  343. */
  344. staging_rxon->reserved4 = 0;
  345. staging_rxon->reserved5 = 0;
  346. /* Apply the new configuration */
  347. rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
  348. sizeof(struct iwl3945_rxon_cmd),
  349. staging_rxon);
  350. if (rc) {
  351. IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
  352. return rc;
  353. }
  354. memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
  355. iwl3945_clear_stations_table(priv);
  356. /* If we issue a new RXON command which required a tune then we must
  357. * send a new TXPOWER command or we won't be able to Tx any frames */
  358. rc = priv->cfg->ops->lib->send_tx_power(priv);
  359. if (rc) {
  360. IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
  361. return rc;
  362. }
  363. /* Add the broadcast address so we can send broadcast frames */
  364. if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
  365. IWL_INVALID_STATION) {
  366. IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
  367. return -EIO;
  368. }
  369. /* If we have set the ASSOC_MSK and we are in BSS mode then
  370. * add the IWL_AP_ID to the station rate table */
  371. if (iwl_is_associated(priv) &&
  372. (priv->iw_mode == NL80211_IFTYPE_STATION))
  373. if (iwl3945_add_station(priv, priv->active_rxon.bssid_addr,
  374. 1, 0)
  375. == IWL_INVALID_STATION) {
  376. IWL_ERR(priv, "Error adding AP address for transmit\n");
  377. return -EIO;
  378. }
  379. /* Init the hardware's rate fallback order based on the band */
  380. rc = iwl3945_init_hw_rate_table(priv);
  381. if (rc) {
  382. IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
  383. return -EIO;
  384. }
  385. return 0;
  386. }
  387. static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
  388. struct ieee80211_key_conf *keyconf,
  389. u8 sta_id)
  390. {
  391. unsigned long flags;
  392. __le16 key_flags = 0;
  393. switch (keyconf->alg) {
  394. case ALG_CCMP:
  395. key_flags |= STA_KEY_FLG_CCMP;
  396. key_flags |= cpu_to_le16(
  397. keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  398. key_flags &= ~STA_KEY_FLG_INVALID;
  399. break;
  400. case ALG_TKIP:
  401. case ALG_WEP:
  402. default:
  403. return -EINVAL;
  404. }
  405. spin_lock_irqsave(&priv->sta_lock, flags);
  406. priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
  407. priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
  408. memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
  409. keyconf->keylen);
  410. memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
  411. keyconf->keylen);
  412. priv->stations_39[sta_id].sta.key.key_flags = key_flags;
  413. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  414. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  415. spin_unlock_irqrestore(&priv->sta_lock, flags);
  416. IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
  417. iwl_send_add_sta(priv,
  418. (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
  419. return 0;
  420. }
  421. static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
  422. {
  423. unsigned long flags;
  424. spin_lock_irqsave(&priv->sta_lock, flags);
  425. memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
  426. memset(&priv->stations_39[sta_id].sta.key, 0,
  427. sizeof(struct iwl4965_keyinfo));
  428. priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  429. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  430. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  431. spin_unlock_irqrestore(&priv->sta_lock, flags);
  432. IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
  433. iwl_send_add_sta(priv,
  434. (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
  435. return 0;
  436. }
  437. static void iwl3945_clear_free_frames(struct iwl_priv *priv)
  438. {
  439. struct list_head *element;
  440. IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
  441. priv->frames_count);
  442. while (!list_empty(&priv->free_frames)) {
  443. element = priv->free_frames.next;
  444. list_del(element);
  445. kfree(list_entry(element, struct iwl3945_frame, list));
  446. priv->frames_count--;
  447. }
  448. if (priv->frames_count) {
  449. IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
  450. priv->frames_count);
  451. priv->frames_count = 0;
  452. }
  453. }
  454. static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
  455. {
  456. struct iwl3945_frame *frame;
  457. struct list_head *element;
  458. if (list_empty(&priv->free_frames)) {
  459. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  460. if (!frame) {
  461. IWL_ERR(priv, "Could not allocate frame!\n");
  462. return NULL;
  463. }
  464. priv->frames_count++;
  465. return frame;
  466. }
  467. element = priv->free_frames.next;
  468. list_del(element);
  469. return list_entry(element, struct iwl3945_frame, list);
  470. }
  471. static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
  472. {
  473. memset(frame, 0, sizeof(*frame));
  474. list_add(&frame->list, &priv->free_frames);
  475. }
  476. unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
  477. struct ieee80211_hdr *hdr,
  478. int left)
  479. {
  480. if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
  481. ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
  482. (priv->iw_mode != NL80211_IFTYPE_AP)))
  483. return 0;
  484. if (priv->ibss_beacon->len > left)
  485. return 0;
  486. memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
  487. return priv->ibss_beacon->len;
  488. }
  489. static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
  490. {
  491. struct iwl3945_frame *frame;
  492. unsigned int frame_size;
  493. int rc;
  494. u8 rate;
  495. frame = iwl3945_get_free_frame(priv);
  496. if (!frame) {
  497. IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
  498. "command.\n");
  499. return -ENOMEM;
  500. }
  501. rate = iwl_rate_get_lowest_plcp(priv);
  502. frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
  503. rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  504. &frame->u.cmd[0]);
  505. iwl3945_free_frame(priv, frame);
  506. return rc;
  507. }
  508. static void iwl3945_unset_hw_params(struct iwl_priv *priv)
  509. {
  510. if (priv->shared_virt)
  511. pci_free_consistent(priv->pci_dev,
  512. sizeof(struct iwl3945_shared),
  513. priv->shared_virt,
  514. priv->shared_phys);
  515. }
  516. /*
  517. * QoS support
  518. */
  519. static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
  520. struct iwl_qosparam_cmd *qos)
  521. {
  522. return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
  523. sizeof(struct iwl_qosparam_cmd), qos);
  524. }
  525. static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
  526. {
  527. unsigned long flags;
  528. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  529. return;
  530. spin_lock_irqsave(&priv->lock, flags);
  531. priv->qos_data.def_qos_parm.qos_flags = 0;
  532. if (priv->qos_data.qos_cap.q_AP.queue_request &&
  533. !priv->qos_data.qos_cap.q_AP.txop_request)
  534. priv->qos_data.def_qos_parm.qos_flags |=
  535. QOS_PARAM_FLG_TXOP_TYPE_MSK;
  536. if (priv->qos_data.qos_active)
  537. priv->qos_data.def_qos_parm.qos_flags |=
  538. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  539. spin_unlock_irqrestore(&priv->lock, flags);
  540. if (force || iwl_is_associated(priv)) {
  541. IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
  542. priv->qos_data.qos_active);
  543. iwl3945_send_qos_params_command(priv,
  544. &(priv->qos_data.def_qos_parm));
  545. }
  546. }
  547. /*
  548. * Power management (not Tx power!) functions
  549. */
  550. #define MSEC_TO_USEC 1024
  551. /* default power management (not Tx power) table values */
  552. /* for TIM 0-10 */
  553. static struct iwl_power_vec_entry range_0[IWL_POWER_MAX] = {
  554. {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  555. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
  556. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
  557. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
  558. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
  559. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
  560. };
  561. /* for TIM > 10 */
  562. static struct iwl_power_vec_entry range_1[IWL_POWER_MAX] = {
  563. {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  564. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
  565. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
  566. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
  567. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  568. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
  569. };
  570. int iwl3945_power_init_handle(struct iwl_priv *priv)
  571. {
  572. int rc = 0, i;
  573. struct iwl_power_mgr *pow_data;
  574. int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_MAX;
  575. u16 pci_pm;
  576. IWL_DEBUG_POWER("Initialize power \n");
  577. pow_data = &priv->power_data;
  578. memset(pow_data, 0, sizeof(*pow_data));
  579. pow_data->dtim_period = 1;
  580. memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
  581. memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
  582. rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
  583. if (rc != 0)
  584. return 0;
  585. else {
  586. struct iwl_powertable_cmd *cmd;
  587. IWL_DEBUG_POWER("adjust power command flags\n");
  588. for (i = 0; i < IWL_POWER_MAX; i++) {
  589. cmd = &pow_data->pwr_range_0[i].cmd;
  590. if (pci_pm & 0x1)
  591. cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
  592. else
  593. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  594. }
  595. }
  596. return rc;
  597. }
  598. static int iwl3945_update_power_cmd(struct iwl_priv *priv,
  599. struct iwl_powertable_cmd *cmd, u32 mode)
  600. {
  601. struct iwl_power_mgr *pow_data;
  602. struct iwl_power_vec_entry *range;
  603. u32 max_sleep = 0;
  604. int i;
  605. u8 period = 0;
  606. bool skip;
  607. if (mode > IWL_POWER_INDEX_5) {
  608. IWL_DEBUG_POWER("Error invalid power mode \n");
  609. return -EINVAL;
  610. }
  611. pow_data = &priv->power_data;
  612. if (pow_data->dtim_period < 10)
  613. range = &pow_data->pwr_range_0[0];
  614. else
  615. range = &pow_data->pwr_range_1[1];
  616. memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
  617. if (period == 0) {
  618. period = 1;
  619. skip = false;
  620. } else {
  621. skip = !!range[mode].no_dtim;
  622. }
  623. if (skip) {
  624. __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
  625. max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
  626. cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
  627. } else {
  628. max_sleep = period;
  629. cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
  630. }
  631. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  632. if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
  633. cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
  634. IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
  635. IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  636. IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  637. IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  638. le32_to_cpu(cmd->sleep_interval[0]),
  639. le32_to_cpu(cmd->sleep_interval[1]),
  640. le32_to_cpu(cmd->sleep_interval[2]),
  641. le32_to_cpu(cmd->sleep_interval[3]),
  642. le32_to_cpu(cmd->sleep_interval[4]));
  643. return 0;
  644. }
  645. static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
  646. {
  647. u32 uninitialized_var(final_mode);
  648. int rc;
  649. struct iwl_powertable_cmd cmd;
  650. /* If on battery, set to 3,
  651. * if plugged into AC power, set to CAM ("continuously aware mode"),
  652. * else user level */
  653. switch (mode) {
  654. case IWL39_POWER_BATTERY:
  655. final_mode = IWL_POWER_INDEX_3;
  656. break;
  657. case IWL39_POWER_AC:
  658. final_mode = IWL_POWER_MODE_CAM;
  659. break;
  660. default:
  661. final_mode = mode;
  662. break;
  663. }
  664. iwl3945_update_power_cmd(priv, &cmd, final_mode);
  665. /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
  666. rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
  667. sizeof(struct iwl3945_powertable_cmd), &cmd);
  668. if (final_mode == IWL_POWER_MODE_CAM)
  669. clear_bit(STATUS_POWER_PMI, &priv->status);
  670. else
  671. set_bit(STATUS_POWER_PMI, &priv->status);
  672. return rc;
  673. }
  674. #define MAX_UCODE_BEACON_INTERVAL 1024
  675. #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
  676. static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
  677. {
  678. u16 new_val = 0;
  679. u16 beacon_factor = 0;
  680. beacon_factor =
  681. (beacon_val + MAX_UCODE_BEACON_INTERVAL)
  682. / MAX_UCODE_BEACON_INTERVAL;
  683. new_val = beacon_val / beacon_factor;
  684. return cpu_to_le16(new_val);
  685. }
  686. static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
  687. {
  688. u64 interval_tm_unit;
  689. u64 tsf, result;
  690. unsigned long flags;
  691. struct ieee80211_conf *conf = NULL;
  692. u16 beacon_int = 0;
  693. conf = ieee80211_get_hw_conf(priv->hw);
  694. spin_lock_irqsave(&priv->lock, flags);
  695. priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
  696. priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
  697. tsf = priv->timestamp;
  698. beacon_int = priv->beacon_int;
  699. spin_unlock_irqrestore(&priv->lock, flags);
  700. if (priv->iw_mode == NL80211_IFTYPE_STATION) {
  701. if (beacon_int == 0) {
  702. priv->rxon_timing.beacon_interval = cpu_to_le16(100);
  703. priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
  704. } else {
  705. priv->rxon_timing.beacon_interval =
  706. cpu_to_le16(beacon_int);
  707. priv->rxon_timing.beacon_interval =
  708. iwl3945_adjust_beacon_interval(
  709. le16_to_cpu(priv->rxon_timing.beacon_interval));
  710. }
  711. priv->rxon_timing.atim_window = 0;
  712. } else {
  713. priv->rxon_timing.beacon_interval =
  714. iwl3945_adjust_beacon_interval(conf->beacon_int);
  715. /* TODO: we need to get atim_window from upper stack
  716. * for now we set to 0 */
  717. priv->rxon_timing.atim_window = 0;
  718. }
  719. interval_tm_unit =
  720. (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
  721. result = do_div(tsf, interval_tm_unit);
  722. priv->rxon_timing.beacon_init_val =
  723. cpu_to_le32((u32) ((u64) interval_tm_unit - result));
  724. IWL_DEBUG_ASSOC
  725. ("beacon interval %d beacon timer %d beacon tim %d\n",
  726. le16_to_cpu(priv->rxon_timing.beacon_interval),
  727. le32_to_cpu(priv->rxon_timing.beacon_init_val),
  728. le16_to_cpu(priv->rxon_timing.atim_window));
  729. }
  730. static int iwl3945_scan_initiate(struct iwl_priv *priv)
  731. {
  732. if (!iwl_is_ready_rf(priv)) {
  733. IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
  734. return -EIO;
  735. }
  736. if (test_bit(STATUS_SCANNING, &priv->status)) {
  737. IWL_DEBUG_SCAN("Scan already in progress.\n");
  738. return -EAGAIN;
  739. }
  740. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  741. IWL_DEBUG_SCAN("Scan request while abort pending. "
  742. "Queuing.\n");
  743. return -EAGAIN;
  744. }
  745. IWL_DEBUG_INFO("Starting scan...\n");
  746. if (priv->cfg->sku & IWL_SKU_G)
  747. priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
  748. if (priv->cfg->sku & IWL_SKU_A)
  749. priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
  750. set_bit(STATUS_SCANNING, &priv->status);
  751. priv->scan_start = jiffies;
  752. priv->scan_pass_start = priv->scan_start;
  753. queue_work(priv->workqueue, &priv->request_scan);
  754. return 0;
  755. }
  756. static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
  757. {
  758. if (mode == NL80211_IFTYPE_ADHOC) {
  759. const struct iwl_channel_info *ch_info;
  760. ch_info = iwl_get_channel_info(priv,
  761. priv->band,
  762. le16_to_cpu(priv->staging_rxon.channel));
  763. if (!ch_info || !is_channel_ibss(ch_info)) {
  764. IWL_ERR(priv, "channel %d not IBSS channel\n",
  765. le16_to_cpu(priv->staging_rxon.channel));
  766. return -EINVAL;
  767. }
  768. }
  769. iwl_connection_init_rx_config(priv, mode);
  770. iwl3945_clear_stations_table(priv);
  771. /* don't commit rxon if rf-kill is on*/
  772. if (!iwl_is_ready_rf(priv))
  773. return -EAGAIN;
  774. cancel_delayed_work(&priv->scan_check);
  775. if (iwl_scan_cancel_timeout(priv, 100)) {
  776. IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
  777. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  778. return -EAGAIN;
  779. }
  780. iwl3945_commit_rxon(priv);
  781. return 0;
  782. }
  783. static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
  784. struct ieee80211_tx_info *info,
  785. struct iwl_cmd *cmd,
  786. struct sk_buff *skb_frag,
  787. int last_frag)
  788. {
  789. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  790. struct iwl3945_hw_key *keyinfo =
  791. &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
  792. switch (keyinfo->alg) {
  793. case ALG_CCMP:
  794. tx->sec_ctl = TX_CMD_SEC_CCM;
  795. memcpy(tx->key, keyinfo->key, keyinfo->keylen);
  796. IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
  797. break;
  798. case ALG_TKIP:
  799. #if 0
  800. tx->sec_ctl = TX_CMD_SEC_TKIP;
  801. if (last_frag)
  802. memcpy(tx->tkip_mic.byte, skb_frag->tail - 8,
  803. 8);
  804. else
  805. memset(tx->tkip_mic.byte, 0, 8);
  806. #endif
  807. break;
  808. case ALG_WEP:
  809. tx->sec_ctl = TX_CMD_SEC_WEP |
  810. (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
  811. if (keyinfo->keylen == 13)
  812. tx->sec_ctl |= TX_CMD_SEC_KEY128;
  813. memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
  814. IWL_DEBUG_TX("Configuring packet for WEP encryption "
  815. "with key %d\n", info->control.hw_key->hw_key_idx);
  816. break;
  817. default:
  818. IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
  819. break;
  820. }
  821. }
  822. /*
  823. * handle build REPLY_TX command notification.
  824. */
  825. static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
  826. struct iwl_cmd *cmd,
  827. struct ieee80211_tx_info *info,
  828. struct ieee80211_hdr *hdr, u8 std_id)
  829. {
  830. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  831. __le32 tx_flags = tx->tx_flags;
  832. __le16 fc = hdr->frame_control;
  833. u8 rc_flags = info->control.rates[0].flags;
  834. tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  835. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  836. tx_flags |= TX_CMD_FLG_ACK_MSK;
  837. if (ieee80211_is_mgmt(fc))
  838. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  839. if (ieee80211_is_probe_resp(fc) &&
  840. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  841. tx_flags |= TX_CMD_FLG_TSF_MSK;
  842. } else {
  843. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  844. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  845. }
  846. tx->sta_id = std_id;
  847. if (ieee80211_has_morefrags(fc))
  848. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  849. if (ieee80211_is_data_qos(fc)) {
  850. u8 *qc = ieee80211_get_qos_ctl(hdr);
  851. tx->tid_tspec = qc[0] & 0xf;
  852. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  853. } else {
  854. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  855. }
  856. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  857. tx_flags |= TX_CMD_FLG_RTS_MSK;
  858. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  859. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  860. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  861. tx_flags |= TX_CMD_FLG_CTS_MSK;
  862. }
  863. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  864. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  865. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  866. if (ieee80211_is_mgmt(fc)) {
  867. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  868. tx->timeout.pm_frame_timeout = cpu_to_le16(3);
  869. else
  870. tx->timeout.pm_frame_timeout = cpu_to_le16(2);
  871. } else {
  872. tx->timeout.pm_frame_timeout = 0;
  873. #ifdef CONFIG_IWL3945_LEDS
  874. priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
  875. #endif
  876. }
  877. tx->driver_txop = 0;
  878. tx->tx_flags = tx_flags;
  879. tx->next_frame_len = 0;
  880. }
  881. /**
  882. * iwl3945_get_sta_id - Find station's index within station table
  883. */
  884. static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  885. {
  886. int sta_id;
  887. u16 fc = le16_to_cpu(hdr->frame_control);
  888. /* If this frame is broadcast or management, use broadcast station id */
  889. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  890. is_multicast_ether_addr(hdr->addr1))
  891. return priv->hw_params.bcast_sta_id;
  892. switch (priv->iw_mode) {
  893. /* If we are a client station in a BSS network, use the special
  894. * AP station entry (that's the only station we communicate with) */
  895. case NL80211_IFTYPE_STATION:
  896. return IWL_AP_ID;
  897. /* If we are an AP, then find the station, or use BCAST */
  898. case NL80211_IFTYPE_AP:
  899. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  900. if (sta_id != IWL_INVALID_STATION)
  901. return sta_id;
  902. return priv->hw_params.bcast_sta_id;
  903. /* If this frame is going out to an IBSS network, find the station,
  904. * or create a new station table entry */
  905. case NL80211_IFTYPE_ADHOC: {
  906. /* Create new station table entry */
  907. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  908. if (sta_id != IWL_INVALID_STATION)
  909. return sta_id;
  910. sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
  911. if (sta_id != IWL_INVALID_STATION)
  912. return sta_id;
  913. IWL_DEBUG_DROP("Station %pM not in station map. "
  914. "Defaulting to broadcast...\n",
  915. hdr->addr1);
  916. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  917. return priv->hw_params.bcast_sta_id;
  918. }
  919. /* If we are in monitor mode, use BCAST. This is required for
  920. * packet injection. */
  921. case NL80211_IFTYPE_MONITOR:
  922. return priv->hw_params.bcast_sta_id;
  923. default:
  924. IWL_WARN(priv, "Unknown mode of operation: %d\n",
  925. priv->iw_mode);
  926. return priv->hw_params.bcast_sta_id;
  927. }
  928. }
  929. /*
  930. * start REPLY_TX command process
  931. */
  932. static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  933. {
  934. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  935. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  936. struct iwl3945_tx_cmd *tx;
  937. struct iwl_tx_queue *txq = NULL;
  938. struct iwl_queue *q = NULL;
  939. struct iwl_cmd *out_cmd = NULL;
  940. dma_addr_t phys_addr;
  941. dma_addr_t txcmd_phys;
  942. int txq_id = skb_get_queue_mapping(skb);
  943. u16 len, idx, len_org, hdr_len;
  944. u8 id;
  945. u8 unicast;
  946. u8 sta_id;
  947. u8 tid = 0;
  948. u16 seq_number = 0;
  949. __le16 fc;
  950. u8 wait_write_ptr = 0;
  951. u8 *qc = NULL;
  952. unsigned long flags;
  953. int rc;
  954. spin_lock_irqsave(&priv->lock, flags);
  955. if (iwl_is_rfkill(priv)) {
  956. IWL_DEBUG_DROP("Dropping - RF KILL\n");
  957. goto drop_unlock;
  958. }
  959. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
  960. IWL_ERR(priv, "ERROR: No TX rate available.\n");
  961. goto drop_unlock;
  962. }
  963. unicast = !is_multicast_ether_addr(hdr->addr1);
  964. id = 0;
  965. fc = hdr->frame_control;
  966. #ifdef CONFIG_IWLWIFI_DEBUG
  967. if (ieee80211_is_auth(fc))
  968. IWL_DEBUG_TX("Sending AUTH frame\n");
  969. else if (ieee80211_is_assoc_req(fc))
  970. IWL_DEBUG_TX("Sending ASSOC frame\n");
  971. else if (ieee80211_is_reassoc_req(fc))
  972. IWL_DEBUG_TX("Sending REASSOC frame\n");
  973. #endif
  974. /* drop all data frame if we are not associated */
  975. if (ieee80211_is_data(fc) &&
  976. (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
  977. (!iwl_is_associated(priv) ||
  978. ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
  979. IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
  980. goto drop_unlock;
  981. }
  982. spin_unlock_irqrestore(&priv->lock, flags);
  983. hdr_len = ieee80211_hdrlen(fc);
  984. /* Find (or create) index into station table for destination station */
  985. sta_id = iwl3945_get_sta_id(priv, hdr);
  986. if (sta_id == IWL_INVALID_STATION) {
  987. IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
  988. hdr->addr1);
  989. goto drop;
  990. }
  991. IWL_DEBUG_RATE("station Id %d\n", sta_id);
  992. if (ieee80211_is_data_qos(fc)) {
  993. qc = ieee80211_get_qos_ctl(hdr);
  994. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  995. seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
  996. IEEE80211_SCTL_SEQ;
  997. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  998. (hdr->seq_ctrl &
  999. __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
  1000. seq_number += 0x10;
  1001. }
  1002. /* Descriptor for chosen Tx queue */
  1003. txq = &priv->txq[txq_id];
  1004. q = &txq->q;
  1005. spin_lock_irqsave(&priv->lock, flags);
  1006. idx = get_cmd_index(q, q->write_ptr, 0);
  1007. /* Set up driver data for this TFD */
  1008. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  1009. txq->txb[q->write_ptr].skb[0] = skb;
  1010. /* Init first empty entry in queue's array of Tx/cmd buffers */
  1011. out_cmd = txq->cmd[idx];
  1012. tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
  1013. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  1014. memset(tx, 0, sizeof(*tx));
  1015. /*
  1016. * Set up the Tx-command (not MAC!) header.
  1017. * Store the chosen Tx queue and TFD index within the sequence field;
  1018. * after Tx, uCode's Tx response will return this value so driver can
  1019. * locate the frame within the tx queue and do post-tx processing.
  1020. */
  1021. out_cmd->hdr.cmd = REPLY_TX;
  1022. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1023. INDEX_TO_SEQ(q->write_ptr)));
  1024. /* Copy MAC header from skb into command buffer */
  1025. memcpy(tx->hdr, hdr, hdr_len);
  1026. /*
  1027. * Use the first empty entry in this queue's command buffer array
  1028. * to contain the Tx command and MAC header concatenated together
  1029. * (payload data will be in another buffer).
  1030. * Size of this varies, due to varying MAC header length.
  1031. * If end is not dword aligned, we'll have 2 extra bytes at the end
  1032. * of the MAC header (device reads on dword boundaries).
  1033. * We'll tell device about this padding later.
  1034. */
  1035. len = sizeof(struct iwl3945_tx_cmd) +
  1036. sizeof(struct iwl_cmd_header) + hdr_len;
  1037. len_org = len;
  1038. len = (len + 3) & ~3;
  1039. if (len_org != len)
  1040. len_org = 1;
  1041. else
  1042. len_org = 0;
  1043. /* Physical address of this Tx command's header (not MAC header!),
  1044. * within command buffer array. */
  1045. txcmd_phys = pci_map_single(priv->pci_dev,
  1046. out_cmd, sizeof(struct iwl_cmd),
  1047. PCI_DMA_TODEVICE);
  1048. pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
  1049. pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
  1050. /* Add buffer containing Tx command and MAC(!) header to TFD's
  1051. * first entry */
  1052. txcmd_phys += offsetof(struct iwl_cmd, hdr);
  1053. /* Add buffer containing Tx command and MAC(!) header to TFD's
  1054. * first entry */
  1055. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  1056. txcmd_phys, len, 1, 0);
  1057. if (info->control.hw_key)
  1058. iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
  1059. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  1060. * if any (802.11 null frames have no payload). */
  1061. len = skb->len - hdr_len;
  1062. if (len) {
  1063. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  1064. len, PCI_DMA_TODEVICE);
  1065. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  1066. phys_addr, len,
  1067. 0, U32_PAD(len));
  1068. }
  1069. /* Total # bytes to be transmitted */
  1070. len = (u16)skb->len;
  1071. tx->len = cpu_to_le16(len);
  1072. /* TODO need this for burst mode later on */
  1073. iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
  1074. /* set is_hcca to 0; it probably will never be implemented */
  1075. iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
  1076. tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  1077. tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  1078. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  1079. txq->need_update = 1;
  1080. if (qc)
  1081. priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
  1082. } else {
  1083. wait_write_ptr = 1;
  1084. txq->need_update = 0;
  1085. }
  1086. iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
  1087. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
  1088. ieee80211_hdrlen(fc));
  1089. /* Tell device the write index *just past* this latest filled TFD */
  1090. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1091. rc = iwl_txq_update_write_ptr(priv, txq);
  1092. spin_unlock_irqrestore(&priv->lock, flags);
  1093. if (rc)
  1094. return rc;
  1095. if ((iwl_queue_space(q) < q->high_mark)
  1096. && priv->mac80211_registered) {
  1097. if (wait_write_ptr) {
  1098. spin_lock_irqsave(&priv->lock, flags);
  1099. txq->need_update = 1;
  1100. iwl_txq_update_write_ptr(priv, txq);
  1101. spin_unlock_irqrestore(&priv->lock, flags);
  1102. }
  1103. ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
  1104. }
  1105. return 0;
  1106. drop_unlock:
  1107. spin_unlock_irqrestore(&priv->lock, flags);
  1108. drop:
  1109. return -1;
  1110. }
  1111. static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
  1112. {
  1113. unsigned long flags;
  1114. if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
  1115. return;
  1116. IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
  1117. disable_radio ? "OFF" : "ON");
  1118. if (disable_radio) {
  1119. iwl_scan_cancel(priv);
  1120. /* FIXME: This is a workaround for AP */
  1121. if (priv->iw_mode != NL80211_IFTYPE_AP) {
  1122. spin_lock_irqsave(&priv->lock, flags);
  1123. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  1124. CSR_UCODE_SW_BIT_RFKILL);
  1125. spin_unlock_irqrestore(&priv->lock, flags);
  1126. iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
  1127. set_bit(STATUS_RF_KILL_SW, &priv->status);
  1128. }
  1129. return;
  1130. }
  1131. spin_lock_irqsave(&priv->lock, flags);
  1132. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1133. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  1134. spin_unlock_irqrestore(&priv->lock, flags);
  1135. /* wake up ucode */
  1136. msleep(10);
  1137. spin_lock_irqsave(&priv->lock, flags);
  1138. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  1139. if (!iwl_grab_nic_access(priv))
  1140. iwl_release_nic_access(priv);
  1141. spin_unlock_irqrestore(&priv->lock, flags);
  1142. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  1143. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  1144. "disabled by HW switch\n");
  1145. return;
  1146. }
  1147. if (priv->is_open)
  1148. queue_work(priv->workqueue, &priv->restart);
  1149. return;
  1150. }
  1151. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  1152. #include "iwl-spectrum.h"
  1153. #define BEACON_TIME_MASK_LOW 0x00FFFFFF
  1154. #define BEACON_TIME_MASK_HIGH 0xFF000000
  1155. #define TIME_UNIT 1024
  1156. /*
  1157. * extended beacon time format
  1158. * time in usec will be changed into a 32-bit value in 8:24 format
  1159. * the high 1 byte is the beacon counts
  1160. * the lower 3 bytes is the time in usec within one beacon interval
  1161. */
  1162. static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
  1163. {
  1164. u32 quot;
  1165. u32 rem;
  1166. u32 interval = beacon_interval * 1024;
  1167. if (!interval || !usec)
  1168. return 0;
  1169. quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
  1170. rem = (usec % interval) & BEACON_TIME_MASK_LOW;
  1171. return (quot << 24) + rem;
  1172. }
  1173. /* base is usually what we get from ucode with each received frame,
  1174. * the same as HW timer counter counting down
  1175. */
  1176. static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
  1177. {
  1178. u32 base_low = base & BEACON_TIME_MASK_LOW;
  1179. u32 addon_low = addon & BEACON_TIME_MASK_LOW;
  1180. u32 interval = beacon_interval * TIME_UNIT;
  1181. u32 res = (base & BEACON_TIME_MASK_HIGH) +
  1182. (addon & BEACON_TIME_MASK_HIGH);
  1183. if (base_low > addon_low)
  1184. res += base_low - addon_low;
  1185. else if (base_low < addon_low) {
  1186. res += interval + base_low - addon_low;
  1187. res += (1 << 24);
  1188. } else
  1189. res += (1 << 24);
  1190. return cpu_to_le32(res);
  1191. }
  1192. static int iwl3945_get_measurement(struct iwl_priv *priv,
  1193. struct ieee80211_measurement_params *params,
  1194. u8 type)
  1195. {
  1196. struct iwl_spectrum_cmd spectrum;
  1197. struct iwl_rx_packet *res;
  1198. struct iwl_host_cmd cmd = {
  1199. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  1200. .data = (void *)&spectrum,
  1201. .meta.flags = CMD_WANT_SKB,
  1202. };
  1203. u32 add_time = le64_to_cpu(params->start_time);
  1204. int rc;
  1205. int spectrum_resp_status;
  1206. int duration = le16_to_cpu(params->duration);
  1207. if (iwl_is_associated(priv))
  1208. add_time =
  1209. iwl3945_usecs_to_beacons(
  1210. le64_to_cpu(params->start_time) - priv->last_tsf,
  1211. le16_to_cpu(priv->rxon_timing.beacon_interval));
  1212. memset(&spectrum, 0, sizeof(spectrum));
  1213. spectrum.channel_count = cpu_to_le16(1);
  1214. spectrum.flags =
  1215. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  1216. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  1217. cmd.len = sizeof(spectrum);
  1218. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  1219. if (iwl_is_associated(priv))
  1220. spectrum.start_time =
  1221. iwl3945_add_beacon_time(priv->last_beacon_time,
  1222. add_time,
  1223. le16_to_cpu(priv->rxon_timing.beacon_interval));
  1224. else
  1225. spectrum.start_time = 0;
  1226. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  1227. spectrum.channels[0].channel = params->channel;
  1228. spectrum.channels[0].type = type;
  1229. if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
  1230. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  1231. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  1232. rc = iwl_send_cmd_sync(priv, &cmd);
  1233. if (rc)
  1234. return rc;
  1235. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  1236. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  1237. IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
  1238. rc = -EIO;
  1239. }
  1240. spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
  1241. switch (spectrum_resp_status) {
  1242. case 0: /* Command will be handled */
  1243. if (res->u.spectrum.id != 0xff) {
  1244. IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
  1245. res->u.spectrum.id);
  1246. priv->measurement_status &= ~MEASUREMENT_READY;
  1247. }
  1248. priv->measurement_status |= MEASUREMENT_ACTIVE;
  1249. rc = 0;
  1250. break;
  1251. case 1: /* Command will not be handled */
  1252. rc = -EAGAIN;
  1253. break;
  1254. }
  1255. dev_kfree_skb_any(cmd.meta.u.skb);
  1256. return rc;
  1257. }
  1258. #endif
  1259. static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
  1260. struct iwl_rx_mem_buffer *rxb)
  1261. {
  1262. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1263. struct iwl_alive_resp *palive;
  1264. struct delayed_work *pwork;
  1265. palive = &pkt->u.alive_frame;
  1266. IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
  1267. "0x%01X 0x%01X\n",
  1268. palive->is_valid, palive->ver_type,
  1269. palive->ver_subtype);
  1270. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  1271. IWL_DEBUG_INFO("Initialization Alive received.\n");
  1272. memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
  1273. sizeof(struct iwl_alive_resp));
  1274. pwork = &priv->init_alive_start;
  1275. } else {
  1276. IWL_DEBUG_INFO("Runtime Alive received.\n");
  1277. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  1278. sizeof(struct iwl_alive_resp));
  1279. pwork = &priv->alive_start;
  1280. iwl3945_disable_events(priv);
  1281. }
  1282. /* We delay the ALIVE response by 5ms to
  1283. * give the HW RF Kill time to activate... */
  1284. if (palive->is_valid == UCODE_VALID_OK)
  1285. queue_delayed_work(priv->workqueue, pwork,
  1286. msecs_to_jiffies(5));
  1287. else
  1288. IWL_WARN(priv, "uCode did not respond OK.\n");
  1289. }
  1290. static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
  1291. struct iwl_rx_mem_buffer *rxb)
  1292. {
  1293. #ifdef CONFIG_IWLWIFI_DEBUG
  1294. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1295. #endif
  1296. IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  1297. return;
  1298. }
  1299. static void iwl3945_rx_reply_error(struct iwl_priv *priv,
  1300. struct iwl_rx_mem_buffer *rxb)
  1301. {
  1302. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1303. IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
  1304. "seq 0x%04X ser 0x%08X\n",
  1305. le32_to_cpu(pkt->u.err_resp.error_type),
  1306. get_cmd_string(pkt->u.err_resp.cmd_id),
  1307. pkt->u.err_resp.cmd_id,
  1308. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  1309. le32_to_cpu(pkt->u.err_resp.error_info));
  1310. }
  1311. static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
  1312. struct iwl_rx_mem_buffer *rxb)
  1313. {
  1314. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  1315. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1316. struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
  1317. if (!report->state) {
  1318. IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
  1319. "Spectrum Measure Notification: Start\n");
  1320. return;
  1321. }
  1322. memcpy(&priv->measure_report, report, sizeof(*report));
  1323. priv->measurement_status |= MEASUREMENT_READY;
  1324. #endif
  1325. }
  1326. static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
  1327. struct iwl_rx_mem_buffer *rxb)
  1328. {
  1329. #ifdef CONFIG_IWLWIFI_DEBUG
  1330. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1331. struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
  1332. IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
  1333. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  1334. #endif
  1335. }
  1336. static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
  1337. struct iwl_rx_mem_buffer *rxb)
  1338. {
  1339. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1340. IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
  1341. "notification for %s:\n",
  1342. le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
  1343. iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
  1344. le32_to_cpu(pkt->len));
  1345. }
  1346. static void iwl3945_bg_beacon_update(struct work_struct *work)
  1347. {
  1348. struct iwl_priv *priv =
  1349. container_of(work, struct iwl_priv, beacon_update);
  1350. struct sk_buff *beacon;
  1351. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  1352. beacon = ieee80211_beacon_get(priv->hw, priv->vif);
  1353. if (!beacon) {
  1354. IWL_ERR(priv, "update beacon failed\n");
  1355. return;
  1356. }
  1357. mutex_lock(&priv->mutex);
  1358. /* new beacon skb is allocated every time; dispose previous.*/
  1359. if (priv->ibss_beacon)
  1360. dev_kfree_skb(priv->ibss_beacon);
  1361. priv->ibss_beacon = beacon;
  1362. mutex_unlock(&priv->mutex);
  1363. iwl3945_send_beacon_cmd(priv);
  1364. }
  1365. static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
  1366. struct iwl_rx_mem_buffer *rxb)
  1367. {
  1368. #ifdef CONFIG_IWLWIFI_DEBUG
  1369. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1370. struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
  1371. u8 rate = beacon->beacon_notify_hdr.rate;
  1372. IWL_DEBUG_RX("beacon status %x retries %d iss %d "
  1373. "tsf %d %d rate %d\n",
  1374. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  1375. beacon->beacon_notify_hdr.failure_frame,
  1376. le32_to_cpu(beacon->ibss_mgr_status),
  1377. le32_to_cpu(beacon->high_tsf),
  1378. le32_to_cpu(beacon->low_tsf), rate);
  1379. #endif
  1380. if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
  1381. (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
  1382. queue_work(priv->workqueue, &priv->beacon_update);
  1383. }
  1384. /* Service response to REPLY_SCAN_CMD (0x80) */
  1385. static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
  1386. struct iwl_rx_mem_buffer *rxb)
  1387. {
  1388. #ifdef CONFIG_IWLWIFI_DEBUG
  1389. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1390. struct iwl_scanreq_notification *notif =
  1391. (struct iwl_scanreq_notification *)pkt->u.raw;
  1392. IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
  1393. #endif
  1394. }
  1395. /* Service SCAN_START_NOTIFICATION (0x82) */
  1396. static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
  1397. struct iwl_rx_mem_buffer *rxb)
  1398. {
  1399. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1400. struct iwl_scanstart_notification *notif =
  1401. (struct iwl_scanstart_notification *)pkt->u.raw;
  1402. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  1403. IWL_DEBUG_SCAN("Scan start: "
  1404. "%d [802.11%s] "
  1405. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  1406. notif->channel,
  1407. notif->band ? "bg" : "a",
  1408. notif->tsf_high,
  1409. notif->tsf_low, notif->status, notif->beacon_timer);
  1410. }
  1411. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  1412. static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
  1413. struct iwl_rx_mem_buffer *rxb)
  1414. {
  1415. #ifdef CONFIG_IWLWIFI_DEBUG
  1416. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1417. struct iwl_scanresults_notification *notif =
  1418. (struct iwl_scanresults_notification *)pkt->u.raw;
  1419. #endif
  1420. IWL_DEBUG_SCAN("Scan ch.res: "
  1421. "%d [802.11%s] "
  1422. "(TSF: 0x%08X:%08X) - %d "
  1423. "elapsed=%lu usec (%dms since last)\n",
  1424. notif->channel,
  1425. notif->band ? "bg" : "a",
  1426. le32_to_cpu(notif->tsf_high),
  1427. le32_to_cpu(notif->tsf_low),
  1428. le32_to_cpu(notif->statistics[0]),
  1429. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
  1430. jiffies_to_msecs(elapsed_jiffies
  1431. (priv->last_scan_jiffies, jiffies)));
  1432. priv->last_scan_jiffies = jiffies;
  1433. priv->next_scan_jiffies = 0;
  1434. }
  1435. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  1436. static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
  1437. struct iwl_rx_mem_buffer *rxb)
  1438. {
  1439. #ifdef CONFIG_IWLWIFI_DEBUG
  1440. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1441. struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  1442. #endif
  1443. IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  1444. scan_notif->scanned_channels,
  1445. scan_notif->tsf_low,
  1446. scan_notif->tsf_high, scan_notif->status);
  1447. /* The HW is no longer scanning */
  1448. clear_bit(STATUS_SCAN_HW, &priv->status);
  1449. /* The scan completion notification came in, so kill that timer... */
  1450. cancel_delayed_work(&priv->scan_check);
  1451. IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
  1452. (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
  1453. "2.4" : "5.2",
  1454. jiffies_to_msecs(elapsed_jiffies
  1455. (priv->scan_pass_start, jiffies)));
  1456. /* Remove this scanned band from the list of pending
  1457. * bands to scan, band G precedes A in order of scanning
  1458. * as seen in iwl3945_bg_request_scan */
  1459. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
  1460. priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
  1461. else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
  1462. priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
  1463. /* If a request to abort was given, or the scan did not succeed
  1464. * then we reset the scan state machine and terminate,
  1465. * re-queuing another scan if one has been requested */
  1466. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1467. IWL_DEBUG_INFO("Aborted scan completed.\n");
  1468. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  1469. } else {
  1470. /* If there are more bands on this scan pass reschedule */
  1471. if (priv->scan_bands > 0)
  1472. goto reschedule;
  1473. }
  1474. priv->last_scan_jiffies = jiffies;
  1475. priv->next_scan_jiffies = 0;
  1476. IWL_DEBUG_INFO("Setting scan to off\n");
  1477. clear_bit(STATUS_SCANNING, &priv->status);
  1478. IWL_DEBUG_INFO("Scan took %dms\n",
  1479. jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
  1480. queue_work(priv->workqueue, &priv->scan_completed);
  1481. return;
  1482. reschedule:
  1483. priv->scan_pass_start = jiffies;
  1484. queue_work(priv->workqueue, &priv->request_scan);
  1485. }
  1486. /* Handle notification from uCode that card's power state is changing
  1487. * due to software, hardware, or critical temperature RFKILL */
  1488. static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
  1489. struct iwl_rx_mem_buffer *rxb)
  1490. {
  1491. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1492. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  1493. unsigned long status = priv->status;
  1494. IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
  1495. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  1496. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  1497. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  1498. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  1499. if (flags & HW_CARD_DISABLED)
  1500. set_bit(STATUS_RF_KILL_HW, &priv->status);
  1501. else
  1502. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  1503. if (flags & SW_CARD_DISABLED)
  1504. set_bit(STATUS_RF_KILL_SW, &priv->status);
  1505. else
  1506. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  1507. iwl_scan_cancel(priv);
  1508. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  1509. test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
  1510. (test_bit(STATUS_RF_KILL_SW, &status) !=
  1511. test_bit(STATUS_RF_KILL_SW, &priv->status)))
  1512. queue_work(priv->workqueue, &priv->rf_kill);
  1513. else
  1514. wake_up_interruptible(&priv->wait_command_queue);
  1515. }
  1516. /**
  1517. * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
  1518. *
  1519. * Setup the RX handlers for each of the reply types sent from the uCode
  1520. * to the host.
  1521. *
  1522. * This function chains into the hardware specific files for them to setup
  1523. * any hardware specific handlers as well.
  1524. */
  1525. static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
  1526. {
  1527. priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
  1528. priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
  1529. priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
  1530. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
  1531. priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  1532. iwl3945_rx_spectrum_measure_notif;
  1533. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
  1534. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  1535. iwl3945_rx_pm_debug_statistics_notif;
  1536. priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
  1537. /*
  1538. * The same handler is used for both the REPLY to a discrete
  1539. * statistics request from the host as well as for the periodic
  1540. * statistics notifications (after received beacons) from the uCode.
  1541. */
  1542. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
  1543. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
  1544. priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
  1545. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
  1546. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  1547. iwl3945_rx_scan_results_notif;
  1548. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  1549. iwl3945_rx_scan_complete_notif;
  1550. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
  1551. /* Set up hardware specific Rx handlers */
  1552. iwl3945_hw_rx_handler_setup(priv);
  1553. }
  1554. /**
  1555. * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
  1556. * When FW advances 'R' index, all entries between old and new 'R' index
  1557. * need to be reclaimed.
  1558. */
  1559. static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
  1560. int txq_id, int index)
  1561. {
  1562. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  1563. struct iwl_queue *q = &txq->q;
  1564. int nfreed = 0;
  1565. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  1566. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  1567. "is out of range [0-%d] %d %d.\n", txq_id,
  1568. index, q->n_bd, q->write_ptr, q->read_ptr);
  1569. return;
  1570. }
  1571. for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
  1572. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  1573. if (nfreed > 1) {
  1574. IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
  1575. q->write_ptr, q->read_ptr);
  1576. queue_work(priv->workqueue, &priv->restart);
  1577. break;
  1578. }
  1579. nfreed++;
  1580. }
  1581. }
  1582. /**
  1583. * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  1584. * @rxb: Rx buffer to reclaim
  1585. *
  1586. * If an Rx buffer has an async callback associated with it the callback
  1587. * will be executed. The attached skb (if present) will only be freed
  1588. * if the callback returns 1
  1589. */
  1590. static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
  1591. struct iwl_rx_mem_buffer *rxb)
  1592. {
  1593. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1594. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1595. int txq_id = SEQ_TO_QUEUE(sequence);
  1596. int index = SEQ_TO_INDEX(sequence);
  1597. int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
  1598. int cmd_index;
  1599. struct iwl_cmd *cmd;
  1600. if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
  1601. "wrong command queue %d, sequence 0x%X readp=%d writep=%d\n",
  1602. txq_id, sequence,
  1603. priv->txq[IWL_CMD_QUEUE_NUM].q.read_ptr,
  1604. priv->txq[IWL_CMD_QUEUE_NUM].q.write_ptr)) {
  1605. iwl_print_hex_dump(priv, IWL_DL_INFO , rxb, 32);
  1606. return;
  1607. }
  1608. cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
  1609. cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
  1610. /* Input error checking is done when commands are added to queue. */
  1611. if (cmd->meta.flags & CMD_WANT_SKB) {
  1612. cmd->meta.source->u.skb = rxb->skb;
  1613. rxb->skb = NULL;
  1614. } else if (cmd->meta.u.callback &&
  1615. !cmd->meta.u.callback(priv, cmd, rxb->skb))
  1616. rxb->skb = NULL;
  1617. iwl3945_cmd_queue_reclaim(priv, txq_id, index);
  1618. if (!(cmd->meta.flags & CMD_ASYNC)) {
  1619. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  1620. wake_up_interruptible(&priv->wait_command_queue);
  1621. }
  1622. }
  1623. /************************** RX-FUNCTIONS ****************************/
  1624. /*
  1625. * Rx theory of operation
  1626. *
  1627. * The host allocates 32 DMA target addresses and passes the host address
  1628. * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  1629. * 0 to 31
  1630. *
  1631. * Rx Queue Indexes
  1632. * The host/firmware share two index registers for managing the Rx buffers.
  1633. *
  1634. * The READ index maps to the first position that the firmware may be writing
  1635. * to -- the driver can read up to (but not including) this position and get
  1636. * good data.
  1637. * The READ index is managed by the firmware once the card is enabled.
  1638. *
  1639. * The WRITE index maps to the last position the driver has read from -- the
  1640. * position preceding WRITE is the last slot the firmware can place a packet.
  1641. *
  1642. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  1643. * WRITE = READ.
  1644. *
  1645. * During initialization, the host sets up the READ queue position to the first
  1646. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  1647. *
  1648. * When the firmware places a packet in a buffer, it will advance the READ index
  1649. * and fire the RX interrupt. The driver can then query the READ index and
  1650. * process as many packets as possible, moving the WRITE index forward as it
  1651. * resets the Rx queue buffers with new memory.
  1652. *
  1653. * The management in the driver is as follows:
  1654. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  1655. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  1656. * to replenish the iwl->rxq->rx_free.
  1657. * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
  1658. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  1659. * 'processed' and 'read' driver indexes as well)
  1660. * + A received packet is processed and handed to the kernel network stack,
  1661. * detached from the iwl->rxq. The driver 'processed' index is updated.
  1662. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  1663. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  1664. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  1665. * were enough free buffers and RX_STALLED is set it is cleared.
  1666. *
  1667. *
  1668. * Driver sequence:
  1669. *
  1670. * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
  1671. * iwl3945_rx_queue_restock
  1672. * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
  1673. * queue, updates firmware pointers, and updates
  1674. * the WRITE index. If insufficient rx_free buffers
  1675. * are available, schedules iwl3945_rx_replenish
  1676. *
  1677. * -- enable interrupts --
  1678. * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
  1679. * READ INDEX, detaching the SKB from the pool.
  1680. * Moves the packet buffer from queue to rx_used.
  1681. * Calls iwl3945_rx_queue_restock to refill any empty
  1682. * slots.
  1683. * ...
  1684. *
  1685. */
  1686. /**
  1687. * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  1688. */
  1689. static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
  1690. dma_addr_t dma_addr)
  1691. {
  1692. return cpu_to_le32((u32)dma_addr);
  1693. }
  1694. /**
  1695. * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
  1696. *
  1697. * If there are slots in the RX queue that need to be restocked,
  1698. * and we have free pre-allocated buffers, fill the ranks as much
  1699. * as we can, pulling from rx_free.
  1700. *
  1701. * This moves the 'write' index forward to catch up with 'processed', and
  1702. * also updates the memory address in the firmware to reference the new
  1703. * target buffer.
  1704. */
  1705. static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
  1706. {
  1707. struct iwl_rx_queue *rxq = &priv->rxq;
  1708. struct list_head *element;
  1709. struct iwl_rx_mem_buffer *rxb;
  1710. unsigned long flags;
  1711. int write, rc;
  1712. spin_lock_irqsave(&rxq->lock, flags);
  1713. write = rxq->write & ~0x7;
  1714. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  1715. /* Get next free Rx buffer, remove from free list */
  1716. element = rxq->rx_free.next;
  1717. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  1718. list_del(element);
  1719. /* Point to Rx buffer via next RBD in circular buffer */
  1720. rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
  1721. rxq->queue[rxq->write] = rxb;
  1722. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  1723. rxq->free_count--;
  1724. }
  1725. spin_unlock_irqrestore(&rxq->lock, flags);
  1726. /* If the pre-allocated buffer pool is dropping low, schedule to
  1727. * refill it */
  1728. if (rxq->free_count <= RX_LOW_WATERMARK)
  1729. queue_work(priv->workqueue, &priv->rx_replenish);
  1730. /* If we've added more space for the firmware to place data, tell it.
  1731. * Increment device's write pointer in multiples of 8. */
  1732. if ((write != (rxq->write & ~0x7))
  1733. || (abs(rxq->write - rxq->read) > 7)) {
  1734. spin_lock_irqsave(&rxq->lock, flags);
  1735. rxq->need_update = 1;
  1736. spin_unlock_irqrestore(&rxq->lock, flags);
  1737. rc = iwl_rx_queue_update_write_ptr(priv, rxq);
  1738. if (rc)
  1739. return rc;
  1740. }
  1741. return 0;
  1742. }
  1743. /**
  1744. * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
  1745. *
  1746. * When moving to rx_free an SKB is allocated for the slot.
  1747. *
  1748. * Also restock the Rx queue via iwl3945_rx_queue_restock.
  1749. * This is called as a scheduled work item (except for during initialization)
  1750. */
  1751. static void iwl3945_rx_allocate(struct iwl_priv *priv)
  1752. {
  1753. struct iwl_rx_queue *rxq = &priv->rxq;
  1754. struct list_head *element;
  1755. struct iwl_rx_mem_buffer *rxb;
  1756. unsigned long flags;
  1757. spin_lock_irqsave(&rxq->lock, flags);
  1758. while (!list_empty(&rxq->rx_used)) {
  1759. element = rxq->rx_used.next;
  1760. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  1761. /* Alloc a new receive buffer */
  1762. rxb->skb =
  1763. alloc_skb(priv->hw_params.rx_buf_size,
  1764. __GFP_NOWARN | GFP_ATOMIC);
  1765. if (!rxb->skb) {
  1766. if (net_ratelimit())
  1767. IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
  1768. /* We don't reschedule replenish work here -- we will
  1769. * call the restock method and if it still needs
  1770. * more buffers it will schedule replenish */
  1771. break;
  1772. }
  1773. /* If radiotap head is required, reserve some headroom here.
  1774. * The physical head count is a variable rx_stats->phy_count.
  1775. * We reserve 4 bytes here. Plus these extra bytes, the
  1776. * headroom of the physical head should be enough for the
  1777. * radiotap head that iwl3945 supported. See iwl3945_rt.
  1778. */
  1779. skb_reserve(rxb->skb, 4);
  1780. priv->alloc_rxb_skb++;
  1781. list_del(element);
  1782. /* Get physical address of RB/SKB */
  1783. rxb->real_dma_addr = pci_map_single(priv->pci_dev,
  1784. rxb->skb->data,
  1785. priv->hw_params.rx_buf_size,
  1786. PCI_DMA_FROMDEVICE);
  1787. list_add_tail(&rxb->list, &rxq->rx_free);
  1788. rxq->free_count++;
  1789. }
  1790. spin_unlock_irqrestore(&rxq->lock, flags);
  1791. }
  1792. /*
  1793. * this should be called while priv->lock is locked
  1794. */
  1795. static void __iwl3945_rx_replenish(void *data)
  1796. {
  1797. struct iwl_priv *priv = data;
  1798. iwl3945_rx_allocate(priv);
  1799. iwl3945_rx_queue_restock(priv);
  1800. }
  1801. void iwl3945_rx_replenish(void *data)
  1802. {
  1803. struct iwl_priv *priv = data;
  1804. unsigned long flags;
  1805. iwl3945_rx_allocate(priv);
  1806. spin_lock_irqsave(&priv->lock, flags);
  1807. iwl3945_rx_queue_restock(priv);
  1808. spin_unlock_irqrestore(&priv->lock, flags);
  1809. }
  1810. /* Convert linear signal-to-noise ratio into dB */
  1811. static u8 ratio2dB[100] = {
  1812. /* 0 1 2 3 4 5 6 7 8 9 */
  1813. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  1814. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  1815. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  1816. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  1817. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  1818. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  1819. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  1820. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  1821. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  1822. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  1823. };
  1824. /* Calculates a relative dB value from a ratio of linear
  1825. * (i.e. not dB) signal levels.
  1826. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  1827. int iwl3945_calc_db_from_ratio(int sig_ratio)
  1828. {
  1829. /* 1000:1 or higher just report as 60 dB */
  1830. if (sig_ratio >= 1000)
  1831. return 60;
  1832. /* 100:1 or higher, divide by 10 and use table,
  1833. * add 20 dB to make up for divide by 10 */
  1834. if (sig_ratio >= 100)
  1835. return 20 + (int)ratio2dB[sig_ratio/10];
  1836. /* We shouldn't see this */
  1837. if (sig_ratio < 1)
  1838. return 0;
  1839. /* Use table for ratios 1:1 - 99:1 */
  1840. return (int)ratio2dB[sig_ratio];
  1841. }
  1842. #define PERFECT_RSSI (-20) /* dBm */
  1843. #define WORST_RSSI (-95) /* dBm */
  1844. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  1845. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  1846. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  1847. * about formulas used below. */
  1848. int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
  1849. {
  1850. int sig_qual;
  1851. int degradation = PERFECT_RSSI - rssi_dbm;
  1852. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  1853. * as indicator; formula is (signal dbm - noise dbm).
  1854. * SNR at or above 40 is a great signal (100%).
  1855. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  1856. * Weakest usable signal is usually 10 - 15 dB SNR. */
  1857. if (noise_dbm) {
  1858. if (rssi_dbm - noise_dbm >= 40)
  1859. return 100;
  1860. else if (rssi_dbm < noise_dbm)
  1861. return 0;
  1862. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  1863. /* Else use just the signal level.
  1864. * This formula is a least squares fit of data points collected and
  1865. * compared with a reference system that had a percentage (%) display
  1866. * for signal quality. */
  1867. } else
  1868. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  1869. (15 * RSSI_RANGE + 62 * degradation)) /
  1870. (RSSI_RANGE * RSSI_RANGE);
  1871. if (sig_qual > 100)
  1872. sig_qual = 100;
  1873. else if (sig_qual < 1)
  1874. sig_qual = 0;
  1875. return sig_qual;
  1876. }
  1877. /**
  1878. * iwl3945_rx_handle - Main entry function for receiving responses from uCode
  1879. *
  1880. * Uses the priv->rx_handlers callback function array to invoke
  1881. * the appropriate handlers, including command responses,
  1882. * frame-received notifications, and other notifications.
  1883. */
  1884. static void iwl3945_rx_handle(struct iwl_priv *priv)
  1885. {
  1886. struct iwl_rx_mem_buffer *rxb;
  1887. struct iwl_rx_packet *pkt;
  1888. struct iwl_rx_queue *rxq = &priv->rxq;
  1889. u32 r, i;
  1890. int reclaim;
  1891. unsigned long flags;
  1892. u8 fill_rx = 0;
  1893. u32 count = 8;
  1894. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  1895. * buffer that the driver may process (last buffer filled by ucode). */
  1896. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  1897. i = rxq->read;
  1898. if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
  1899. fill_rx = 1;
  1900. /* Rx interrupt, but nothing sent from uCode */
  1901. if (i == r)
  1902. IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
  1903. while (i != r) {
  1904. rxb = rxq->queue[i];
  1905. /* If an RXB doesn't have a Rx queue slot associated with it,
  1906. * then a bug has been introduced in the queue refilling
  1907. * routines -- catch it here */
  1908. BUG_ON(rxb == NULL);
  1909. rxq->queue[i] = NULL;
  1910. pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
  1911. priv->hw_params.rx_buf_size,
  1912. PCI_DMA_FROMDEVICE);
  1913. pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1914. /* Reclaim a command buffer only if this packet is a response
  1915. * to a (driver-originated) command.
  1916. * If the packet (e.g. Rx frame) originated from uCode,
  1917. * there is no command buffer to reclaim.
  1918. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  1919. * but apparently a few don't get set; catch them here. */
  1920. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  1921. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  1922. (pkt->hdr.cmd != REPLY_TX);
  1923. /* Based on type of command response or notification,
  1924. * handle those that need handling via function in
  1925. * rx_handlers table. See iwl3945_setup_rx_handlers() */
  1926. if (priv->rx_handlers[pkt->hdr.cmd]) {
  1927. IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  1928. "r = %d, i = %d, %s, 0x%02x\n", r, i,
  1929. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  1930. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  1931. } else {
  1932. /* No handling needed */
  1933. IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  1934. "r %d i %d No handler needed for %s, 0x%02x\n",
  1935. r, i, get_cmd_string(pkt->hdr.cmd),
  1936. pkt->hdr.cmd);
  1937. }
  1938. if (reclaim) {
  1939. /* Invoke any callbacks, transfer the skb to caller, and
  1940. * fire off the (possibly) blocking iwl_send_cmd()
  1941. * as we reclaim the driver command queue */
  1942. if (rxb && rxb->skb)
  1943. iwl3945_tx_cmd_complete(priv, rxb);
  1944. else
  1945. IWL_WARN(priv, "Claim null rxb?\n");
  1946. }
  1947. /* For now we just don't re-use anything. We can tweak this
  1948. * later to try and re-use notification packets and SKBs that
  1949. * fail to Rx correctly */
  1950. if (rxb->skb != NULL) {
  1951. priv->alloc_rxb_skb--;
  1952. dev_kfree_skb_any(rxb->skb);
  1953. rxb->skb = NULL;
  1954. }
  1955. pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
  1956. priv->hw_params.rx_buf_size,
  1957. PCI_DMA_FROMDEVICE);
  1958. spin_lock_irqsave(&rxq->lock, flags);
  1959. list_add_tail(&rxb->list, &priv->rxq.rx_used);
  1960. spin_unlock_irqrestore(&rxq->lock, flags);
  1961. i = (i + 1) & RX_QUEUE_MASK;
  1962. /* If there are a lot of unused frames,
  1963. * restock the Rx queue so ucode won't assert. */
  1964. if (fill_rx) {
  1965. count++;
  1966. if (count >= 8) {
  1967. priv->rxq.read = i;
  1968. __iwl3945_rx_replenish(priv);
  1969. count = 0;
  1970. }
  1971. }
  1972. }
  1973. /* Backtrack one entry */
  1974. priv->rxq.read = i;
  1975. iwl3945_rx_queue_restock(priv);
  1976. }
  1977. static void iwl3945_enable_interrupts(struct iwl_priv *priv)
  1978. {
  1979. IWL_DEBUG_ISR("Enabling interrupts\n");
  1980. set_bit(STATUS_INT_ENABLED, &priv->status);
  1981. iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
  1982. }
  1983. /* call this function to flush any scheduled tasklet */
  1984. static inline void iwl_synchronize_irq(struct iwl_priv *priv)
  1985. {
  1986. /* wait to make sure we flush pending tasklet*/
  1987. synchronize_irq(priv->pci_dev->irq);
  1988. tasklet_kill(&priv->irq_tasklet);
  1989. }
  1990. static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
  1991. {
  1992. clear_bit(STATUS_INT_ENABLED, &priv->status);
  1993. /* disable interrupts from uCode/NIC to host */
  1994. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  1995. /* acknowledge/clear/reset any interrupts still pending
  1996. * from uCode or flow handler (Rx/Tx DMA) */
  1997. iwl_write32(priv, CSR_INT, 0xffffffff);
  1998. iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
  1999. IWL_DEBUG_ISR("Disabled interrupts\n");
  2000. }
  2001. static const char *desc_lookup(int i)
  2002. {
  2003. switch (i) {
  2004. case 1:
  2005. return "FAIL";
  2006. case 2:
  2007. return "BAD_PARAM";
  2008. case 3:
  2009. return "BAD_CHECKSUM";
  2010. case 4:
  2011. return "NMI_INTERRUPT";
  2012. case 5:
  2013. return "SYSASSERT";
  2014. case 6:
  2015. return "FATAL_ERROR";
  2016. }
  2017. return "UNKNOWN";
  2018. }
  2019. #define ERROR_START_OFFSET (1 * sizeof(u32))
  2020. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  2021. static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
  2022. {
  2023. u32 i;
  2024. u32 desc, time, count, base, data1;
  2025. u32 blink1, blink2, ilink1, ilink2;
  2026. int rc;
  2027. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  2028. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  2029. IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
  2030. return;
  2031. }
  2032. rc = iwl_grab_nic_access(priv);
  2033. if (rc) {
  2034. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  2035. return;
  2036. }
  2037. count = iwl_read_targ_mem(priv, base);
  2038. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  2039. IWL_ERR(priv, "Start IWL Error Log Dump:\n");
  2040. IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
  2041. priv->status, count);
  2042. }
  2043. IWL_ERR(priv, "Desc Time asrtPC blink2 "
  2044. "ilink1 nmiPC Line\n");
  2045. for (i = ERROR_START_OFFSET;
  2046. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  2047. i += ERROR_ELEM_SIZE) {
  2048. desc = iwl_read_targ_mem(priv, base + i);
  2049. time =
  2050. iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
  2051. blink1 =
  2052. iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
  2053. blink2 =
  2054. iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
  2055. ilink1 =
  2056. iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
  2057. ilink2 =
  2058. iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
  2059. data1 =
  2060. iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
  2061. IWL_ERR(priv,
  2062. "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  2063. desc_lookup(desc), desc, time, blink1, blink2,
  2064. ilink1, ilink2, data1);
  2065. }
  2066. iwl_release_nic_access(priv);
  2067. }
  2068. #define EVENT_START_OFFSET (6 * sizeof(u32))
  2069. /**
  2070. * iwl3945_print_event_log - Dump error event log to syslog
  2071. *
  2072. * NOTE: Must be called with iwl_grab_nic_access() already obtained!
  2073. */
  2074. static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
  2075. u32 num_events, u32 mode)
  2076. {
  2077. u32 i;
  2078. u32 base; /* SRAM byte address of event log header */
  2079. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  2080. u32 ptr; /* SRAM byte address of log data */
  2081. u32 ev, time, data; /* event log data */
  2082. if (num_events == 0)
  2083. return;
  2084. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  2085. if (mode == 0)
  2086. event_size = 2 * sizeof(u32);
  2087. else
  2088. event_size = 3 * sizeof(u32);
  2089. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  2090. /* "time" is actually "data" for mode 0 (no timestamp).
  2091. * place event id # at far right for easier visual parsing. */
  2092. for (i = 0; i < num_events; i++) {
  2093. ev = iwl_read_targ_mem(priv, ptr);
  2094. ptr += sizeof(u32);
  2095. time = iwl_read_targ_mem(priv, ptr);
  2096. ptr += sizeof(u32);
  2097. if (mode == 0) {
  2098. /* data, ev */
  2099. IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
  2100. } else {
  2101. data = iwl_read_targ_mem(priv, ptr);
  2102. ptr += sizeof(u32);
  2103. IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
  2104. }
  2105. }
  2106. }
  2107. static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
  2108. {
  2109. int rc;
  2110. u32 base; /* SRAM byte address of event log header */
  2111. u32 capacity; /* event log capacity in # entries */
  2112. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  2113. u32 num_wraps; /* # times uCode wrapped to top of log */
  2114. u32 next_entry; /* index of next entry to be written by uCode */
  2115. u32 size; /* # entries that we'll print */
  2116. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  2117. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  2118. IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
  2119. return;
  2120. }
  2121. rc = iwl_grab_nic_access(priv);
  2122. if (rc) {
  2123. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  2124. return;
  2125. }
  2126. /* event log header */
  2127. capacity = iwl_read_targ_mem(priv, base);
  2128. mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
  2129. num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
  2130. next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
  2131. size = num_wraps ? capacity : next_entry;
  2132. /* bail out if nothing in log */
  2133. if (size == 0) {
  2134. IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
  2135. iwl_release_nic_access(priv);
  2136. return;
  2137. }
  2138. IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
  2139. size, num_wraps);
  2140. /* if uCode has wrapped back to top of log, start at the oldest entry,
  2141. * i.e the next one that uCode would fill. */
  2142. if (num_wraps)
  2143. iwl3945_print_event_log(priv, next_entry,
  2144. capacity - next_entry, mode);
  2145. /* (then/else) start at top of log */
  2146. iwl3945_print_event_log(priv, 0, next_entry, mode);
  2147. iwl_release_nic_access(priv);
  2148. }
  2149. static void iwl3945_error_recovery(struct iwl_priv *priv)
  2150. {
  2151. unsigned long flags;
  2152. memcpy(&priv->staging_rxon, &priv->recovery_rxon,
  2153. sizeof(priv->staging_rxon));
  2154. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2155. iwl3945_commit_rxon(priv);
  2156. iwl3945_add_station(priv, priv->bssid, 1, 0);
  2157. spin_lock_irqsave(&priv->lock, flags);
  2158. priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
  2159. priv->error_recovering = 0;
  2160. spin_unlock_irqrestore(&priv->lock, flags);
  2161. }
  2162. static void iwl3945_irq_tasklet(struct iwl_priv *priv)
  2163. {
  2164. u32 inta, handled = 0;
  2165. u32 inta_fh;
  2166. unsigned long flags;
  2167. #ifdef CONFIG_IWLWIFI_DEBUG
  2168. u32 inta_mask;
  2169. #endif
  2170. spin_lock_irqsave(&priv->lock, flags);
  2171. /* Ack/clear/reset pending uCode interrupts.
  2172. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  2173. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  2174. inta = iwl_read32(priv, CSR_INT);
  2175. iwl_write32(priv, CSR_INT, inta);
  2176. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  2177. * Any new interrupts that happen after this, either while we're
  2178. * in this tasklet, or later, will show up in next ISR/tasklet. */
  2179. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  2180. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  2181. #ifdef CONFIG_IWLWIFI_DEBUG
  2182. if (priv->debug_level & IWL_DL_ISR) {
  2183. /* just for debug */
  2184. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  2185. IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  2186. inta, inta_mask, inta_fh);
  2187. }
  2188. #endif
  2189. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  2190. * atomic, make sure that inta covers all the interrupts that
  2191. * we've discovered, even if FH interrupt came in just after
  2192. * reading CSR_INT. */
  2193. if (inta_fh & CSR39_FH_INT_RX_MASK)
  2194. inta |= CSR_INT_BIT_FH_RX;
  2195. if (inta_fh & CSR39_FH_INT_TX_MASK)
  2196. inta |= CSR_INT_BIT_FH_TX;
  2197. /* Now service all interrupt bits discovered above. */
  2198. if (inta & CSR_INT_BIT_HW_ERR) {
  2199. IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
  2200. /* Tell the device to stop sending interrupts */
  2201. iwl3945_disable_interrupts(priv);
  2202. iwl_irq_handle_error(priv);
  2203. handled |= CSR_INT_BIT_HW_ERR;
  2204. spin_unlock_irqrestore(&priv->lock, flags);
  2205. return;
  2206. }
  2207. #ifdef CONFIG_IWLWIFI_DEBUG
  2208. if (priv->debug_level & (IWL_DL_ISR)) {
  2209. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  2210. if (inta & CSR_INT_BIT_SCD)
  2211. IWL_DEBUG_ISR("Scheduler finished to transmit "
  2212. "the frame/frames.\n");
  2213. /* Alive notification via Rx interrupt will do the real work */
  2214. if (inta & CSR_INT_BIT_ALIVE)
  2215. IWL_DEBUG_ISR("Alive interrupt\n");
  2216. }
  2217. #endif
  2218. /* Safely ignore these bits for debug checks below */
  2219. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  2220. /* Error detected by uCode */
  2221. if (inta & CSR_INT_BIT_SW_ERR) {
  2222. IWL_ERR(priv, "Microcode SW error detected. "
  2223. "Restarting 0x%X.\n", inta);
  2224. iwl_irq_handle_error(priv);
  2225. handled |= CSR_INT_BIT_SW_ERR;
  2226. }
  2227. /* uCode wakes up after power-down sleep */
  2228. if (inta & CSR_INT_BIT_WAKEUP) {
  2229. IWL_DEBUG_ISR("Wakeup interrupt\n");
  2230. iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
  2231. iwl_txq_update_write_ptr(priv, &priv->txq[0]);
  2232. iwl_txq_update_write_ptr(priv, &priv->txq[1]);
  2233. iwl_txq_update_write_ptr(priv, &priv->txq[2]);
  2234. iwl_txq_update_write_ptr(priv, &priv->txq[3]);
  2235. iwl_txq_update_write_ptr(priv, &priv->txq[4]);
  2236. iwl_txq_update_write_ptr(priv, &priv->txq[5]);
  2237. handled |= CSR_INT_BIT_WAKEUP;
  2238. }
  2239. /* All uCode command responses, including Tx command responses,
  2240. * Rx "responses" (frame-received notification), and other
  2241. * notifications from uCode come through here*/
  2242. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  2243. iwl3945_rx_handle(priv);
  2244. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  2245. }
  2246. if (inta & CSR_INT_BIT_FH_TX) {
  2247. IWL_DEBUG_ISR("Tx interrupt\n");
  2248. iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
  2249. if (!iwl_grab_nic_access(priv)) {
  2250. iwl_write_direct32(priv, FH39_TCSR_CREDIT
  2251. (FH39_SRVC_CHNL), 0x0);
  2252. iwl_release_nic_access(priv);
  2253. }
  2254. handled |= CSR_INT_BIT_FH_TX;
  2255. }
  2256. if (inta & ~handled)
  2257. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  2258. if (inta & ~CSR_INI_SET_MASK) {
  2259. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  2260. inta & ~CSR_INI_SET_MASK);
  2261. IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
  2262. }
  2263. /* Re-enable all interrupts */
  2264. /* only Re-enable if disabled by irq */
  2265. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  2266. iwl3945_enable_interrupts(priv);
  2267. #ifdef CONFIG_IWLWIFI_DEBUG
  2268. if (priv->debug_level & (IWL_DL_ISR)) {
  2269. inta = iwl_read32(priv, CSR_INT);
  2270. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  2271. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  2272. IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  2273. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  2274. }
  2275. #endif
  2276. spin_unlock_irqrestore(&priv->lock, flags);
  2277. }
  2278. static irqreturn_t iwl3945_isr(int irq, void *data)
  2279. {
  2280. struct iwl_priv *priv = data;
  2281. u32 inta, inta_mask;
  2282. u32 inta_fh;
  2283. if (!priv)
  2284. return IRQ_NONE;
  2285. spin_lock(&priv->lock);
  2286. /* Disable (but don't clear!) interrupts here to avoid
  2287. * back-to-back ISRs and sporadic interrupts from our NIC.
  2288. * If we have something to service, the tasklet will re-enable ints.
  2289. * If we *don't* have something, we'll re-enable before leaving here. */
  2290. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  2291. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  2292. /* Discover which interrupts are active/pending */
  2293. inta = iwl_read32(priv, CSR_INT);
  2294. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  2295. /* Ignore interrupt if there's nothing in NIC to service.
  2296. * This may be due to IRQ shared with another device,
  2297. * or due to sporadic interrupts thrown from our NIC. */
  2298. if (!inta && !inta_fh) {
  2299. IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
  2300. goto none;
  2301. }
  2302. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  2303. /* Hardware disappeared */
  2304. IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  2305. goto unplugged;
  2306. }
  2307. IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  2308. inta, inta_mask, inta_fh);
  2309. inta &= ~CSR_INT_BIT_SCD;
  2310. /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
  2311. if (likely(inta || inta_fh))
  2312. tasklet_schedule(&priv->irq_tasklet);
  2313. unplugged:
  2314. spin_unlock(&priv->lock);
  2315. return IRQ_HANDLED;
  2316. none:
  2317. /* re-enable interrupts here since we don't have anything to service. */
  2318. /* only Re-enable if disabled by irq */
  2319. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  2320. iwl3945_enable_interrupts(priv);
  2321. spin_unlock(&priv->lock);
  2322. return IRQ_NONE;
  2323. }
  2324. static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
  2325. enum ieee80211_band band,
  2326. u8 is_active, u8 n_probes,
  2327. struct iwl3945_scan_channel *scan_ch)
  2328. {
  2329. const struct ieee80211_channel *channels = NULL;
  2330. const struct ieee80211_supported_band *sband;
  2331. const struct iwl_channel_info *ch_info;
  2332. u16 passive_dwell = 0;
  2333. u16 active_dwell = 0;
  2334. int added, i;
  2335. sband = iwl_get_hw_mode(priv, band);
  2336. if (!sband)
  2337. return 0;
  2338. channels = sband->channels;
  2339. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  2340. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  2341. if (passive_dwell <= active_dwell)
  2342. passive_dwell = active_dwell + 1;
  2343. for (i = 0, added = 0; i < sband->n_channels; i++) {
  2344. if (channels[i].flags & IEEE80211_CHAN_DISABLED)
  2345. continue;
  2346. scan_ch->channel = channels[i].hw_value;
  2347. ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
  2348. if (!is_channel_valid(ch_info)) {
  2349. IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
  2350. scan_ch->channel);
  2351. continue;
  2352. }
  2353. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  2354. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  2355. /* If passive , set up for auto-switch
  2356. * and use long active_dwell time.
  2357. */
  2358. if (!is_active || is_channel_passive(ch_info) ||
  2359. (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
  2360. scan_ch->type = 0; /* passive */
  2361. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  2362. scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
  2363. } else {
  2364. scan_ch->type = 1; /* active */
  2365. }
  2366. /* Set direct probe bits. These may be used both for active
  2367. * scan channels (probes gets sent right away),
  2368. * or for passive channels (probes get se sent only after
  2369. * hearing clear Rx packet).*/
  2370. if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
  2371. if (n_probes)
  2372. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  2373. } else {
  2374. /* uCode v1 does not allow setting direct probe bits on
  2375. * passive channel. */
  2376. if ((scan_ch->type & 1) && n_probes)
  2377. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  2378. }
  2379. /* Set txpower levels to defaults */
  2380. scan_ch->tpc.dsp_atten = 110;
  2381. /* scan_pwr_info->tpc.dsp_atten; */
  2382. /*scan_pwr_info->tpc.tx_gain; */
  2383. if (band == IEEE80211_BAND_5GHZ)
  2384. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  2385. else {
  2386. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  2387. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  2388. * power level:
  2389. * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
  2390. */
  2391. }
  2392. IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
  2393. scan_ch->channel,
  2394. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  2395. (scan_ch->type & 1) ?
  2396. active_dwell : passive_dwell);
  2397. scan_ch++;
  2398. added++;
  2399. }
  2400. IWL_DEBUG_SCAN("total channels to scan %d \n", added);
  2401. return added;
  2402. }
  2403. static void iwl3945_init_hw_rates(struct iwl_priv *priv,
  2404. struct ieee80211_rate *rates)
  2405. {
  2406. int i;
  2407. for (i = 0; i < IWL_RATE_COUNT; i++) {
  2408. rates[i].bitrate = iwl3945_rates[i].ieee * 5;
  2409. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  2410. rates[i].hw_value_short = i;
  2411. rates[i].flags = 0;
  2412. if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  2413. /*
  2414. * If CCK != 1M then set short preamble rate flag.
  2415. */
  2416. rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
  2417. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  2418. }
  2419. }
  2420. }
  2421. /******************************************************************************
  2422. *
  2423. * uCode download functions
  2424. *
  2425. ******************************************************************************/
  2426. static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
  2427. {
  2428. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
  2429. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
  2430. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  2431. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
  2432. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  2433. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
  2434. }
  2435. /**
  2436. * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
  2437. * looking at all data.
  2438. */
  2439. static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
  2440. {
  2441. u32 val;
  2442. u32 save_len = len;
  2443. int rc = 0;
  2444. u32 errcnt;
  2445. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  2446. rc = iwl_grab_nic_access(priv);
  2447. if (rc)
  2448. return rc;
  2449. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  2450. IWL39_RTC_INST_LOWER_BOUND);
  2451. errcnt = 0;
  2452. for (; len > 0; len -= sizeof(u32), image++) {
  2453. /* read data comes through single port, auto-incr addr */
  2454. /* NOTE: Use the debugless read so we don't flood kernel log
  2455. * if IWL_DL_IO is set */
  2456. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  2457. if (val != le32_to_cpu(*image)) {
  2458. IWL_ERR(priv, "uCode INST section is invalid at "
  2459. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  2460. save_len - len, val, le32_to_cpu(*image));
  2461. rc = -EIO;
  2462. errcnt++;
  2463. if (errcnt >= 20)
  2464. break;
  2465. }
  2466. }
  2467. iwl_release_nic_access(priv);
  2468. if (!errcnt)
  2469. IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
  2470. return rc;
  2471. }
  2472. /**
  2473. * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
  2474. * using sample data 100 bytes apart. If these sample points are good,
  2475. * it's a pretty good bet that everything between them is good, too.
  2476. */
  2477. static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  2478. {
  2479. u32 val;
  2480. int rc = 0;
  2481. u32 errcnt = 0;
  2482. u32 i;
  2483. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  2484. rc = iwl_grab_nic_access(priv);
  2485. if (rc)
  2486. return rc;
  2487. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  2488. /* read data comes through single port, auto-incr addr */
  2489. /* NOTE: Use the debugless read so we don't flood kernel log
  2490. * if IWL_DL_IO is set */
  2491. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  2492. i + IWL39_RTC_INST_LOWER_BOUND);
  2493. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  2494. if (val != le32_to_cpu(*image)) {
  2495. #if 0 /* Enable this if you want to see details */
  2496. IWL_ERR(priv, "uCode INST section is invalid at "
  2497. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  2498. i, val, *image);
  2499. #endif
  2500. rc = -EIO;
  2501. errcnt++;
  2502. if (errcnt >= 3)
  2503. break;
  2504. }
  2505. }
  2506. iwl_release_nic_access(priv);
  2507. return rc;
  2508. }
  2509. /**
  2510. * iwl3945_verify_ucode - determine which instruction image is in SRAM,
  2511. * and verify its contents
  2512. */
  2513. static int iwl3945_verify_ucode(struct iwl_priv *priv)
  2514. {
  2515. __le32 *image;
  2516. u32 len;
  2517. int rc = 0;
  2518. /* Try bootstrap */
  2519. image = (__le32 *)priv->ucode_boot.v_addr;
  2520. len = priv->ucode_boot.len;
  2521. rc = iwl3945_verify_inst_sparse(priv, image, len);
  2522. if (rc == 0) {
  2523. IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
  2524. return 0;
  2525. }
  2526. /* Try initialize */
  2527. image = (__le32 *)priv->ucode_init.v_addr;
  2528. len = priv->ucode_init.len;
  2529. rc = iwl3945_verify_inst_sparse(priv, image, len);
  2530. if (rc == 0) {
  2531. IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
  2532. return 0;
  2533. }
  2534. /* Try runtime/protocol */
  2535. image = (__le32 *)priv->ucode_code.v_addr;
  2536. len = priv->ucode_code.len;
  2537. rc = iwl3945_verify_inst_sparse(priv, image, len);
  2538. if (rc == 0) {
  2539. IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
  2540. return 0;
  2541. }
  2542. IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  2543. /* Since nothing seems to match, show first several data entries in
  2544. * instruction SRAM, so maybe visual inspection will give a clue.
  2545. * Selection of bootstrap image (vs. other images) is arbitrary. */
  2546. image = (__le32 *)priv->ucode_boot.v_addr;
  2547. len = priv->ucode_boot.len;
  2548. rc = iwl3945_verify_inst_full(priv, image, len);
  2549. return rc;
  2550. }
  2551. static void iwl3945_nic_start(struct iwl_priv *priv)
  2552. {
  2553. /* Remove all resets to allow NIC to operate */
  2554. iwl_write32(priv, CSR_RESET, 0);
  2555. }
  2556. /**
  2557. * iwl3945_read_ucode - Read uCode images from disk file.
  2558. *
  2559. * Copy into buffers for card to fetch via bus-mastering
  2560. */
  2561. static int iwl3945_read_ucode(struct iwl_priv *priv)
  2562. {
  2563. struct iwl_ucode *ucode;
  2564. int ret = -EINVAL, index;
  2565. const struct firmware *ucode_raw;
  2566. /* firmware file name contains uCode/driver compatibility version */
  2567. const char *name_pre = priv->cfg->fw_name_pre;
  2568. const unsigned int api_max = priv->cfg->ucode_api_max;
  2569. const unsigned int api_min = priv->cfg->ucode_api_min;
  2570. char buf[25];
  2571. u8 *src;
  2572. size_t len;
  2573. u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
  2574. /* Ask kernel firmware_class module to get the boot firmware off disk.
  2575. * request_firmware() is synchronous, file is in memory on return. */
  2576. for (index = api_max; index >= api_min; index--) {
  2577. sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
  2578. ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
  2579. if (ret < 0) {
  2580. IWL_ERR(priv, "%s firmware file req failed: %d\n",
  2581. buf, ret);
  2582. if (ret == -ENOENT)
  2583. continue;
  2584. else
  2585. goto error;
  2586. } else {
  2587. if (index < api_max)
  2588. IWL_ERR(priv, "Loaded firmware %s, "
  2589. "which is deprecated. "
  2590. " Please use API v%u instead.\n",
  2591. buf, api_max);
  2592. IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
  2593. buf, ucode_raw->size);
  2594. break;
  2595. }
  2596. }
  2597. if (ret < 0)
  2598. goto error;
  2599. /* Make sure that we got at least our header! */
  2600. if (ucode_raw->size < sizeof(*ucode)) {
  2601. IWL_ERR(priv, "File size way too small!\n");
  2602. ret = -EINVAL;
  2603. goto err_release;
  2604. }
  2605. /* Data from ucode file: header followed by uCode images */
  2606. ucode = (void *)ucode_raw->data;
  2607. priv->ucode_ver = le32_to_cpu(ucode->ver);
  2608. api_ver = IWL_UCODE_API(priv->ucode_ver);
  2609. inst_size = le32_to_cpu(ucode->inst_size);
  2610. data_size = le32_to_cpu(ucode->data_size);
  2611. init_size = le32_to_cpu(ucode->init_size);
  2612. init_data_size = le32_to_cpu(ucode->init_data_size);
  2613. boot_size = le32_to_cpu(ucode->boot_size);
  2614. /* api_ver should match the api version forming part of the
  2615. * firmware filename ... but we don't check for that and only rely
  2616. * on the API version read from firware header from here on forward */
  2617. if (api_ver < api_min || api_ver > api_max) {
  2618. IWL_ERR(priv, "Driver unable to support your firmware API. "
  2619. "Driver supports v%u, firmware is v%u.\n",
  2620. api_max, api_ver);
  2621. priv->ucode_ver = 0;
  2622. ret = -EINVAL;
  2623. goto err_release;
  2624. }
  2625. if (api_ver != api_max)
  2626. IWL_ERR(priv, "Firmware has old API version. Expected %u, "
  2627. "got %u. New firmware can be obtained "
  2628. "from http://www.intellinuxwireless.org.\n",
  2629. api_max, api_ver);
  2630. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
  2631. IWL_UCODE_MAJOR(priv->ucode_ver),
  2632. IWL_UCODE_MINOR(priv->ucode_ver),
  2633. IWL_UCODE_API(priv->ucode_ver),
  2634. IWL_UCODE_SERIAL(priv->ucode_ver));
  2635. IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
  2636. priv->ucode_ver);
  2637. IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
  2638. IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
  2639. IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
  2640. IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
  2641. IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
  2642. /* Verify size of file vs. image size info in file's header */
  2643. if (ucode_raw->size < sizeof(*ucode) +
  2644. inst_size + data_size + init_size +
  2645. init_data_size + boot_size) {
  2646. IWL_DEBUG_INFO("uCode file size %d too small\n",
  2647. (int)ucode_raw->size);
  2648. ret = -EINVAL;
  2649. goto err_release;
  2650. }
  2651. /* Verify that uCode images will fit in card's SRAM */
  2652. if (inst_size > IWL39_MAX_INST_SIZE) {
  2653. IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
  2654. inst_size);
  2655. ret = -EINVAL;
  2656. goto err_release;
  2657. }
  2658. if (data_size > IWL39_MAX_DATA_SIZE) {
  2659. IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
  2660. data_size);
  2661. ret = -EINVAL;
  2662. goto err_release;
  2663. }
  2664. if (init_size > IWL39_MAX_INST_SIZE) {
  2665. IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
  2666. init_size);
  2667. ret = -EINVAL;
  2668. goto err_release;
  2669. }
  2670. if (init_data_size > IWL39_MAX_DATA_SIZE) {
  2671. IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
  2672. init_data_size);
  2673. ret = -EINVAL;
  2674. goto err_release;
  2675. }
  2676. if (boot_size > IWL39_MAX_BSM_SIZE) {
  2677. IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
  2678. boot_size);
  2679. ret = -EINVAL;
  2680. goto err_release;
  2681. }
  2682. /* Allocate ucode buffers for card's bus-master loading ... */
  2683. /* Runtime instructions and 2 copies of data:
  2684. * 1) unmodified from disk
  2685. * 2) backup cache for save/restore during power-downs */
  2686. priv->ucode_code.len = inst_size;
  2687. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
  2688. priv->ucode_data.len = data_size;
  2689. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
  2690. priv->ucode_data_backup.len = data_size;
  2691. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  2692. if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
  2693. !priv->ucode_data_backup.v_addr)
  2694. goto err_pci_alloc;
  2695. /* Initialization instructions and data */
  2696. if (init_size && init_data_size) {
  2697. priv->ucode_init.len = init_size;
  2698. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
  2699. priv->ucode_init_data.len = init_data_size;
  2700. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  2701. if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
  2702. goto err_pci_alloc;
  2703. }
  2704. /* Bootstrap (instructions only, no data) */
  2705. if (boot_size) {
  2706. priv->ucode_boot.len = boot_size;
  2707. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
  2708. if (!priv->ucode_boot.v_addr)
  2709. goto err_pci_alloc;
  2710. }
  2711. /* Copy images into buffers for card's bus-master reads ... */
  2712. /* Runtime instructions (first block of data in file) */
  2713. src = &ucode->data[0];
  2714. len = priv->ucode_code.len;
  2715. IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
  2716. memcpy(priv->ucode_code.v_addr, src, len);
  2717. IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  2718. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  2719. /* Runtime data (2nd block)
  2720. * NOTE: Copy into backup buffer will be done in iwl3945_up() */
  2721. src = &ucode->data[inst_size];
  2722. len = priv->ucode_data.len;
  2723. IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
  2724. memcpy(priv->ucode_data.v_addr, src, len);
  2725. memcpy(priv->ucode_data_backup.v_addr, src, len);
  2726. /* Initialization instructions (3rd block) */
  2727. if (init_size) {
  2728. src = &ucode->data[inst_size + data_size];
  2729. len = priv->ucode_init.len;
  2730. IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
  2731. len);
  2732. memcpy(priv->ucode_init.v_addr, src, len);
  2733. }
  2734. /* Initialization data (4th block) */
  2735. if (init_data_size) {
  2736. src = &ucode->data[inst_size + data_size + init_size];
  2737. len = priv->ucode_init_data.len;
  2738. IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
  2739. (int)len);
  2740. memcpy(priv->ucode_init_data.v_addr, src, len);
  2741. }
  2742. /* Bootstrap instructions (5th block) */
  2743. src = &ucode->data[inst_size + data_size + init_size + init_data_size];
  2744. len = priv->ucode_boot.len;
  2745. IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
  2746. (int)len);
  2747. memcpy(priv->ucode_boot.v_addr, src, len);
  2748. /* We have our copies now, allow OS release its copies */
  2749. release_firmware(ucode_raw);
  2750. return 0;
  2751. err_pci_alloc:
  2752. IWL_ERR(priv, "failed to allocate pci memory\n");
  2753. ret = -ENOMEM;
  2754. iwl3945_dealloc_ucode_pci(priv);
  2755. err_release:
  2756. release_firmware(ucode_raw);
  2757. error:
  2758. return ret;
  2759. }
  2760. /**
  2761. * iwl3945_set_ucode_ptrs - Set uCode address location
  2762. *
  2763. * Tell initialization uCode where to find runtime uCode.
  2764. *
  2765. * BSM registers initially contain pointers to initialization uCode.
  2766. * We need to replace them to load runtime uCode inst and data,
  2767. * and to save runtime data when powering down.
  2768. */
  2769. static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
  2770. {
  2771. dma_addr_t pinst;
  2772. dma_addr_t pdata;
  2773. int rc = 0;
  2774. unsigned long flags;
  2775. /* bits 31:0 for 3945 */
  2776. pinst = priv->ucode_code.p_addr;
  2777. pdata = priv->ucode_data_backup.p_addr;
  2778. spin_lock_irqsave(&priv->lock, flags);
  2779. rc = iwl_grab_nic_access(priv);
  2780. if (rc) {
  2781. spin_unlock_irqrestore(&priv->lock, flags);
  2782. return rc;
  2783. }
  2784. /* Tell bootstrap uCode where to find image to load */
  2785. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  2786. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  2787. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  2788. priv->ucode_data.len);
  2789. /* Inst byte count must be last to set up, bit 31 signals uCode
  2790. * that all new ptr/size info is in place */
  2791. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  2792. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  2793. iwl_release_nic_access(priv);
  2794. spin_unlock_irqrestore(&priv->lock, flags);
  2795. IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
  2796. return rc;
  2797. }
  2798. /**
  2799. * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
  2800. *
  2801. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  2802. *
  2803. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  2804. */
  2805. static void iwl3945_init_alive_start(struct iwl_priv *priv)
  2806. {
  2807. /* Check alive response for "valid" sign from uCode */
  2808. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  2809. /* We had an error bringing up the hardware, so take it
  2810. * all the way back down so we can try again */
  2811. IWL_DEBUG_INFO("Initialize Alive failed.\n");
  2812. goto restart;
  2813. }
  2814. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  2815. * This is a paranoid check, because we would not have gotten the
  2816. * "initialize" alive if code weren't properly loaded. */
  2817. if (iwl3945_verify_ucode(priv)) {
  2818. /* Runtime instruction load was bad;
  2819. * take it all the way back down so we can try again */
  2820. IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
  2821. goto restart;
  2822. }
  2823. /* Send pointers to protocol/runtime uCode image ... init code will
  2824. * load and launch runtime uCode, which will send us another "Alive"
  2825. * notification. */
  2826. IWL_DEBUG_INFO("Initialization Alive received.\n");
  2827. if (iwl3945_set_ucode_ptrs(priv)) {
  2828. /* Runtime instruction load won't happen;
  2829. * take it all the way back down so we can try again */
  2830. IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
  2831. goto restart;
  2832. }
  2833. return;
  2834. restart:
  2835. queue_work(priv->workqueue, &priv->restart);
  2836. }
  2837. /* temporary */
  2838. static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
  2839. struct sk_buff *skb);
  2840. /**
  2841. * iwl3945_alive_start - called after REPLY_ALIVE notification received
  2842. * from protocol/runtime uCode (initialization uCode's
  2843. * Alive gets handled by iwl3945_init_alive_start()).
  2844. */
  2845. static void iwl3945_alive_start(struct iwl_priv *priv)
  2846. {
  2847. int rc = 0;
  2848. int thermal_spin = 0;
  2849. u32 rfkill;
  2850. IWL_DEBUG_INFO("Runtime Alive received.\n");
  2851. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  2852. /* We had an error bringing up the hardware, so take it
  2853. * all the way back down so we can try again */
  2854. IWL_DEBUG_INFO("Alive failed.\n");
  2855. goto restart;
  2856. }
  2857. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  2858. * This is a paranoid check, because we would not have gotten the
  2859. * "runtime" alive if code weren't properly loaded. */
  2860. if (iwl3945_verify_ucode(priv)) {
  2861. /* Runtime instruction load was bad;
  2862. * take it all the way back down so we can try again */
  2863. IWL_DEBUG_INFO("Bad runtime uCode load.\n");
  2864. goto restart;
  2865. }
  2866. iwl3945_clear_stations_table(priv);
  2867. rc = iwl_grab_nic_access(priv);
  2868. if (rc) {
  2869. IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
  2870. return;
  2871. }
  2872. rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
  2873. IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
  2874. iwl_release_nic_access(priv);
  2875. if (rfkill & 0x1) {
  2876. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2877. /* if RFKILL is not on, then wait for thermal
  2878. * sensor in adapter to kick in */
  2879. while (iwl3945_hw_get_temperature(priv) == 0) {
  2880. thermal_spin++;
  2881. udelay(10);
  2882. }
  2883. if (thermal_spin)
  2884. IWL_DEBUG_INFO("Thermal calibration took %dus\n",
  2885. thermal_spin * 10);
  2886. } else
  2887. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2888. /* After the ALIVE response, we can send commands to 3945 uCode */
  2889. set_bit(STATUS_ALIVE, &priv->status);
  2890. /* Clear out the uCode error bit if it is set */
  2891. clear_bit(STATUS_FW_ERROR, &priv->status);
  2892. if (iwl_is_rfkill(priv))
  2893. return;
  2894. ieee80211_wake_queues(priv->hw);
  2895. priv->active_rate = priv->rates_mask;
  2896. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  2897. iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
  2898. if (iwl_is_associated(priv)) {
  2899. struct iwl3945_rxon_cmd *active_rxon =
  2900. (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
  2901. memcpy(&priv->staging_rxon, &priv->active_rxon,
  2902. sizeof(priv->staging_rxon));
  2903. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2904. } else {
  2905. /* Initialize our rx_config data */
  2906. iwl_connection_init_rx_config(priv, priv->iw_mode);
  2907. }
  2908. /* Configure Bluetooth device coexistence support */
  2909. iwl_send_bt_config(priv);
  2910. /* Configure the adapter for unassociated operation */
  2911. iwl3945_commit_rxon(priv);
  2912. iwl3945_reg_txpower_periodic(priv);
  2913. iwl3945_led_register(priv);
  2914. IWL_DEBUG_INFO("ALIVE processing complete.\n");
  2915. set_bit(STATUS_READY, &priv->status);
  2916. wake_up_interruptible(&priv->wait_command_queue);
  2917. if (priv->error_recovering)
  2918. iwl3945_error_recovery(priv);
  2919. /* reassociate for ADHOC mode */
  2920. if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
  2921. struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
  2922. priv->vif);
  2923. if (beacon)
  2924. iwl3945_mac_beacon_update(priv->hw, beacon);
  2925. }
  2926. return;
  2927. restart:
  2928. queue_work(priv->workqueue, &priv->restart);
  2929. }
  2930. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
  2931. static void __iwl3945_down(struct iwl_priv *priv)
  2932. {
  2933. unsigned long flags;
  2934. int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
  2935. struct ieee80211_conf *conf = NULL;
  2936. IWL_DEBUG_INFO(DRV_NAME " is going down\n");
  2937. conf = ieee80211_get_hw_conf(priv->hw);
  2938. if (!exit_pending)
  2939. set_bit(STATUS_EXIT_PENDING, &priv->status);
  2940. iwl3945_led_unregister(priv);
  2941. iwl3945_clear_stations_table(priv);
  2942. /* Unblock any waiting calls */
  2943. wake_up_interruptible_all(&priv->wait_command_queue);
  2944. /* Wipe out the EXIT_PENDING status bit if we are not actually
  2945. * exiting the module */
  2946. if (!exit_pending)
  2947. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  2948. /* stop and reset the on-board processor */
  2949. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  2950. /* tell the device to stop sending interrupts */
  2951. spin_lock_irqsave(&priv->lock, flags);
  2952. iwl3945_disable_interrupts(priv);
  2953. spin_unlock_irqrestore(&priv->lock, flags);
  2954. iwl_synchronize_irq(priv);
  2955. if (priv->mac80211_registered)
  2956. ieee80211_stop_queues(priv->hw);
  2957. /* If we have not previously called iwl3945_init() then
  2958. * clear all bits but the RF Kill and SUSPEND bits and return */
  2959. if (!iwl_is_init(priv)) {
  2960. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  2961. STATUS_RF_KILL_HW |
  2962. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  2963. STATUS_RF_KILL_SW |
  2964. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  2965. STATUS_GEO_CONFIGURED |
  2966. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  2967. STATUS_IN_SUSPEND |
  2968. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  2969. STATUS_EXIT_PENDING;
  2970. goto exit;
  2971. }
  2972. /* ...otherwise clear out all the status bits but the RF Kill and
  2973. * SUSPEND bits and continue taking the NIC down. */
  2974. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  2975. STATUS_RF_KILL_HW |
  2976. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  2977. STATUS_RF_KILL_SW |
  2978. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  2979. STATUS_GEO_CONFIGURED |
  2980. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  2981. STATUS_IN_SUSPEND |
  2982. test_bit(STATUS_FW_ERROR, &priv->status) <<
  2983. STATUS_FW_ERROR |
  2984. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  2985. STATUS_EXIT_PENDING;
  2986. priv->cfg->ops->lib->apm_ops.reset(priv);
  2987. spin_lock_irqsave(&priv->lock, flags);
  2988. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2989. spin_unlock_irqrestore(&priv->lock, flags);
  2990. iwl3945_hw_txq_ctx_stop(priv);
  2991. iwl3945_hw_rxq_stop(priv);
  2992. spin_lock_irqsave(&priv->lock, flags);
  2993. if (!iwl_grab_nic_access(priv)) {
  2994. iwl_write_prph(priv, APMG_CLK_DIS_REG,
  2995. APMG_CLK_VAL_DMA_CLK_RQT);
  2996. iwl_release_nic_access(priv);
  2997. }
  2998. spin_unlock_irqrestore(&priv->lock, flags);
  2999. udelay(5);
  3000. if (exit_pending || test_bit(STATUS_IN_SUSPEND, &priv->status))
  3001. priv->cfg->ops->lib->apm_ops.stop(priv);
  3002. else
  3003. priv->cfg->ops->lib->apm_ops.reset(priv);
  3004. exit:
  3005. memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
  3006. if (priv->ibss_beacon)
  3007. dev_kfree_skb(priv->ibss_beacon);
  3008. priv->ibss_beacon = NULL;
  3009. /* clear out any free frames */
  3010. iwl3945_clear_free_frames(priv);
  3011. }
  3012. static void iwl3945_down(struct iwl_priv *priv)
  3013. {
  3014. mutex_lock(&priv->mutex);
  3015. __iwl3945_down(priv);
  3016. mutex_unlock(&priv->mutex);
  3017. iwl3945_cancel_deferred_work(priv);
  3018. }
  3019. #define MAX_HW_RESTARTS 5
  3020. static int __iwl3945_up(struct iwl_priv *priv)
  3021. {
  3022. int rc, i;
  3023. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  3024. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  3025. return -EIO;
  3026. }
  3027. if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  3028. IWL_WARN(priv, "Radio disabled by SW RF kill (module "
  3029. "parameter)\n");
  3030. return -ENODEV;
  3031. }
  3032. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  3033. IWL_ERR(priv, "ucode not available for device bring up\n");
  3034. return -EIO;
  3035. }
  3036. /* If platform's RF_KILL switch is NOT set to KILL */
  3037. if (iwl_read32(priv, CSR_GP_CNTRL) &
  3038. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  3039. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  3040. else {
  3041. set_bit(STATUS_RF_KILL_HW, &priv->status);
  3042. if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
  3043. IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
  3044. return -ENODEV;
  3045. }
  3046. }
  3047. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  3048. rc = iwl3945_hw_nic_init(priv);
  3049. if (rc) {
  3050. IWL_ERR(priv, "Unable to int nic\n");
  3051. return rc;
  3052. }
  3053. /* make sure rfkill handshake bits are cleared */
  3054. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  3055. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  3056. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  3057. /* clear (again), then enable host interrupts */
  3058. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  3059. iwl3945_enable_interrupts(priv);
  3060. /* really make sure rfkill handshake bits are cleared */
  3061. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  3062. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  3063. /* Copy original ucode data image from disk into backup cache.
  3064. * This will be used to initialize the on-board processor's
  3065. * data SRAM for a clean start when the runtime program first loads. */
  3066. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  3067. priv->ucode_data.len);
  3068. /* We return success when we resume from suspend and rf_kill is on. */
  3069. if (test_bit(STATUS_RF_KILL_HW, &priv->status))
  3070. return 0;
  3071. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  3072. iwl3945_clear_stations_table(priv);
  3073. /* load bootstrap state machine,
  3074. * load bootstrap program into processor's memory,
  3075. * prepare to load the "initialize" uCode */
  3076. priv->cfg->ops->lib->load_ucode(priv);
  3077. if (rc) {
  3078. IWL_ERR(priv,
  3079. "Unable to set up bootstrap uCode: %d\n", rc);
  3080. continue;
  3081. }
  3082. /* start card; "initialize" will load runtime ucode */
  3083. iwl3945_nic_start(priv);
  3084. IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
  3085. return 0;
  3086. }
  3087. set_bit(STATUS_EXIT_PENDING, &priv->status);
  3088. __iwl3945_down(priv);
  3089. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  3090. /* tried to restart and config the device for as long as our
  3091. * patience could withstand */
  3092. IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
  3093. return -EIO;
  3094. }
  3095. /*****************************************************************************
  3096. *
  3097. * Workqueue callbacks
  3098. *
  3099. *****************************************************************************/
  3100. static void iwl3945_bg_init_alive_start(struct work_struct *data)
  3101. {
  3102. struct iwl_priv *priv =
  3103. container_of(data, struct iwl_priv, init_alive_start.work);
  3104. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  3105. return;
  3106. mutex_lock(&priv->mutex);
  3107. iwl3945_init_alive_start(priv);
  3108. mutex_unlock(&priv->mutex);
  3109. }
  3110. static void iwl3945_bg_alive_start(struct work_struct *data)
  3111. {
  3112. struct iwl_priv *priv =
  3113. container_of(data, struct iwl_priv, alive_start.work);
  3114. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  3115. return;
  3116. mutex_lock(&priv->mutex);
  3117. iwl3945_alive_start(priv);
  3118. mutex_unlock(&priv->mutex);
  3119. }
  3120. static void iwl3945_rfkill_poll(struct work_struct *data)
  3121. {
  3122. struct iwl_priv *priv =
  3123. container_of(data, struct iwl_priv, rfkill_poll.work);
  3124. unsigned long status = priv->status;
  3125. if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  3126. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  3127. else
  3128. set_bit(STATUS_RF_KILL_HW, &priv->status);
  3129. if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
  3130. queue_work(priv->workqueue, &priv->rf_kill);
  3131. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  3132. round_jiffies_relative(2 * HZ));
  3133. }
  3134. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  3135. static void iwl3945_bg_request_scan(struct work_struct *data)
  3136. {
  3137. struct iwl_priv *priv =
  3138. container_of(data, struct iwl_priv, request_scan);
  3139. struct iwl_host_cmd cmd = {
  3140. .id = REPLY_SCAN_CMD,
  3141. .len = sizeof(struct iwl3945_scan_cmd),
  3142. .meta.flags = CMD_SIZE_HUGE,
  3143. };
  3144. int rc = 0;
  3145. struct iwl3945_scan_cmd *scan;
  3146. struct ieee80211_conf *conf = NULL;
  3147. u8 n_probes = 2;
  3148. enum ieee80211_band band;
  3149. DECLARE_SSID_BUF(ssid);
  3150. conf = ieee80211_get_hw_conf(priv->hw);
  3151. mutex_lock(&priv->mutex);
  3152. if (!iwl_is_ready(priv)) {
  3153. IWL_WARN(priv, "request scan called when driver not ready.\n");
  3154. goto done;
  3155. }
  3156. /* Make sure the scan wasn't canceled before this queued work
  3157. * was given the chance to run... */
  3158. if (!test_bit(STATUS_SCANNING, &priv->status))
  3159. goto done;
  3160. /* This should never be called or scheduled if there is currently
  3161. * a scan active in the hardware. */
  3162. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  3163. IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
  3164. "Ignoring second request.\n");
  3165. rc = -EIO;
  3166. goto done;
  3167. }
  3168. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  3169. IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
  3170. goto done;
  3171. }
  3172. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  3173. IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
  3174. goto done;
  3175. }
  3176. if (iwl_is_rfkill(priv)) {
  3177. IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
  3178. goto done;
  3179. }
  3180. if (!test_bit(STATUS_READY, &priv->status)) {
  3181. IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
  3182. goto done;
  3183. }
  3184. if (!priv->scan_bands) {
  3185. IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
  3186. goto done;
  3187. }
  3188. if (!priv->scan) {
  3189. priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
  3190. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  3191. if (!priv->scan) {
  3192. rc = -ENOMEM;
  3193. goto done;
  3194. }
  3195. }
  3196. scan = priv->scan;
  3197. memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
  3198. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  3199. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  3200. if (iwl_is_associated(priv)) {
  3201. u16 interval = 0;
  3202. u32 extra;
  3203. u32 suspend_time = 100;
  3204. u32 scan_suspend_time = 100;
  3205. unsigned long flags;
  3206. IWL_DEBUG_INFO("Scanning while associated...\n");
  3207. spin_lock_irqsave(&priv->lock, flags);
  3208. interval = priv->beacon_int;
  3209. spin_unlock_irqrestore(&priv->lock, flags);
  3210. scan->suspend_time = 0;
  3211. scan->max_out_time = cpu_to_le32(200 * 1024);
  3212. if (!interval)
  3213. interval = suspend_time;
  3214. /*
  3215. * suspend time format:
  3216. * 0-19: beacon interval in usec (time before exec.)
  3217. * 20-23: 0
  3218. * 24-31: number of beacons (suspend between channels)
  3219. */
  3220. extra = (suspend_time / interval) << 24;
  3221. scan_suspend_time = 0xFF0FFFFF &
  3222. (extra | ((suspend_time % interval) * 1024));
  3223. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  3224. IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
  3225. scan_suspend_time, interval);
  3226. }
  3227. /* We should add the ability for user to lock to PASSIVE ONLY */
  3228. if (priv->one_direct_scan) {
  3229. IWL_DEBUG_SCAN
  3230. ("Kicking off one direct scan for '%s'\n",
  3231. print_ssid(ssid, priv->direct_ssid,
  3232. priv->direct_ssid_len));
  3233. scan->direct_scan[0].id = WLAN_EID_SSID;
  3234. scan->direct_scan[0].len = priv->direct_ssid_len;
  3235. memcpy(scan->direct_scan[0].ssid,
  3236. priv->direct_ssid, priv->direct_ssid_len);
  3237. n_probes++;
  3238. } else
  3239. IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
  3240. /* We don't build a direct scan probe request; the uCode will do
  3241. * that based on the direct_mask added to each channel entry */
  3242. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  3243. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  3244. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  3245. /* flags + rate selection */
  3246. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
  3247. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  3248. scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
  3249. scan->good_CRC_th = 0;
  3250. band = IEEE80211_BAND_2GHZ;
  3251. } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
  3252. scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
  3253. scan->good_CRC_th = IWL_GOOD_CRC_TH;
  3254. band = IEEE80211_BAND_5GHZ;
  3255. } else {
  3256. IWL_WARN(priv, "Invalid scan band count\n");
  3257. goto done;
  3258. }
  3259. scan->tx_cmd.len = cpu_to_le16(
  3260. iwl_fill_probe_req(priv, band,
  3261. (struct ieee80211_mgmt *)scan->data,
  3262. IWL_MAX_SCAN_SIZE - sizeof(*scan)));
  3263. /* select Rx antennas */
  3264. scan->flags |= iwl3945_get_antenna_flags(priv);
  3265. if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
  3266. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  3267. scan->channel_count =
  3268. iwl3945_get_channels_for_scan(priv, band, 1, /* active */
  3269. n_probes,
  3270. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  3271. if (scan->channel_count == 0) {
  3272. IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
  3273. goto done;
  3274. }
  3275. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  3276. scan->channel_count * sizeof(struct iwl3945_scan_channel);
  3277. cmd.data = scan;
  3278. scan->len = cpu_to_le16(cmd.len);
  3279. set_bit(STATUS_SCAN_HW, &priv->status);
  3280. rc = iwl_send_cmd_sync(priv, &cmd);
  3281. if (rc)
  3282. goto done;
  3283. queue_delayed_work(priv->workqueue, &priv->scan_check,
  3284. IWL_SCAN_CHECK_WATCHDOG);
  3285. mutex_unlock(&priv->mutex);
  3286. return;
  3287. done:
  3288. /* can not perform scan make sure we clear scanning
  3289. * bits from status so next scan request can be performed.
  3290. * if we dont clear scanning status bit here all next scan
  3291. * will fail
  3292. */
  3293. clear_bit(STATUS_SCAN_HW, &priv->status);
  3294. clear_bit(STATUS_SCANNING, &priv->status);
  3295. /* inform mac80211 scan aborted */
  3296. queue_work(priv->workqueue, &priv->scan_completed);
  3297. mutex_unlock(&priv->mutex);
  3298. }
  3299. static void iwl3945_bg_up(struct work_struct *data)
  3300. {
  3301. struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
  3302. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  3303. return;
  3304. mutex_lock(&priv->mutex);
  3305. __iwl3945_up(priv);
  3306. mutex_unlock(&priv->mutex);
  3307. iwl_rfkill_set_hw_state(priv);
  3308. }
  3309. static void iwl3945_bg_restart(struct work_struct *data)
  3310. {
  3311. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  3312. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  3313. return;
  3314. iwl3945_down(priv);
  3315. queue_work(priv->workqueue, &priv->up);
  3316. }
  3317. static void iwl3945_bg_rx_replenish(struct work_struct *data)
  3318. {
  3319. struct iwl_priv *priv =
  3320. container_of(data, struct iwl_priv, rx_replenish);
  3321. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  3322. return;
  3323. mutex_lock(&priv->mutex);
  3324. iwl3945_rx_replenish(priv);
  3325. mutex_unlock(&priv->mutex);
  3326. }
  3327. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  3328. static void iwl3945_post_associate(struct iwl_priv *priv)
  3329. {
  3330. int rc = 0;
  3331. struct ieee80211_conf *conf = NULL;
  3332. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  3333. IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
  3334. return;
  3335. }
  3336. IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
  3337. priv->assoc_id, priv->active_rxon.bssid_addr);
  3338. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  3339. return;
  3340. if (!priv->vif || !priv->is_open)
  3341. return;
  3342. iwl_scan_cancel_timeout(priv, 200);
  3343. conf = ieee80211_get_hw_conf(priv->hw);
  3344. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3345. iwl3945_commit_rxon(priv);
  3346. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  3347. iwl3945_setup_rxon_timing(priv);
  3348. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  3349. sizeof(priv->rxon_timing), &priv->rxon_timing);
  3350. if (rc)
  3351. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  3352. "Attempting to continue.\n");
  3353. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  3354. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  3355. IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
  3356. priv->assoc_id, priv->beacon_int);
  3357. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  3358. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  3359. else
  3360. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  3361. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  3362. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  3363. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  3364. else
  3365. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  3366. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  3367. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  3368. }
  3369. iwl3945_commit_rxon(priv);
  3370. switch (priv->iw_mode) {
  3371. case NL80211_IFTYPE_STATION:
  3372. iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
  3373. break;
  3374. case NL80211_IFTYPE_ADHOC:
  3375. priv->assoc_id = 1;
  3376. iwl3945_add_station(priv, priv->bssid, 0, 0);
  3377. iwl3945_sync_sta(priv, IWL_STA_ID,
  3378. (priv->band == IEEE80211_BAND_5GHZ) ?
  3379. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
  3380. CMD_ASYNC);
  3381. iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
  3382. iwl3945_send_beacon_cmd(priv);
  3383. break;
  3384. default:
  3385. IWL_ERR(priv, "%s Should not be called in %d mode\n",
  3386. __func__, priv->iw_mode);
  3387. break;
  3388. }
  3389. iwl3945_activate_qos(priv, 0);
  3390. /* we have just associated, don't start scan too early */
  3391. priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
  3392. }
  3393. static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
  3394. /*****************************************************************************
  3395. *
  3396. * mac80211 entry point functions
  3397. *
  3398. *****************************************************************************/
  3399. #define UCODE_READY_TIMEOUT (2 * HZ)
  3400. static int iwl3945_mac_start(struct ieee80211_hw *hw)
  3401. {
  3402. struct iwl_priv *priv = hw->priv;
  3403. int ret;
  3404. IWL_DEBUG_MAC80211("enter\n");
  3405. /* we should be verifying the device is ready to be opened */
  3406. mutex_lock(&priv->mutex);
  3407. memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
  3408. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  3409. * ucode filename and max sizes are card-specific. */
  3410. if (!priv->ucode_code.len) {
  3411. ret = iwl3945_read_ucode(priv);
  3412. if (ret) {
  3413. IWL_ERR(priv, "Could not read microcode: %d\n", ret);
  3414. mutex_unlock(&priv->mutex);
  3415. goto out_release_irq;
  3416. }
  3417. }
  3418. ret = __iwl3945_up(priv);
  3419. mutex_unlock(&priv->mutex);
  3420. iwl_rfkill_set_hw_state(priv);
  3421. if (ret)
  3422. goto out_release_irq;
  3423. IWL_DEBUG_INFO("Start UP work.\n");
  3424. if (test_bit(STATUS_IN_SUSPEND, &priv->status))
  3425. return 0;
  3426. /* Wait for START_ALIVE from ucode. Otherwise callbacks from
  3427. * mac80211 will not be run successfully. */
  3428. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  3429. test_bit(STATUS_READY, &priv->status),
  3430. UCODE_READY_TIMEOUT);
  3431. if (!ret) {
  3432. if (!test_bit(STATUS_READY, &priv->status)) {
  3433. IWL_ERR(priv,
  3434. "Wait for START_ALIVE timeout after %dms.\n",
  3435. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  3436. ret = -ETIMEDOUT;
  3437. goto out_release_irq;
  3438. }
  3439. }
  3440. /* ucode is running and will send rfkill notifications,
  3441. * no need to poll the killswitch state anymore */
  3442. cancel_delayed_work(&priv->rfkill_poll);
  3443. priv->is_open = 1;
  3444. IWL_DEBUG_MAC80211("leave\n");
  3445. return 0;
  3446. out_release_irq:
  3447. priv->is_open = 0;
  3448. IWL_DEBUG_MAC80211("leave - failed\n");
  3449. return ret;
  3450. }
  3451. static void iwl3945_mac_stop(struct ieee80211_hw *hw)
  3452. {
  3453. struct iwl_priv *priv = hw->priv;
  3454. IWL_DEBUG_MAC80211("enter\n");
  3455. if (!priv->is_open) {
  3456. IWL_DEBUG_MAC80211("leave - skip\n");
  3457. return;
  3458. }
  3459. priv->is_open = 0;
  3460. if (iwl_is_ready_rf(priv)) {
  3461. /* stop mac, cancel any scan request and clear
  3462. * RXON_FILTER_ASSOC_MSK BIT
  3463. */
  3464. mutex_lock(&priv->mutex);
  3465. iwl_scan_cancel_timeout(priv, 100);
  3466. mutex_unlock(&priv->mutex);
  3467. }
  3468. iwl3945_down(priv);
  3469. flush_workqueue(priv->workqueue);
  3470. /* start polling the killswitch state again */
  3471. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  3472. round_jiffies_relative(2 * HZ));
  3473. IWL_DEBUG_MAC80211("leave\n");
  3474. }
  3475. static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  3476. {
  3477. struct iwl_priv *priv = hw->priv;
  3478. IWL_DEBUG_MAC80211("enter\n");
  3479. IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  3480. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  3481. if (iwl3945_tx_skb(priv, skb))
  3482. dev_kfree_skb_any(skb);
  3483. IWL_DEBUG_MAC80211("leave\n");
  3484. return NETDEV_TX_OK;
  3485. }
  3486. static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
  3487. struct ieee80211_if_init_conf *conf)
  3488. {
  3489. struct iwl_priv *priv = hw->priv;
  3490. unsigned long flags;
  3491. IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
  3492. if (priv->vif) {
  3493. IWL_DEBUG_MAC80211("leave - vif != NULL\n");
  3494. return -EOPNOTSUPP;
  3495. }
  3496. spin_lock_irqsave(&priv->lock, flags);
  3497. priv->vif = conf->vif;
  3498. priv->iw_mode = conf->type;
  3499. spin_unlock_irqrestore(&priv->lock, flags);
  3500. mutex_lock(&priv->mutex);
  3501. if (conf->mac_addr) {
  3502. IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
  3503. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  3504. }
  3505. if (iwl_is_ready(priv))
  3506. iwl3945_set_mode(priv, conf->type);
  3507. mutex_unlock(&priv->mutex);
  3508. IWL_DEBUG_MAC80211("leave\n");
  3509. return 0;
  3510. }
  3511. /**
  3512. * iwl3945_mac_config - mac80211 config callback
  3513. *
  3514. * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
  3515. * be set inappropriately and the driver currently sets the hardware up to
  3516. * use it whenever needed.
  3517. */
  3518. static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
  3519. {
  3520. struct iwl_priv *priv = hw->priv;
  3521. const struct iwl_channel_info *ch_info;
  3522. struct ieee80211_conf *conf = &hw->conf;
  3523. unsigned long flags;
  3524. int ret = 0;
  3525. mutex_lock(&priv->mutex);
  3526. IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
  3527. if (!iwl_is_ready(priv)) {
  3528. IWL_DEBUG_MAC80211("leave - not ready\n");
  3529. ret = -EIO;
  3530. goto out;
  3531. }
  3532. if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
  3533. test_bit(STATUS_SCANNING, &priv->status))) {
  3534. IWL_DEBUG_MAC80211("leave - scanning\n");
  3535. set_bit(STATUS_CONF_PENDING, &priv->status);
  3536. mutex_unlock(&priv->mutex);
  3537. return 0;
  3538. }
  3539. spin_lock_irqsave(&priv->lock, flags);
  3540. ch_info = iwl_get_channel_info(priv, conf->channel->band,
  3541. conf->channel->hw_value);
  3542. if (!is_channel_valid(ch_info)) {
  3543. IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
  3544. conf->channel->hw_value, conf->channel->band);
  3545. IWL_DEBUG_MAC80211("leave - invalid channel\n");
  3546. spin_unlock_irqrestore(&priv->lock, flags);
  3547. ret = -EINVAL;
  3548. goto out;
  3549. }
  3550. iwl_set_rxon_channel(priv, conf->channel);
  3551. iwl_set_flags_for_band(priv, conf->channel->band);
  3552. /* The list of supported rates and rate mask can be different
  3553. * for each phymode; since the phymode may have changed, reset
  3554. * the rate mask to what mac80211 lists */
  3555. iwl_set_rate(priv);
  3556. spin_unlock_irqrestore(&priv->lock, flags);
  3557. #ifdef IEEE80211_CONF_CHANNEL_SWITCH
  3558. if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
  3559. iwl3945_hw_channel_switch(priv, conf->channel);
  3560. goto out;
  3561. }
  3562. #endif
  3563. iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
  3564. if (!conf->radio_enabled) {
  3565. IWL_DEBUG_MAC80211("leave - radio disabled\n");
  3566. goto out;
  3567. }
  3568. if (iwl_is_rfkill(priv)) {
  3569. IWL_DEBUG_MAC80211("leave - RF kill\n");
  3570. ret = -EIO;
  3571. goto out;
  3572. }
  3573. iwl_set_rate(priv);
  3574. if (memcmp(&priv->active_rxon,
  3575. &priv->staging_rxon, sizeof(priv->staging_rxon)))
  3576. iwl3945_commit_rxon(priv);
  3577. else
  3578. IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
  3579. IWL_DEBUG_MAC80211("leave\n");
  3580. out:
  3581. clear_bit(STATUS_CONF_PENDING, &priv->status);
  3582. mutex_unlock(&priv->mutex);
  3583. return ret;
  3584. }
  3585. static void iwl3945_config_ap(struct iwl_priv *priv)
  3586. {
  3587. int rc = 0;
  3588. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  3589. return;
  3590. /* The following should be done only at AP bring up */
  3591. if (!(iwl_is_associated(priv))) {
  3592. /* RXON - unassoc (to set timing command) */
  3593. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3594. iwl3945_commit_rxon(priv);
  3595. /* RXON Timing */
  3596. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  3597. iwl3945_setup_rxon_timing(priv);
  3598. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  3599. sizeof(priv->rxon_timing),
  3600. &priv->rxon_timing);
  3601. if (rc)
  3602. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  3603. "Attempting to continue.\n");
  3604. /* FIXME: what should be the assoc_id for AP? */
  3605. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  3606. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  3607. priv->staging_rxon.flags |=
  3608. RXON_FLG_SHORT_PREAMBLE_MSK;
  3609. else
  3610. priv->staging_rxon.flags &=
  3611. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  3612. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  3613. if (priv->assoc_capability &
  3614. WLAN_CAPABILITY_SHORT_SLOT_TIME)
  3615. priv->staging_rxon.flags |=
  3616. RXON_FLG_SHORT_SLOT_MSK;
  3617. else
  3618. priv->staging_rxon.flags &=
  3619. ~RXON_FLG_SHORT_SLOT_MSK;
  3620. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  3621. priv->staging_rxon.flags &=
  3622. ~RXON_FLG_SHORT_SLOT_MSK;
  3623. }
  3624. /* restore RXON assoc */
  3625. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  3626. iwl3945_commit_rxon(priv);
  3627. iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
  3628. }
  3629. iwl3945_send_beacon_cmd(priv);
  3630. /* FIXME - we need to add code here to detect a totally new
  3631. * configuration, reset the AP, unassoc, rxon timing, assoc,
  3632. * clear sta table, add BCAST sta... */
  3633. }
  3634. static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
  3635. struct ieee80211_vif *vif,
  3636. struct ieee80211_if_conf *conf)
  3637. {
  3638. struct iwl_priv *priv = hw->priv;
  3639. int rc;
  3640. if (conf == NULL)
  3641. return -EIO;
  3642. if (priv->vif != vif) {
  3643. IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
  3644. return 0;
  3645. }
  3646. /* handle this temporarily here */
  3647. if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
  3648. conf->changed & IEEE80211_IFCC_BEACON) {
  3649. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  3650. if (!beacon)
  3651. return -ENOMEM;
  3652. mutex_lock(&priv->mutex);
  3653. rc = iwl3945_mac_beacon_update(hw, beacon);
  3654. mutex_unlock(&priv->mutex);
  3655. if (rc)
  3656. return rc;
  3657. }
  3658. if (!iwl_is_alive(priv))
  3659. return -EAGAIN;
  3660. mutex_lock(&priv->mutex);
  3661. if (conf->bssid)
  3662. IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
  3663. /*
  3664. * very dubious code was here; the probe filtering flag is never set:
  3665. *
  3666. if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
  3667. !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
  3668. */
  3669. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  3670. if (!conf->bssid) {
  3671. conf->bssid = priv->mac_addr;
  3672. memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
  3673. IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
  3674. conf->bssid);
  3675. }
  3676. if (priv->ibss_beacon)
  3677. dev_kfree_skb(priv->ibss_beacon);
  3678. priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
  3679. }
  3680. if (iwl_is_rfkill(priv))
  3681. goto done;
  3682. if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
  3683. !is_multicast_ether_addr(conf->bssid)) {
  3684. /* If there is currently a HW scan going on in the background
  3685. * then we need to cancel it else the RXON below will fail. */
  3686. if (iwl_scan_cancel_timeout(priv, 100)) {
  3687. IWL_WARN(priv, "Aborted scan still in progress "
  3688. "after 100ms\n");
  3689. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  3690. mutex_unlock(&priv->mutex);
  3691. return -EAGAIN;
  3692. }
  3693. memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
  3694. /* TODO: Audit driver for usage of these members and see
  3695. * if mac80211 deprecates them (priv->bssid looks like it
  3696. * shouldn't be there, but I haven't scanned the IBSS code
  3697. * to verify) - jpk */
  3698. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  3699. if (priv->iw_mode == NL80211_IFTYPE_AP)
  3700. iwl3945_config_ap(priv);
  3701. else {
  3702. rc = iwl3945_commit_rxon(priv);
  3703. if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
  3704. iwl3945_add_station(priv,
  3705. priv->active_rxon.bssid_addr, 1, 0);
  3706. }
  3707. } else {
  3708. iwl_scan_cancel_timeout(priv, 100);
  3709. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3710. iwl3945_commit_rxon(priv);
  3711. }
  3712. done:
  3713. IWL_DEBUG_MAC80211("leave\n");
  3714. mutex_unlock(&priv->mutex);
  3715. return 0;
  3716. }
  3717. static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
  3718. struct ieee80211_if_init_conf *conf)
  3719. {
  3720. struct iwl_priv *priv = hw->priv;
  3721. IWL_DEBUG_MAC80211("enter\n");
  3722. mutex_lock(&priv->mutex);
  3723. if (iwl_is_ready_rf(priv)) {
  3724. iwl_scan_cancel_timeout(priv, 100);
  3725. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3726. iwl3945_commit_rxon(priv);
  3727. }
  3728. if (priv->vif == conf->vif) {
  3729. priv->vif = NULL;
  3730. memset(priv->bssid, 0, ETH_ALEN);
  3731. }
  3732. mutex_unlock(&priv->mutex);
  3733. IWL_DEBUG_MAC80211("leave\n");
  3734. }
  3735. #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
  3736. static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
  3737. struct ieee80211_vif *vif,
  3738. struct ieee80211_bss_conf *bss_conf,
  3739. u32 changes)
  3740. {
  3741. struct iwl_priv *priv = hw->priv;
  3742. IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
  3743. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  3744. IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
  3745. bss_conf->use_short_preamble);
  3746. if (bss_conf->use_short_preamble)
  3747. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  3748. else
  3749. priv->staging_rxon.flags &=
  3750. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  3751. }
  3752. if (changes & BSS_CHANGED_ERP_CTS_PROT) {
  3753. IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
  3754. if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
  3755. priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
  3756. else
  3757. priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  3758. }
  3759. if (changes & BSS_CHANGED_ASSOC) {
  3760. IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
  3761. /* This should never happen as this function should
  3762. * never be called from interrupt context. */
  3763. if (WARN_ON_ONCE(in_interrupt()))
  3764. return;
  3765. if (bss_conf->assoc) {
  3766. priv->assoc_id = bss_conf->aid;
  3767. priv->beacon_int = bss_conf->beacon_int;
  3768. priv->timestamp = bss_conf->timestamp;
  3769. priv->assoc_capability = bss_conf->assoc_capability;
  3770. priv->power_data.dtim_period = bss_conf->dtim_period;
  3771. priv->next_scan_jiffies = jiffies +
  3772. IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
  3773. mutex_lock(&priv->mutex);
  3774. iwl3945_post_associate(priv);
  3775. mutex_unlock(&priv->mutex);
  3776. } else {
  3777. priv->assoc_id = 0;
  3778. IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
  3779. }
  3780. } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
  3781. IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
  3782. iwl3945_send_rxon_assoc(priv);
  3783. }
  3784. }
  3785. static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
  3786. {
  3787. int rc = 0;
  3788. unsigned long flags;
  3789. struct iwl_priv *priv = hw->priv;
  3790. DECLARE_SSID_BUF(ssid_buf);
  3791. IWL_DEBUG_MAC80211("enter\n");
  3792. mutex_lock(&priv->mutex);
  3793. spin_lock_irqsave(&priv->lock, flags);
  3794. if (!iwl_is_ready_rf(priv)) {
  3795. rc = -EIO;
  3796. IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
  3797. goto out_unlock;
  3798. }
  3799. /* we don't schedule scan within next_scan_jiffies period */
  3800. if (priv->next_scan_jiffies &&
  3801. time_after(priv->next_scan_jiffies, jiffies)) {
  3802. rc = -EAGAIN;
  3803. goto out_unlock;
  3804. }
  3805. /* if we just finished scan ask for delay for a broadcast scan */
  3806. if ((len == 0) && priv->last_scan_jiffies &&
  3807. time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
  3808. jiffies)) {
  3809. rc = -EAGAIN;
  3810. goto out_unlock;
  3811. }
  3812. if (len) {
  3813. IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
  3814. print_ssid(ssid_buf, ssid, len), (int)len);
  3815. priv->one_direct_scan = 1;
  3816. priv->direct_ssid_len = (u8)
  3817. min((u8) len, (u8) IW_ESSID_MAX_SIZE);
  3818. memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
  3819. } else
  3820. priv->one_direct_scan = 0;
  3821. rc = iwl3945_scan_initiate(priv);
  3822. IWL_DEBUG_MAC80211("leave\n");
  3823. out_unlock:
  3824. spin_unlock_irqrestore(&priv->lock, flags);
  3825. mutex_unlock(&priv->mutex);
  3826. return rc;
  3827. }
  3828. static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  3829. struct ieee80211_vif *vif,
  3830. struct ieee80211_sta *sta,
  3831. struct ieee80211_key_conf *key)
  3832. {
  3833. struct iwl_priv *priv = hw->priv;
  3834. const u8 *addr;
  3835. int ret;
  3836. u8 sta_id;
  3837. IWL_DEBUG_MAC80211("enter\n");
  3838. if (iwl3945_mod_params.sw_crypto) {
  3839. IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
  3840. return -EOPNOTSUPP;
  3841. }
  3842. addr = sta ? sta->addr : iwl_bcast_addr;
  3843. sta_id = iwl3945_hw_find_station(priv, addr);
  3844. if (sta_id == IWL_INVALID_STATION) {
  3845. IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
  3846. addr);
  3847. return -EINVAL;
  3848. }
  3849. mutex_lock(&priv->mutex);
  3850. iwl_scan_cancel_timeout(priv, 100);
  3851. switch (cmd) {
  3852. case SET_KEY:
  3853. ret = iwl3945_update_sta_key_info(priv, key, sta_id);
  3854. if (!ret) {
  3855. iwl_set_rxon_hwcrypto(priv, 1);
  3856. iwl3945_commit_rxon(priv);
  3857. key->hw_key_idx = sta_id;
  3858. IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
  3859. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  3860. }
  3861. break;
  3862. case DISABLE_KEY:
  3863. ret = iwl3945_clear_sta_key_info(priv, sta_id);
  3864. if (!ret) {
  3865. iwl_set_rxon_hwcrypto(priv, 0);
  3866. iwl3945_commit_rxon(priv);
  3867. IWL_DEBUG_MAC80211("disable hwcrypto key\n");
  3868. }
  3869. break;
  3870. default:
  3871. ret = -EINVAL;
  3872. }
  3873. IWL_DEBUG_MAC80211("leave\n");
  3874. mutex_unlock(&priv->mutex);
  3875. return ret;
  3876. }
  3877. static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
  3878. const struct ieee80211_tx_queue_params *params)
  3879. {
  3880. struct iwl_priv *priv = hw->priv;
  3881. unsigned long flags;
  3882. int q;
  3883. IWL_DEBUG_MAC80211("enter\n");
  3884. if (!iwl_is_ready_rf(priv)) {
  3885. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  3886. return -EIO;
  3887. }
  3888. if (queue >= AC_NUM) {
  3889. IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
  3890. return 0;
  3891. }
  3892. q = AC_NUM - 1 - queue;
  3893. spin_lock_irqsave(&priv->lock, flags);
  3894. priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
  3895. priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
  3896. priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  3897. priv->qos_data.def_qos_parm.ac[q].edca_txop =
  3898. cpu_to_le16((params->txop * 32));
  3899. priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  3900. priv->qos_data.qos_active = 1;
  3901. spin_unlock_irqrestore(&priv->lock, flags);
  3902. mutex_lock(&priv->mutex);
  3903. if (priv->iw_mode == NL80211_IFTYPE_AP)
  3904. iwl3945_activate_qos(priv, 1);
  3905. else if (priv->assoc_id && iwl_is_associated(priv))
  3906. iwl3945_activate_qos(priv, 0);
  3907. mutex_unlock(&priv->mutex);
  3908. IWL_DEBUG_MAC80211("leave\n");
  3909. return 0;
  3910. }
  3911. static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
  3912. struct ieee80211_tx_queue_stats *stats)
  3913. {
  3914. struct iwl_priv *priv = hw->priv;
  3915. int i, avail;
  3916. struct iwl_tx_queue *txq;
  3917. struct iwl_queue *q;
  3918. unsigned long flags;
  3919. IWL_DEBUG_MAC80211("enter\n");
  3920. if (!iwl_is_ready_rf(priv)) {
  3921. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  3922. return -EIO;
  3923. }
  3924. spin_lock_irqsave(&priv->lock, flags);
  3925. for (i = 0; i < AC_NUM; i++) {
  3926. txq = &priv->txq[i];
  3927. q = &txq->q;
  3928. avail = iwl_queue_space(q);
  3929. stats[i].len = q->n_window - avail;
  3930. stats[i].limit = q->n_window - q->high_mark;
  3931. stats[i].count = q->n_window;
  3932. }
  3933. spin_unlock_irqrestore(&priv->lock, flags);
  3934. IWL_DEBUG_MAC80211("leave\n");
  3935. return 0;
  3936. }
  3937. static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
  3938. {
  3939. struct iwl_priv *priv = hw->priv;
  3940. unsigned long flags;
  3941. mutex_lock(&priv->mutex);
  3942. IWL_DEBUG_MAC80211("enter\n");
  3943. iwl_reset_qos(priv);
  3944. spin_lock_irqsave(&priv->lock, flags);
  3945. priv->assoc_id = 0;
  3946. priv->assoc_capability = 0;
  3947. /* new association get rid of ibss beacon skb */
  3948. if (priv->ibss_beacon)
  3949. dev_kfree_skb(priv->ibss_beacon);
  3950. priv->ibss_beacon = NULL;
  3951. priv->beacon_int = priv->hw->conf.beacon_int;
  3952. priv->timestamp = 0;
  3953. if ((priv->iw_mode == NL80211_IFTYPE_STATION))
  3954. priv->beacon_int = 0;
  3955. spin_unlock_irqrestore(&priv->lock, flags);
  3956. if (!iwl_is_ready_rf(priv)) {
  3957. IWL_DEBUG_MAC80211("leave - not ready\n");
  3958. mutex_unlock(&priv->mutex);
  3959. return;
  3960. }
  3961. /* we are restarting association process
  3962. * clear RXON_FILTER_ASSOC_MSK bit
  3963. */
  3964. if (priv->iw_mode != NL80211_IFTYPE_AP) {
  3965. iwl_scan_cancel_timeout(priv, 100);
  3966. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3967. iwl3945_commit_rxon(priv);
  3968. }
  3969. /* Per mac80211.h: This is only used in IBSS mode... */
  3970. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  3971. IWL_DEBUG_MAC80211("leave - not in IBSS\n");
  3972. mutex_unlock(&priv->mutex);
  3973. return;
  3974. }
  3975. iwl_set_rate(priv);
  3976. mutex_unlock(&priv->mutex);
  3977. IWL_DEBUG_MAC80211("leave\n");
  3978. }
  3979. static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
  3980. {
  3981. struct iwl_priv *priv = hw->priv;
  3982. unsigned long flags;
  3983. IWL_DEBUG_MAC80211("enter\n");
  3984. if (!iwl_is_ready_rf(priv)) {
  3985. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  3986. return -EIO;
  3987. }
  3988. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  3989. IWL_DEBUG_MAC80211("leave - not IBSS\n");
  3990. return -EIO;
  3991. }
  3992. spin_lock_irqsave(&priv->lock, flags);
  3993. if (priv->ibss_beacon)
  3994. dev_kfree_skb(priv->ibss_beacon);
  3995. priv->ibss_beacon = skb;
  3996. priv->assoc_id = 0;
  3997. IWL_DEBUG_MAC80211("leave\n");
  3998. spin_unlock_irqrestore(&priv->lock, flags);
  3999. iwl_reset_qos(priv);
  4000. iwl3945_post_associate(priv);
  4001. return 0;
  4002. }
  4003. /*****************************************************************************
  4004. *
  4005. * sysfs attributes
  4006. *
  4007. *****************************************************************************/
  4008. #ifdef CONFIG_IWLWIFI_DEBUG
  4009. /*
  4010. * The following adds a new attribute to the sysfs representation
  4011. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  4012. * used for controlling the debug level.
  4013. *
  4014. * See the level definitions in iwl for details.
  4015. */
  4016. static ssize_t show_debug_level(struct device *d,
  4017. struct device_attribute *attr, char *buf)
  4018. {
  4019. struct iwl_priv *priv = d->driver_data;
  4020. return sprintf(buf, "0x%08X\n", priv->debug_level);
  4021. }
  4022. static ssize_t store_debug_level(struct device *d,
  4023. struct device_attribute *attr,
  4024. const char *buf, size_t count)
  4025. {
  4026. struct iwl_priv *priv = d->driver_data;
  4027. unsigned long val;
  4028. int ret;
  4029. ret = strict_strtoul(buf, 0, &val);
  4030. if (ret)
  4031. IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
  4032. else
  4033. priv->debug_level = val;
  4034. return strnlen(buf, count);
  4035. }
  4036. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  4037. show_debug_level, store_debug_level);
  4038. #endif /* CONFIG_IWLWIFI_DEBUG */
  4039. static ssize_t show_temperature(struct device *d,
  4040. struct device_attribute *attr, char *buf)
  4041. {
  4042. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  4043. if (!iwl_is_alive(priv))
  4044. return -EAGAIN;
  4045. return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
  4046. }
  4047. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  4048. static ssize_t show_tx_power(struct device *d,
  4049. struct device_attribute *attr, char *buf)
  4050. {
  4051. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  4052. return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
  4053. }
  4054. static ssize_t store_tx_power(struct device *d,
  4055. struct device_attribute *attr,
  4056. const char *buf, size_t count)
  4057. {
  4058. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  4059. char *p = (char *)buf;
  4060. u32 val;
  4061. val = simple_strtoul(p, &p, 10);
  4062. if (p == buf)
  4063. IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
  4064. else
  4065. iwl3945_hw_reg_set_txpower(priv, val);
  4066. return count;
  4067. }
  4068. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  4069. static ssize_t show_flags(struct device *d,
  4070. struct device_attribute *attr, char *buf)
  4071. {
  4072. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  4073. return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
  4074. }
  4075. static ssize_t store_flags(struct device *d,
  4076. struct device_attribute *attr,
  4077. const char *buf, size_t count)
  4078. {
  4079. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  4080. u32 flags = simple_strtoul(buf, NULL, 0);
  4081. mutex_lock(&priv->mutex);
  4082. if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
  4083. /* Cancel any currently running scans... */
  4084. if (iwl_scan_cancel_timeout(priv, 100))
  4085. IWL_WARN(priv, "Could not cancel scan.\n");
  4086. else {
  4087. IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
  4088. flags);
  4089. priv->staging_rxon.flags = cpu_to_le32(flags);
  4090. iwl3945_commit_rxon(priv);
  4091. }
  4092. }
  4093. mutex_unlock(&priv->mutex);
  4094. return count;
  4095. }
  4096. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
  4097. static ssize_t show_filter_flags(struct device *d,
  4098. struct device_attribute *attr, char *buf)
  4099. {
  4100. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  4101. return sprintf(buf, "0x%04X\n",
  4102. le32_to_cpu(priv->active_rxon.filter_flags));
  4103. }
  4104. static ssize_t store_filter_flags(struct device *d,
  4105. struct device_attribute *attr,
  4106. const char *buf, size_t count)
  4107. {
  4108. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  4109. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  4110. mutex_lock(&priv->mutex);
  4111. if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
  4112. /* Cancel any currently running scans... */
  4113. if (iwl_scan_cancel_timeout(priv, 100))
  4114. IWL_WARN(priv, "Could not cancel scan.\n");
  4115. else {
  4116. IWL_DEBUG_INFO("Committing rxon.filter_flags = "
  4117. "0x%04X\n", filter_flags);
  4118. priv->staging_rxon.filter_flags =
  4119. cpu_to_le32(filter_flags);
  4120. iwl3945_commit_rxon(priv);
  4121. }
  4122. }
  4123. mutex_unlock(&priv->mutex);
  4124. return count;
  4125. }
  4126. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
  4127. store_filter_flags);
  4128. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  4129. static ssize_t show_measurement(struct device *d,
  4130. struct device_attribute *attr, char *buf)
  4131. {
  4132. struct iwl_priv *priv = dev_get_drvdata(d);
  4133. struct iwl_spectrum_notification measure_report;
  4134. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  4135. u8 *data = (u8 *)&measure_report;
  4136. unsigned long flags;
  4137. spin_lock_irqsave(&priv->lock, flags);
  4138. if (!(priv->measurement_status & MEASUREMENT_READY)) {
  4139. spin_unlock_irqrestore(&priv->lock, flags);
  4140. return 0;
  4141. }
  4142. memcpy(&measure_report, &priv->measure_report, size);
  4143. priv->measurement_status = 0;
  4144. spin_unlock_irqrestore(&priv->lock, flags);
  4145. while (size && (PAGE_SIZE - len)) {
  4146. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  4147. PAGE_SIZE - len, 1);
  4148. len = strlen(buf);
  4149. if (PAGE_SIZE - len)
  4150. buf[len++] = '\n';
  4151. ofs += 16;
  4152. size -= min(size, 16U);
  4153. }
  4154. return len;
  4155. }
  4156. static ssize_t store_measurement(struct device *d,
  4157. struct device_attribute *attr,
  4158. const char *buf, size_t count)
  4159. {
  4160. struct iwl_priv *priv = dev_get_drvdata(d);
  4161. struct ieee80211_measurement_params params = {
  4162. .channel = le16_to_cpu(priv->active_rxon.channel),
  4163. .start_time = cpu_to_le64(priv->last_tsf),
  4164. .duration = cpu_to_le16(1),
  4165. };
  4166. u8 type = IWL_MEASURE_BASIC;
  4167. u8 buffer[32];
  4168. u8 channel;
  4169. if (count) {
  4170. char *p = buffer;
  4171. strncpy(buffer, buf, min(sizeof(buffer), count));
  4172. channel = simple_strtoul(p, NULL, 0);
  4173. if (channel)
  4174. params.channel = channel;
  4175. p = buffer;
  4176. while (*p && *p != ' ')
  4177. p++;
  4178. if (*p)
  4179. type = simple_strtoul(p + 1, NULL, 0);
  4180. }
  4181. IWL_DEBUG_INFO("Invoking measurement of type %d on "
  4182. "channel %d (for '%s')\n", type, params.channel, buf);
  4183. iwl3945_get_measurement(priv, &params, type);
  4184. return count;
  4185. }
  4186. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
  4187. show_measurement, store_measurement);
  4188. #endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
  4189. static ssize_t store_retry_rate(struct device *d,
  4190. struct device_attribute *attr,
  4191. const char *buf, size_t count)
  4192. {
  4193. struct iwl_priv *priv = dev_get_drvdata(d);
  4194. priv->retry_rate = simple_strtoul(buf, NULL, 0);
  4195. if (priv->retry_rate <= 0)
  4196. priv->retry_rate = 1;
  4197. return count;
  4198. }
  4199. static ssize_t show_retry_rate(struct device *d,
  4200. struct device_attribute *attr, char *buf)
  4201. {
  4202. struct iwl_priv *priv = dev_get_drvdata(d);
  4203. return sprintf(buf, "%d", priv->retry_rate);
  4204. }
  4205. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
  4206. store_retry_rate);
  4207. static ssize_t store_power_level(struct device *d,
  4208. struct device_attribute *attr,
  4209. const char *buf, size_t count)
  4210. {
  4211. struct iwl_priv *priv = dev_get_drvdata(d);
  4212. int rc;
  4213. int mode;
  4214. mode = simple_strtoul(buf, NULL, 0);
  4215. mutex_lock(&priv->mutex);
  4216. if (!iwl_is_ready(priv)) {
  4217. rc = -EAGAIN;
  4218. goto out;
  4219. }
  4220. if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
  4221. (mode == IWL39_POWER_AC))
  4222. mode = IWL39_POWER_AC;
  4223. else
  4224. mode |= IWL_POWER_ENABLED;
  4225. if (mode != priv->power_mode) {
  4226. rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
  4227. if (rc) {
  4228. IWL_DEBUG_MAC80211("failed setting power mode.\n");
  4229. goto out;
  4230. }
  4231. priv->power_mode = mode;
  4232. }
  4233. rc = count;
  4234. out:
  4235. mutex_unlock(&priv->mutex);
  4236. return rc;
  4237. }
  4238. #define MAX_WX_STRING 80
  4239. /* Values are in microsecond */
  4240. static const s32 timeout_duration[] = {
  4241. 350000,
  4242. 250000,
  4243. 75000,
  4244. 37000,
  4245. 25000,
  4246. };
  4247. static const s32 period_duration[] = {
  4248. 400000,
  4249. 700000,
  4250. 1000000,
  4251. 1000000,
  4252. 1000000
  4253. };
  4254. static ssize_t show_power_level(struct device *d,
  4255. struct device_attribute *attr, char *buf)
  4256. {
  4257. struct iwl_priv *priv = dev_get_drvdata(d);
  4258. int level = IWL_POWER_LEVEL(priv->power_mode);
  4259. char *p = buf;
  4260. p += sprintf(p, "%d ", level);
  4261. switch (level) {
  4262. case IWL_POWER_MODE_CAM:
  4263. case IWL39_POWER_AC:
  4264. p += sprintf(p, "(AC)");
  4265. break;
  4266. case IWL39_POWER_BATTERY:
  4267. p += sprintf(p, "(BATTERY)");
  4268. break;
  4269. default:
  4270. p += sprintf(p,
  4271. "(Timeout %dms, Period %dms)",
  4272. timeout_duration[level - 1] / 1000,
  4273. period_duration[level - 1] / 1000);
  4274. }
  4275. if (!(priv->power_mode & IWL_POWER_ENABLED))
  4276. p += sprintf(p, " OFF\n");
  4277. else
  4278. p += sprintf(p, " \n");
  4279. return p - buf + 1;
  4280. }
  4281. static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
  4282. store_power_level);
  4283. static ssize_t show_channels(struct device *d,
  4284. struct device_attribute *attr, char *buf)
  4285. {
  4286. /* all this shit doesn't belong into sysfs anyway */
  4287. return 0;
  4288. }
  4289. static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
  4290. static ssize_t show_statistics(struct device *d,
  4291. struct device_attribute *attr, char *buf)
  4292. {
  4293. struct iwl_priv *priv = dev_get_drvdata(d);
  4294. u32 size = sizeof(struct iwl3945_notif_statistics);
  4295. u32 len = 0, ofs = 0;
  4296. u8 *data = (u8 *)&priv->statistics_39;
  4297. int rc = 0;
  4298. if (!iwl_is_alive(priv))
  4299. return -EAGAIN;
  4300. mutex_lock(&priv->mutex);
  4301. rc = iwl_send_statistics_request(priv, 0);
  4302. mutex_unlock(&priv->mutex);
  4303. if (rc) {
  4304. len = sprintf(buf,
  4305. "Error sending statistics request: 0x%08X\n", rc);
  4306. return len;
  4307. }
  4308. while (size && (PAGE_SIZE - len)) {
  4309. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  4310. PAGE_SIZE - len, 1);
  4311. len = strlen(buf);
  4312. if (PAGE_SIZE - len)
  4313. buf[len++] = '\n';
  4314. ofs += 16;
  4315. size -= min(size, 16U);
  4316. }
  4317. return len;
  4318. }
  4319. static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
  4320. static ssize_t show_antenna(struct device *d,
  4321. struct device_attribute *attr, char *buf)
  4322. {
  4323. struct iwl_priv *priv = dev_get_drvdata(d);
  4324. if (!iwl_is_alive(priv))
  4325. return -EAGAIN;
  4326. return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
  4327. }
  4328. static ssize_t store_antenna(struct device *d,
  4329. struct device_attribute *attr,
  4330. const char *buf, size_t count)
  4331. {
  4332. struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
  4333. int ant;
  4334. if (count == 0)
  4335. return 0;
  4336. if (sscanf(buf, "%1i", &ant) != 1) {
  4337. IWL_DEBUG_INFO("not in hex or decimal form.\n");
  4338. return count;
  4339. }
  4340. if ((ant >= 0) && (ant <= 2)) {
  4341. IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
  4342. iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
  4343. } else
  4344. IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
  4345. return count;
  4346. }
  4347. static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
  4348. static ssize_t show_status(struct device *d,
  4349. struct device_attribute *attr, char *buf)
  4350. {
  4351. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  4352. if (!iwl_is_alive(priv))
  4353. return -EAGAIN;
  4354. return sprintf(buf, "0x%08x\n", (int)priv->status);
  4355. }
  4356. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  4357. static ssize_t dump_error_log(struct device *d,
  4358. struct device_attribute *attr,
  4359. const char *buf, size_t count)
  4360. {
  4361. char *p = (char *)buf;
  4362. if (p[0] == '1')
  4363. iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
  4364. return strnlen(buf, count);
  4365. }
  4366. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
  4367. static ssize_t dump_event_log(struct device *d,
  4368. struct device_attribute *attr,
  4369. const char *buf, size_t count)
  4370. {
  4371. char *p = (char *)buf;
  4372. if (p[0] == '1')
  4373. iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
  4374. return strnlen(buf, count);
  4375. }
  4376. static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
  4377. /*****************************************************************************
  4378. *
  4379. * driver setup and tear down
  4380. *
  4381. *****************************************************************************/
  4382. static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
  4383. {
  4384. priv->workqueue = create_workqueue(DRV_NAME);
  4385. init_waitqueue_head(&priv->wait_command_queue);
  4386. INIT_WORK(&priv->up, iwl3945_bg_up);
  4387. INIT_WORK(&priv->restart, iwl3945_bg_restart);
  4388. INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
  4389. INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
  4390. INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
  4391. INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
  4392. INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
  4393. INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
  4394. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  4395. INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
  4396. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  4397. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  4398. iwl3945_hw_setup_deferred_work(priv);
  4399. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  4400. iwl3945_irq_tasklet, (unsigned long)priv);
  4401. }
  4402. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
  4403. {
  4404. iwl3945_hw_cancel_deferred_work(priv);
  4405. cancel_delayed_work_sync(&priv->init_alive_start);
  4406. cancel_delayed_work(&priv->scan_check);
  4407. cancel_delayed_work(&priv->alive_start);
  4408. cancel_work_sync(&priv->beacon_update);
  4409. }
  4410. static struct attribute *iwl3945_sysfs_entries[] = {
  4411. &dev_attr_antenna.attr,
  4412. &dev_attr_channels.attr,
  4413. &dev_attr_dump_errors.attr,
  4414. &dev_attr_dump_events.attr,
  4415. &dev_attr_flags.attr,
  4416. &dev_attr_filter_flags.attr,
  4417. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  4418. &dev_attr_measurement.attr,
  4419. #endif
  4420. &dev_attr_power_level.attr,
  4421. &dev_attr_retry_rate.attr,
  4422. &dev_attr_statistics.attr,
  4423. &dev_attr_status.attr,
  4424. &dev_attr_temperature.attr,
  4425. &dev_attr_tx_power.attr,
  4426. #ifdef CONFIG_IWLWIFI_DEBUG
  4427. &dev_attr_debug_level.attr,
  4428. #endif
  4429. NULL
  4430. };
  4431. static struct attribute_group iwl3945_attribute_group = {
  4432. .name = NULL, /* put in device directory */
  4433. .attrs = iwl3945_sysfs_entries,
  4434. };
  4435. static struct ieee80211_ops iwl3945_hw_ops = {
  4436. .tx = iwl3945_mac_tx,
  4437. .start = iwl3945_mac_start,
  4438. .stop = iwl3945_mac_stop,
  4439. .add_interface = iwl3945_mac_add_interface,
  4440. .remove_interface = iwl3945_mac_remove_interface,
  4441. .config = iwl3945_mac_config,
  4442. .config_interface = iwl3945_mac_config_interface,
  4443. .configure_filter = iwl_configure_filter,
  4444. .set_key = iwl3945_mac_set_key,
  4445. .get_tx_stats = iwl3945_mac_get_tx_stats,
  4446. .conf_tx = iwl3945_mac_conf_tx,
  4447. .reset_tsf = iwl3945_mac_reset_tsf,
  4448. .bss_info_changed = iwl3945_bss_info_changed,
  4449. .hw_scan = iwl3945_mac_hw_scan
  4450. };
  4451. static int iwl3945_init_drv(struct iwl_priv *priv)
  4452. {
  4453. int ret;
  4454. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  4455. priv->retry_rate = 1;
  4456. priv->ibss_beacon = NULL;
  4457. spin_lock_init(&priv->lock);
  4458. spin_lock_init(&priv->power_data.lock);
  4459. spin_lock_init(&priv->sta_lock);
  4460. spin_lock_init(&priv->hcmd_lock);
  4461. INIT_LIST_HEAD(&priv->free_frames);
  4462. mutex_init(&priv->mutex);
  4463. /* Clear the driver's (not device's) station table */
  4464. iwl3945_clear_stations_table(priv);
  4465. priv->data_retry_limit = -1;
  4466. priv->ieee_channels = NULL;
  4467. priv->ieee_rates = NULL;
  4468. priv->band = IEEE80211_BAND_2GHZ;
  4469. priv->iw_mode = NL80211_IFTYPE_STATION;
  4470. iwl_reset_qos(priv);
  4471. priv->qos_data.qos_active = 0;
  4472. priv->qos_data.qos_cap.val = 0;
  4473. priv->rates_mask = IWL_RATES_MASK;
  4474. /* If power management is turned on, default to AC mode */
  4475. priv->power_mode = IWL39_POWER_AC;
  4476. priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
  4477. if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
  4478. IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
  4479. eeprom->version);
  4480. ret = -EINVAL;
  4481. goto err;
  4482. }
  4483. ret = iwl_init_channel_map(priv);
  4484. if (ret) {
  4485. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  4486. goto err;
  4487. }
  4488. /* Set up txpower settings in driver for all channels */
  4489. if (iwl3945_txpower_set_from_eeprom(priv)) {
  4490. ret = -EIO;
  4491. goto err_free_channel_map;
  4492. }
  4493. ret = iwlcore_init_geos(priv);
  4494. if (ret) {
  4495. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  4496. goto err_free_channel_map;
  4497. }
  4498. iwl3945_init_hw_rates(priv, priv->ieee_rates);
  4499. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  4500. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  4501. &priv->bands[IEEE80211_BAND_2GHZ];
  4502. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  4503. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  4504. &priv->bands[IEEE80211_BAND_5GHZ];
  4505. return 0;
  4506. err_free_channel_map:
  4507. iwl_free_channel_map(priv);
  4508. err:
  4509. return ret;
  4510. }
  4511. static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  4512. {
  4513. int err = 0;
  4514. struct iwl_priv *priv;
  4515. struct ieee80211_hw *hw;
  4516. struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
  4517. struct iwl3945_eeprom *eeprom;
  4518. unsigned long flags;
  4519. /***********************
  4520. * 1. Allocating HW data
  4521. * ********************/
  4522. /* mac80211 allocates memory for this device instance, including
  4523. * space for this driver's private structure */
  4524. hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
  4525. if (hw == NULL) {
  4526. printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
  4527. err = -ENOMEM;
  4528. goto out;
  4529. }
  4530. priv = hw->priv;
  4531. SET_IEEE80211_DEV(hw, &pdev->dev);
  4532. if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
  4533. (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
  4534. IWL_ERR(priv,
  4535. "invalid queues_num, should be between %d and %d\n",
  4536. IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
  4537. err = -EINVAL;
  4538. goto out;
  4539. }
  4540. /*
  4541. * Disabling hardware scan means that mac80211 will perform scans
  4542. * "the hard way", rather than using device's scan.
  4543. */
  4544. if (iwl3945_mod_params.disable_hw_scan) {
  4545. IWL_DEBUG_INFO("Disabling hw_scan\n");
  4546. iwl3945_hw_ops.hw_scan = NULL;
  4547. }
  4548. IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
  4549. priv->cfg = cfg;
  4550. priv->pci_dev = pdev;
  4551. #ifdef CONFIG_IWLWIFI_DEBUG
  4552. priv->debug_level = iwl3945_mod_params.debug;
  4553. atomic_set(&priv->restrict_refcnt, 0);
  4554. #endif
  4555. hw->rate_control_algorithm = "iwl-3945-rs";
  4556. hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
  4557. /* Tell mac80211 our characteristics */
  4558. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  4559. IEEE80211_HW_NOISE_DBM;
  4560. hw->wiphy->interface_modes =
  4561. BIT(NL80211_IFTYPE_STATION) |
  4562. BIT(NL80211_IFTYPE_ADHOC);
  4563. hw->wiphy->custom_regulatory = true;
  4564. /* 4 EDCA QOS priorities */
  4565. hw->queues = 4;
  4566. /***************************
  4567. * 2. Initializing PCI bus
  4568. * *************************/
  4569. if (pci_enable_device(pdev)) {
  4570. err = -ENODEV;
  4571. goto out_ieee80211_free_hw;
  4572. }
  4573. pci_set_master(pdev);
  4574. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  4575. if (!err)
  4576. err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  4577. if (err) {
  4578. IWL_WARN(priv, "No suitable DMA available.\n");
  4579. goto out_pci_disable_device;
  4580. }
  4581. pci_set_drvdata(pdev, priv);
  4582. err = pci_request_regions(pdev, DRV_NAME);
  4583. if (err)
  4584. goto out_pci_disable_device;
  4585. /***********************
  4586. * 3. Read REV Register
  4587. * ********************/
  4588. priv->hw_base = pci_iomap(pdev, 0, 0);
  4589. if (!priv->hw_base) {
  4590. err = -ENODEV;
  4591. goto out_pci_release_regions;
  4592. }
  4593. IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
  4594. (unsigned long long) pci_resource_len(pdev, 0));
  4595. IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
  4596. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  4597. * PCI Tx retries from interfering with C3 CPU state */
  4598. pci_write_config_byte(pdev, 0x41, 0x00);
  4599. /* amp init */
  4600. err = priv->cfg->ops->lib->apm_ops.init(priv);
  4601. if (err < 0) {
  4602. IWL_DEBUG_INFO("Failed to init APMG\n");
  4603. goto out_iounmap;
  4604. }
  4605. /***********************
  4606. * 4. Read EEPROM
  4607. * ********************/
  4608. /* Read the EEPROM */
  4609. err = iwl_eeprom_init(priv);
  4610. if (err) {
  4611. IWL_ERR(priv, "Unable to init EEPROM\n");
  4612. goto out_remove_sysfs;
  4613. }
  4614. /* MAC Address location in EEPROM same for 3945/4965 */
  4615. eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  4616. memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
  4617. IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
  4618. SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
  4619. /***********************
  4620. * 5. Setup HW Constants
  4621. * ********************/
  4622. /* Device-specific setup */
  4623. if (iwl3945_hw_set_hw_params(priv)) {
  4624. IWL_ERR(priv, "failed to set hw settings\n");
  4625. goto out_iounmap;
  4626. }
  4627. /***********************
  4628. * 6. Setup priv
  4629. * ********************/
  4630. err = iwl3945_init_drv(priv);
  4631. if (err) {
  4632. IWL_ERR(priv, "initializing driver failed\n");
  4633. goto out_free_geos;
  4634. }
  4635. IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
  4636. priv->cfg->name);
  4637. /***********************************
  4638. * 7. Initialize Module Parameters
  4639. * **********************************/
  4640. /* Initialize module parameter values here */
  4641. /* Disable radio (SW RF KILL) via parameter when loading driver */
  4642. if (iwl3945_mod_params.disable) {
  4643. set_bit(STATUS_RF_KILL_SW, &priv->status);
  4644. IWL_DEBUG_INFO("Radio disabled.\n");
  4645. }
  4646. /***********************
  4647. * 8. Setup Services
  4648. * ********************/
  4649. spin_lock_irqsave(&priv->lock, flags);
  4650. iwl3945_disable_interrupts(priv);
  4651. spin_unlock_irqrestore(&priv->lock, flags);
  4652. pci_enable_msi(priv->pci_dev);
  4653. err = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
  4654. DRV_NAME, priv);
  4655. if (err) {
  4656. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
  4657. goto out_disable_msi;
  4658. }
  4659. err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  4660. if (err) {
  4661. IWL_ERR(priv, "failed to create sysfs device attributes\n");
  4662. goto out_release_irq;
  4663. }
  4664. iwl_set_rxon_channel(priv,
  4665. &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
  4666. iwl3945_setup_deferred_work(priv);
  4667. iwl3945_setup_rx_handlers(priv);
  4668. /*********************************
  4669. * 9. Setup and Register mac80211
  4670. * *******************************/
  4671. err = ieee80211_register_hw(priv->hw);
  4672. if (err) {
  4673. IWL_ERR(priv, "Failed to register network device: %d\n", err);
  4674. goto out_remove_sysfs;
  4675. }
  4676. priv->hw->conf.beacon_int = 100;
  4677. priv->mac80211_registered = 1;
  4678. err = iwl_rfkill_init(priv);
  4679. if (err)
  4680. IWL_ERR(priv, "Unable to initialize RFKILL system. "
  4681. "Ignoring error: %d\n", err);
  4682. /* Start monitoring the killswitch */
  4683. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  4684. 2 * HZ);
  4685. return 0;
  4686. out_remove_sysfs:
  4687. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  4688. out_free_geos:
  4689. iwlcore_free_geos(priv);
  4690. out_release_irq:
  4691. free_irq(priv->pci_dev->irq, priv);
  4692. destroy_workqueue(priv->workqueue);
  4693. priv->workqueue = NULL;
  4694. iwl3945_unset_hw_params(priv);
  4695. out_disable_msi:
  4696. pci_disable_msi(priv->pci_dev);
  4697. out_iounmap:
  4698. pci_iounmap(pdev, priv->hw_base);
  4699. out_pci_release_regions:
  4700. pci_release_regions(pdev);
  4701. out_pci_disable_device:
  4702. pci_disable_device(pdev);
  4703. pci_set_drvdata(pdev, NULL);
  4704. out_ieee80211_free_hw:
  4705. ieee80211_free_hw(priv->hw);
  4706. out:
  4707. return err;
  4708. }
  4709. static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
  4710. {
  4711. struct iwl_priv *priv = pci_get_drvdata(pdev);
  4712. unsigned long flags;
  4713. if (!priv)
  4714. return;
  4715. IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
  4716. set_bit(STATUS_EXIT_PENDING, &priv->status);
  4717. if (priv->mac80211_registered) {
  4718. ieee80211_unregister_hw(priv->hw);
  4719. priv->mac80211_registered = 0;
  4720. } else {
  4721. iwl3945_down(priv);
  4722. }
  4723. /* make sure we flush any pending irq or
  4724. * tasklet for the driver
  4725. */
  4726. spin_lock_irqsave(&priv->lock, flags);
  4727. iwl3945_disable_interrupts(priv);
  4728. spin_unlock_irqrestore(&priv->lock, flags);
  4729. iwl_synchronize_irq(priv);
  4730. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  4731. iwl_rfkill_unregister(priv);
  4732. cancel_delayed_work(&priv->rfkill_poll);
  4733. iwl3945_dealloc_ucode_pci(priv);
  4734. if (priv->rxq.bd)
  4735. iwl_rx_queue_free(priv, &priv->rxq);
  4736. iwl3945_hw_txq_ctx_free(priv);
  4737. iwl3945_unset_hw_params(priv);
  4738. iwl3945_clear_stations_table(priv);
  4739. /*netif_stop_queue(dev); */
  4740. flush_workqueue(priv->workqueue);
  4741. /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
  4742. * priv->workqueue... so we can't take down the workqueue
  4743. * until now... */
  4744. destroy_workqueue(priv->workqueue);
  4745. priv->workqueue = NULL;
  4746. free_irq(pdev->irq, priv);
  4747. pci_disable_msi(pdev);
  4748. pci_iounmap(pdev, priv->hw_base);
  4749. pci_release_regions(pdev);
  4750. pci_disable_device(pdev);
  4751. pci_set_drvdata(pdev, NULL);
  4752. iwl_free_channel_map(priv);
  4753. iwlcore_free_geos(priv);
  4754. kfree(priv->scan);
  4755. if (priv->ibss_beacon)
  4756. dev_kfree_skb(priv->ibss_beacon);
  4757. ieee80211_free_hw(priv->hw);
  4758. }
  4759. #ifdef CONFIG_PM
  4760. static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  4761. {
  4762. struct iwl_priv *priv = pci_get_drvdata(pdev);
  4763. if (priv->is_open) {
  4764. set_bit(STATUS_IN_SUSPEND, &priv->status);
  4765. iwl3945_mac_stop(priv->hw);
  4766. priv->is_open = 1;
  4767. }
  4768. pci_save_state(pdev);
  4769. pci_disable_device(pdev);
  4770. pci_set_power_state(pdev, PCI_D3hot);
  4771. return 0;
  4772. }
  4773. static int iwl3945_pci_resume(struct pci_dev *pdev)
  4774. {
  4775. struct iwl_priv *priv = pci_get_drvdata(pdev);
  4776. pci_set_power_state(pdev, PCI_D0);
  4777. pci_enable_device(pdev);
  4778. pci_restore_state(pdev);
  4779. if (priv->is_open)
  4780. iwl3945_mac_start(priv->hw);
  4781. clear_bit(STATUS_IN_SUSPEND, &priv->status);
  4782. return 0;
  4783. }
  4784. #endif /* CONFIG_PM */
  4785. /*****************************************************************************
  4786. *
  4787. * driver and module entry point
  4788. *
  4789. *****************************************************************************/
  4790. static struct pci_driver iwl3945_driver = {
  4791. .name = DRV_NAME,
  4792. .id_table = iwl3945_hw_card_ids,
  4793. .probe = iwl3945_pci_probe,
  4794. .remove = __devexit_p(iwl3945_pci_remove),
  4795. #ifdef CONFIG_PM
  4796. .suspend = iwl3945_pci_suspend,
  4797. .resume = iwl3945_pci_resume,
  4798. #endif
  4799. };
  4800. static int __init iwl3945_init(void)
  4801. {
  4802. int ret;
  4803. printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
  4804. printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
  4805. ret = iwl3945_rate_control_register();
  4806. if (ret) {
  4807. printk(KERN_ERR DRV_NAME
  4808. "Unable to register rate control algorithm: %d\n", ret);
  4809. return ret;
  4810. }
  4811. ret = pci_register_driver(&iwl3945_driver);
  4812. if (ret) {
  4813. printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
  4814. goto error_register;
  4815. }
  4816. return ret;
  4817. error_register:
  4818. iwl3945_rate_control_unregister();
  4819. return ret;
  4820. }
  4821. static void __exit iwl3945_exit(void)
  4822. {
  4823. pci_unregister_driver(&iwl3945_driver);
  4824. iwl3945_rate_control_unregister();
  4825. }
  4826. MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
  4827. module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
  4828. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  4829. module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
  4830. MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
  4831. module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
  4832. MODULE_PARM_DESC(swcrypto,
  4833. "using software crypto (default 1 [software])\n");
  4834. module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
  4835. MODULE_PARM_DESC(debug, "debug output mask");
  4836. module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
  4837. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
  4838. module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
  4839. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  4840. module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
  4841. MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
  4842. module_exit(iwl3945_exit);
  4843. module_init(iwl3945_init);