iwl-4965.c 141 KB

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