iwl-4965.c 140 KB

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