iwl-4965.c 115 KB

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