iwl-4965.c 138 KB

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