iwl-4965.c 121 KB

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