iwl-4965.c 123 KB

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