iwl-4965.c 138 KB

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