iwl-4965.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * James P. Ketrenos <ipw2100-admin@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/version.h>
  29. #include <linux/init.h>
  30. #include <linux/pci.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/delay.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/wireless.h>
  36. #include <net/mac80211.h>
  37. #include <linux/etherdevice.h>
  38. #include <asm/unaligned.h>
  39. #include "iwl-4965.h"
  40. #include "iwl-helpers.h"
  41. static void iwl4965_hw_card_show_info(struct iwl4965_priv *priv);
  42. #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
  43. [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
  44. IWL_RATE_SISO_##s##M_PLCP, \
  45. IWL_RATE_MIMO_##s##M_PLCP, \
  46. IWL_RATE_##r##M_IEEE, \
  47. IWL_RATE_##ip##M_INDEX, \
  48. IWL_RATE_##in##M_INDEX, \
  49. IWL_RATE_##rp##M_INDEX, \
  50. IWL_RATE_##rn##M_INDEX, \
  51. IWL_RATE_##pp##M_INDEX, \
  52. IWL_RATE_##np##M_INDEX }
  53. /*
  54. * Parameter order:
  55. * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
  56. *
  57. * If there isn't a valid next or previous rate then INV is used which
  58. * maps to IWL_RATE_INVALID
  59. *
  60. */
  61. const struct iwl4965_rate_info iwl4965_rates[IWL_RATE_COUNT] = {
  62. IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
  63. IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
  64. IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
  65. IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
  66. IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
  67. IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
  68. IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
  69. IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
  70. IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
  71. IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
  72. IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
  73. IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
  74. IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
  75. };
  76. static int is_fat_channel(__le32 rxon_flags)
  77. {
  78. return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
  79. (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
  80. }
  81. static u8 is_single_stream(struct iwl4965_priv *priv)
  82. {
  83. #ifdef CONFIG_IWL4965_HT
  84. if (!priv->current_ht_config.is_ht ||
  85. (priv->current_ht_config.supp_mcs_set[1] == 0) ||
  86. (priv->ps_mode == IWL_MIMO_PS_STATIC))
  87. return 1;
  88. #else
  89. return 1;
  90. #endif /*CONFIG_IWL4965_HT */
  91. return 0;
  92. }
  93. /*
  94. * Determine how many receiver/antenna chains to use.
  95. * More provides better reception via diversity. Fewer saves power.
  96. * MIMO (dual stream) requires at least 2, but works better with 3.
  97. * This does not determine *which* chains to use, just how many.
  98. */
  99. static int iwl4965_get_rx_chain_counter(struct iwl4965_priv *priv,
  100. u8 *idle_state, u8 *rx_state)
  101. {
  102. u8 is_single = is_single_stream(priv);
  103. u8 is_cam = test_bit(STATUS_POWER_PMI, &priv->status) ? 0 : 1;
  104. /* # of Rx chains to use when expecting MIMO. */
  105. if (is_single || (!is_cam && (priv->ps_mode == IWL_MIMO_PS_STATIC)))
  106. *rx_state = 2;
  107. else
  108. *rx_state = 3;
  109. /* # Rx chains when idling and maybe trying to save power */
  110. switch (priv->ps_mode) {
  111. case IWL_MIMO_PS_STATIC:
  112. case IWL_MIMO_PS_DYNAMIC:
  113. *idle_state = (is_cam) ? 2 : 1;
  114. break;
  115. case IWL_MIMO_PS_NONE:
  116. *idle_state = (is_cam) ? *rx_state : 1;
  117. break;
  118. default:
  119. *idle_state = 1;
  120. break;
  121. }
  122. return 0;
  123. }
  124. int iwl4965_hw_rxq_stop(struct iwl4965_priv *priv)
  125. {
  126. int rc;
  127. unsigned long flags;
  128. spin_lock_irqsave(&priv->lock, flags);
  129. rc = iwl4965_grab_nic_access(priv);
  130. if (rc) {
  131. spin_unlock_irqrestore(&priv->lock, flags);
  132. return rc;
  133. }
  134. /* stop Rx DMA */
  135. iwl4965_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  136. rc = iwl4965_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  137. (1 << 24), 1000);
  138. if (rc < 0)
  139. IWL_ERROR("Can't stop Rx DMA.\n");
  140. iwl4965_release_nic_access(priv);
  141. spin_unlock_irqrestore(&priv->lock, flags);
  142. return 0;
  143. }
  144. u8 iwl4965_hw_find_station(struct iwl4965_priv *priv, const u8 *addr)
  145. {
  146. int i;
  147. int start = 0;
  148. int ret = IWL_INVALID_STATION;
  149. unsigned long flags;
  150. DECLARE_MAC_BUF(mac);
  151. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) ||
  152. (priv->iw_mode == IEEE80211_IF_TYPE_AP))
  153. start = IWL_STA_ID;
  154. if (is_broadcast_ether_addr(addr))
  155. return IWL4965_BROADCAST_ID;
  156. spin_lock_irqsave(&priv->sta_lock, flags);
  157. for (i = start; i < priv->hw_setting.max_stations; i++)
  158. if ((priv->stations[i].used) &&
  159. (!compare_ether_addr
  160. (priv->stations[i].sta.sta.addr, addr))) {
  161. ret = i;
  162. goto out;
  163. }
  164. IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
  165. print_mac(mac, addr), priv->num_stations);
  166. out:
  167. spin_unlock_irqrestore(&priv->sta_lock, flags);
  168. return ret;
  169. }
  170. static int iwl4965_nic_set_pwr_src(struct iwl4965_priv *priv, int pwr_max)
  171. {
  172. int ret;
  173. unsigned long flags;
  174. spin_lock_irqsave(&priv->lock, flags);
  175. ret = iwl4965_grab_nic_access(priv);
  176. if (ret) {
  177. spin_unlock_irqrestore(&priv->lock, flags);
  178. return ret;
  179. }
  180. if (!pwr_max) {
  181. u32 val;
  182. ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
  183. &val);
  184. if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT)
  185. iwl4965_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  186. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  187. ~APMG_PS_CTRL_MSK_PWR_SRC);
  188. } else
  189. iwl4965_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  190. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  191. ~APMG_PS_CTRL_MSK_PWR_SRC);
  192. iwl4965_release_nic_access(priv);
  193. spin_unlock_irqrestore(&priv->lock, flags);
  194. return ret;
  195. }
  196. static int iwl4965_rx_init(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
  197. {
  198. int rc;
  199. unsigned long flags;
  200. unsigned int rb_size;
  201. spin_lock_irqsave(&priv->lock, flags);
  202. rc = iwl4965_grab_nic_access(priv);
  203. if (rc) {
  204. spin_unlock_irqrestore(&priv->lock, flags);
  205. return rc;
  206. }
  207. if (iwl4965_param_amsdu_size_8K)
  208. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  209. else
  210. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  211. /* Stop Rx DMA */
  212. iwl4965_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  213. /* Reset driver's Rx queue write index */
  214. iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  215. /* Tell device where to find RBD circular buffer in DRAM */
  216. iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  217. rxq->dma_addr >> 8);
  218. /* Tell device where in DRAM to update its Rx status */
  219. iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  220. (priv->hw_setting.shared_phys +
  221. offsetof(struct iwl4965_shared, val0)) >> 4);
  222. /* Enable Rx DMA, enable host interrupt, Rx buffer size 4k, 256 RBDs */
  223. iwl4965_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  224. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  225. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  226. rb_size |
  227. /*0x10 << 4 | */
  228. (RX_QUEUE_SIZE_LOG <<
  229. FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
  230. /*
  231. * iwl4965_write32(priv,CSR_INT_COAL_REG,0);
  232. */
  233. iwl4965_release_nic_access(priv);
  234. spin_unlock_irqrestore(&priv->lock, flags);
  235. return 0;
  236. }
  237. /* Tell 4965 where to find the "keep warm" buffer */
  238. static int iwl4965_kw_init(struct iwl4965_priv *priv)
  239. {
  240. unsigned long flags;
  241. int rc;
  242. spin_lock_irqsave(&priv->lock, flags);
  243. rc = iwl4965_grab_nic_access(priv);
  244. if (rc)
  245. goto out;
  246. iwl4965_write_direct32(priv, IWL_FH_KW_MEM_ADDR_REG,
  247. priv->kw.dma_addr >> 4);
  248. iwl4965_release_nic_access(priv);
  249. out:
  250. spin_unlock_irqrestore(&priv->lock, flags);
  251. return rc;
  252. }
  253. static int iwl4965_kw_alloc(struct iwl4965_priv *priv)
  254. {
  255. struct pci_dev *dev = priv->pci_dev;
  256. struct iwl4965_kw *kw = &priv->kw;
  257. kw->size = IWL4965_KW_SIZE; /* TBW need set somewhere else */
  258. kw->v_addr = pci_alloc_consistent(dev, kw->size, &kw->dma_addr);
  259. if (!kw->v_addr)
  260. return -ENOMEM;
  261. return 0;
  262. }
  263. #define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
  264. ? # x " " : "")
  265. /**
  266. * iwl4965_set_fat_chan_info - Copy fat channel info into driver's priv.
  267. *
  268. * Does not set up a command, or touch hardware.
  269. */
  270. int iwl4965_set_fat_chan_info(struct iwl4965_priv *priv, int phymode, u16 channel,
  271. const struct iwl4965_eeprom_channel *eeprom_ch,
  272. u8 fat_extension_channel)
  273. {
  274. struct iwl4965_channel_info *ch_info;
  275. ch_info = (struct iwl4965_channel_info *)
  276. iwl4965_get_channel_info(priv, phymode, channel);
  277. if (!is_channel_valid(ch_info))
  278. return -1;
  279. IWL_DEBUG_INFO("FAT Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
  280. " %ddBm): Ad-Hoc %ssupported\n",
  281. ch_info->channel,
  282. is_channel_a_band(ch_info) ?
  283. "5.2" : "2.4",
  284. CHECK_AND_PRINT(IBSS),
  285. CHECK_AND_PRINT(ACTIVE),
  286. CHECK_AND_PRINT(RADAR),
  287. CHECK_AND_PRINT(WIDE),
  288. CHECK_AND_PRINT(NARROW),
  289. CHECK_AND_PRINT(DFS),
  290. eeprom_ch->flags,
  291. eeprom_ch->max_power_avg,
  292. ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS)
  293. && !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ?
  294. "" : "not ");
  295. ch_info->fat_eeprom = *eeprom_ch;
  296. ch_info->fat_max_power_avg = eeprom_ch->max_power_avg;
  297. ch_info->fat_curr_txpow = eeprom_ch->max_power_avg;
  298. ch_info->fat_min_power = 0;
  299. ch_info->fat_scan_power = eeprom_ch->max_power_avg;
  300. ch_info->fat_flags = eeprom_ch->flags;
  301. ch_info->fat_extension_channel = fat_extension_channel;
  302. return 0;
  303. }
  304. /**
  305. * iwl4965_kw_free - Free the "keep warm" buffer
  306. */
  307. static void iwl4965_kw_free(struct iwl4965_priv *priv)
  308. {
  309. struct pci_dev *dev = priv->pci_dev;
  310. struct iwl4965_kw *kw = &priv->kw;
  311. if (kw->v_addr) {
  312. pci_free_consistent(dev, kw->size, kw->v_addr, kw->dma_addr);
  313. memset(kw, 0, sizeof(*kw));
  314. }
  315. }
  316. /**
  317. * iwl4965_txq_ctx_reset - Reset TX queue context
  318. * Destroys all DMA structures and initialise them again
  319. *
  320. * @param priv
  321. * @return error code
  322. */
  323. static int iwl4965_txq_ctx_reset(struct iwl4965_priv *priv)
  324. {
  325. int rc = 0;
  326. int txq_id, slots_num;
  327. unsigned long flags;
  328. iwl4965_kw_free(priv);
  329. /* Free all tx/cmd queues and keep-warm buffer */
  330. iwl4965_hw_txq_ctx_free(priv);
  331. /* Alloc keep-warm buffer */
  332. rc = iwl4965_kw_alloc(priv);
  333. if (rc) {
  334. IWL_ERROR("Keep Warm allocation failed");
  335. goto error_kw;
  336. }
  337. spin_lock_irqsave(&priv->lock, flags);
  338. rc = iwl4965_grab_nic_access(priv);
  339. if (unlikely(rc)) {
  340. IWL_ERROR("TX reset failed");
  341. spin_unlock_irqrestore(&priv->lock, flags);
  342. goto error_reset;
  343. }
  344. /* Turn off all Tx DMA channels */
  345. iwl4965_write_prph(priv, KDR_SCD_TXFACT, 0);
  346. iwl4965_release_nic_access(priv);
  347. spin_unlock_irqrestore(&priv->lock, flags);
  348. /* Tell 4965 where to find the keep-warm buffer */
  349. rc = iwl4965_kw_init(priv);
  350. if (rc) {
  351. IWL_ERROR("kw_init failed\n");
  352. goto error_reset;
  353. }
  354. /* Alloc and init all (default 16) Tx queues,
  355. * including the command queue (#4) */
  356. for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++) {
  357. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  358. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  359. rc = iwl4965_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  360. txq_id);
  361. if (rc) {
  362. IWL_ERROR("Tx %d queue init failed\n", txq_id);
  363. goto error;
  364. }
  365. }
  366. return rc;
  367. error:
  368. iwl4965_hw_txq_ctx_free(priv);
  369. error_reset:
  370. iwl4965_kw_free(priv);
  371. error_kw:
  372. return rc;
  373. }
  374. int iwl4965_hw_nic_init(struct iwl4965_priv *priv)
  375. {
  376. int rc;
  377. unsigned long flags;
  378. struct iwl4965_rx_queue *rxq = &priv->rxq;
  379. u8 rev_id;
  380. u32 val;
  381. u8 val_link;
  382. iwl4965_power_init_handle(priv);
  383. /* nic_init */
  384. spin_lock_irqsave(&priv->lock, flags);
  385. iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  386. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  387. iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  388. rc = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
  389. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  390. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  391. if (rc < 0) {
  392. spin_unlock_irqrestore(&priv->lock, flags);
  393. IWL_DEBUG_INFO("Failed to init the card\n");
  394. return rc;
  395. }
  396. rc = iwl4965_grab_nic_access(priv);
  397. if (rc) {
  398. spin_unlock_irqrestore(&priv->lock, flags);
  399. return rc;
  400. }
  401. iwl4965_read_prph(priv, APMG_CLK_CTRL_REG);
  402. iwl4965_write_prph(priv, APMG_CLK_CTRL_REG,
  403. APMG_CLK_VAL_DMA_CLK_RQT |
  404. APMG_CLK_VAL_BSM_CLK_RQT);
  405. iwl4965_read_prph(priv, APMG_CLK_CTRL_REG);
  406. udelay(20);
  407. iwl4965_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
  408. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  409. iwl4965_release_nic_access(priv);
  410. iwl4965_write32(priv, CSR_INT_COALESCING, 512 / 32);
  411. spin_unlock_irqrestore(&priv->lock, flags);
  412. /* Determine HW type */
  413. rc = pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &rev_id);
  414. if (rc)
  415. return rc;
  416. IWL_DEBUG_INFO("HW Revision ID = 0x%X\n", rev_id);
  417. iwl4965_nic_set_pwr_src(priv, 1);
  418. spin_lock_irqsave(&priv->lock, flags);
  419. if ((rev_id & 0x80) == 0x80 && (rev_id & 0x7f) < 8) {
  420. pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
  421. /* Enable No Snoop field */
  422. pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
  423. val & ~(1 << 11));
  424. }
  425. spin_unlock_irqrestore(&priv->lock, flags);
  426. /* Read the EEPROM */
  427. rc = iwl4965_eeprom_init(priv);
  428. if (rc)
  429. return rc;
  430. if (priv->eeprom.calib_version < EEPROM_TX_POWER_VERSION_NEW) {
  431. IWL_ERROR("Older EEPROM detected! Aborting.\n");
  432. return -EINVAL;
  433. }
  434. pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
  435. /* disable L1 entry -- workaround for pre-B1 */
  436. pci_write_config_byte(priv->pci_dev, PCI_LINK_CTRL, val_link & ~0x02);
  437. spin_lock_irqsave(&priv->lock, flags);
  438. /* set CSR_HW_CONFIG_REG for uCode use */
  439. iwl4965_set_bit(priv, CSR_SW_VER, CSR_HW_IF_CONFIG_REG_BIT_KEDRON_R |
  440. CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
  441. CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
  442. rc = iwl4965_grab_nic_access(priv);
  443. if (rc < 0) {
  444. spin_unlock_irqrestore(&priv->lock, flags);
  445. IWL_DEBUG_INFO("Failed to init the card\n");
  446. return rc;
  447. }
  448. iwl4965_read_prph(priv, APMG_PS_CTRL_REG);
  449. iwl4965_set_bits_prph(priv, APMG_PS_CTRL_REG,
  450. APMG_PS_CTRL_VAL_RESET_REQ);
  451. udelay(5);
  452. iwl4965_clear_bits_prph(priv, APMG_PS_CTRL_REG,
  453. APMG_PS_CTRL_VAL_RESET_REQ);
  454. iwl4965_release_nic_access(priv);
  455. spin_unlock_irqrestore(&priv->lock, flags);
  456. iwl4965_hw_card_show_info(priv);
  457. /* end nic_init */
  458. /* Allocate the RX queue, or reset if it is already allocated */
  459. if (!rxq->bd) {
  460. rc = iwl4965_rx_queue_alloc(priv);
  461. if (rc) {
  462. IWL_ERROR("Unable to initialize Rx queue\n");
  463. return -ENOMEM;
  464. }
  465. } else
  466. iwl4965_rx_queue_reset(priv, rxq);
  467. iwl4965_rx_replenish(priv);
  468. iwl4965_rx_init(priv, rxq);
  469. spin_lock_irqsave(&priv->lock, flags);
  470. rxq->need_update = 1;
  471. iwl4965_rx_queue_update_write_ptr(priv, rxq);
  472. spin_unlock_irqrestore(&priv->lock, flags);
  473. /* Allocate and init all Tx and Command queues */
  474. rc = iwl4965_txq_ctx_reset(priv);
  475. if (rc)
  476. return rc;
  477. if (priv->eeprom.sku_cap & EEPROM_SKU_CAP_SW_RF_KILL_ENABLE)
  478. IWL_DEBUG_RF_KILL("SW RF KILL supported in EEPROM.\n");
  479. if (priv->eeprom.sku_cap & EEPROM_SKU_CAP_HW_RF_KILL_ENABLE)
  480. IWL_DEBUG_RF_KILL("HW RF KILL supported in EEPROM.\n");
  481. set_bit(STATUS_INIT, &priv->status);
  482. return 0;
  483. }
  484. int iwl4965_hw_nic_stop_master(struct iwl4965_priv *priv)
  485. {
  486. int rc = 0;
  487. u32 reg_val;
  488. unsigned long flags;
  489. spin_lock_irqsave(&priv->lock, flags);
  490. /* set stop master bit */
  491. iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  492. reg_val = iwl4965_read32(priv, CSR_GP_CNTRL);
  493. if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
  494. (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
  495. IWL_DEBUG_INFO("Card in power save, master is already "
  496. "stopped\n");
  497. else {
  498. rc = iwl4965_poll_bit(priv, CSR_RESET,
  499. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  500. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  501. if (rc < 0) {
  502. spin_unlock_irqrestore(&priv->lock, flags);
  503. return rc;
  504. }
  505. }
  506. spin_unlock_irqrestore(&priv->lock, flags);
  507. IWL_DEBUG_INFO("stop master\n");
  508. return rc;
  509. }
  510. /**
  511. * iwl4965_hw_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
  512. */
  513. void iwl4965_hw_txq_ctx_stop(struct iwl4965_priv *priv)
  514. {
  515. int txq_id;
  516. unsigned long flags;
  517. /* Stop each Tx DMA channel, and wait for it to be idle */
  518. for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++) {
  519. spin_lock_irqsave(&priv->lock, flags);
  520. if (iwl4965_grab_nic_access(priv)) {
  521. spin_unlock_irqrestore(&priv->lock, flags);
  522. continue;
  523. }
  524. iwl4965_write_direct32(priv,
  525. IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
  526. 0x0);
  527. iwl4965_poll_direct_bit(priv, IWL_FH_TSSR_TX_STATUS_REG,
  528. IWL_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
  529. (txq_id), 200);
  530. iwl4965_release_nic_access(priv);
  531. spin_unlock_irqrestore(&priv->lock, flags);
  532. }
  533. /* Deallocate memory for all Tx queues */
  534. iwl4965_hw_txq_ctx_free(priv);
  535. }
  536. int iwl4965_hw_nic_reset(struct iwl4965_priv *priv)
  537. {
  538. int rc = 0;
  539. unsigned long flags;
  540. iwl4965_hw_nic_stop_master(priv);
  541. spin_lock_irqsave(&priv->lock, flags);
  542. iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  543. udelay(10);
  544. iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  545. rc = iwl4965_poll_bit(priv, CSR_RESET,
  546. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  547. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
  548. udelay(10);
  549. rc = iwl4965_grab_nic_access(priv);
  550. if (!rc) {
  551. iwl4965_write_prph(priv, APMG_CLK_EN_REG,
  552. APMG_CLK_VAL_DMA_CLK_RQT |
  553. APMG_CLK_VAL_BSM_CLK_RQT);
  554. udelay(10);
  555. iwl4965_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
  556. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  557. iwl4965_release_nic_access(priv);
  558. }
  559. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  560. wake_up_interruptible(&priv->wait_command_queue);
  561. spin_unlock_irqrestore(&priv->lock, flags);
  562. return rc;
  563. }
  564. #define REG_RECALIB_PERIOD (60)
  565. /**
  566. * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
  567. *
  568. * This callback is provided in order to queue the statistics_work
  569. * in work_queue context (v. softirq)
  570. *
  571. * This timer function is continually reset to execute within
  572. * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
  573. * was received. We need to ensure we receive the statistics in order
  574. * to update the temperature used for calibrating the TXPOWER. However,
  575. * we can't send the statistics command from softirq context (which
  576. * is the context which timers run at) so we have to queue off the
  577. * statistics_work to actually send the command to the hardware.
  578. */
  579. static void iwl4965_bg_statistics_periodic(unsigned long data)
  580. {
  581. struct iwl4965_priv *priv = (struct iwl4965_priv *)data;
  582. queue_work(priv->workqueue, &priv->statistics_work);
  583. }
  584. /**
  585. * iwl4965_bg_statistics_work - Send the statistics request to the hardware.
  586. *
  587. * This is queued by iwl4965_bg_statistics_periodic.
  588. */
  589. static void iwl4965_bg_statistics_work(struct work_struct *work)
  590. {
  591. struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
  592. statistics_work);
  593. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  594. return;
  595. mutex_lock(&priv->mutex);
  596. iwl4965_send_statistics_request(priv);
  597. mutex_unlock(&priv->mutex);
  598. }
  599. #define CT_LIMIT_CONST 259
  600. #define TM_CT_KILL_THRESHOLD 110
  601. void iwl4965_rf_kill_ct_config(struct iwl4965_priv *priv)
  602. {
  603. struct iwl4965_ct_kill_config cmd;
  604. u32 R1, R2, R3;
  605. u32 temp_th;
  606. u32 crit_temperature;
  607. unsigned long flags;
  608. int rc = 0;
  609. spin_lock_irqsave(&priv->lock, flags);
  610. iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  611. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  612. spin_unlock_irqrestore(&priv->lock, flags);
  613. if (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK) {
  614. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
  615. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
  616. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
  617. } else {
  618. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
  619. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
  620. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
  621. }
  622. temp_th = CELSIUS_TO_KELVIN(TM_CT_KILL_THRESHOLD);
  623. crit_temperature = ((temp_th * (R3-R1))/CT_LIMIT_CONST) + R2;
  624. cmd.critical_temperature_R = cpu_to_le32(crit_temperature);
  625. rc = iwl4965_send_cmd_pdu(priv,
  626. REPLY_CT_KILL_CONFIG_CMD, sizeof(cmd), &cmd);
  627. if (rc)
  628. IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
  629. else
  630. IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded\n");
  631. }
  632. #ifdef CONFIG_IWL4965_SENSITIVITY
  633. /* "false alarms" are signals that our DSP tries to lock onto,
  634. * but then determines that they are either noise, or transmissions
  635. * from a distant wireless network (also "noise", really) that get
  636. * "stepped on" by stronger transmissions within our own network.
  637. * This algorithm attempts to set a sensitivity level that is high
  638. * enough to receive all of our own network traffic, but not so
  639. * high that our DSP gets too busy trying to lock onto non-network
  640. * activity/noise. */
  641. static int iwl4965_sens_energy_cck(struct iwl4965_priv *priv,
  642. u32 norm_fa,
  643. u32 rx_enable_time,
  644. struct statistics_general_data *rx_info)
  645. {
  646. u32 max_nrg_cck = 0;
  647. int i = 0;
  648. u8 max_silence_rssi = 0;
  649. u32 silence_ref = 0;
  650. u8 silence_rssi_a = 0;
  651. u8 silence_rssi_b = 0;
  652. u8 silence_rssi_c = 0;
  653. u32 val;
  654. /* "false_alarms" values below are cross-multiplications to assess the
  655. * numbers of false alarms within the measured period of actual Rx
  656. * (Rx is off when we're txing), vs the min/max expected false alarms
  657. * (some should be expected if rx is sensitive enough) in a
  658. * hypothetical listening period of 200 time units (TU), 204.8 msec:
  659. *
  660. * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
  661. *
  662. * */
  663. u32 false_alarms = norm_fa * 200 * 1024;
  664. u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
  665. u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
  666. struct iwl4965_sensitivity_data *data = NULL;
  667. data = &(priv->sensitivity_data);
  668. data->nrg_auto_corr_silence_diff = 0;
  669. /* Find max silence rssi among all 3 receivers.
  670. * This is background noise, which may include transmissions from other
  671. * networks, measured during silence before our network's beacon */
  672. silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
  673. ALL_BAND_FILTER)>>8);
  674. silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
  675. ALL_BAND_FILTER)>>8);
  676. silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
  677. ALL_BAND_FILTER)>>8);
  678. val = max(silence_rssi_b, silence_rssi_c);
  679. max_silence_rssi = max(silence_rssi_a, (u8) val);
  680. /* Store silence rssi in 20-beacon history table */
  681. data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
  682. data->nrg_silence_idx++;
  683. if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
  684. data->nrg_silence_idx = 0;
  685. /* Find max silence rssi across 20 beacon history */
  686. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
  687. val = data->nrg_silence_rssi[i];
  688. silence_ref = max(silence_ref, val);
  689. }
  690. IWL_DEBUG_CALIB("silence a %u, b %u, c %u, 20-bcn max %u\n",
  691. silence_rssi_a, silence_rssi_b, silence_rssi_c,
  692. silence_ref);
  693. /* Find max rx energy (min value!) among all 3 receivers,
  694. * measured during beacon frame.
  695. * Save it in 10-beacon history table. */
  696. i = data->nrg_energy_idx;
  697. val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
  698. data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
  699. data->nrg_energy_idx++;
  700. if (data->nrg_energy_idx >= 10)
  701. data->nrg_energy_idx = 0;
  702. /* Find min rx energy (max value) across 10 beacon history.
  703. * This is the minimum signal level that we want to receive well.
  704. * Add backoff (margin so we don't miss slightly lower energy frames).
  705. * This establishes an upper bound (min value) for energy threshold. */
  706. max_nrg_cck = data->nrg_value[0];
  707. for (i = 1; i < 10; i++)
  708. max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
  709. max_nrg_cck += 6;
  710. IWL_DEBUG_CALIB("rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
  711. rx_info->beacon_energy_a, rx_info->beacon_energy_b,
  712. rx_info->beacon_energy_c, max_nrg_cck - 6);
  713. /* Count number of consecutive beacons with fewer-than-desired
  714. * false alarms. */
  715. if (false_alarms < min_false_alarms)
  716. data->num_in_cck_no_fa++;
  717. else
  718. data->num_in_cck_no_fa = 0;
  719. IWL_DEBUG_CALIB("consecutive bcns with few false alarms = %u\n",
  720. data->num_in_cck_no_fa);
  721. /* If we got too many false alarms this time, reduce sensitivity */
  722. if (false_alarms > max_false_alarms) {
  723. IWL_DEBUG_CALIB("norm FA %u > max FA %u\n",
  724. false_alarms, max_false_alarms);
  725. IWL_DEBUG_CALIB("... reducing sensitivity\n");
  726. data->nrg_curr_state = IWL_FA_TOO_MANY;
  727. if (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK) {
  728. /* Store for "fewer than desired" on later beacon */
  729. data->nrg_silence_ref = silence_ref;
  730. /* increase energy threshold (reduce nrg value)
  731. * to decrease sensitivity */
  732. if (data->nrg_th_cck > (NRG_MAX_CCK + NRG_STEP_CCK))
  733. data->nrg_th_cck = data->nrg_th_cck
  734. - NRG_STEP_CCK;
  735. }
  736. /* increase auto_corr values to decrease sensitivity */
  737. if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
  738. data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
  739. else {
  740. val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
  741. data->auto_corr_cck = min((u32)AUTO_CORR_MAX_CCK, val);
  742. }
  743. val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
  744. data->auto_corr_cck_mrc = min((u32)AUTO_CORR_MAX_CCK_MRC, val);
  745. /* Else if we got fewer than desired, increase sensitivity */
  746. } else if (false_alarms < min_false_alarms) {
  747. data->nrg_curr_state = IWL_FA_TOO_FEW;
  748. /* Compare silence level with silence level for most recent
  749. * healthy number or too many false alarms */
  750. data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
  751. (s32)silence_ref;
  752. IWL_DEBUG_CALIB("norm FA %u < min FA %u, silence diff %d\n",
  753. false_alarms, min_false_alarms,
  754. data->nrg_auto_corr_silence_diff);
  755. /* Increase value to increase sensitivity, but only if:
  756. * 1a) previous beacon did *not* have *too many* false alarms
  757. * 1b) AND there's a significant difference in Rx levels
  758. * from a previous beacon with too many, or healthy # FAs
  759. * OR 2) We've seen a lot of beacons (100) with too few
  760. * false alarms */
  761. if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
  762. ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
  763. (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
  764. IWL_DEBUG_CALIB("... increasing sensitivity\n");
  765. /* Increase nrg value to increase sensitivity */
  766. val = data->nrg_th_cck + NRG_STEP_CCK;
  767. data->nrg_th_cck = min((u32)NRG_MIN_CCK, val);
  768. /* Decrease auto_corr values to increase sensitivity */
  769. val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
  770. data->auto_corr_cck = max((u32)AUTO_CORR_MIN_CCK, val);
  771. val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
  772. data->auto_corr_cck_mrc =
  773. max((u32)AUTO_CORR_MIN_CCK_MRC, val);
  774. } else
  775. IWL_DEBUG_CALIB("... but not changing sensitivity\n");
  776. /* Else we got a healthy number of false alarms, keep status quo */
  777. } else {
  778. IWL_DEBUG_CALIB(" FA in safe zone\n");
  779. data->nrg_curr_state = IWL_FA_GOOD_RANGE;
  780. /* Store for use in "fewer than desired" with later beacon */
  781. data->nrg_silence_ref = silence_ref;
  782. /* If previous beacon had too many false alarms,
  783. * give it some extra margin by reducing sensitivity again
  784. * (but don't go below measured energy of desired Rx) */
  785. if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
  786. IWL_DEBUG_CALIB("... increasing margin\n");
  787. data->nrg_th_cck -= NRG_MARGIN;
  788. }
  789. }
  790. /* Make sure the energy threshold does not go above the measured
  791. * energy of the desired Rx signals (reduced by backoff margin),
  792. * or else we might start missing Rx frames.
  793. * Lower value is higher energy, so we use max()!
  794. */
  795. data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
  796. IWL_DEBUG_CALIB("new nrg_th_cck %u\n", data->nrg_th_cck);
  797. data->nrg_prev_state = data->nrg_curr_state;
  798. return 0;
  799. }
  800. static int iwl4965_sens_auto_corr_ofdm(struct iwl4965_priv *priv,
  801. u32 norm_fa,
  802. u32 rx_enable_time)
  803. {
  804. u32 val;
  805. u32 false_alarms = norm_fa * 200 * 1024;
  806. u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
  807. u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
  808. struct iwl4965_sensitivity_data *data = NULL;
  809. data = &(priv->sensitivity_data);
  810. /* If we got too many false alarms this time, reduce sensitivity */
  811. if (false_alarms > max_false_alarms) {
  812. IWL_DEBUG_CALIB("norm FA %u > max FA %u)\n",
  813. false_alarms, max_false_alarms);
  814. val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
  815. data->auto_corr_ofdm =
  816. min((u32)AUTO_CORR_MAX_OFDM, val);
  817. val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
  818. data->auto_corr_ofdm_mrc =
  819. min((u32)AUTO_CORR_MAX_OFDM_MRC, val);
  820. val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
  821. data->auto_corr_ofdm_x1 =
  822. min((u32)AUTO_CORR_MAX_OFDM_X1, val);
  823. val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
  824. data->auto_corr_ofdm_mrc_x1 =
  825. min((u32)AUTO_CORR_MAX_OFDM_MRC_X1, val);
  826. }
  827. /* Else if we got fewer than desired, increase sensitivity */
  828. else if (false_alarms < min_false_alarms) {
  829. IWL_DEBUG_CALIB("norm FA %u < min FA %u\n",
  830. false_alarms, min_false_alarms);
  831. val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
  832. data->auto_corr_ofdm =
  833. max((u32)AUTO_CORR_MIN_OFDM, val);
  834. val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
  835. data->auto_corr_ofdm_mrc =
  836. max((u32)AUTO_CORR_MIN_OFDM_MRC, val);
  837. val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
  838. data->auto_corr_ofdm_x1 =
  839. max((u32)AUTO_CORR_MIN_OFDM_X1, val);
  840. val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
  841. data->auto_corr_ofdm_mrc_x1 =
  842. max((u32)AUTO_CORR_MIN_OFDM_MRC_X1, val);
  843. }
  844. else
  845. IWL_DEBUG_CALIB("min FA %u < norm FA %u < max FA %u OK\n",
  846. min_false_alarms, false_alarms, max_false_alarms);
  847. return 0;
  848. }
  849. static int iwl4965_sensitivity_callback(struct iwl4965_priv *priv,
  850. struct iwl4965_cmd *cmd, struct sk_buff *skb)
  851. {
  852. /* We didn't cache the SKB; let the caller free it */
  853. return 1;
  854. }
  855. /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
  856. static int iwl4965_sensitivity_write(struct iwl4965_priv *priv, u8 flags)
  857. {
  858. int rc = 0;
  859. struct iwl4965_sensitivity_cmd cmd ;
  860. struct iwl4965_sensitivity_data *data = NULL;
  861. struct iwl4965_host_cmd cmd_out = {
  862. .id = SENSITIVITY_CMD,
  863. .len = sizeof(struct iwl4965_sensitivity_cmd),
  864. .meta.flags = flags,
  865. .data = &cmd,
  866. };
  867. data = &(priv->sensitivity_data);
  868. memset(&cmd, 0, sizeof(cmd));
  869. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
  870. cpu_to_le16((u16)data->auto_corr_ofdm);
  871. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
  872. cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
  873. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
  874. cpu_to_le16((u16)data->auto_corr_ofdm_x1);
  875. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
  876. cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
  877. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
  878. cpu_to_le16((u16)data->auto_corr_cck);
  879. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
  880. cpu_to_le16((u16)data->auto_corr_cck_mrc);
  881. cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
  882. cpu_to_le16((u16)data->nrg_th_cck);
  883. cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
  884. cpu_to_le16((u16)data->nrg_th_ofdm);
  885. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
  886. __constant_cpu_to_le16(190);
  887. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
  888. __constant_cpu_to_le16(390);
  889. cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
  890. __constant_cpu_to_le16(62);
  891. IWL_DEBUG_CALIB("ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
  892. data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
  893. data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
  894. data->nrg_th_ofdm);
  895. IWL_DEBUG_CALIB("cck: ac %u mrc %u thresh %u\n",
  896. data->auto_corr_cck, data->auto_corr_cck_mrc,
  897. data->nrg_th_cck);
  898. /* Update uCode's "work" table, and copy it to DSP */
  899. cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
  900. if (flags & CMD_ASYNC)
  901. cmd_out.meta.u.callback = iwl4965_sensitivity_callback;
  902. /* Don't send command to uCode if nothing has changed */
  903. if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
  904. sizeof(u16)*HD_TABLE_SIZE)) {
  905. IWL_DEBUG_CALIB("No change in SENSITIVITY_CMD\n");
  906. return 0;
  907. }
  908. /* Copy table for comparison next time */
  909. memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
  910. sizeof(u16)*HD_TABLE_SIZE);
  911. rc = iwl4965_send_cmd(priv, &cmd_out);
  912. if (!rc) {
  913. IWL_DEBUG_CALIB("SENSITIVITY_CMD succeeded\n");
  914. return rc;
  915. }
  916. return 0;
  917. }
  918. void iwl4965_init_sensitivity(struct iwl4965_priv *priv, u8 flags, u8 force)
  919. {
  920. int rc = 0;
  921. int i;
  922. struct iwl4965_sensitivity_data *data = NULL;
  923. IWL_DEBUG_CALIB("Start iwl4965_init_sensitivity\n");
  924. if (force)
  925. memset(&(priv->sensitivity_tbl[0]), 0,
  926. sizeof(u16)*HD_TABLE_SIZE);
  927. /* Clear driver's sensitivity algo data */
  928. data = &(priv->sensitivity_data);
  929. memset(data, 0, sizeof(struct iwl4965_sensitivity_data));
  930. data->num_in_cck_no_fa = 0;
  931. data->nrg_curr_state = IWL_FA_TOO_MANY;
  932. data->nrg_prev_state = IWL_FA_TOO_MANY;
  933. data->nrg_silence_ref = 0;
  934. data->nrg_silence_idx = 0;
  935. data->nrg_energy_idx = 0;
  936. for (i = 0; i < 10; i++)
  937. data->nrg_value[i] = 0;
  938. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
  939. data->nrg_silence_rssi[i] = 0;
  940. data->auto_corr_ofdm = 90;
  941. data->auto_corr_ofdm_mrc = 170;
  942. data->auto_corr_ofdm_x1 = 105;
  943. data->auto_corr_ofdm_mrc_x1 = 220;
  944. data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
  945. data->auto_corr_cck_mrc = 200;
  946. data->nrg_th_cck = 100;
  947. data->nrg_th_ofdm = 100;
  948. data->last_bad_plcp_cnt_ofdm = 0;
  949. data->last_fa_cnt_ofdm = 0;
  950. data->last_bad_plcp_cnt_cck = 0;
  951. data->last_fa_cnt_cck = 0;
  952. /* Clear prior Sensitivity command data to force send to uCode */
  953. if (force)
  954. memset(&(priv->sensitivity_tbl[0]), 0,
  955. sizeof(u16)*HD_TABLE_SIZE);
  956. rc |= iwl4965_sensitivity_write(priv, flags);
  957. IWL_DEBUG_CALIB("<<return 0x%X\n", rc);
  958. return;
  959. }
  960. /* Reset differential Rx gains in NIC to prepare for chain noise calibration.
  961. * Called after every association, but this runs only once!
  962. * ... once chain noise is calibrated the first time, it's good forever. */
  963. void iwl4965_chain_noise_reset(struct iwl4965_priv *priv)
  964. {
  965. struct iwl4965_chain_noise_data *data = NULL;
  966. int rc = 0;
  967. data = &(priv->chain_noise_data);
  968. if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl4965_is_associated(priv)) {
  969. struct iwl4965_calibration_cmd cmd;
  970. memset(&cmd, 0, sizeof(cmd));
  971. cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
  972. cmd.diff_gain_a = 0;
  973. cmd.diff_gain_b = 0;
  974. cmd.diff_gain_c = 0;
  975. rc = iwl4965_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
  976. sizeof(cmd), &cmd);
  977. msleep(4);
  978. data->state = IWL_CHAIN_NOISE_ACCUMULATE;
  979. IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
  980. }
  981. return;
  982. }
  983. /*
  984. * Accumulate 20 beacons of signal and noise statistics for each of
  985. * 3 receivers/antennas/rx-chains, then figure out:
  986. * 1) Which antennas are connected.
  987. * 2) Differential rx gain settings to balance the 3 receivers.
  988. */
  989. static void iwl4965_noise_calibration(struct iwl4965_priv *priv,
  990. struct iwl4965_notif_statistics *stat_resp)
  991. {
  992. struct iwl4965_chain_noise_data *data = NULL;
  993. int rc = 0;
  994. u32 chain_noise_a;
  995. u32 chain_noise_b;
  996. u32 chain_noise_c;
  997. u32 chain_sig_a;
  998. u32 chain_sig_b;
  999. u32 chain_sig_c;
  1000. u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  1001. u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  1002. u32 max_average_sig;
  1003. u16 max_average_sig_antenna_i;
  1004. u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
  1005. u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
  1006. u16 i = 0;
  1007. u16 chan_num = INITIALIZATION_VALUE;
  1008. u32 band = INITIALIZATION_VALUE;
  1009. u32 active_chains = 0;
  1010. unsigned long flags;
  1011. struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
  1012. data = &(priv->chain_noise_data);
  1013. /* Accumulate just the first 20 beacons after the first association,
  1014. * then we're done forever. */
  1015. if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
  1016. if (data->state == IWL_CHAIN_NOISE_ALIVE)
  1017. IWL_DEBUG_CALIB("Wait for noise calib reset\n");
  1018. return;
  1019. }
  1020. spin_lock_irqsave(&priv->lock, flags);
  1021. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  1022. IWL_DEBUG_CALIB(" << Interference data unavailable\n");
  1023. spin_unlock_irqrestore(&priv->lock, flags);
  1024. return;
  1025. }
  1026. band = (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) ? 0 : 1;
  1027. chan_num = le16_to_cpu(priv->staging_rxon.channel);
  1028. /* Make sure we accumulate data for just the associated channel
  1029. * (even if scanning). */
  1030. if ((chan_num != (le32_to_cpu(stat_resp->flag) >> 16)) ||
  1031. ((STATISTICS_REPLY_FLG_BAND_24G_MSK ==
  1032. (stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK)) && band)) {
  1033. IWL_DEBUG_CALIB("Stats not from chan=%d, band=%d\n",
  1034. chan_num, band);
  1035. spin_unlock_irqrestore(&priv->lock, flags);
  1036. return;
  1037. }
  1038. /* Accumulate beacon statistics values across 20 beacons */
  1039. chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
  1040. IN_BAND_FILTER;
  1041. chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
  1042. IN_BAND_FILTER;
  1043. chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
  1044. IN_BAND_FILTER;
  1045. chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
  1046. chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
  1047. chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
  1048. spin_unlock_irqrestore(&priv->lock, flags);
  1049. data->beacon_count++;
  1050. data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
  1051. data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
  1052. data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
  1053. data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
  1054. data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
  1055. data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
  1056. IWL_DEBUG_CALIB("chan=%d, band=%d, beacon=%d\n", chan_num, band,
  1057. data->beacon_count);
  1058. IWL_DEBUG_CALIB("chain_sig: a %d b %d c %d\n",
  1059. chain_sig_a, chain_sig_b, chain_sig_c);
  1060. IWL_DEBUG_CALIB("chain_noise: a %d b %d c %d\n",
  1061. chain_noise_a, chain_noise_b, chain_noise_c);
  1062. /* If this is the 20th beacon, determine:
  1063. * 1) Disconnected antennas (using signal strengths)
  1064. * 2) Differential gain (using silence noise) to balance receivers */
  1065. if (data->beacon_count == CAL_NUM_OF_BEACONS) {
  1066. /* Analyze signal for disconnected antenna */
  1067. average_sig[0] = (data->chain_signal_a) / CAL_NUM_OF_BEACONS;
  1068. average_sig[1] = (data->chain_signal_b) / CAL_NUM_OF_BEACONS;
  1069. average_sig[2] = (data->chain_signal_c) / CAL_NUM_OF_BEACONS;
  1070. if (average_sig[0] >= average_sig[1]) {
  1071. max_average_sig = average_sig[0];
  1072. max_average_sig_antenna_i = 0;
  1073. active_chains = (1 << max_average_sig_antenna_i);
  1074. } else {
  1075. max_average_sig = average_sig[1];
  1076. max_average_sig_antenna_i = 1;
  1077. active_chains = (1 << max_average_sig_antenna_i);
  1078. }
  1079. if (average_sig[2] >= max_average_sig) {
  1080. max_average_sig = average_sig[2];
  1081. max_average_sig_antenna_i = 2;
  1082. active_chains = (1 << max_average_sig_antenna_i);
  1083. }
  1084. IWL_DEBUG_CALIB("average_sig: a %d b %d c %d\n",
  1085. average_sig[0], average_sig[1], average_sig[2]);
  1086. IWL_DEBUG_CALIB("max_average_sig = %d, antenna %d\n",
  1087. max_average_sig, max_average_sig_antenna_i);
  1088. /* Compare signal strengths for all 3 receivers. */
  1089. for (i = 0; i < NUM_RX_CHAINS; i++) {
  1090. if (i != max_average_sig_antenna_i) {
  1091. s32 rssi_delta = (max_average_sig -
  1092. average_sig[i]);
  1093. /* If signal is very weak, compared with
  1094. * strongest, mark it as disconnected. */
  1095. if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
  1096. data->disconn_array[i] = 1;
  1097. else
  1098. active_chains |= (1 << i);
  1099. IWL_DEBUG_CALIB("i = %d rssiDelta = %d "
  1100. "disconn_array[i] = %d\n",
  1101. i, rssi_delta, data->disconn_array[i]);
  1102. }
  1103. }
  1104. /*If both chains A & B are disconnected -
  1105. * connect B and leave A as is */
  1106. if (data->disconn_array[CHAIN_A] &&
  1107. data->disconn_array[CHAIN_B]) {
  1108. data->disconn_array[CHAIN_B] = 0;
  1109. active_chains |= (1 << CHAIN_B);
  1110. IWL_DEBUG_CALIB("both A & B chains are disconnected! "
  1111. "W/A - declare B as connected\n");
  1112. }
  1113. IWL_DEBUG_CALIB("active_chains (bitwise) = 0x%x\n",
  1114. active_chains);
  1115. /* Save for use within RXON, TX, SCAN commands, etc. */
  1116. priv->valid_antenna = active_chains;
  1117. /* Analyze noise for rx balance */
  1118. average_noise[0] = ((data->chain_noise_a)/CAL_NUM_OF_BEACONS);
  1119. average_noise[1] = ((data->chain_noise_b)/CAL_NUM_OF_BEACONS);
  1120. average_noise[2] = ((data->chain_noise_c)/CAL_NUM_OF_BEACONS);
  1121. for (i = 0; i < NUM_RX_CHAINS; i++) {
  1122. if (!(data->disconn_array[i]) &&
  1123. (average_noise[i] <= min_average_noise)) {
  1124. /* This means that chain i is active and has
  1125. * lower noise values so far: */
  1126. min_average_noise = average_noise[i];
  1127. min_average_noise_antenna_i = i;
  1128. }
  1129. }
  1130. data->delta_gain_code[min_average_noise_antenna_i] = 0;
  1131. IWL_DEBUG_CALIB("average_noise: a %d b %d c %d\n",
  1132. average_noise[0], average_noise[1],
  1133. average_noise[2]);
  1134. IWL_DEBUG_CALIB("min_average_noise = %d, antenna %d\n",
  1135. min_average_noise, min_average_noise_antenna_i);
  1136. for (i = 0; i < NUM_RX_CHAINS; i++) {
  1137. s32 delta_g = 0;
  1138. if (!(data->disconn_array[i]) &&
  1139. (data->delta_gain_code[i] ==
  1140. CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
  1141. delta_g = average_noise[i] - min_average_noise;
  1142. data->delta_gain_code[i] = (u8)((delta_g *
  1143. 10) / 15);
  1144. if (CHAIN_NOISE_MAX_DELTA_GAIN_CODE <
  1145. data->delta_gain_code[i])
  1146. data->delta_gain_code[i] =
  1147. CHAIN_NOISE_MAX_DELTA_GAIN_CODE;
  1148. data->delta_gain_code[i] =
  1149. (data->delta_gain_code[i] | (1 << 2));
  1150. } else
  1151. data->delta_gain_code[i] = 0;
  1152. }
  1153. IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
  1154. data->delta_gain_code[0],
  1155. data->delta_gain_code[1],
  1156. data->delta_gain_code[2]);
  1157. /* Differential gain gets sent to uCode only once */
  1158. if (!data->radio_write) {
  1159. struct iwl4965_calibration_cmd cmd;
  1160. data->radio_write = 1;
  1161. memset(&cmd, 0, sizeof(cmd));
  1162. cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
  1163. cmd.diff_gain_a = data->delta_gain_code[0];
  1164. cmd.diff_gain_b = data->delta_gain_code[1];
  1165. cmd.diff_gain_c = data->delta_gain_code[2];
  1166. rc = iwl4965_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
  1167. sizeof(cmd), &cmd);
  1168. if (rc)
  1169. IWL_DEBUG_CALIB("fail sending cmd "
  1170. "REPLY_PHY_CALIBRATION_CMD \n");
  1171. /* TODO we might want recalculate
  1172. * rx_chain in rxon cmd */
  1173. /* Mark so we run this algo only once! */
  1174. data->state = IWL_CHAIN_NOISE_CALIBRATED;
  1175. }
  1176. data->chain_noise_a = 0;
  1177. data->chain_noise_b = 0;
  1178. data->chain_noise_c = 0;
  1179. data->chain_signal_a = 0;
  1180. data->chain_signal_b = 0;
  1181. data->chain_signal_c = 0;
  1182. data->beacon_count = 0;
  1183. }
  1184. return;
  1185. }
  1186. static void iwl4965_sensitivity_calibration(struct iwl4965_priv *priv,
  1187. struct iwl4965_notif_statistics *resp)
  1188. {
  1189. int rc = 0;
  1190. u32 rx_enable_time;
  1191. u32 fa_cck;
  1192. u32 fa_ofdm;
  1193. u32 bad_plcp_cck;
  1194. u32 bad_plcp_ofdm;
  1195. u32 norm_fa_ofdm;
  1196. u32 norm_fa_cck;
  1197. struct iwl4965_sensitivity_data *data = NULL;
  1198. struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
  1199. struct statistics_rx *statistics = &(resp->rx);
  1200. unsigned long flags;
  1201. struct statistics_general_data statis;
  1202. data = &(priv->sensitivity_data);
  1203. if (!iwl4965_is_associated(priv)) {
  1204. IWL_DEBUG_CALIB("<< - not associated\n");
  1205. return;
  1206. }
  1207. spin_lock_irqsave(&priv->lock, flags);
  1208. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  1209. IWL_DEBUG_CALIB("<< invalid data.\n");
  1210. spin_unlock_irqrestore(&priv->lock, flags);
  1211. return;
  1212. }
  1213. /* Extract Statistics: */
  1214. rx_enable_time = le32_to_cpu(rx_info->channel_load);
  1215. fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
  1216. fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
  1217. bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
  1218. bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
  1219. statis.beacon_silence_rssi_a =
  1220. le32_to_cpu(statistics->general.beacon_silence_rssi_a);
  1221. statis.beacon_silence_rssi_b =
  1222. le32_to_cpu(statistics->general.beacon_silence_rssi_b);
  1223. statis.beacon_silence_rssi_c =
  1224. le32_to_cpu(statistics->general.beacon_silence_rssi_c);
  1225. statis.beacon_energy_a =
  1226. le32_to_cpu(statistics->general.beacon_energy_a);
  1227. statis.beacon_energy_b =
  1228. le32_to_cpu(statistics->general.beacon_energy_b);
  1229. statis.beacon_energy_c =
  1230. le32_to_cpu(statistics->general.beacon_energy_c);
  1231. spin_unlock_irqrestore(&priv->lock, flags);
  1232. IWL_DEBUG_CALIB("rx_enable_time = %u usecs\n", rx_enable_time);
  1233. if (!rx_enable_time) {
  1234. IWL_DEBUG_CALIB("<< RX Enable Time == 0! \n");
  1235. return;
  1236. }
  1237. /* These statistics increase monotonically, and do not reset
  1238. * at each beacon. Calculate difference from last value, or just
  1239. * use the new statistics value if it has reset or wrapped around. */
  1240. if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
  1241. data->last_bad_plcp_cnt_cck = bad_plcp_cck;
  1242. else {
  1243. bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
  1244. data->last_bad_plcp_cnt_cck += bad_plcp_cck;
  1245. }
  1246. if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
  1247. data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
  1248. else {
  1249. bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
  1250. data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
  1251. }
  1252. if (data->last_fa_cnt_ofdm > fa_ofdm)
  1253. data->last_fa_cnt_ofdm = fa_ofdm;
  1254. else {
  1255. fa_ofdm -= data->last_fa_cnt_ofdm;
  1256. data->last_fa_cnt_ofdm += fa_ofdm;
  1257. }
  1258. if (data->last_fa_cnt_cck > fa_cck)
  1259. data->last_fa_cnt_cck = fa_cck;
  1260. else {
  1261. fa_cck -= data->last_fa_cnt_cck;
  1262. data->last_fa_cnt_cck += fa_cck;
  1263. }
  1264. /* Total aborted signal locks */
  1265. norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
  1266. norm_fa_cck = fa_cck + bad_plcp_cck;
  1267. IWL_DEBUG_CALIB("cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
  1268. bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
  1269. iwl4965_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
  1270. iwl4965_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
  1271. rc |= iwl4965_sensitivity_write(priv, CMD_ASYNC);
  1272. return;
  1273. }
  1274. static void iwl4965_bg_sensitivity_work(struct work_struct *work)
  1275. {
  1276. struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
  1277. sensitivity_work);
  1278. mutex_lock(&priv->mutex);
  1279. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  1280. test_bit(STATUS_SCANNING, &priv->status)) {
  1281. mutex_unlock(&priv->mutex);
  1282. return;
  1283. }
  1284. if (priv->start_calib) {
  1285. iwl4965_noise_calibration(priv, &priv->statistics);
  1286. if (priv->sensitivity_data.state ==
  1287. IWL_SENS_CALIB_NEED_REINIT) {
  1288. iwl4965_init_sensitivity(priv, CMD_ASYNC, 0);
  1289. priv->sensitivity_data.state = IWL_SENS_CALIB_ALLOWED;
  1290. } else
  1291. iwl4965_sensitivity_calibration(priv,
  1292. &priv->statistics);
  1293. }
  1294. mutex_unlock(&priv->mutex);
  1295. return;
  1296. }
  1297. #endif /*CONFIG_IWL4965_SENSITIVITY*/
  1298. static void iwl4965_bg_txpower_work(struct work_struct *work)
  1299. {
  1300. struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
  1301. txpower_work);
  1302. /* If a scan happened to start before we got here
  1303. * then just return; the statistics notification will
  1304. * kick off another scheduled work to compensate for
  1305. * any temperature delta we missed here. */
  1306. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  1307. test_bit(STATUS_SCANNING, &priv->status))
  1308. return;
  1309. mutex_lock(&priv->mutex);
  1310. /* Regardless of if we are assocaited, we must reconfigure the
  1311. * TX power since frames can be sent on non-radar channels while
  1312. * not associated */
  1313. iwl4965_hw_reg_send_txpower(priv);
  1314. /* Update last_temperature to keep is_calib_needed from running
  1315. * when it isn't needed... */
  1316. priv->last_temperature = priv->temperature;
  1317. mutex_unlock(&priv->mutex);
  1318. }
  1319. /*
  1320. * Acquire priv->lock before calling this function !
  1321. */
  1322. static void iwl4965_set_wr_ptrs(struct iwl4965_priv *priv, int txq_id, u32 index)
  1323. {
  1324. iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
  1325. (index & 0xff) | (txq_id << 8));
  1326. iwl4965_write_prph(priv, KDR_SCD_QUEUE_RDPTR(txq_id), index);
  1327. }
  1328. /**
  1329. * iwl4965_tx_queue_set_status - (optionally) start Tx/Cmd queue
  1330. * @tx_fifo_id: Tx DMA/FIFO channel (range 0-7) that the queue will feed
  1331. * @scd_retry: (1) Indicates queue will be used in aggregation mode
  1332. *
  1333. * NOTE: Acquire priv->lock before calling this function !
  1334. */
  1335. static void iwl4965_tx_queue_set_status(struct iwl4965_priv *priv,
  1336. struct iwl4965_tx_queue *txq,
  1337. int tx_fifo_id, int scd_retry)
  1338. {
  1339. int txq_id = txq->q.id;
  1340. /* Find out whether to activate Tx queue */
  1341. int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
  1342. /* Set up and activate */
  1343. iwl4965_write_prph(priv, KDR_SCD_QUEUE_STATUS_BITS(txq_id),
  1344. (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  1345. (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
  1346. (scd_retry << SCD_QUEUE_STTS_REG_POS_WSL) |
  1347. (scd_retry << SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
  1348. SCD_QUEUE_STTS_REG_MSK);
  1349. txq->sched_retry = scd_retry;
  1350. IWL_DEBUG_INFO("%s %s Queue %d on AC %d\n",
  1351. active ? "Activate" : "Deactivate",
  1352. scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
  1353. }
  1354. static const u16 default_queue_to_tx_fifo[] = {
  1355. IWL_TX_FIFO_AC3,
  1356. IWL_TX_FIFO_AC2,
  1357. IWL_TX_FIFO_AC1,
  1358. IWL_TX_FIFO_AC0,
  1359. IWL_CMD_FIFO_NUM,
  1360. IWL_TX_FIFO_HCCA_1,
  1361. IWL_TX_FIFO_HCCA_2
  1362. };
  1363. static inline void iwl4965_txq_ctx_activate(struct iwl4965_priv *priv, int txq_id)
  1364. {
  1365. set_bit(txq_id, &priv->txq_ctx_active_msk);
  1366. }
  1367. static inline void iwl4965_txq_ctx_deactivate(struct iwl4965_priv *priv, int txq_id)
  1368. {
  1369. clear_bit(txq_id, &priv->txq_ctx_active_msk);
  1370. }
  1371. int iwl4965_alive_notify(struct iwl4965_priv *priv)
  1372. {
  1373. u32 a;
  1374. int i = 0;
  1375. unsigned long flags;
  1376. int rc;
  1377. spin_lock_irqsave(&priv->lock, flags);
  1378. #ifdef CONFIG_IWL4965_SENSITIVITY
  1379. memset(&(priv->sensitivity_data), 0,
  1380. sizeof(struct iwl4965_sensitivity_data));
  1381. memset(&(priv->chain_noise_data), 0,
  1382. sizeof(struct iwl4965_chain_noise_data));
  1383. for (i = 0; i < NUM_RX_CHAINS; i++)
  1384. priv->chain_noise_data.delta_gain_code[i] =
  1385. CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
  1386. #endif /* CONFIG_IWL4965_SENSITIVITY*/
  1387. rc = iwl4965_grab_nic_access(priv);
  1388. if (rc) {
  1389. spin_unlock_irqrestore(&priv->lock, flags);
  1390. return rc;
  1391. }
  1392. /* Clear 4965's internal Tx Scheduler data base */
  1393. priv->scd_base_addr = iwl4965_read_prph(priv, KDR_SCD_SRAM_BASE_ADDR);
  1394. a = priv->scd_base_addr + SCD_CONTEXT_DATA_OFFSET;
  1395. for (; a < priv->scd_base_addr + SCD_TX_STTS_BITMAP_OFFSET; a += 4)
  1396. iwl4965_write_targ_mem(priv, a, 0);
  1397. for (; a < priv->scd_base_addr + SCD_TRANSLATE_TBL_OFFSET; a += 4)
  1398. iwl4965_write_targ_mem(priv, a, 0);
  1399. for (; a < sizeof(u16) * priv->hw_setting.max_txq_num; a += 4)
  1400. iwl4965_write_targ_mem(priv, a, 0);
  1401. /* Tel 4965 where to find Tx byte count tables */
  1402. iwl4965_write_prph(priv, KDR_SCD_DRAM_BASE_ADDR,
  1403. (priv->hw_setting.shared_phys +
  1404. offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
  1405. /* Disable chain mode for all queues */
  1406. iwl4965_write_prph(priv, KDR_SCD_QUEUECHAIN_SEL, 0);
  1407. /* Initialize each Tx queue (including the command queue) */
  1408. for (i = 0; i < priv->hw_setting.max_txq_num; i++) {
  1409. /* TFD circular buffer read/write indexes */
  1410. iwl4965_write_prph(priv, KDR_SCD_QUEUE_RDPTR(i), 0);
  1411. iwl4965_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
  1412. /* Max Tx Window size for Scheduler-ACK mode */
  1413. iwl4965_write_targ_mem(priv, priv->scd_base_addr +
  1414. SCD_CONTEXT_QUEUE_OFFSET(i),
  1415. (SCD_WIN_SIZE <<
  1416. SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  1417. SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  1418. /* Frame limit */
  1419. iwl4965_write_targ_mem(priv, priv->scd_base_addr +
  1420. SCD_CONTEXT_QUEUE_OFFSET(i) +
  1421. sizeof(u32),
  1422. (SCD_FRAME_LIMIT <<
  1423. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  1424. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  1425. }
  1426. iwl4965_write_prph(priv, KDR_SCD_INTERRUPT_MASK,
  1427. (1 << priv->hw_setting.max_txq_num) - 1);
  1428. /* Activate all Tx DMA/FIFO channels */
  1429. iwl4965_write_prph(priv, KDR_SCD_TXFACT,
  1430. SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
  1431. iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
  1432. /* Map each Tx/cmd queue to its corresponding fifo */
  1433. for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
  1434. int ac = default_queue_to_tx_fifo[i];
  1435. iwl4965_txq_ctx_activate(priv, i);
  1436. iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
  1437. }
  1438. iwl4965_release_nic_access(priv);
  1439. spin_unlock_irqrestore(&priv->lock, flags);
  1440. return 0;
  1441. }
  1442. /**
  1443. * iwl4965_hw_set_hw_setting
  1444. *
  1445. * Called when initializing driver
  1446. */
  1447. int iwl4965_hw_set_hw_setting(struct iwl4965_priv *priv)
  1448. {
  1449. /* Allocate area for Tx byte count tables and Rx queue status */
  1450. priv->hw_setting.shared_virt =
  1451. pci_alloc_consistent(priv->pci_dev,
  1452. sizeof(struct iwl4965_shared),
  1453. &priv->hw_setting.shared_phys);
  1454. if (!priv->hw_setting.shared_virt)
  1455. return -1;
  1456. memset(priv->hw_setting.shared_virt, 0, sizeof(struct iwl4965_shared));
  1457. priv->hw_setting.max_txq_num = iwl4965_param_queues_num;
  1458. priv->hw_setting.ac_queue_count = AC_NUM;
  1459. priv->hw_setting.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
  1460. priv->hw_setting.max_rxq_size = RX_QUEUE_SIZE;
  1461. priv->hw_setting.max_rxq_log = RX_QUEUE_SIZE_LOG;
  1462. if (iwl4965_param_amsdu_size_8K)
  1463. priv->hw_setting.rx_buf_size = IWL_RX_BUF_SIZE_8K;
  1464. else
  1465. priv->hw_setting.rx_buf_size = IWL_RX_BUF_SIZE_4K;
  1466. priv->hw_setting.max_pkt_size = priv->hw_setting.rx_buf_size - 256;
  1467. priv->hw_setting.max_stations = IWL4965_STATION_COUNT;
  1468. priv->hw_setting.bcast_sta_id = IWL4965_BROADCAST_ID;
  1469. return 0;
  1470. }
  1471. /**
  1472. * iwl4965_hw_txq_ctx_free - Free TXQ Context
  1473. *
  1474. * Destroy all TX DMA queues and structures
  1475. */
  1476. void iwl4965_hw_txq_ctx_free(struct iwl4965_priv *priv)
  1477. {
  1478. int txq_id;
  1479. /* Tx queues */
  1480. for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++)
  1481. iwl4965_tx_queue_free(priv, &priv->txq[txq_id]);
  1482. /* Keep-warm buffer */
  1483. iwl4965_kw_free(priv);
  1484. }
  1485. /**
  1486. * iwl4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  1487. *
  1488. * Does NOT advance any TFD circular buffer read/write indexes
  1489. * Does NOT free the TFD itself (which is within circular buffer)
  1490. */
  1491. int iwl4965_hw_txq_free_tfd(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
  1492. {
  1493. struct iwl4965_tfd_frame *bd_tmp = (struct iwl4965_tfd_frame *)&txq->bd[0];
  1494. struct iwl4965_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
  1495. struct pci_dev *dev = priv->pci_dev;
  1496. int i;
  1497. int counter = 0;
  1498. int index, is_odd;
  1499. /* Host command buffers stay mapped in memory, nothing to clean */
  1500. if (txq->q.id == IWL_CMD_QUEUE_NUM)
  1501. return 0;
  1502. /* Sanity check on number of chunks */
  1503. counter = IWL_GET_BITS(*bd, num_tbs);
  1504. if (counter > MAX_NUM_OF_TBS) {
  1505. IWL_ERROR("Too many chunks: %i\n", counter);
  1506. /* @todo issue fatal error, it is quite serious situation */
  1507. return 0;
  1508. }
  1509. /* Unmap chunks, if any.
  1510. * TFD info for odd chunks is different format than for even chunks. */
  1511. for (i = 0; i < counter; i++) {
  1512. index = i / 2;
  1513. is_odd = i & 0x1;
  1514. if (is_odd)
  1515. pci_unmap_single(
  1516. dev,
  1517. IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
  1518. (IWL_GET_BITS(bd->pa[index],
  1519. tb2_addr_hi20) << 16),
  1520. IWL_GET_BITS(bd->pa[index], tb2_len),
  1521. PCI_DMA_TODEVICE);
  1522. else if (i > 0)
  1523. pci_unmap_single(dev,
  1524. le32_to_cpu(bd->pa[index].tb1_addr),
  1525. IWL_GET_BITS(bd->pa[index], tb1_len),
  1526. PCI_DMA_TODEVICE);
  1527. /* Free SKB, if any, for this chunk */
  1528. if (txq->txb[txq->q.read_ptr].skb[i]) {
  1529. struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[i];
  1530. dev_kfree_skb(skb);
  1531. txq->txb[txq->q.read_ptr].skb[i] = NULL;
  1532. }
  1533. }
  1534. return 0;
  1535. }
  1536. int iwl4965_hw_reg_set_txpower(struct iwl4965_priv *priv, s8 power)
  1537. {
  1538. IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
  1539. return -EINVAL;
  1540. }
  1541. static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
  1542. {
  1543. s32 sign = 1;
  1544. if (num < 0) {
  1545. sign = -sign;
  1546. num = -num;
  1547. }
  1548. if (denom < 0) {
  1549. sign = -sign;
  1550. denom = -denom;
  1551. }
  1552. *res = 1;
  1553. *res = ((num * 2 + denom) / (denom * 2)) * sign;
  1554. return 1;
  1555. }
  1556. /**
  1557. * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
  1558. *
  1559. * Determines power supply voltage compensation for txpower calculations.
  1560. * Returns number of 1/2-dB steps to subtract from gain table index,
  1561. * to compensate for difference between power supply voltage during
  1562. * factory measurements, vs. current power supply voltage.
  1563. *
  1564. * Voltage indication is higher for lower voltage.
  1565. * Lower voltage requires more gain (lower gain table index).
  1566. */
  1567. static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
  1568. s32 current_voltage)
  1569. {
  1570. s32 comp = 0;
  1571. if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
  1572. (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
  1573. return 0;
  1574. iwl4965_math_div_round(current_voltage - eeprom_voltage,
  1575. TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
  1576. if (current_voltage > eeprom_voltage)
  1577. comp *= 2;
  1578. if ((comp < -2) || (comp > 2))
  1579. comp = 0;
  1580. return comp;
  1581. }
  1582. static const struct iwl4965_channel_info *
  1583. iwl4965_get_channel_txpower_info(struct iwl4965_priv *priv, u8 phymode, u16 channel)
  1584. {
  1585. const struct iwl4965_channel_info *ch_info;
  1586. ch_info = iwl4965_get_channel_info(priv, phymode, channel);
  1587. if (!is_channel_valid(ch_info))
  1588. return NULL;
  1589. return ch_info;
  1590. }
  1591. static s32 iwl4965_get_tx_atten_grp(u16 channel)
  1592. {
  1593. if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
  1594. channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
  1595. return CALIB_CH_GROUP_5;
  1596. if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
  1597. channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
  1598. return CALIB_CH_GROUP_1;
  1599. if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
  1600. channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
  1601. return CALIB_CH_GROUP_2;
  1602. if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
  1603. channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
  1604. return CALIB_CH_GROUP_3;
  1605. if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
  1606. channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
  1607. return CALIB_CH_GROUP_4;
  1608. IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
  1609. return -1;
  1610. }
  1611. static u32 iwl4965_get_sub_band(const struct iwl4965_priv *priv, u32 channel)
  1612. {
  1613. s32 b = -1;
  1614. for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
  1615. if (priv->eeprom.calib_info.band_info[b].ch_from == 0)
  1616. continue;
  1617. if ((channel >= priv->eeprom.calib_info.band_info[b].ch_from)
  1618. && (channel <= priv->eeprom.calib_info.band_info[b].ch_to))
  1619. break;
  1620. }
  1621. return b;
  1622. }
  1623. static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
  1624. {
  1625. s32 val;
  1626. if (x2 == x1)
  1627. return y1;
  1628. else {
  1629. iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
  1630. return val + y2;
  1631. }
  1632. }
  1633. /**
  1634. * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
  1635. *
  1636. * Interpolates factory measurements from the two sample channels within a
  1637. * sub-band, to apply to channel of interest. Interpolation is proportional to
  1638. * differences in channel frequencies, which is proportional to differences
  1639. * in channel number.
  1640. */
  1641. static int iwl4965_interpolate_chan(struct iwl4965_priv *priv, u32 channel,
  1642. struct iwl4965_eeprom_calib_ch_info *chan_info)
  1643. {
  1644. s32 s = -1;
  1645. u32 c;
  1646. u32 m;
  1647. const struct iwl4965_eeprom_calib_measure *m1;
  1648. const struct iwl4965_eeprom_calib_measure *m2;
  1649. struct iwl4965_eeprom_calib_measure *omeas;
  1650. u32 ch_i1;
  1651. u32 ch_i2;
  1652. s = iwl4965_get_sub_band(priv, channel);
  1653. if (s >= EEPROM_TX_POWER_BANDS) {
  1654. IWL_ERROR("Tx Power can not find channel %d ", channel);
  1655. return -1;
  1656. }
  1657. ch_i1 = priv->eeprom.calib_info.band_info[s].ch1.ch_num;
  1658. ch_i2 = priv->eeprom.calib_info.band_info[s].ch2.ch_num;
  1659. chan_info->ch_num = (u8) channel;
  1660. IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
  1661. channel, s, ch_i1, ch_i2);
  1662. for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
  1663. for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
  1664. m1 = &(priv->eeprom.calib_info.band_info[s].ch1.
  1665. measurements[c][m]);
  1666. m2 = &(priv->eeprom.calib_info.band_info[s].ch2.
  1667. measurements[c][m]);
  1668. omeas = &(chan_info->measurements[c][m]);
  1669. omeas->actual_pow =
  1670. (u8) iwl4965_interpolate_value(channel, ch_i1,
  1671. m1->actual_pow,
  1672. ch_i2,
  1673. m2->actual_pow);
  1674. omeas->gain_idx =
  1675. (u8) iwl4965_interpolate_value(channel, ch_i1,
  1676. m1->gain_idx, ch_i2,
  1677. m2->gain_idx);
  1678. omeas->temperature =
  1679. (u8) iwl4965_interpolate_value(channel, ch_i1,
  1680. m1->temperature,
  1681. ch_i2,
  1682. m2->temperature);
  1683. omeas->pa_det =
  1684. (s8) iwl4965_interpolate_value(channel, ch_i1,
  1685. m1->pa_det, ch_i2,
  1686. m2->pa_det);
  1687. IWL_DEBUG_TXPOWER
  1688. ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
  1689. m1->actual_pow, m2->actual_pow, omeas->actual_pow);
  1690. IWL_DEBUG_TXPOWER
  1691. ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
  1692. m1->gain_idx, m2->gain_idx, omeas->gain_idx);
  1693. IWL_DEBUG_TXPOWER
  1694. ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
  1695. m1->pa_det, m2->pa_det, omeas->pa_det);
  1696. IWL_DEBUG_TXPOWER
  1697. ("chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
  1698. m1->temperature, m2->temperature,
  1699. omeas->temperature);
  1700. }
  1701. }
  1702. return 0;
  1703. }
  1704. /* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
  1705. * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
  1706. static s32 back_off_table[] = {
  1707. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
  1708. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
  1709. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
  1710. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
  1711. 10 /* CCK */
  1712. };
  1713. /* Thermal compensation values for txpower for various frequency ranges ...
  1714. * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
  1715. static struct iwl4965_txpower_comp_entry {
  1716. s32 degrees_per_05db_a;
  1717. s32 degrees_per_05db_a_denom;
  1718. } tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
  1719. {9, 2}, /* group 0 5.2, ch 34-43 */
  1720. {4, 1}, /* group 1 5.2, ch 44-70 */
  1721. {4, 1}, /* group 2 5.2, ch 71-124 */
  1722. {4, 1}, /* group 3 5.2, ch 125-200 */
  1723. {3, 1} /* group 4 2.4, ch all */
  1724. };
  1725. static s32 get_min_power_index(s32 rate_power_index, u32 band)
  1726. {
  1727. if (!band) {
  1728. if ((rate_power_index & 7) <= 4)
  1729. return MIN_TX_GAIN_INDEX_52GHZ_EXT;
  1730. }
  1731. return MIN_TX_GAIN_INDEX;
  1732. }
  1733. struct gain_entry {
  1734. u8 dsp;
  1735. u8 radio;
  1736. };
  1737. static const struct gain_entry gain_table[2][108] = {
  1738. /* 5.2GHz power gain index table */
  1739. {
  1740. {123, 0x3F}, /* highest txpower */
  1741. {117, 0x3F},
  1742. {110, 0x3F},
  1743. {104, 0x3F},
  1744. {98, 0x3F},
  1745. {110, 0x3E},
  1746. {104, 0x3E},
  1747. {98, 0x3E},
  1748. {110, 0x3D},
  1749. {104, 0x3D},
  1750. {98, 0x3D},
  1751. {110, 0x3C},
  1752. {104, 0x3C},
  1753. {98, 0x3C},
  1754. {110, 0x3B},
  1755. {104, 0x3B},
  1756. {98, 0x3B},
  1757. {110, 0x3A},
  1758. {104, 0x3A},
  1759. {98, 0x3A},
  1760. {110, 0x39},
  1761. {104, 0x39},
  1762. {98, 0x39},
  1763. {110, 0x38},
  1764. {104, 0x38},
  1765. {98, 0x38},
  1766. {110, 0x37},
  1767. {104, 0x37},
  1768. {98, 0x37},
  1769. {110, 0x36},
  1770. {104, 0x36},
  1771. {98, 0x36},
  1772. {110, 0x35},
  1773. {104, 0x35},
  1774. {98, 0x35},
  1775. {110, 0x34},
  1776. {104, 0x34},
  1777. {98, 0x34},
  1778. {110, 0x33},
  1779. {104, 0x33},
  1780. {98, 0x33},
  1781. {110, 0x32},
  1782. {104, 0x32},
  1783. {98, 0x32},
  1784. {110, 0x31},
  1785. {104, 0x31},
  1786. {98, 0x31},
  1787. {110, 0x30},
  1788. {104, 0x30},
  1789. {98, 0x30},
  1790. {110, 0x25},
  1791. {104, 0x25},
  1792. {98, 0x25},
  1793. {110, 0x24},
  1794. {104, 0x24},
  1795. {98, 0x24},
  1796. {110, 0x23},
  1797. {104, 0x23},
  1798. {98, 0x23},
  1799. {110, 0x22},
  1800. {104, 0x18},
  1801. {98, 0x18},
  1802. {110, 0x17},
  1803. {104, 0x17},
  1804. {98, 0x17},
  1805. {110, 0x16},
  1806. {104, 0x16},
  1807. {98, 0x16},
  1808. {110, 0x15},
  1809. {104, 0x15},
  1810. {98, 0x15},
  1811. {110, 0x14},
  1812. {104, 0x14},
  1813. {98, 0x14},
  1814. {110, 0x13},
  1815. {104, 0x13},
  1816. {98, 0x13},
  1817. {110, 0x12},
  1818. {104, 0x08},
  1819. {98, 0x08},
  1820. {110, 0x07},
  1821. {104, 0x07},
  1822. {98, 0x07},
  1823. {110, 0x06},
  1824. {104, 0x06},
  1825. {98, 0x06},
  1826. {110, 0x05},
  1827. {104, 0x05},
  1828. {98, 0x05},
  1829. {110, 0x04},
  1830. {104, 0x04},
  1831. {98, 0x04},
  1832. {110, 0x03},
  1833. {104, 0x03},
  1834. {98, 0x03},
  1835. {110, 0x02},
  1836. {104, 0x02},
  1837. {98, 0x02},
  1838. {110, 0x01},
  1839. {104, 0x01},
  1840. {98, 0x01},
  1841. {110, 0x00},
  1842. {104, 0x00},
  1843. {98, 0x00},
  1844. {93, 0x00},
  1845. {88, 0x00},
  1846. {83, 0x00},
  1847. {78, 0x00},
  1848. },
  1849. /* 2.4GHz power gain index table */
  1850. {
  1851. {110, 0x3f}, /* highest txpower */
  1852. {104, 0x3f},
  1853. {98, 0x3f},
  1854. {110, 0x3e},
  1855. {104, 0x3e},
  1856. {98, 0x3e},
  1857. {110, 0x3d},
  1858. {104, 0x3d},
  1859. {98, 0x3d},
  1860. {110, 0x3c},
  1861. {104, 0x3c},
  1862. {98, 0x3c},
  1863. {110, 0x3b},
  1864. {104, 0x3b},
  1865. {98, 0x3b},
  1866. {110, 0x3a},
  1867. {104, 0x3a},
  1868. {98, 0x3a},
  1869. {110, 0x39},
  1870. {104, 0x39},
  1871. {98, 0x39},
  1872. {110, 0x38},
  1873. {104, 0x38},
  1874. {98, 0x38},
  1875. {110, 0x37},
  1876. {104, 0x37},
  1877. {98, 0x37},
  1878. {110, 0x36},
  1879. {104, 0x36},
  1880. {98, 0x36},
  1881. {110, 0x35},
  1882. {104, 0x35},
  1883. {98, 0x35},
  1884. {110, 0x34},
  1885. {104, 0x34},
  1886. {98, 0x34},
  1887. {110, 0x33},
  1888. {104, 0x33},
  1889. {98, 0x33},
  1890. {110, 0x32},
  1891. {104, 0x32},
  1892. {98, 0x32},
  1893. {110, 0x31},
  1894. {104, 0x31},
  1895. {98, 0x31},
  1896. {110, 0x30},
  1897. {104, 0x30},
  1898. {98, 0x30},
  1899. {110, 0x6},
  1900. {104, 0x6},
  1901. {98, 0x6},
  1902. {110, 0x5},
  1903. {104, 0x5},
  1904. {98, 0x5},
  1905. {110, 0x4},
  1906. {104, 0x4},
  1907. {98, 0x4},
  1908. {110, 0x3},
  1909. {104, 0x3},
  1910. {98, 0x3},
  1911. {110, 0x2},
  1912. {104, 0x2},
  1913. {98, 0x2},
  1914. {110, 0x1},
  1915. {104, 0x1},
  1916. {98, 0x1},
  1917. {110, 0x0},
  1918. {104, 0x0},
  1919. {98, 0x0},
  1920. {97, 0},
  1921. {96, 0},
  1922. {95, 0},
  1923. {94, 0},
  1924. {93, 0},
  1925. {92, 0},
  1926. {91, 0},
  1927. {90, 0},
  1928. {89, 0},
  1929. {88, 0},
  1930. {87, 0},
  1931. {86, 0},
  1932. {85, 0},
  1933. {84, 0},
  1934. {83, 0},
  1935. {82, 0},
  1936. {81, 0},
  1937. {80, 0},
  1938. {79, 0},
  1939. {78, 0},
  1940. {77, 0},
  1941. {76, 0},
  1942. {75, 0},
  1943. {74, 0},
  1944. {73, 0},
  1945. {72, 0},
  1946. {71, 0},
  1947. {70, 0},
  1948. {69, 0},
  1949. {68, 0},
  1950. {67, 0},
  1951. {66, 0},
  1952. {65, 0},
  1953. {64, 0},
  1954. {63, 0},
  1955. {62, 0},
  1956. {61, 0},
  1957. {60, 0},
  1958. {59, 0},
  1959. }
  1960. };
  1961. static int iwl4965_fill_txpower_tbl(struct iwl4965_priv *priv, u8 band, u16 channel,
  1962. u8 is_fat, u8 ctrl_chan_high,
  1963. struct iwl4965_tx_power_db *tx_power_tbl)
  1964. {
  1965. u8 saturation_power;
  1966. s32 target_power;
  1967. s32 user_target_power;
  1968. s32 power_limit;
  1969. s32 current_temp;
  1970. s32 reg_limit;
  1971. s32 current_regulatory;
  1972. s32 txatten_grp = CALIB_CH_GROUP_MAX;
  1973. int i;
  1974. int c;
  1975. const struct iwl4965_channel_info *ch_info = NULL;
  1976. struct iwl4965_eeprom_calib_ch_info ch_eeprom_info;
  1977. const struct iwl4965_eeprom_calib_measure *measurement;
  1978. s16 voltage;
  1979. s32 init_voltage;
  1980. s32 voltage_compensation;
  1981. s32 degrees_per_05db_num;
  1982. s32 degrees_per_05db_denom;
  1983. s32 factory_temp;
  1984. s32 temperature_comp[2];
  1985. s32 factory_gain_index[2];
  1986. s32 factory_actual_pwr[2];
  1987. s32 power_index;
  1988. /* Sanity check requested level (dBm) */
  1989. if (priv->user_txpower_limit < IWL_TX_POWER_TARGET_POWER_MIN) {
  1990. IWL_WARNING("Requested user TXPOWER %d below limit.\n",
  1991. priv->user_txpower_limit);
  1992. return -EINVAL;
  1993. }
  1994. if (priv->user_txpower_limit > IWL_TX_POWER_TARGET_POWER_MAX) {
  1995. IWL_WARNING("Requested user TXPOWER %d above limit.\n",
  1996. priv->user_txpower_limit);
  1997. return -EINVAL;
  1998. }
  1999. /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
  2000. * are used for indexing into txpower table) */
  2001. user_target_power = 2 * priv->user_txpower_limit;
  2002. /* Get current (RXON) channel, band, width */
  2003. ch_info =
  2004. iwl4965_get_channel_txpower_info(priv, priv->phymode, channel);
  2005. IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
  2006. is_fat);
  2007. if (!ch_info)
  2008. return -EINVAL;
  2009. /* get txatten group, used to select 1) thermal txpower adjustment
  2010. * and 2) mimo txpower balance between Tx chains. */
  2011. txatten_grp = iwl4965_get_tx_atten_grp(channel);
  2012. if (txatten_grp < 0)
  2013. return -EINVAL;
  2014. IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
  2015. channel, txatten_grp);
  2016. if (is_fat) {
  2017. if (ctrl_chan_high)
  2018. channel -= 2;
  2019. else
  2020. channel += 2;
  2021. }
  2022. /* hardware txpower limits ...
  2023. * saturation (clipping distortion) txpowers are in half-dBm */
  2024. if (band)
  2025. saturation_power = priv->eeprom.calib_info.saturation_power24;
  2026. else
  2027. saturation_power = priv->eeprom.calib_info.saturation_power52;
  2028. if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
  2029. saturation_power > IWL_TX_POWER_SATURATION_MAX) {
  2030. if (band)
  2031. saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
  2032. else
  2033. saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
  2034. }
  2035. /* regulatory txpower limits ... reg_limit values are in half-dBm,
  2036. * max_power_avg values are in dBm, convert * 2 */
  2037. if (is_fat)
  2038. reg_limit = ch_info->fat_max_power_avg * 2;
  2039. else
  2040. reg_limit = ch_info->max_power_avg * 2;
  2041. if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
  2042. (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
  2043. if (band)
  2044. reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
  2045. else
  2046. reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
  2047. }
  2048. /* Interpolate txpower calibration values for this channel,
  2049. * based on factory calibration tests on spaced channels. */
  2050. iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
  2051. /* calculate tx gain adjustment based on power supply voltage */
  2052. voltage = priv->eeprom.calib_info.voltage;
  2053. init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
  2054. voltage_compensation =
  2055. iwl4965_get_voltage_compensation(voltage, init_voltage);
  2056. IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
  2057. init_voltage,
  2058. voltage, voltage_compensation);
  2059. /* get current temperature (Celsius) */
  2060. current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
  2061. current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
  2062. current_temp = KELVIN_TO_CELSIUS(current_temp);
  2063. /* select thermal txpower adjustment params, based on channel group
  2064. * (same frequency group used for mimo txatten adjustment) */
  2065. degrees_per_05db_num =
  2066. tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
  2067. degrees_per_05db_denom =
  2068. tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
  2069. /* get per-chain txpower values from factory measurements */
  2070. for (c = 0; c < 2; c++) {
  2071. measurement = &ch_eeprom_info.measurements[c][1];
  2072. /* txgain adjustment (in half-dB steps) based on difference
  2073. * between factory and current temperature */
  2074. factory_temp = measurement->temperature;
  2075. iwl4965_math_div_round((current_temp - factory_temp) *
  2076. degrees_per_05db_denom,
  2077. degrees_per_05db_num,
  2078. &temperature_comp[c]);
  2079. factory_gain_index[c] = measurement->gain_idx;
  2080. factory_actual_pwr[c] = measurement->actual_pow;
  2081. IWL_DEBUG_TXPOWER("chain = %d\n", c);
  2082. IWL_DEBUG_TXPOWER("fctry tmp %d, "
  2083. "curr tmp %d, comp %d steps\n",
  2084. factory_temp, current_temp,
  2085. temperature_comp[c]);
  2086. IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
  2087. factory_gain_index[c],
  2088. factory_actual_pwr[c]);
  2089. }
  2090. /* for each of 33 bit-rates (including 1 for CCK) */
  2091. for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
  2092. u8 is_mimo_rate;
  2093. union iwl4965_tx_power_dual_stream tx_power;
  2094. /* for mimo, reduce each chain's txpower by half
  2095. * (3dB, 6 steps), so total output power is regulatory
  2096. * compliant. */
  2097. if (i & 0x8) {
  2098. current_regulatory = reg_limit -
  2099. IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
  2100. is_mimo_rate = 1;
  2101. } else {
  2102. current_regulatory = reg_limit;
  2103. is_mimo_rate = 0;
  2104. }
  2105. /* find txpower limit, either hardware or regulatory */
  2106. power_limit = saturation_power - back_off_table[i];
  2107. if (power_limit > current_regulatory)
  2108. power_limit = current_regulatory;
  2109. /* reduce user's txpower request if necessary
  2110. * for this rate on this channel */
  2111. target_power = user_target_power;
  2112. if (target_power > power_limit)
  2113. target_power = power_limit;
  2114. IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
  2115. i, saturation_power - back_off_table[i],
  2116. current_regulatory, user_target_power,
  2117. target_power);
  2118. /* for each of 2 Tx chains (radio transmitters) */
  2119. for (c = 0; c < 2; c++) {
  2120. s32 atten_value;
  2121. if (is_mimo_rate)
  2122. atten_value =
  2123. (s32)le32_to_cpu(priv->card_alive_init.
  2124. tx_atten[txatten_grp][c]);
  2125. else
  2126. atten_value = 0;
  2127. /* calculate index; higher index means lower txpower */
  2128. power_index = (u8) (factory_gain_index[c] -
  2129. (target_power -
  2130. factory_actual_pwr[c]) -
  2131. temperature_comp[c] -
  2132. voltage_compensation +
  2133. atten_value);
  2134. /* IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
  2135. power_index); */
  2136. if (power_index < get_min_power_index(i, band))
  2137. power_index = get_min_power_index(i, band);
  2138. /* adjust 5 GHz index to support negative indexes */
  2139. if (!band)
  2140. power_index += 9;
  2141. /* CCK, rate 32, reduce txpower for CCK */
  2142. if (i == POWER_TABLE_CCK_ENTRY)
  2143. power_index +=
  2144. IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
  2145. /* stay within the table! */
  2146. if (power_index > 107) {
  2147. IWL_WARNING("txpower index %d > 107\n",
  2148. power_index);
  2149. power_index = 107;
  2150. }
  2151. if (power_index < 0) {
  2152. IWL_WARNING("txpower index %d < 0\n",
  2153. power_index);
  2154. power_index = 0;
  2155. }
  2156. /* fill txpower command for this rate/chain */
  2157. tx_power.s.radio_tx_gain[c] =
  2158. gain_table[band][power_index].radio;
  2159. tx_power.s.dsp_predis_atten[c] =
  2160. gain_table[band][power_index].dsp;
  2161. IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
  2162. "gain 0x%02x dsp %d\n",
  2163. c, atten_value, power_index,
  2164. tx_power.s.radio_tx_gain[c],
  2165. tx_power.s.dsp_predis_atten[c]);
  2166. }/* for each chain */
  2167. tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
  2168. }/* for each rate */
  2169. return 0;
  2170. }
  2171. /**
  2172. * iwl4965_hw_reg_send_txpower - Configure the TXPOWER level user limit
  2173. *
  2174. * Uses the active RXON for channel, band, and characteristics (fat, high)
  2175. * The power limit is taken from priv->user_txpower_limit.
  2176. */
  2177. int iwl4965_hw_reg_send_txpower(struct iwl4965_priv *priv)
  2178. {
  2179. struct iwl4965_txpowertable_cmd cmd = { 0 };
  2180. int rc = 0;
  2181. u8 band = 0;
  2182. u8 is_fat = 0;
  2183. u8 ctrl_chan_high = 0;
  2184. if (test_bit(STATUS_SCANNING, &priv->status)) {
  2185. /* If this gets hit a lot, switch it to a BUG() and catch
  2186. * the stack trace to find out who is calling this during
  2187. * a scan. */
  2188. IWL_WARNING("TX Power requested while scanning!\n");
  2189. return -EAGAIN;
  2190. }
  2191. band = ((priv->phymode == MODE_IEEE80211B) ||
  2192. (priv->phymode == MODE_IEEE80211G));
  2193. is_fat = is_fat_channel(priv->active_rxon.flags);
  2194. if (is_fat &&
  2195. (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
  2196. ctrl_chan_high = 1;
  2197. cmd.band = band;
  2198. cmd.channel = priv->active_rxon.channel;
  2199. rc = iwl4965_fill_txpower_tbl(priv, band,
  2200. le16_to_cpu(priv->active_rxon.channel),
  2201. is_fat, ctrl_chan_high, &cmd.tx_power);
  2202. if (rc)
  2203. return rc;
  2204. rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
  2205. return rc;
  2206. }
  2207. int iwl4965_hw_channel_switch(struct iwl4965_priv *priv, u16 channel)
  2208. {
  2209. int rc;
  2210. u8 band = 0;
  2211. u8 is_fat = 0;
  2212. u8 ctrl_chan_high = 0;
  2213. struct iwl4965_channel_switch_cmd cmd = { 0 };
  2214. const struct iwl4965_channel_info *ch_info;
  2215. band = ((priv->phymode == MODE_IEEE80211B) ||
  2216. (priv->phymode == MODE_IEEE80211G));
  2217. ch_info = iwl4965_get_channel_info(priv, priv->phymode, channel);
  2218. is_fat = is_fat_channel(priv->staging_rxon.flags);
  2219. if (is_fat &&
  2220. (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
  2221. ctrl_chan_high = 1;
  2222. cmd.band = band;
  2223. cmd.expect_beacon = 0;
  2224. cmd.channel = cpu_to_le16(channel);
  2225. cmd.rxon_flags = priv->active_rxon.flags;
  2226. cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
  2227. cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
  2228. if (ch_info)
  2229. cmd.expect_beacon = is_channel_radar(ch_info);
  2230. else
  2231. cmd.expect_beacon = 1;
  2232. rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
  2233. ctrl_chan_high, &cmd.tx_power);
  2234. if (rc) {
  2235. IWL_DEBUG_11H("error:%d fill txpower_tbl\n", rc);
  2236. return rc;
  2237. }
  2238. rc = iwl4965_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
  2239. return rc;
  2240. }
  2241. #define RTS_HCCA_RETRY_LIMIT 3
  2242. #define RTS_DFAULT_RETRY_LIMIT 60
  2243. void iwl4965_hw_build_tx_cmd_rate(struct iwl4965_priv *priv,
  2244. struct iwl4965_cmd *cmd,
  2245. struct ieee80211_tx_control *ctrl,
  2246. struct ieee80211_hdr *hdr, int sta_id,
  2247. int is_hcca)
  2248. {
  2249. u8 rate;
  2250. u8 rts_retry_limit = 0;
  2251. u8 data_retry_limit = 0;
  2252. __le32 tx_flags;
  2253. u16 fc = le16_to_cpu(hdr->frame_control);
  2254. tx_flags = cmd->cmd.tx.tx_flags;
  2255. rate = iwl4965_rates[ctrl->tx_rate].plcp;
  2256. rts_retry_limit = (is_hcca) ?
  2257. RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
  2258. if (ieee80211_is_probe_response(fc)) {
  2259. data_retry_limit = 3;
  2260. if (data_retry_limit < rts_retry_limit)
  2261. rts_retry_limit = data_retry_limit;
  2262. } else
  2263. data_retry_limit = IWL_DEFAULT_TX_RETRY;
  2264. if (priv->data_retry_limit != -1)
  2265. data_retry_limit = priv->data_retry_limit;
  2266. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
  2267. switch (fc & IEEE80211_FCTL_STYPE) {
  2268. case IEEE80211_STYPE_AUTH:
  2269. case IEEE80211_STYPE_DEAUTH:
  2270. case IEEE80211_STYPE_ASSOC_REQ:
  2271. case IEEE80211_STYPE_REASSOC_REQ:
  2272. if (tx_flags & TX_CMD_FLG_RTS_MSK) {
  2273. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  2274. tx_flags |= TX_CMD_FLG_CTS_MSK;
  2275. }
  2276. break;
  2277. default:
  2278. break;
  2279. }
  2280. }
  2281. cmd->cmd.tx.rts_retry_limit = rts_retry_limit;
  2282. cmd->cmd.tx.data_retry_limit = data_retry_limit;
  2283. cmd->cmd.tx.rate_n_flags = iwl4965_hw_set_rate_n_flags(rate, 0);
  2284. cmd->cmd.tx.tx_flags = tx_flags;
  2285. }
  2286. int iwl4965_hw_get_rx_read(struct iwl4965_priv *priv)
  2287. {
  2288. struct iwl4965_shared *shared_data = priv->hw_setting.shared_virt;
  2289. return IWL_GET_BITS(*shared_data, rb_closed_stts_rb_num);
  2290. }
  2291. int iwl4965_hw_get_temperature(struct iwl4965_priv *priv)
  2292. {
  2293. return priv->temperature;
  2294. }
  2295. unsigned int iwl4965_hw_get_beacon_cmd(struct iwl4965_priv *priv,
  2296. struct iwl4965_frame *frame, u8 rate)
  2297. {
  2298. struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
  2299. unsigned int frame_size;
  2300. tx_beacon_cmd = &frame->u.beacon;
  2301. memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
  2302. tx_beacon_cmd->tx.sta_id = IWL4965_BROADCAST_ID;
  2303. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2304. frame_size = iwl4965_fill_beacon_frame(priv,
  2305. tx_beacon_cmd->frame,
  2306. iwl4965_broadcast_addr,
  2307. sizeof(frame->u) - sizeof(*tx_beacon_cmd));
  2308. BUG_ON(frame_size > MAX_MPDU_SIZE);
  2309. tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
  2310. if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
  2311. tx_beacon_cmd->tx.rate_n_flags =
  2312. iwl4965_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
  2313. else
  2314. tx_beacon_cmd->tx.rate_n_flags =
  2315. iwl4965_hw_set_rate_n_flags(rate, 0);
  2316. tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
  2317. TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
  2318. return (sizeof(*tx_beacon_cmd) + frame_size);
  2319. }
  2320. /*
  2321. * Tell 4965 where to find circular buffer of Tx Frame Descriptors for
  2322. * given Tx queue, and enable the DMA channel used for that queue.
  2323. *
  2324. * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  2325. * channels supported in hardware.
  2326. */
  2327. int iwl4965_hw_tx_queue_init(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
  2328. {
  2329. int rc;
  2330. unsigned long flags;
  2331. int txq_id = txq->q.id;
  2332. spin_lock_irqsave(&priv->lock, flags);
  2333. rc = iwl4965_grab_nic_access(priv);
  2334. if (rc) {
  2335. spin_unlock_irqrestore(&priv->lock, flags);
  2336. return rc;
  2337. }
  2338. /* Circular buffer (TFD queue in DRAM) physical base address */
  2339. iwl4965_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  2340. txq->q.dma_addr >> 8);
  2341. /* Enable DMA channel, using same id as for TFD queue */
  2342. iwl4965_write_direct32(
  2343. priv, IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
  2344. IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  2345. IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
  2346. iwl4965_release_nic_access(priv);
  2347. spin_unlock_irqrestore(&priv->lock, flags);
  2348. return 0;
  2349. }
  2350. int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl4965_priv *priv, void *ptr,
  2351. dma_addr_t addr, u16 len)
  2352. {
  2353. int index, is_odd;
  2354. struct iwl4965_tfd_frame *tfd = ptr;
  2355. u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
  2356. /* Each TFD can point to a maximum 20 Tx buffers */
  2357. if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
  2358. IWL_ERROR("Error can not send more than %d chunks\n",
  2359. MAX_NUM_OF_TBS);
  2360. return -EINVAL;
  2361. }
  2362. index = num_tbs / 2;
  2363. is_odd = num_tbs & 0x1;
  2364. if (!is_odd) {
  2365. tfd->pa[index].tb1_addr = cpu_to_le32(addr);
  2366. IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
  2367. iwl_get_dma_hi_address(addr));
  2368. IWL_SET_BITS(tfd->pa[index], tb1_len, len);
  2369. } else {
  2370. IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
  2371. (u32) (addr & 0xffff));
  2372. IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
  2373. IWL_SET_BITS(tfd->pa[index], tb2_len, len);
  2374. }
  2375. IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
  2376. return 0;
  2377. }
  2378. static void iwl4965_hw_card_show_info(struct iwl4965_priv *priv)
  2379. {
  2380. u16 hw_version = priv->eeprom.board_revision_4965;
  2381. IWL_DEBUG_INFO("4965ABGN HW Version %u.%u.%u\n",
  2382. ((hw_version >> 8) & 0x0F),
  2383. ((hw_version >> 8) >> 4), (hw_version & 0x00FF));
  2384. IWL_DEBUG_INFO("4965ABGN PBA Number %.16s\n",
  2385. priv->eeprom.board_pba_number_4965);
  2386. }
  2387. #define IWL_TX_CRC_SIZE 4
  2388. #define IWL_TX_DELIMITER_SIZE 4
  2389. /**
  2390. * iwl4965_tx_queue_update_wr_ptr - Set up entry in Tx byte-count array
  2391. */
  2392. int iwl4965_tx_queue_update_wr_ptr(struct iwl4965_priv *priv,
  2393. struct iwl4965_tx_queue *txq, u16 byte_cnt)
  2394. {
  2395. int len;
  2396. int txq_id = txq->q.id;
  2397. struct iwl4965_shared *shared_data = priv->hw_setting.shared_virt;
  2398. if (txq->need_update == 0)
  2399. return 0;
  2400. len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  2401. /* Set up byte count within first 256 entries */
  2402. IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
  2403. tfd_offset[txq->q.write_ptr], byte_cnt, len);
  2404. /* If within first 64 entries, duplicate at end */
  2405. if (txq->q.write_ptr < IWL4965_MAX_WIN_SIZE)
  2406. IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
  2407. tfd_offset[IWL4965_QUEUE_SIZE + txq->q.write_ptr],
  2408. byte_cnt, len);
  2409. return 0;
  2410. }
  2411. /**
  2412. * iwl4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  2413. *
  2414. * Selects how many and which Rx receivers/antennas/chains to use.
  2415. * This should not be used for scan command ... it puts data in wrong place.
  2416. */
  2417. void iwl4965_set_rxon_chain(struct iwl4965_priv *priv)
  2418. {
  2419. u8 is_single = is_single_stream(priv);
  2420. u8 idle_state, rx_state;
  2421. priv->staging_rxon.rx_chain = 0;
  2422. rx_state = idle_state = 3;
  2423. /* Tell uCode which antennas are actually connected.
  2424. * Before first association, we assume all antennas are connected.
  2425. * Just after first association, iwl4965_noise_calibration()
  2426. * checks which antennas actually *are* connected. */
  2427. priv->staging_rxon.rx_chain |=
  2428. cpu_to_le16(priv->valid_antenna << RXON_RX_CHAIN_VALID_POS);
  2429. /* How many receivers should we use? */
  2430. iwl4965_get_rx_chain_counter(priv, &idle_state, &rx_state);
  2431. priv->staging_rxon.rx_chain |=
  2432. cpu_to_le16(rx_state << RXON_RX_CHAIN_MIMO_CNT_POS);
  2433. priv->staging_rxon.rx_chain |=
  2434. cpu_to_le16(idle_state << RXON_RX_CHAIN_CNT_POS);
  2435. if (!is_single && (rx_state >= 2) &&
  2436. !test_bit(STATUS_POWER_PMI, &priv->status))
  2437. priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  2438. else
  2439. priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  2440. IWL_DEBUG_ASSOC("rx chain %X\n", priv->staging_rxon.rx_chain);
  2441. }
  2442. #ifdef CONFIG_IWL4965_HT
  2443. #ifdef CONFIG_IWL4965_HT_AGG
  2444. /*
  2445. get the traffic load value for tid
  2446. */
  2447. static u32 iwl4965_tl_get_load(struct iwl4965_priv *priv, u8 tid)
  2448. {
  2449. u32 load = 0;
  2450. u32 current_time = jiffies_to_msecs(jiffies);
  2451. u32 time_diff;
  2452. s32 index;
  2453. unsigned long flags;
  2454. struct iwl4965_traffic_load *tid_ptr = NULL;
  2455. if (tid >= TID_MAX_LOAD_COUNT)
  2456. return 0;
  2457. tid_ptr = &(priv->lq_mngr.agg_ctrl.traffic_load[tid]);
  2458. current_time -= current_time % TID_ROUND_VALUE;
  2459. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2460. if (!(tid_ptr->queue_count))
  2461. goto out;
  2462. time_diff = TIME_WRAP_AROUND(tid_ptr->time_stamp, current_time);
  2463. index = time_diff / TID_QUEUE_CELL_SPACING;
  2464. if (index >= TID_QUEUE_MAX_SIZE) {
  2465. u32 oldest_time = current_time - TID_MAX_TIME_DIFF;
  2466. while (tid_ptr->queue_count &&
  2467. (tid_ptr->time_stamp < oldest_time)) {
  2468. tid_ptr->total -= tid_ptr->packet_count[tid_ptr->head];
  2469. tid_ptr->packet_count[tid_ptr->head] = 0;
  2470. tid_ptr->time_stamp += TID_QUEUE_CELL_SPACING;
  2471. tid_ptr->queue_count--;
  2472. tid_ptr->head++;
  2473. if (tid_ptr->head >= TID_QUEUE_MAX_SIZE)
  2474. tid_ptr->head = 0;
  2475. }
  2476. }
  2477. load = tid_ptr->total;
  2478. out:
  2479. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2480. return load;
  2481. }
  2482. /*
  2483. increment traffic load value for tid and also remove
  2484. any old values if passed the certian time period
  2485. */
  2486. static void iwl4965_tl_add_packet(struct iwl4965_priv *priv, u8 tid)
  2487. {
  2488. u32 current_time = jiffies_to_msecs(jiffies);
  2489. u32 time_diff;
  2490. s32 index;
  2491. unsigned long flags;
  2492. struct iwl4965_traffic_load *tid_ptr = NULL;
  2493. if (tid >= TID_MAX_LOAD_COUNT)
  2494. return;
  2495. tid_ptr = &(priv->lq_mngr.agg_ctrl.traffic_load[tid]);
  2496. current_time -= current_time % TID_ROUND_VALUE;
  2497. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2498. if (!(tid_ptr->queue_count)) {
  2499. tid_ptr->total = 1;
  2500. tid_ptr->time_stamp = current_time;
  2501. tid_ptr->queue_count = 1;
  2502. tid_ptr->head = 0;
  2503. tid_ptr->packet_count[0] = 1;
  2504. goto out;
  2505. }
  2506. time_diff = TIME_WRAP_AROUND(tid_ptr->time_stamp, current_time);
  2507. index = time_diff / TID_QUEUE_CELL_SPACING;
  2508. if (index >= TID_QUEUE_MAX_SIZE) {
  2509. u32 oldest_time = current_time - TID_MAX_TIME_DIFF;
  2510. while (tid_ptr->queue_count &&
  2511. (tid_ptr->time_stamp < oldest_time)) {
  2512. tid_ptr->total -= tid_ptr->packet_count[tid_ptr->head];
  2513. tid_ptr->packet_count[tid_ptr->head] = 0;
  2514. tid_ptr->time_stamp += TID_QUEUE_CELL_SPACING;
  2515. tid_ptr->queue_count--;
  2516. tid_ptr->head++;
  2517. if (tid_ptr->head >= TID_QUEUE_MAX_SIZE)
  2518. tid_ptr->head = 0;
  2519. }
  2520. }
  2521. index = (tid_ptr->head + index) % TID_QUEUE_MAX_SIZE;
  2522. tid_ptr->packet_count[index] = tid_ptr->packet_count[index] + 1;
  2523. tid_ptr->total = tid_ptr->total + 1;
  2524. if ((index + 1) > tid_ptr->queue_count)
  2525. tid_ptr->queue_count = index + 1;
  2526. out:
  2527. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2528. }
  2529. #define MMAC_SCHED_MAX_NUMBER_OF_HT_BACK_FLOWS 7
  2530. enum HT_STATUS {
  2531. BA_STATUS_FAILURE = 0,
  2532. BA_STATUS_INITIATOR_DELBA,
  2533. BA_STATUS_RECIPIENT_DELBA,
  2534. BA_STATUS_RENEW_ADDBA_REQUEST,
  2535. BA_STATUS_ACTIVE,
  2536. };
  2537. /**
  2538. * iwl4964_tl_ba_avail - Find out if an unused aggregation queue is available
  2539. */
  2540. static u8 iwl4964_tl_ba_avail(struct iwl4965_priv *priv)
  2541. {
  2542. int i;
  2543. struct iwl4965_lq_mngr *lq;
  2544. u8 count = 0;
  2545. u16 msk;
  2546. lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2547. /* Find out how many agg queues are in use */
  2548. for (i = 0; i < TID_MAX_LOAD_COUNT ; i++) {
  2549. msk = 1 << i;
  2550. if ((lq->agg_ctrl.granted_ba & msk) ||
  2551. (lq->agg_ctrl.wait_for_agg_status & msk))
  2552. count++;
  2553. }
  2554. if (count < MMAC_SCHED_MAX_NUMBER_OF_HT_BACK_FLOWS)
  2555. return 1;
  2556. return 0;
  2557. }
  2558. static void iwl4965_ba_status(struct iwl4965_priv *priv,
  2559. u8 tid, enum HT_STATUS status);
  2560. static int iwl4965_perform_addba(struct iwl4965_priv *priv, u8 tid, u32 length,
  2561. u32 ba_timeout)
  2562. {
  2563. int rc;
  2564. rc = ieee80211_start_BA_session(priv->hw, priv->bssid, tid);
  2565. if (rc)
  2566. iwl4965_ba_status(priv, tid, BA_STATUS_FAILURE);
  2567. return rc;
  2568. }
  2569. static int iwl4965_perform_delba(struct iwl4965_priv *priv, u8 tid)
  2570. {
  2571. int rc;
  2572. rc = ieee80211_stop_BA_session(priv->hw, priv->bssid, tid);
  2573. if (rc)
  2574. iwl4965_ba_status(priv, tid, BA_STATUS_FAILURE);
  2575. return rc;
  2576. }
  2577. static void iwl4965_turn_on_agg_for_tid(struct iwl4965_priv *priv,
  2578. struct iwl4965_lq_mngr *lq,
  2579. u8 auto_agg, u8 tid)
  2580. {
  2581. u32 tid_msk = (1 << tid);
  2582. unsigned long flags;
  2583. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2584. /*
  2585. if ((auto_agg) && (!lq->enable_counter)){
  2586. lq->agg_ctrl.next_retry = 0;
  2587. lq->agg_ctrl.tid_retry = 0;
  2588. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2589. return;
  2590. }
  2591. */
  2592. if (!(lq->agg_ctrl.granted_ba & tid_msk) &&
  2593. (lq->agg_ctrl.requested_ba & tid_msk)) {
  2594. u8 available_queues;
  2595. u32 load;
  2596. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2597. available_queues = iwl4964_tl_ba_avail(priv);
  2598. load = iwl4965_tl_get_load(priv, tid);
  2599. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2600. if (!available_queues) {
  2601. if (auto_agg)
  2602. lq->agg_ctrl.tid_retry |= tid_msk;
  2603. else {
  2604. lq->agg_ctrl.requested_ba &= ~tid_msk;
  2605. lq->agg_ctrl.wait_for_agg_status &= ~tid_msk;
  2606. }
  2607. } else if ((auto_agg) &&
  2608. ((load <= lq->agg_ctrl.tid_traffic_load_threshold) ||
  2609. ((lq->agg_ctrl.wait_for_agg_status & tid_msk))))
  2610. lq->agg_ctrl.tid_retry |= tid_msk;
  2611. else {
  2612. lq->agg_ctrl.wait_for_agg_status |= tid_msk;
  2613. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2614. iwl4965_perform_addba(priv, tid, 0x40,
  2615. lq->agg_ctrl.ba_timeout);
  2616. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2617. }
  2618. }
  2619. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2620. }
  2621. static void iwl4965_turn_on_agg(struct iwl4965_priv *priv, u8 tid)
  2622. {
  2623. struct iwl4965_lq_mngr *lq;
  2624. unsigned long flags;
  2625. lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2626. if ((tid < TID_MAX_LOAD_COUNT))
  2627. iwl4965_turn_on_agg_for_tid(priv, lq, lq->agg_ctrl.auto_agg,
  2628. tid);
  2629. else if (tid == TID_ALL_SPECIFIED) {
  2630. if (lq->agg_ctrl.requested_ba) {
  2631. for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
  2632. iwl4965_turn_on_agg_for_tid(priv, lq,
  2633. lq->agg_ctrl.auto_agg, tid);
  2634. } else {
  2635. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2636. lq->agg_ctrl.tid_retry = 0;
  2637. lq->agg_ctrl.next_retry = 0;
  2638. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2639. }
  2640. }
  2641. }
  2642. void iwl4965_turn_off_agg(struct iwl4965_priv *priv, u8 tid)
  2643. {
  2644. u32 tid_msk;
  2645. struct iwl4965_lq_mngr *lq;
  2646. unsigned long flags;
  2647. lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2648. if ((tid < TID_MAX_LOAD_COUNT)) {
  2649. tid_msk = 1 << tid;
  2650. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2651. lq->agg_ctrl.wait_for_agg_status |= tid_msk;
  2652. lq->agg_ctrl.requested_ba &= ~tid_msk;
  2653. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2654. iwl4965_perform_delba(priv, tid);
  2655. } else if (tid == TID_ALL_SPECIFIED) {
  2656. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2657. for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++) {
  2658. tid_msk = 1 << tid;
  2659. lq->agg_ctrl.wait_for_agg_status |= tid_msk;
  2660. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2661. iwl4965_perform_delba(priv, tid);
  2662. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2663. }
  2664. lq->agg_ctrl.requested_ba = 0;
  2665. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2666. }
  2667. }
  2668. /**
  2669. * iwl4965_ba_status - Update driver's link quality mgr with tid's HT status
  2670. */
  2671. static void iwl4965_ba_status(struct iwl4965_priv *priv,
  2672. u8 tid, enum HT_STATUS status)
  2673. {
  2674. struct iwl4965_lq_mngr *lq;
  2675. u32 tid_msk = (1 << tid);
  2676. unsigned long flags;
  2677. lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2678. if ((tid >= TID_MAX_LOAD_COUNT))
  2679. goto out;
  2680. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2681. switch (status) {
  2682. case BA_STATUS_ACTIVE:
  2683. if (!(lq->agg_ctrl.granted_ba & tid_msk))
  2684. lq->agg_ctrl.granted_ba |= tid_msk;
  2685. break;
  2686. default:
  2687. if ((lq->agg_ctrl.granted_ba & tid_msk))
  2688. lq->agg_ctrl.granted_ba &= ~tid_msk;
  2689. break;
  2690. }
  2691. lq->agg_ctrl.wait_for_agg_status &= ~tid_msk;
  2692. if (status != BA_STATUS_ACTIVE) {
  2693. if (lq->agg_ctrl.auto_agg) {
  2694. lq->agg_ctrl.tid_retry |= tid_msk;
  2695. lq->agg_ctrl.next_retry =
  2696. jiffies + msecs_to_jiffies(500);
  2697. } else
  2698. lq->agg_ctrl.requested_ba &= ~tid_msk;
  2699. }
  2700. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2701. out:
  2702. return;
  2703. }
  2704. static void iwl4965_bg_agg_work(struct work_struct *work)
  2705. {
  2706. struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
  2707. agg_work);
  2708. u32 tid;
  2709. u32 retry_tid;
  2710. u32 tid_msk;
  2711. unsigned long flags;
  2712. struct iwl4965_lq_mngr *lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2713. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2714. retry_tid = lq->agg_ctrl.tid_retry;
  2715. lq->agg_ctrl.tid_retry = 0;
  2716. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2717. if (retry_tid == TID_ALL_SPECIFIED)
  2718. iwl4965_turn_on_agg(priv, TID_ALL_SPECIFIED);
  2719. else {
  2720. for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++) {
  2721. tid_msk = (1 << tid);
  2722. if (retry_tid & tid_msk)
  2723. iwl4965_turn_on_agg(priv, tid);
  2724. }
  2725. }
  2726. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2727. if (lq->agg_ctrl.tid_retry)
  2728. lq->agg_ctrl.next_retry = jiffies + msecs_to_jiffies(500);
  2729. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2730. return;
  2731. }
  2732. #endif /*CONFIG_IWL4965_HT_AGG */
  2733. #endif /* CONFIG_IWL4965_HT */
  2734. int iwl4965_tx_cmd(struct iwl4965_priv *priv, struct iwl4965_cmd *out_cmd,
  2735. u8 sta_id, dma_addr_t txcmd_phys,
  2736. struct ieee80211_hdr *hdr, u8 hdr_len,
  2737. struct ieee80211_tx_control *ctrl, void *sta_in)
  2738. {
  2739. struct iwl4965_tx_cmd *tx = &out_cmd->cmd.tx;
  2740. dma_addr_t scratch_phys;
  2741. u8 unicast = 0;
  2742. u8 is_data = 1;
  2743. u16 fc;
  2744. u16 rate_flags;
  2745. int rate_index = min(ctrl->tx_rate & 0xffff, IWL_RATE_COUNT - 1);
  2746. #ifdef CONFIG_IWL4965_HT
  2747. #ifdef CONFIG_IWL4965_HT_AGG
  2748. __le16 *qc;
  2749. #endif /*CONFIG_IWL4965_HT_AGG */
  2750. #endif /* CONFIG_IWL4965_HT */
  2751. unicast = !is_multicast_ether_addr(hdr->addr1);
  2752. fc = le16_to_cpu(hdr->frame_control);
  2753. if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  2754. is_data = 0;
  2755. scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
  2756. offsetof(struct iwl4965_tx_cmd, scratch);
  2757. tx->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  2758. tx->dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
  2759. /* Hard coded to start at the highest retry fallback position
  2760. * until the 4965 specific rate control algorithm is tied in */
  2761. tx->initial_rate_index = LINK_QUAL_MAX_RETRY_NUM - 1;
  2762. /* Alternate between antenna A and B for successive frames */
  2763. if (priv->use_ant_b_for_management_frame) {
  2764. priv->use_ant_b_for_management_frame = 0;
  2765. rate_flags = RATE_MCS_ANT_B_MSK;
  2766. } else {
  2767. priv->use_ant_b_for_management_frame = 1;
  2768. rate_flags = RATE_MCS_ANT_A_MSK;
  2769. }
  2770. if (!unicast || !is_data) {
  2771. if ((rate_index >= IWL_FIRST_CCK_RATE) &&
  2772. (rate_index <= IWL_LAST_CCK_RATE))
  2773. rate_flags |= RATE_MCS_CCK_MSK;
  2774. } else {
  2775. tx->initial_rate_index = 0;
  2776. tx->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  2777. }
  2778. tx->rate_n_flags = iwl4965_hw_set_rate_n_flags(iwl4965_rates[rate_index].plcp,
  2779. rate_flags);
  2780. if (ieee80211_is_back_request(fc))
  2781. tx->tx_flags |= TX_CMD_FLG_ACK_MSK |
  2782. TX_CMD_FLG_IMM_BA_RSP_MASK;
  2783. #ifdef CONFIG_IWL4965_HT
  2784. #ifdef CONFIG_IWL4965_HT_AGG
  2785. qc = ieee80211_get_qos_ctrl(hdr);
  2786. if (qc &&
  2787. (priv->iw_mode != IEEE80211_IF_TYPE_IBSS)) {
  2788. u8 tid = 0;
  2789. tid = (u8) (le16_to_cpu(*qc) & 0xF);
  2790. if (tid < TID_MAX_LOAD_COUNT)
  2791. iwl4965_tl_add_packet(priv, tid);
  2792. }
  2793. if (priv->lq_mngr.agg_ctrl.next_retry &&
  2794. (time_after(priv->lq_mngr.agg_ctrl.next_retry, jiffies))) {
  2795. unsigned long flags;
  2796. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2797. priv->lq_mngr.agg_ctrl.next_retry = 0;
  2798. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2799. schedule_work(&priv->agg_work);
  2800. }
  2801. #endif
  2802. #endif
  2803. return 0;
  2804. }
  2805. /**
  2806. * sign_extend - Sign extend a value using specified bit as sign-bit
  2807. *
  2808. * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
  2809. * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
  2810. *
  2811. * @param oper value to sign extend
  2812. * @param index 0 based bit index (0<=index<32) to sign bit
  2813. */
  2814. static s32 sign_extend(u32 oper, int index)
  2815. {
  2816. u8 shift = 31 - index;
  2817. return (s32)(oper << shift) >> shift;
  2818. }
  2819. /**
  2820. * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
  2821. * @statistics: Provides the temperature reading from the uCode
  2822. *
  2823. * A return of <0 indicates bogus data in the statistics
  2824. */
  2825. int iwl4965_get_temperature(const struct iwl4965_priv *priv)
  2826. {
  2827. s32 temperature;
  2828. s32 vt;
  2829. s32 R1, R2, R3;
  2830. u32 R4;
  2831. if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
  2832. (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
  2833. IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
  2834. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
  2835. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
  2836. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
  2837. R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
  2838. } else {
  2839. IWL_DEBUG_TEMP("Running temperature calibration\n");
  2840. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
  2841. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
  2842. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
  2843. R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
  2844. }
  2845. /*
  2846. * Temperature is only 23 bits, so sign extend out to 32.
  2847. *
  2848. * NOTE If we haven't received a statistics notification yet
  2849. * with an updated temperature, use R4 provided to us in the
  2850. * "initialize" ALIVE response.
  2851. */
  2852. if (!test_bit(STATUS_TEMPERATURE, &priv->status))
  2853. vt = sign_extend(R4, 23);
  2854. else
  2855. vt = sign_extend(
  2856. le32_to_cpu(priv->statistics.general.temperature), 23);
  2857. IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
  2858. R1, R2, R3, vt);
  2859. if (R3 == R1) {
  2860. IWL_ERROR("Calibration conflict R1 == R3\n");
  2861. return -1;
  2862. }
  2863. /* Calculate temperature in degrees Kelvin, adjust by 97%.
  2864. * Add offset to center the adjustment around 0 degrees Centigrade. */
  2865. temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
  2866. temperature /= (R3 - R1);
  2867. temperature = (temperature * 97) / 100 +
  2868. TEMPERATURE_CALIB_KELVIN_OFFSET;
  2869. IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
  2870. KELVIN_TO_CELSIUS(temperature));
  2871. return temperature;
  2872. }
  2873. /* Adjust Txpower only if temperature variance is greater than threshold. */
  2874. #define IWL_TEMPERATURE_THRESHOLD 3
  2875. /**
  2876. * iwl4965_is_temp_calib_needed - determines if new calibration is needed
  2877. *
  2878. * If the temperature changed has changed sufficiently, then a recalibration
  2879. * is needed.
  2880. *
  2881. * Assumes caller will replace priv->last_temperature once calibration
  2882. * executed.
  2883. */
  2884. static int iwl4965_is_temp_calib_needed(struct iwl4965_priv *priv)
  2885. {
  2886. int temp_diff;
  2887. if (!test_bit(STATUS_STATISTICS, &priv->status)) {
  2888. IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
  2889. return 0;
  2890. }
  2891. temp_diff = priv->temperature - priv->last_temperature;
  2892. /* get absolute value */
  2893. if (temp_diff < 0) {
  2894. IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
  2895. temp_diff = -temp_diff;
  2896. } else if (temp_diff == 0)
  2897. IWL_DEBUG_POWER("Same temp, \n");
  2898. else
  2899. IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
  2900. if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
  2901. IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
  2902. return 0;
  2903. }
  2904. IWL_DEBUG_POWER("Thermal txpower calib needed\n");
  2905. return 1;
  2906. }
  2907. /* Calculate noise level, based on measurements during network silence just
  2908. * before arriving beacon. This measurement can be done only if we know
  2909. * exactly when to expect beacons, therefore only when we're associated. */
  2910. static void iwl4965_rx_calc_noise(struct iwl4965_priv *priv)
  2911. {
  2912. struct statistics_rx_non_phy *rx_info
  2913. = &(priv->statistics.rx.general);
  2914. int num_active_rx = 0;
  2915. int total_silence = 0;
  2916. int bcn_silence_a =
  2917. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  2918. int bcn_silence_b =
  2919. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  2920. int bcn_silence_c =
  2921. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  2922. if (bcn_silence_a) {
  2923. total_silence += bcn_silence_a;
  2924. num_active_rx++;
  2925. }
  2926. if (bcn_silence_b) {
  2927. total_silence += bcn_silence_b;
  2928. num_active_rx++;
  2929. }
  2930. if (bcn_silence_c) {
  2931. total_silence += bcn_silence_c;
  2932. num_active_rx++;
  2933. }
  2934. /* Average among active antennas */
  2935. if (num_active_rx)
  2936. priv->last_rx_noise = (total_silence / num_active_rx) - 107;
  2937. else
  2938. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  2939. IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
  2940. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  2941. priv->last_rx_noise);
  2942. }
  2943. void iwl4965_hw_rx_statistics(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
  2944. {
  2945. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2946. int change;
  2947. s32 temp;
  2948. IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
  2949. (int)sizeof(priv->statistics), pkt->len);
  2950. change = ((priv->statistics.general.temperature !=
  2951. pkt->u.stats.general.temperature) ||
  2952. ((priv->statistics.flag &
  2953. STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
  2954. (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
  2955. memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
  2956. set_bit(STATUS_STATISTICS, &priv->status);
  2957. /* Reschedule the statistics timer to occur in
  2958. * REG_RECALIB_PERIOD seconds to ensure we get a
  2959. * thermal update even if the uCode doesn't give
  2960. * us one */
  2961. mod_timer(&priv->statistics_periodic, jiffies +
  2962. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  2963. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  2964. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  2965. iwl4965_rx_calc_noise(priv);
  2966. #ifdef CONFIG_IWL4965_SENSITIVITY
  2967. queue_work(priv->workqueue, &priv->sensitivity_work);
  2968. #endif
  2969. }
  2970. /* If the hardware hasn't reported a change in
  2971. * temperature then don't bother computing a
  2972. * calibrated temperature value */
  2973. if (!change)
  2974. return;
  2975. temp = iwl4965_get_temperature(priv);
  2976. if (temp < 0)
  2977. return;
  2978. if (priv->temperature != temp) {
  2979. if (priv->temperature)
  2980. IWL_DEBUG_TEMP("Temperature changed "
  2981. "from %dC to %dC\n",
  2982. KELVIN_TO_CELSIUS(priv->temperature),
  2983. KELVIN_TO_CELSIUS(temp));
  2984. else
  2985. IWL_DEBUG_TEMP("Temperature "
  2986. "initialized to %dC\n",
  2987. KELVIN_TO_CELSIUS(temp));
  2988. }
  2989. priv->temperature = temp;
  2990. set_bit(STATUS_TEMPERATURE, &priv->status);
  2991. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  2992. iwl4965_is_temp_calib_needed(priv))
  2993. queue_work(priv->workqueue, &priv->txpower_work);
  2994. }
  2995. static void iwl4965_add_radiotap(struct iwl4965_priv *priv,
  2996. struct sk_buff *skb,
  2997. struct iwl4965_rx_phy_res *rx_start,
  2998. struct ieee80211_rx_status *stats,
  2999. u32 ampdu_status)
  3000. {
  3001. s8 signal = stats->ssi;
  3002. s8 noise = 0;
  3003. int rate = stats->rate;
  3004. u64 tsf = stats->mactime;
  3005. __le16 phy_flags_hw = rx_start->phy_flags;
  3006. struct iwl4965_rt_rx_hdr {
  3007. struct ieee80211_radiotap_header rt_hdr;
  3008. __le64 rt_tsf; /* TSF */
  3009. u8 rt_flags; /* radiotap packet flags */
  3010. u8 rt_rate; /* rate in 500kb/s */
  3011. __le16 rt_channelMHz; /* channel in MHz */
  3012. __le16 rt_chbitmask; /* channel bitfield */
  3013. s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
  3014. s8 rt_dbmnoise;
  3015. u8 rt_antenna; /* antenna number */
  3016. } __attribute__ ((packed)) *iwl4965_rt;
  3017. /* TODO: We won't have enough headroom for HT frames. Fix it later. */
  3018. if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
  3019. if (net_ratelimit())
  3020. printk(KERN_ERR "not enough headroom [%d] for "
  3021. "radiotap head [%zd]\n",
  3022. skb_headroom(skb), sizeof(*iwl4965_rt));
  3023. return;
  3024. }
  3025. /* put radiotap header in front of 802.11 header and data */
  3026. iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
  3027. /* initialise radiotap header */
  3028. iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  3029. iwl4965_rt->rt_hdr.it_pad = 0;
  3030. /* total header + data */
  3031. put_unaligned(cpu_to_le16(sizeof(*iwl4965_rt)),
  3032. &iwl4965_rt->rt_hdr.it_len);
  3033. /* Indicate all the fields we add to the radiotap header */
  3034. put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
  3035. (1 << IEEE80211_RADIOTAP_FLAGS) |
  3036. (1 << IEEE80211_RADIOTAP_RATE) |
  3037. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  3038. (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
  3039. (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
  3040. (1 << IEEE80211_RADIOTAP_ANTENNA)),
  3041. &iwl4965_rt->rt_hdr.it_present);
  3042. /* Zero the flags, we'll add to them as we go */
  3043. iwl4965_rt->rt_flags = 0;
  3044. put_unaligned(cpu_to_le64(tsf), &iwl4965_rt->rt_tsf);
  3045. iwl4965_rt->rt_dbmsignal = signal;
  3046. iwl4965_rt->rt_dbmnoise = noise;
  3047. /* Convert the channel frequency and set the flags */
  3048. put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
  3049. if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
  3050. put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
  3051. IEEE80211_CHAN_5GHZ),
  3052. &iwl4965_rt->rt_chbitmask);
  3053. else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
  3054. put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
  3055. IEEE80211_CHAN_2GHZ),
  3056. &iwl4965_rt->rt_chbitmask);
  3057. else /* 802.11g */
  3058. put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
  3059. IEEE80211_CHAN_2GHZ),
  3060. &iwl4965_rt->rt_chbitmask);
  3061. rate = iwl4965_rate_index_from_plcp(rate);
  3062. if (rate == -1)
  3063. iwl4965_rt->rt_rate = 0;
  3064. else
  3065. iwl4965_rt->rt_rate = iwl4965_rates[rate].ieee;
  3066. /*
  3067. * "antenna number"
  3068. *
  3069. * It seems that the antenna field in the phy flags value
  3070. * is actually a bitfield. This is undefined by radiotap,
  3071. * it wants an actual antenna number but I always get "7"
  3072. * for most legacy frames I receive indicating that the
  3073. * same frame was received on all three RX chains.
  3074. *
  3075. * I think this field should be removed in favour of a
  3076. * new 802.11n radiotap field "RX chains" that is defined
  3077. * as a bitmask.
  3078. */
  3079. iwl4965_rt->rt_antenna =
  3080. le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
  3081. /* set the preamble flag if appropriate */
  3082. if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  3083. iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
  3084. stats->flag |= RX_FLAG_RADIOTAP;
  3085. }
  3086. static void iwl4965_handle_data_packet(struct iwl4965_priv *priv, int is_data,
  3087. int include_phy,
  3088. struct iwl4965_rx_mem_buffer *rxb,
  3089. struct ieee80211_rx_status *stats)
  3090. {
  3091. struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
  3092. struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
  3093. (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
  3094. struct ieee80211_hdr *hdr;
  3095. u16 len;
  3096. __le32 *rx_end;
  3097. unsigned int skblen;
  3098. u32 ampdu_status;
  3099. if (!include_phy && priv->last_phy_res[0])
  3100. rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
  3101. if (!rx_start) {
  3102. IWL_ERROR("MPDU frame without a PHY data\n");
  3103. return;
  3104. }
  3105. if (include_phy) {
  3106. hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
  3107. rx_start->cfg_phy_cnt);
  3108. len = le16_to_cpu(rx_start->byte_count);
  3109. rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
  3110. sizeof(struct iwl4965_rx_phy_res) +
  3111. rx_start->cfg_phy_cnt + len);
  3112. } else {
  3113. struct iwl4965_rx_mpdu_res_start *amsdu =
  3114. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  3115. hdr = (struct ieee80211_hdr *)(pkt->u.raw +
  3116. sizeof(struct iwl4965_rx_mpdu_res_start));
  3117. len = le16_to_cpu(amsdu->byte_count);
  3118. rx_start->byte_count = amsdu->byte_count;
  3119. rx_end = (__le32 *) (((u8 *) hdr) + len);
  3120. }
  3121. if (len > priv->hw_setting.max_pkt_size || len < 16) {
  3122. IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
  3123. return;
  3124. }
  3125. ampdu_status = le32_to_cpu(*rx_end);
  3126. skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
  3127. /* start from MAC */
  3128. skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
  3129. skb_put(rxb->skb, len); /* end where data ends */
  3130. /* We only process data packets if the interface is open */
  3131. if (unlikely(!priv->is_open)) {
  3132. IWL_DEBUG_DROP_LIMIT
  3133. ("Dropping packet while interface is not open.\n");
  3134. return;
  3135. }
  3136. stats->flag = 0;
  3137. hdr = (struct ieee80211_hdr *)rxb->skb->data;
  3138. if (iwl4965_param_hwcrypto)
  3139. iwl4965_set_decrypted_flag(priv, rxb->skb, ampdu_status, stats);
  3140. if (priv->add_radiotap)
  3141. iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
  3142. ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
  3143. priv->alloc_rxb_skb--;
  3144. rxb->skb = NULL;
  3145. #ifdef LED
  3146. priv->led_packets += len;
  3147. iwl4965_setup_activity_timer(priv);
  3148. #endif
  3149. }
  3150. /* Calc max signal level (dBm) among 3 possible receivers */
  3151. static int iwl4965_calc_rssi(struct iwl4965_rx_phy_res *rx_resp)
  3152. {
  3153. /* data from PHY/DSP regarding signal strength, etc.,
  3154. * contents are always there, not configurable by host. */
  3155. struct iwl4965_rx_non_cfg_phy *ncphy =
  3156. (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
  3157. u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
  3158. >> IWL_AGC_DB_POS;
  3159. u32 valid_antennae =
  3160. (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
  3161. >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
  3162. u8 max_rssi = 0;
  3163. u32 i;
  3164. /* Find max rssi among 3 possible receivers.
  3165. * These values are measured by the digital signal processor (DSP).
  3166. * They should stay fairly constant even as the signal strength varies,
  3167. * if the radio's automatic gain control (AGC) is working right.
  3168. * AGC value (see below) will provide the "interesting" info. */
  3169. for (i = 0; i < 3; i++)
  3170. if (valid_antennae & (1 << i))
  3171. max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
  3172. IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  3173. ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
  3174. max_rssi, agc);
  3175. /* dBm = max_rssi dB - agc dB - constant.
  3176. * Higher AGC (higher radio gain) means lower signal. */
  3177. return (max_rssi - agc - IWL_RSSI_OFFSET);
  3178. }
  3179. #ifdef CONFIG_IWL4965_HT
  3180. /* Parsed Information Elements */
  3181. struct ieee802_11_elems {
  3182. u8 *ds_params;
  3183. u8 ds_params_len;
  3184. u8 *tim;
  3185. u8 tim_len;
  3186. u8 *ibss_params;
  3187. u8 ibss_params_len;
  3188. u8 *erp_info;
  3189. u8 erp_info_len;
  3190. u8 *ht_cap_param;
  3191. u8 ht_cap_param_len;
  3192. u8 *ht_extra_param;
  3193. u8 ht_extra_param_len;
  3194. };
  3195. static int parse_elems(u8 *start, size_t len, struct ieee802_11_elems *elems)
  3196. {
  3197. size_t left = len;
  3198. u8 *pos = start;
  3199. int unknown = 0;
  3200. memset(elems, 0, sizeof(*elems));
  3201. while (left >= 2) {
  3202. u8 id, elen;
  3203. id = *pos++;
  3204. elen = *pos++;
  3205. left -= 2;
  3206. if (elen > left)
  3207. return -1;
  3208. switch (id) {
  3209. case WLAN_EID_DS_PARAMS:
  3210. elems->ds_params = pos;
  3211. elems->ds_params_len = elen;
  3212. break;
  3213. case WLAN_EID_TIM:
  3214. elems->tim = pos;
  3215. elems->tim_len = elen;
  3216. break;
  3217. case WLAN_EID_IBSS_PARAMS:
  3218. elems->ibss_params = pos;
  3219. elems->ibss_params_len = elen;
  3220. break;
  3221. case WLAN_EID_ERP_INFO:
  3222. elems->erp_info = pos;
  3223. elems->erp_info_len = elen;
  3224. break;
  3225. case WLAN_EID_HT_CAPABILITY:
  3226. elems->ht_cap_param = pos;
  3227. elems->ht_cap_param_len = elen;
  3228. break;
  3229. case WLAN_EID_HT_EXTRA_INFO:
  3230. elems->ht_extra_param = pos;
  3231. elems->ht_extra_param_len = elen;
  3232. break;
  3233. default:
  3234. unknown++;
  3235. break;
  3236. }
  3237. left -= elen;
  3238. pos += elen;
  3239. }
  3240. return 0;
  3241. }
  3242. void iwl4965_init_ht_hw_capab(struct ieee80211_ht_info *ht_info, int mode)
  3243. {
  3244. ht_info->cap = 0;
  3245. memset(ht_info->supp_mcs_set, 0, 16);
  3246. ht_info->ht_supported = 1;
  3247. if (mode == MODE_IEEE80211A) {
  3248. ht_info->cap |= (u16)IEEE80211_HT_CAP_SUP_WIDTH;
  3249. ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_40;
  3250. ht_info->supp_mcs_set[4] = 0x01;
  3251. }
  3252. ht_info->cap |= (u16)IEEE80211_HT_CAP_GRN_FLD;
  3253. ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_20;
  3254. ht_info->cap |= (u16)(IEEE80211_HT_CAP_MIMO_PS &
  3255. (IWL_MIMO_PS_NONE << 2));
  3256. if (iwl4965_param_amsdu_size_8K) {
  3257. printk(KERN_DEBUG "iwl4965 in A-MSDU 8K support mode\n");
  3258. ht_info->cap |= (u16)IEEE80211_HT_CAP_MAX_AMSDU;
  3259. }
  3260. ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
  3261. ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
  3262. ht_info->supp_mcs_set[0] = 0xFF;
  3263. ht_info->supp_mcs_set[1] = 0xFF;
  3264. }
  3265. #endif /* CONFIG_IWL4965_HT */
  3266. static void iwl4965_sta_modify_ps_wake(struct iwl4965_priv *priv, int sta_id)
  3267. {
  3268. unsigned long flags;
  3269. spin_lock_irqsave(&priv->sta_lock, flags);
  3270. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  3271. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  3272. priv->stations[sta_id].sta.sta.modify_mask = 0;
  3273. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3274. spin_unlock_irqrestore(&priv->sta_lock, flags);
  3275. iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  3276. }
  3277. static void iwl4965_update_ps_mode(struct iwl4965_priv *priv, u16 ps_bit, u8 *addr)
  3278. {
  3279. /* FIXME: need locking over ps_status ??? */
  3280. u8 sta_id = iwl4965_hw_find_station(priv, addr);
  3281. if (sta_id != IWL_INVALID_STATION) {
  3282. u8 sta_awake = priv->stations[sta_id].
  3283. ps_status == STA_PS_STATUS_WAKE;
  3284. if (sta_awake && ps_bit)
  3285. priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
  3286. else if (!sta_awake && !ps_bit) {
  3287. iwl4965_sta_modify_ps_wake(priv, sta_id);
  3288. priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
  3289. }
  3290. }
  3291. }
  3292. #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
  3293. /* Called for REPLY_4965_RX (legacy ABG frames), or
  3294. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  3295. static void iwl4965_rx_reply_rx(struct iwl4965_priv *priv,
  3296. struct iwl4965_rx_mem_buffer *rxb)
  3297. {
  3298. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  3299. /* Use phy data (Rx signal strength, etc.) contained within
  3300. * this rx packet for legacy frames,
  3301. * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
  3302. int include_phy = (pkt->hdr.cmd == REPLY_4965_RX);
  3303. struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
  3304. (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
  3305. (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
  3306. __le32 *rx_end;
  3307. unsigned int len = 0;
  3308. struct ieee80211_hdr *header;
  3309. u16 fc;
  3310. struct ieee80211_rx_status stats = {
  3311. .mactime = le64_to_cpu(rx_start->timestamp),
  3312. .channel = le16_to_cpu(rx_start->channel),
  3313. .phymode =
  3314. (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  3315. MODE_IEEE80211G : MODE_IEEE80211A,
  3316. .antenna = 0,
  3317. .rate = iwl4965_hw_get_rate(rx_start->rate_n_flags),
  3318. .flag = 0,
  3319. };
  3320. u8 network_packet;
  3321. if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
  3322. IWL_DEBUG_DROP
  3323. ("dsp size out of range [0,20]: "
  3324. "%d/n", rx_start->cfg_phy_cnt);
  3325. return;
  3326. }
  3327. if (!include_phy) {
  3328. if (priv->last_phy_res[0])
  3329. rx_start = (struct iwl4965_rx_phy_res *)
  3330. &priv->last_phy_res[1];
  3331. else
  3332. rx_start = NULL;
  3333. }
  3334. if (!rx_start) {
  3335. IWL_ERROR("MPDU frame without a PHY data\n");
  3336. return;
  3337. }
  3338. if (include_phy) {
  3339. header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
  3340. + rx_start->cfg_phy_cnt);
  3341. len = le16_to_cpu(rx_start->byte_count);
  3342. rx_end = (__le32 *) (pkt->u.raw + rx_start->cfg_phy_cnt +
  3343. sizeof(struct iwl4965_rx_phy_res) + len);
  3344. } else {
  3345. struct iwl4965_rx_mpdu_res_start *amsdu =
  3346. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  3347. header = (void *)(pkt->u.raw +
  3348. sizeof(struct iwl4965_rx_mpdu_res_start));
  3349. len = le16_to_cpu(amsdu->byte_count);
  3350. rx_end = (__le32 *) (pkt->u.raw +
  3351. sizeof(struct iwl4965_rx_mpdu_res_start) + len);
  3352. }
  3353. if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
  3354. !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  3355. IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
  3356. le32_to_cpu(*rx_end));
  3357. return;
  3358. }
  3359. priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
  3360. stats.freq = ieee80211chan2mhz(stats.channel);
  3361. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  3362. stats.ssi = iwl4965_calc_rssi(rx_start);
  3363. /* Meaningful noise values are available only from beacon statistics,
  3364. * which are gathered only when associated, and indicate noise
  3365. * only for the associated network channel ...
  3366. * Ignore these noise values while scanning (other channels) */
  3367. if (iwl4965_is_associated(priv) &&
  3368. !test_bit(STATUS_SCANNING, &priv->status)) {
  3369. stats.noise = priv->last_rx_noise;
  3370. stats.signal = iwl4965_calc_sig_qual(stats.ssi, stats.noise);
  3371. } else {
  3372. stats.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  3373. stats.signal = iwl4965_calc_sig_qual(stats.ssi, 0);
  3374. }
  3375. /* Reset beacon noise level if not associated. */
  3376. if (!iwl4965_is_associated(priv))
  3377. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  3378. #ifdef CONFIG_IWL4965_DEBUG
  3379. /* TODO: Parts of iwl4965_report_frame are broken for 4965 */
  3380. if (iwl4965_debug_level & (IWL_DL_RX))
  3381. /* Set "1" to report good data frames in groups of 100 */
  3382. iwl4965_report_frame(priv, pkt, header, 1);
  3383. if (iwl4965_debug_level & (IWL_DL_RX | IWL_DL_STATS))
  3384. IWL_DEBUG_RX("Rssi %d, noise %d, qual %d, TSF %lu\n",
  3385. stats.ssi, stats.noise, stats.signal,
  3386. (long unsigned int)le64_to_cpu(rx_start->timestamp));
  3387. #endif
  3388. network_packet = iwl4965_is_network_packet(priv, header);
  3389. if (network_packet) {
  3390. priv->last_rx_rssi = stats.ssi;
  3391. priv->last_beacon_time = priv->ucode_beacon_time;
  3392. priv->last_tsf = le64_to_cpu(rx_start->timestamp);
  3393. }
  3394. fc = le16_to_cpu(header->frame_control);
  3395. switch (fc & IEEE80211_FCTL_FTYPE) {
  3396. case IEEE80211_FTYPE_MGMT:
  3397. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  3398. iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  3399. header->addr2);
  3400. switch (fc & IEEE80211_FCTL_STYPE) {
  3401. case IEEE80211_STYPE_PROBE_RESP:
  3402. case IEEE80211_STYPE_BEACON:
  3403. if ((priv->iw_mode == IEEE80211_IF_TYPE_STA &&
  3404. !compare_ether_addr(header->addr2, priv->bssid)) ||
  3405. (priv->iw_mode == IEEE80211_IF_TYPE_IBSS &&
  3406. !compare_ether_addr(header->addr3, priv->bssid))) {
  3407. struct ieee80211_mgmt *mgmt =
  3408. (struct ieee80211_mgmt *)header;
  3409. u64 timestamp =
  3410. le64_to_cpu(mgmt->u.beacon.timestamp);
  3411. priv->timestamp0 = timestamp & 0xFFFFFFFF;
  3412. priv->timestamp1 =
  3413. (timestamp >> 32) & 0xFFFFFFFF;
  3414. priv->beacon_int = le16_to_cpu(
  3415. mgmt->u.beacon.beacon_int);
  3416. if (priv->call_post_assoc_from_beacon &&
  3417. (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
  3418. priv->call_post_assoc_from_beacon = 0;
  3419. queue_work(priv->workqueue,
  3420. &priv->post_associate.work);
  3421. }
  3422. }
  3423. break;
  3424. case IEEE80211_STYPE_ACTION:
  3425. break;
  3426. /*
  3427. * TODO: Use the new callback function from
  3428. * mac80211 instead of sniffing these packets.
  3429. */
  3430. case IEEE80211_STYPE_ASSOC_RESP:
  3431. case IEEE80211_STYPE_REASSOC_RESP:
  3432. if (network_packet) {
  3433. #ifdef CONFIG_IWL4965_HT
  3434. u8 *pos = NULL;
  3435. struct ieee802_11_elems elems;
  3436. #endif /*CONFIG_IWL4965_HT */
  3437. struct ieee80211_mgmt *mgnt =
  3438. (struct ieee80211_mgmt *)header;
  3439. /* We have just associated, give some
  3440. * time for the 4-way handshake if
  3441. * any. Don't start scan too early. */
  3442. priv->next_scan_jiffies = jiffies +
  3443. IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
  3444. priv->assoc_id = (~((1 << 15) | (1 << 14))
  3445. & le16_to_cpu(mgnt->u.assoc_resp.aid));
  3446. priv->assoc_capability =
  3447. le16_to_cpu(
  3448. mgnt->u.assoc_resp.capab_info);
  3449. #ifdef CONFIG_IWL4965_HT
  3450. pos = mgnt->u.assoc_resp.variable;
  3451. if (!parse_elems(pos,
  3452. len - (pos - (u8 *) mgnt),
  3453. &elems)) {
  3454. if (elems.ht_extra_param &&
  3455. elems.ht_cap_param)
  3456. break;
  3457. }
  3458. #endif /*CONFIG_IWL4965_HT */
  3459. /* assoc_id is 0 no association */
  3460. if (!priv->assoc_id)
  3461. break;
  3462. if (priv->beacon_int)
  3463. queue_work(priv->workqueue,
  3464. &priv->post_associate.work);
  3465. else
  3466. priv->call_post_assoc_from_beacon = 1;
  3467. }
  3468. break;
  3469. case IEEE80211_STYPE_PROBE_REQ:
  3470. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  3471. !iwl4965_is_associated(priv)) {
  3472. DECLARE_MAC_BUF(mac1);
  3473. DECLARE_MAC_BUF(mac2);
  3474. DECLARE_MAC_BUF(mac3);
  3475. IWL_DEBUG_DROP("Dropping (non network): "
  3476. "%s, %s, %s\n",
  3477. print_mac(mac1, header->addr1),
  3478. print_mac(mac2, header->addr2),
  3479. print_mac(mac3, header->addr3));
  3480. return;
  3481. }
  3482. }
  3483. iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &stats);
  3484. break;
  3485. case IEEE80211_FTYPE_CTL:
  3486. #ifdef CONFIG_IWL4965_HT
  3487. switch (fc & IEEE80211_FCTL_STYPE) {
  3488. case IEEE80211_STYPE_BACK_REQ:
  3489. IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
  3490. iwl4965_handle_data_packet(priv, 0, include_phy,
  3491. rxb, &stats);
  3492. break;
  3493. default:
  3494. break;
  3495. }
  3496. #endif
  3497. break;
  3498. case IEEE80211_FTYPE_DATA: {
  3499. DECLARE_MAC_BUF(mac1);
  3500. DECLARE_MAC_BUF(mac2);
  3501. DECLARE_MAC_BUF(mac3);
  3502. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  3503. iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  3504. header->addr2);
  3505. if (unlikely(!network_packet))
  3506. IWL_DEBUG_DROP("Dropping (non network): "
  3507. "%s, %s, %s\n",
  3508. print_mac(mac1, header->addr1),
  3509. print_mac(mac2, header->addr2),
  3510. print_mac(mac3, header->addr3));
  3511. else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
  3512. IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
  3513. print_mac(mac1, header->addr1),
  3514. print_mac(mac2, header->addr2),
  3515. print_mac(mac3, header->addr3));
  3516. else
  3517. iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
  3518. &stats);
  3519. break;
  3520. }
  3521. default:
  3522. break;
  3523. }
  3524. }
  3525. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  3526. * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  3527. static void iwl4965_rx_reply_rx_phy(struct iwl4965_priv *priv,
  3528. struct iwl4965_rx_mem_buffer *rxb)
  3529. {
  3530. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  3531. priv->last_phy_res[0] = 1;
  3532. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  3533. sizeof(struct iwl4965_rx_phy_res));
  3534. }
  3535. static void iwl4965_rx_missed_beacon_notif(struct iwl4965_priv *priv,
  3536. struct iwl4965_rx_mem_buffer *rxb)
  3537. {
  3538. #ifdef CONFIG_IWL4965_SENSITIVITY
  3539. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  3540. struct iwl4965_missed_beacon_notif *missed_beacon;
  3541. missed_beacon = &pkt->u.missed_beacon;
  3542. if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
  3543. IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  3544. le32_to_cpu(missed_beacon->consequtive_missed_beacons),
  3545. le32_to_cpu(missed_beacon->total_missed_becons),
  3546. le32_to_cpu(missed_beacon->num_recvd_beacons),
  3547. le32_to_cpu(missed_beacon->num_expected_beacons));
  3548. priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
  3549. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)))
  3550. queue_work(priv->workqueue, &priv->sensitivity_work);
  3551. }
  3552. #endif /*CONFIG_IWL4965_SENSITIVITY*/
  3553. }
  3554. #ifdef CONFIG_IWL4965_HT
  3555. #ifdef CONFIG_IWL4965_HT_AGG
  3556. /**
  3557. * iwl4965_set_tx_status - Update driver's record of one Tx frame's status
  3558. *
  3559. * This will get sent to mac80211.
  3560. */
  3561. static void iwl4965_set_tx_status(struct iwl4965_priv *priv, int txq_id, int idx,
  3562. u32 status, u32 retry_count, u32 rate)
  3563. {
  3564. struct ieee80211_tx_status *tx_status =
  3565. &(priv->txq[txq_id].txb[idx].status);
  3566. tx_status->flags = status ? IEEE80211_TX_STATUS_ACK : 0;
  3567. tx_status->retry_count += retry_count;
  3568. tx_status->control.tx_rate = rate;
  3569. }
  3570. /**
  3571. * iwl4965_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
  3572. */
  3573. static void iwl4965_sta_modify_enable_tid_tx(struct iwl4965_priv *priv,
  3574. int sta_id, int tid)
  3575. {
  3576. unsigned long flags;
  3577. /* Remove "disable" flag, to enable Tx for this TID */
  3578. spin_lock_irqsave(&priv->sta_lock, flags);
  3579. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  3580. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  3581. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3582. spin_unlock_irqrestore(&priv->sta_lock, flags);
  3583. iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  3584. }
  3585. /**
  3586. * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
  3587. *
  3588. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  3589. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  3590. */
  3591. static int iwl4965_tx_status_reply_compressed_ba(struct iwl4965_priv *priv,
  3592. struct iwl4965_ht_agg *agg,
  3593. struct iwl4965_compressed_ba_resp*
  3594. ba_resp)
  3595. {
  3596. int i, sh, ack;
  3597. u16 ba_seq_ctl = le16_to_cpu(ba_resp->ba_seq_ctl);
  3598. u32 bitmap0, bitmap1;
  3599. u32 resp_bitmap0 = le32_to_cpu(ba_resp->ba_bitmap0);
  3600. u32 resp_bitmap1 = le32_to_cpu(ba_resp->ba_bitmap1);
  3601. if (unlikely(!agg->wait_for_ba)) {
  3602. IWL_ERROR("Received BA when not expected\n");
  3603. return -EINVAL;
  3604. }
  3605. /* Mark that the expected block-ack response arrived */
  3606. agg->wait_for_ba = 0;
  3607. IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->ba_seq_ctl);
  3608. /* Calculate shift to align block-ack bits with our Tx window bits */
  3609. sh = agg->start_idx - SEQ_TO_INDEX(ba_seq_ctl>>4);
  3610. if (sh < 0) /* tbw something is wrong with indices */
  3611. sh += 0x100;
  3612. /* don't use 64-bit values for now */
  3613. bitmap0 = resp_bitmap0 >> sh;
  3614. bitmap1 = resp_bitmap1 >> sh;
  3615. bitmap0 |= (resp_bitmap1 & ((1<<sh)|((1<<sh)-1))) << (32 - sh);
  3616. if (agg->frame_count > (64 - sh)) {
  3617. IWL_DEBUG_TX_REPLY("more frames than bitmap size");
  3618. return -1;
  3619. }
  3620. /* check for success or failure according to the
  3621. * transmitted bitmap and block-ack bitmap */
  3622. bitmap0 &= agg->bitmap0;
  3623. bitmap1 &= agg->bitmap1;
  3624. /* For each frame attempted in aggregation,
  3625. * update driver's record of tx frame's status. */
  3626. for (i = 0; i < agg->frame_count ; i++) {
  3627. int idx = (agg->start_idx + i) & 0xff;
  3628. ack = bitmap0 & (1 << i);
  3629. IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
  3630. ack? "ACK":"NACK", i, idx, agg->start_idx + i);
  3631. iwl4965_set_tx_status(priv, agg->txq_id, idx, ack, 0,
  3632. agg->rate_n_flags);
  3633. }
  3634. IWL_DEBUG_TX_REPLY("Bitmap %x%x\n", bitmap0, bitmap1);
  3635. return 0;
  3636. }
  3637. /**
  3638. * iwl4965_queue_dec_wrap - Decrement queue index, wrap back to end if needed
  3639. * @index -- current index
  3640. * @n_bd -- total number of entries in queue (s/b power of 2)
  3641. */
  3642. static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
  3643. {
  3644. return (index == 0) ? n_bd - 1 : index - 1;
  3645. }
  3646. /**
  3647. * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  3648. *
  3649. * Handles block-acknowledge notification from device, which reports success
  3650. * of frames sent via aggregation.
  3651. */
  3652. static void iwl4965_rx_reply_compressed_ba(struct iwl4965_priv *priv,
  3653. struct iwl4965_rx_mem_buffer *rxb)
  3654. {
  3655. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  3656. struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  3657. int index;
  3658. struct iwl4965_tx_queue *txq = NULL;
  3659. struct iwl4965_ht_agg *agg;
  3660. /* "flow" corresponds to Tx queue */
  3661. u16 ba_resp_scd_flow = le16_to_cpu(ba_resp->scd_flow);
  3662. /* "ssn" is start of block-ack Tx window, corresponds to index
  3663. * (in Tx queue's circular buffer) of first TFD/frame in window */
  3664. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  3665. if (ba_resp_scd_flow >= ARRAY_SIZE(priv->txq)) {
  3666. IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
  3667. return;
  3668. }
  3669. txq = &priv->txq[ba_resp_scd_flow];
  3670. agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
  3671. /* Find index just before block-ack window */
  3672. index = iwl4965_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  3673. /* TODO: Need to get this copy more safely - now good for debug */
  3674. /*
  3675. {
  3676. DECLARE_MAC_BUF(mac);
  3677. IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
  3678. "sta_id = %d\n",
  3679. agg->wait_for_ba,
  3680. print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
  3681. ba_resp->sta_id);
  3682. IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%X%X, scd_flow = "
  3683. "%d, scd_ssn = %d\n",
  3684. ba_resp->tid,
  3685. ba_resp->ba_seq_ctl,
  3686. ba_resp->ba_bitmap1,
  3687. ba_resp->ba_bitmap0,
  3688. ba_resp->scd_flow,
  3689. ba_resp->scd_ssn);
  3690. IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%X%X \n",
  3691. agg->start_idx,
  3692. agg->bitmap1,
  3693. agg->bitmap0);
  3694. }
  3695. */
  3696. /* Update driver's record of ACK vs. not for each frame in window */
  3697. iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  3698. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  3699. * block-ack window (we assume that they've been successfully
  3700. * transmitted ... if not, it's too late anyway). */
  3701. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff))
  3702. iwl4965_tx_queue_reclaim(priv, ba_resp_scd_flow, index);
  3703. }
  3704. /**
  3705. * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
  3706. */
  3707. static void iwl4965_tx_queue_stop_scheduler(struct iwl4965_priv *priv, u16 txq_id)
  3708. {
  3709. /* Simply stop the queue, but don't change any configuration;
  3710. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  3711. iwl4965_write_prph(priv,
  3712. KDR_SCD_QUEUE_STATUS_BITS(txq_id),
  3713. (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  3714. (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  3715. }
  3716. /**
  3717. * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
  3718. */
  3719. static int iwl4965_tx_queue_set_q2ratid(struct iwl4965_priv *priv, u16 ra_tid,
  3720. u16 txq_id)
  3721. {
  3722. u32 tbl_dw_addr;
  3723. u32 tbl_dw;
  3724. u16 scd_q2ratid;
  3725. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  3726. tbl_dw_addr = priv->scd_base_addr +
  3727. SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  3728. tbl_dw = iwl4965_read_targ_mem(priv, tbl_dw_addr);
  3729. if (txq_id & 0x1)
  3730. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  3731. else
  3732. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  3733. iwl4965_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
  3734. return 0;
  3735. }
  3736. /**
  3737. * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
  3738. *
  3739. * NOTE: txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID,
  3740. * i.e. it must be one of the higher queues used for aggregation
  3741. */
  3742. static int iwl4965_tx_queue_agg_enable(struct iwl4965_priv *priv, int txq_id,
  3743. int tx_fifo, int sta_id, int tid,
  3744. u16 ssn_idx)
  3745. {
  3746. unsigned long flags;
  3747. int rc;
  3748. u16 ra_tid;
  3749. if (IWL_BACK_QUEUE_FIRST_ID > txq_id)
  3750. IWL_WARNING("queue number too small: %d, must be > %d\n",
  3751. txq_id, IWL_BACK_QUEUE_FIRST_ID);
  3752. ra_tid = BUILD_RAxTID(sta_id, tid);
  3753. /* Modify device's station table to Tx this TID */
  3754. iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
  3755. spin_lock_irqsave(&priv->lock, flags);
  3756. rc = iwl4965_grab_nic_access(priv);
  3757. if (rc) {
  3758. spin_unlock_irqrestore(&priv->lock, flags);
  3759. return rc;
  3760. }
  3761. /* Stop this Tx queue before configuring it */
  3762. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  3763. /* Map receiver-address / traffic-ID to this queue */
  3764. iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
  3765. /* Set this queue as a chain-building queue */
  3766. iwl4965_set_bits_prph(priv, KDR_SCD_QUEUECHAIN_SEL, (1<<txq_id));
  3767. /* Place first TFD at index corresponding to start sequence number.
  3768. * Assumes that ssn_idx is valid (!= 0xFFF) */
  3769. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  3770. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  3771. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  3772. /* Set up Tx window size and frame limit for this queue */
  3773. iwl4965_write_targ_mem(priv,
  3774. priv->scd_base_addr + SCD_CONTEXT_QUEUE_OFFSET(txq_id),
  3775. (SCD_WIN_SIZE << SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  3776. SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  3777. iwl4965_write_targ_mem(priv, priv->scd_base_addr +
  3778. SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  3779. (SCD_FRAME_LIMIT << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
  3780. & SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  3781. iwl4965_set_bits_prph(priv, KDR_SCD_INTERRUPT_MASK, (1 << txq_id));
  3782. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  3783. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
  3784. iwl4965_release_nic_access(priv);
  3785. spin_unlock_irqrestore(&priv->lock, flags);
  3786. return 0;
  3787. }
  3788. /**
  3789. * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
  3790. */
  3791. static int iwl4965_tx_queue_agg_disable(struct iwl4965_priv *priv, u16 txq_id,
  3792. u16 ssn_idx, u8 tx_fifo)
  3793. {
  3794. unsigned long flags;
  3795. int rc;
  3796. if (IWL_BACK_QUEUE_FIRST_ID > txq_id) {
  3797. IWL_WARNING("queue number too small: %d, must be > %d\n",
  3798. txq_id, IWL_BACK_QUEUE_FIRST_ID);
  3799. return -EINVAL;
  3800. }
  3801. spin_lock_irqsave(&priv->lock, flags);
  3802. rc = iwl4965_grab_nic_access(priv);
  3803. if (rc) {
  3804. spin_unlock_irqrestore(&priv->lock, flags);
  3805. return rc;
  3806. }
  3807. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  3808. iwl4965_clear_bits_prph(priv, KDR_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  3809. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  3810. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  3811. /* supposes that ssn_idx is valid (!= 0xFFF) */
  3812. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  3813. iwl4965_clear_bits_prph(priv, KDR_SCD_INTERRUPT_MASK, (1 << txq_id));
  3814. iwl4965_txq_ctx_deactivate(priv, txq_id);
  3815. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
  3816. iwl4965_release_nic_access(priv);
  3817. spin_unlock_irqrestore(&priv->lock, flags);
  3818. return 0;
  3819. }
  3820. #endif/* CONFIG_IWL4965_HT_AGG */
  3821. #endif /* CONFIG_IWL4965_HT */
  3822. /**
  3823. * iwl4965_add_station - Initialize a station's hardware rate table
  3824. *
  3825. * The uCode's station table contains a table of fallback rates
  3826. * for automatic fallback during transmission.
  3827. *
  3828. * NOTE: This sets up a default set of values. These will be replaced later
  3829. * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
  3830. * rc80211_simple.
  3831. *
  3832. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  3833. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  3834. * which requires station table entry to exist).
  3835. */
  3836. void iwl4965_add_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
  3837. {
  3838. int i, r;
  3839. struct iwl4965_link_quality_cmd link_cmd = {
  3840. .reserved1 = 0,
  3841. };
  3842. u16 rate_flags;
  3843. /* Set up the rate scaling to start at selected rate, fall back
  3844. * all the way down to 1M in IEEE order, and then spin on 1M */
  3845. if (is_ap)
  3846. r = IWL_RATE_54M_INDEX;
  3847. else if (priv->phymode == MODE_IEEE80211A)
  3848. r = IWL_RATE_6M_INDEX;
  3849. else
  3850. r = IWL_RATE_1M_INDEX;
  3851. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  3852. rate_flags = 0;
  3853. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  3854. rate_flags |= RATE_MCS_CCK_MSK;
  3855. /* Use Tx antenna B only */
  3856. rate_flags |= RATE_MCS_ANT_B_MSK;
  3857. rate_flags &= ~RATE_MCS_ANT_A_MSK;
  3858. link_cmd.rs_table[i].rate_n_flags =
  3859. iwl4965_hw_set_rate_n_flags(iwl4965_rates[r].plcp, rate_flags);
  3860. r = iwl4965_get_prev_ieee_rate(r);
  3861. }
  3862. link_cmd.general_params.single_stream_ant_msk = 2;
  3863. link_cmd.general_params.dual_stream_ant_msk = 3;
  3864. link_cmd.agg_params.agg_dis_start_th = 3;
  3865. link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
  3866. /* Update the rate scaling for control frame Tx to AP */
  3867. link_cmd.sta_id = is_ap ? IWL_AP_ID : IWL4965_BROADCAST_ID;
  3868. iwl4965_send_cmd_pdu(priv, REPLY_TX_LINK_QUALITY_CMD, sizeof(link_cmd),
  3869. &link_cmd);
  3870. }
  3871. #ifdef CONFIG_IWL4965_HT
  3872. static u8 iwl4965_is_channel_extension(struct iwl4965_priv *priv, int phymode,
  3873. u16 channel, u8 extension_chan_offset)
  3874. {
  3875. const struct iwl4965_channel_info *ch_info;
  3876. ch_info = iwl4965_get_channel_info(priv, phymode, channel);
  3877. if (!is_channel_valid(ch_info))
  3878. return 0;
  3879. if (extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_AUTO)
  3880. return 0;
  3881. if ((ch_info->fat_extension_channel == extension_chan_offset) ||
  3882. (ch_info->fat_extension_channel == HT_IE_EXT_CHANNEL_MAX))
  3883. return 1;
  3884. return 0;
  3885. }
  3886. static u8 iwl4965_is_fat_tx_allowed(struct iwl4965_priv *priv,
  3887. struct ieee80211_ht_info *sta_ht_inf)
  3888. {
  3889. struct iwl_ht_info *iwl_ht_conf = &priv->current_ht_config;
  3890. if ((!iwl_ht_conf->is_ht) ||
  3891. (iwl_ht_conf->supported_chan_width != IWL_CHANNEL_WIDTH_40MHZ) ||
  3892. (iwl_ht_conf->extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_AUTO))
  3893. return 0;
  3894. if (sta_ht_inf) {
  3895. if ((!sta_ht_inf->ht_supported) ||
  3896. (!sta_ht_inf->cap & IEEE80211_HT_CAP_SUP_WIDTH))
  3897. return 0;
  3898. }
  3899. return (iwl4965_is_channel_extension(priv, priv->phymode,
  3900. iwl_ht_conf->control_channel,
  3901. iwl_ht_conf->extension_chan_offset));
  3902. }
  3903. void iwl4965_set_rxon_ht(struct iwl4965_priv *priv, struct iwl_ht_info *ht_info)
  3904. {
  3905. struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
  3906. u32 val;
  3907. if (!ht_info->is_ht)
  3908. return;
  3909. /* Set up channel bandwidth: 20 MHz only, or 20/40 mixed if fat ok */
  3910. if (iwl4965_is_fat_tx_allowed(priv, NULL))
  3911. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  3912. else
  3913. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
  3914. RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
  3915. if (le16_to_cpu(rxon->channel) != ht_info->control_channel) {
  3916. IWL_DEBUG_ASSOC("control diff than current %d %d\n",
  3917. le16_to_cpu(rxon->channel),
  3918. ht_info->control_channel);
  3919. rxon->channel = cpu_to_le16(ht_info->control_channel);
  3920. return;
  3921. }
  3922. /* Note: control channel is opposite of extension channel */
  3923. switch (ht_info->extension_chan_offset) {
  3924. case IWL_EXT_CHANNEL_OFFSET_ABOVE:
  3925. rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  3926. break;
  3927. case IWL_EXT_CHANNEL_OFFSET_BELOW:
  3928. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3929. break;
  3930. case IWL_EXT_CHANNEL_OFFSET_AUTO:
  3931. rxon->flags &= ~RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  3932. break;
  3933. default:
  3934. rxon->flags &= ~RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  3935. break;
  3936. }
  3937. val = ht_info->ht_protection;
  3938. rxon->flags |= cpu_to_le32(val << RXON_FLG_HT_OPERATING_MODE_POS);
  3939. iwl4965_set_rxon_chain(priv);
  3940. IWL_DEBUG_ASSOC("supported HT rate 0x%X %X "
  3941. "rxon flags 0x%X operation mode :0x%X "
  3942. "extension channel offset 0x%x "
  3943. "control chan %d\n",
  3944. ht_info->supp_mcs_set[0], ht_info->supp_mcs_set[1],
  3945. le32_to_cpu(rxon->flags), ht_info->ht_protection,
  3946. ht_info->extension_chan_offset,
  3947. ht_info->control_channel);
  3948. return;
  3949. }
  3950. void iwl4965_set_ht_add_station(struct iwl4965_priv *priv, u8 index,
  3951. struct ieee80211_ht_info *sta_ht_inf)
  3952. {
  3953. __le32 sta_flags;
  3954. if (!sta_ht_inf || !sta_ht_inf->ht_supported)
  3955. goto done;
  3956. sta_flags = priv->stations[index].sta.station_flags;
  3957. if (((sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS >> 2))
  3958. == IWL_MIMO_PS_DYNAMIC)
  3959. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  3960. else
  3961. sta_flags &= ~STA_FLG_RTS_MIMO_PROT_MSK;
  3962. sta_flags |= cpu_to_le32(
  3963. (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  3964. sta_flags |= cpu_to_le32(
  3965. (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  3966. if (iwl4965_is_fat_tx_allowed(priv, sta_ht_inf))
  3967. sta_flags |= STA_FLG_FAT_EN_MSK;
  3968. else
  3969. sta_flags &= (~STA_FLG_FAT_EN_MSK);
  3970. priv->stations[index].sta.station_flags = sta_flags;
  3971. done:
  3972. return;
  3973. }
  3974. static void iwl4965_sta_modify_add_ba_tid(struct iwl4965_priv *priv,
  3975. int sta_id, int tid, u16 ssn)
  3976. {
  3977. unsigned long flags;
  3978. spin_lock_irqsave(&priv->sta_lock, flags);
  3979. priv->stations[sta_id].sta.station_flags_msk = 0;
  3980. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  3981. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  3982. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  3983. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3984. spin_unlock_irqrestore(&priv->sta_lock, flags);
  3985. iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  3986. }
  3987. static void iwl4965_sta_modify_del_ba_tid(struct iwl4965_priv *priv,
  3988. int sta_id, int tid)
  3989. {
  3990. unsigned long flags;
  3991. spin_lock_irqsave(&priv->sta_lock, flags);
  3992. priv->stations[sta_id].sta.station_flags_msk = 0;
  3993. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  3994. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  3995. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3996. spin_unlock_irqrestore(&priv->sta_lock, flags);
  3997. iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  3998. }
  3999. int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
  4000. enum ieee80211_ampdu_mlme_action action,
  4001. const u8 *addr, u16 tid, u16 ssn)
  4002. {
  4003. struct iwl4965_priv *priv = hw->priv;
  4004. int sta_id;
  4005. DECLARE_MAC_BUF(mac);
  4006. IWL_DEBUG_HT("A-MPDU action on da=%s tid=%d ",
  4007. print_mac(mac, addr), tid);
  4008. sta_id = iwl4965_hw_find_station(priv, addr);
  4009. switch (action) {
  4010. case IEEE80211_AMPDU_RX_START:
  4011. IWL_DEBUG_HT("start Rx\n");
  4012. iwl4965_sta_modify_add_ba_tid(priv, sta_id, tid, ssn);
  4013. break;
  4014. case IEEE80211_AMPDU_RX_STOP:
  4015. IWL_DEBUG_HT("stop Rx\n");
  4016. iwl4965_sta_modify_del_ba_tid(priv, sta_id, tid);
  4017. break;
  4018. default:
  4019. IWL_DEBUG_HT("unknown\n");
  4020. return -EINVAL;
  4021. break;
  4022. }
  4023. return 0;
  4024. }
  4025. #ifdef CONFIG_IWL4965_HT_AGG
  4026. static const u16 default_tid_to_tx_fifo[] = {
  4027. IWL_TX_FIFO_AC1,
  4028. IWL_TX_FIFO_AC0,
  4029. IWL_TX_FIFO_AC0,
  4030. IWL_TX_FIFO_AC1,
  4031. IWL_TX_FIFO_AC2,
  4032. IWL_TX_FIFO_AC2,
  4033. IWL_TX_FIFO_AC3,
  4034. IWL_TX_FIFO_AC3,
  4035. IWL_TX_FIFO_NONE,
  4036. IWL_TX_FIFO_NONE,
  4037. IWL_TX_FIFO_NONE,
  4038. IWL_TX_FIFO_NONE,
  4039. IWL_TX_FIFO_NONE,
  4040. IWL_TX_FIFO_NONE,
  4041. IWL_TX_FIFO_NONE,
  4042. IWL_TX_FIFO_NONE,
  4043. IWL_TX_FIFO_AC3
  4044. };
  4045. /*
  4046. * Find first available (lowest unused) Tx Queue, mark it "active".
  4047. * Called only when finding queue for aggregation.
  4048. * Should never return anything < 7, because they should already
  4049. * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
  4050. */
  4051. static int iwl4965_txq_ctx_activate_free(struct iwl4965_priv *priv)
  4052. {
  4053. int txq_id;
  4054. for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++)
  4055. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  4056. return txq_id;
  4057. return -1;
  4058. }
  4059. int iwl4965_mac_ht_tx_agg_start(struct ieee80211_hw *hw, u8 *da, u16 tid,
  4060. u16 *start_seq_num)
  4061. {
  4062. struct iwl4965_priv *priv = hw->priv;
  4063. int sta_id;
  4064. int tx_fifo;
  4065. int txq_id;
  4066. int ssn = -1;
  4067. unsigned long flags;
  4068. struct iwl4965_tid_data *tid_data;
  4069. DECLARE_MAC_BUF(mac);
  4070. /* Determine Tx DMA/FIFO channel for this Traffic ID */
  4071. if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
  4072. tx_fifo = default_tid_to_tx_fifo[tid];
  4073. else
  4074. return -EINVAL;
  4075. IWL_WARNING("iwl-AGG iwl4965_mac_ht_tx_agg_start on da=%s"
  4076. " tid=%d\n", print_mac(mac, da), tid);
  4077. /* Get index into station table */
  4078. sta_id = iwl4965_hw_find_station(priv, da);
  4079. if (sta_id == IWL_INVALID_STATION)
  4080. return -ENXIO;
  4081. /* Find available Tx queue for aggregation */
  4082. txq_id = iwl4965_txq_ctx_activate_free(priv);
  4083. if (txq_id == -1)
  4084. return -ENXIO;
  4085. spin_lock_irqsave(&priv->sta_lock, flags);
  4086. tid_data = &priv->stations[sta_id].tid[tid];
  4087. /* Get starting sequence number for 1st frame in block ack window.
  4088. * We'll use least signif byte as 1st frame's index into Tx queue. */
  4089. ssn = SEQ_TO_SN(tid_data->seq_number);
  4090. tid_data->agg.txq_id = txq_id;
  4091. spin_unlock_irqrestore(&priv->sta_lock, flags);
  4092. *start_seq_num = ssn;
  4093. /* Update driver's link quality manager */
  4094. iwl4965_ba_status(priv, tid, BA_STATUS_ACTIVE);
  4095. /* Set up and enable aggregation for selected Tx queue and FIFO */
  4096. return iwl4965_tx_queue_agg_enable(priv, txq_id, tx_fifo,
  4097. sta_id, tid, ssn);
  4098. }
  4099. int iwl4965_mac_ht_tx_agg_stop(struct ieee80211_hw *hw, u8 *da, u16 tid,
  4100. int generator)
  4101. {
  4102. struct iwl4965_priv *priv = hw->priv;
  4103. int tx_fifo_id, txq_id, sta_id, ssn = -1;
  4104. struct iwl4965_tid_data *tid_data;
  4105. int rc;
  4106. DECLARE_MAC_BUF(mac);
  4107. if (!da) {
  4108. IWL_ERROR("%s: da = NULL\n", __func__);
  4109. return -EINVAL;
  4110. }
  4111. if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
  4112. tx_fifo_id = default_tid_to_tx_fifo[tid];
  4113. else
  4114. return -EINVAL;
  4115. sta_id = iwl4965_hw_find_station(priv, da);
  4116. if (sta_id == IWL_INVALID_STATION)
  4117. return -ENXIO;
  4118. tid_data = &priv->stations[sta_id].tid[tid];
  4119. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  4120. txq_id = tid_data->agg.txq_id;
  4121. rc = iwl4965_tx_queue_agg_disable(priv, txq_id, ssn, tx_fifo_id);
  4122. /* FIXME: need more safe way to handle error condition */
  4123. if (rc)
  4124. return rc;
  4125. iwl4965_ba_status(priv, tid, BA_STATUS_INITIATOR_DELBA);
  4126. IWL_DEBUG_INFO("iwl4965_mac_ht_tx_agg_stop on da=%s tid=%d\n",
  4127. print_mac(mac, da), tid);
  4128. return 0;
  4129. }
  4130. #endif /* CONFIG_IWL4965_HT_AGG */
  4131. #endif /* CONFIG_IWL4965_HT */
  4132. /* Set up 4965-specific Rx frame reply handlers */
  4133. void iwl4965_hw_rx_handler_setup(struct iwl4965_priv *priv)
  4134. {
  4135. /* Legacy Rx frames */
  4136. priv->rx_handlers[REPLY_4965_RX] = iwl4965_rx_reply_rx;
  4137. /* High-throughput (HT) Rx frames */
  4138. priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
  4139. priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
  4140. priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
  4141. iwl4965_rx_missed_beacon_notif;
  4142. #ifdef CONFIG_IWL4965_HT
  4143. #ifdef CONFIG_IWL4965_HT_AGG
  4144. priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
  4145. #endif /* CONFIG_IWL4965_HT_AGG */
  4146. #endif /* CONFIG_IWL4965_HT */
  4147. }
  4148. void iwl4965_hw_setup_deferred_work(struct iwl4965_priv *priv)
  4149. {
  4150. INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
  4151. INIT_WORK(&priv->statistics_work, iwl4965_bg_statistics_work);
  4152. #ifdef CONFIG_IWL4965_SENSITIVITY
  4153. INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
  4154. #endif
  4155. #ifdef CONFIG_IWL4965_HT
  4156. #ifdef CONFIG_IWL4965_HT_AGG
  4157. INIT_WORK(&priv->agg_work, iwl4965_bg_agg_work);
  4158. #endif /* CONFIG_IWL4965_HT_AGG */
  4159. #endif /* CONFIG_IWL4965_HT */
  4160. init_timer(&priv->statistics_periodic);
  4161. priv->statistics_periodic.data = (unsigned long)priv;
  4162. priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
  4163. }
  4164. void iwl4965_hw_cancel_deferred_work(struct iwl4965_priv *priv)
  4165. {
  4166. del_timer_sync(&priv->statistics_periodic);
  4167. cancel_delayed_work(&priv->init_alive_start);
  4168. }
  4169. struct pci_device_id iwl4965_hw_card_ids[] = {
  4170. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4229)},
  4171. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4230)},
  4172. {0}
  4173. };
  4174. /*
  4175. * The device's EEPROM semaphore prevents conflicts between driver and uCode
  4176. * when accessing the EEPROM; each access is a series of pulses to/from the
  4177. * EEPROM chip, not a single event, so even reads could conflict if they
  4178. * weren't arbitrated by the semaphore.
  4179. */
  4180. int iwl4965_eeprom_acquire_semaphore(struct iwl4965_priv *priv)
  4181. {
  4182. u16 count;
  4183. int rc;
  4184. for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
  4185. /* Request semaphore */
  4186. iwl4965_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  4187. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  4188. /* See if we got it */
  4189. rc = iwl4965_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  4190. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  4191. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  4192. EEPROM_SEM_TIMEOUT);
  4193. if (rc >= 0) {
  4194. IWL_DEBUG_IO("Acquired semaphore after %d tries.\n",
  4195. count+1);
  4196. return rc;
  4197. }
  4198. }
  4199. return rc;
  4200. }
  4201. inline void iwl4965_eeprom_release_semaphore(struct iwl4965_priv *priv)
  4202. {
  4203. iwl4965_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
  4204. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  4205. }
  4206. MODULE_DEVICE_TABLE(pci, iwl4965_hw_card_ids);