iwl-4965.c 143 KB

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