iwl-4965.c 131 KB

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