iwl-4965.c 138 KB

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