iwl-4965.c 122 KB

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