iwl4965-base.c 207 KB

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