iwl-4965.c 140 KB

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