iwl-4965.c 122 KB

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