iwl3945-base.c 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/pci.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/wireless.h>
  38. #include <linux/firmware.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/if_arp.h>
  41. #include <net/ieee80211_radiotap.h>
  42. #include <net/lib80211.h>
  43. #include <net/mac80211.h>
  44. #include <asm/div64.h>
  45. #define DRV_NAME "iwl3945"
  46. #include "iwl-fh.h"
  47. #include "iwl-3945-fh.h"
  48. #include "iwl-commands.h"
  49. #include "iwl-sta.h"
  50. #include "iwl-3945.h"
  51. #include "iwl-helpers.h"
  52. #include "iwl-core.h"
  53. #include "iwl-dev.h"
  54. /*
  55. * module name, copyright, version, etc.
  56. */
  57. #define DRV_DESCRIPTION \
  58. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  59. #ifdef CONFIG_IWLWIFI_DEBUG
  60. #define VD "d"
  61. #else
  62. #define VD
  63. #endif
  64. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  65. #define VS "s"
  66. #else
  67. #define VS
  68. #endif
  69. #define IWL39_VERSION "1.2.26k" VD VS
  70. #define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
  71. #define DRV_AUTHOR "<ilw@linux.intel.com>"
  72. #define DRV_VERSION IWL39_VERSION
  73. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  74. MODULE_VERSION(DRV_VERSION);
  75. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  76. MODULE_LICENSE("GPL");
  77. /* module parameters */
  78. struct iwl_mod_params iwl3945_mod_params = {
  79. .num_of_queues = IWL39_MAX_NUM_QUEUES,
  80. .sw_crypto = 1,
  81. .restart_fw = 1,
  82. /* the rest are 0 by default */
  83. };
  84. /*************** STATION TABLE MANAGEMENT ****
  85. * mac80211 should be examined to determine if sta_info is duplicating
  86. * the functionality provided here
  87. */
  88. /**************************************************************/
  89. #if 0 /* temporary disable till we add real remove station */
  90. /**
  91. * iwl3945_remove_station - Remove driver's knowledge of station.
  92. *
  93. * NOTE: This does not remove station from device's station table.
  94. */
  95. static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  96. {
  97. int index = IWL_INVALID_STATION;
  98. int i;
  99. unsigned long flags;
  100. spin_lock_irqsave(&priv->sta_lock, flags);
  101. if (is_ap)
  102. index = IWL_AP_ID;
  103. else if (is_broadcast_ether_addr(addr))
  104. index = priv->hw_params.bcast_sta_id;
  105. else
  106. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  107. if (priv->stations_39[i].used &&
  108. !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  109. addr)) {
  110. index = i;
  111. break;
  112. }
  113. if (unlikely(index == IWL_INVALID_STATION))
  114. goto out;
  115. if (priv->stations_39[index].used) {
  116. priv->stations_39[index].used = 0;
  117. priv->num_stations--;
  118. }
  119. BUG_ON(priv->num_stations < 0);
  120. out:
  121. spin_unlock_irqrestore(&priv->sta_lock, flags);
  122. return 0;
  123. }
  124. #endif
  125. /**
  126. * iwl3945_clear_stations_table - Clear the driver's station table
  127. *
  128. * NOTE: This does not clear or otherwise alter the device's station table.
  129. */
  130. void iwl3945_clear_stations_table(struct iwl_priv *priv)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&priv->sta_lock, flags);
  134. priv->num_stations = 0;
  135. memset(priv->stations_39, 0, sizeof(priv->stations_39));
  136. spin_unlock_irqrestore(&priv->sta_lock, flags);
  137. }
  138. /**
  139. * iwl3945_add_station - Add station to station tables in driver and device
  140. */
  141. u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
  142. {
  143. int i;
  144. int index = IWL_INVALID_STATION;
  145. struct iwl3945_station_entry *station;
  146. unsigned long flags_spin;
  147. u8 rate;
  148. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  149. if (is_ap)
  150. index = IWL_AP_ID;
  151. else if (is_broadcast_ether_addr(addr))
  152. index = priv->hw_params.bcast_sta_id;
  153. else
  154. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  155. if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  156. addr)) {
  157. index = i;
  158. break;
  159. }
  160. if (!priv->stations_39[i].used &&
  161. index == IWL_INVALID_STATION)
  162. index = i;
  163. }
  164. /* These two conditions has the same outcome but keep them separate
  165. since they have different meaning */
  166. if (unlikely(index == IWL_INVALID_STATION)) {
  167. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  168. return index;
  169. }
  170. if (priv->stations_39[index].used &&
  171. !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
  172. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  173. return index;
  174. }
  175. IWL_DEBUG_ASSOC(priv, "Add STA ID %d: %pM\n", index, addr);
  176. station = &priv->stations_39[index];
  177. station->used = 1;
  178. priv->num_stations++;
  179. /* Set up the REPLY_ADD_STA command to send to device */
  180. memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
  181. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  182. station->sta.mode = 0;
  183. station->sta.sta.sta_id = index;
  184. station->sta.station_flags = 0;
  185. if (priv->band == IEEE80211_BAND_5GHZ)
  186. rate = IWL_RATE_6M_PLCP;
  187. else
  188. rate = IWL_RATE_1M_PLCP;
  189. /* Turn on both antennas for the station... */
  190. station->sta.rate_n_flags =
  191. iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
  192. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  193. /* Add station to device's station table */
  194. iwl_send_add_sta(priv,
  195. (struct iwl_addsta_cmd *)&station->sta, flags);
  196. return index;
  197. }
  198. /**
  199. * iwl3945_get_antenna_flags - Get antenna flags for RXON command
  200. * @priv: eeprom and antenna fields are used to determine antenna flags
  201. *
  202. * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
  203. * iwl3945_mod_params.antenna specifies the antenna diversity mode:
  204. *
  205. * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
  206. * IWL_ANTENNA_MAIN - Force MAIN antenna
  207. * IWL_ANTENNA_AUX - Force AUX antenna
  208. */
  209. __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
  210. {
  211. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  212. switch (iwl3945_mod_params.antenna) {
  213. case IWL_ANTENNA_DIVERSITY:
  214. return 0;
  215. case IWL_ANTENNA_MAIN:
  216. if (eeprom->antenna_switch_type)
  217. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  218. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  219. case IWL_ANTENNA_AUX:
  220. if (eeprom->antenna_switch_type)
  221. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  222. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  223. }
  224. /* bad antenna selector value */
  225. IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
  226. iwl3945_mod_params.antenna);
  227. return 0; /* "diversity" is default if error */
  228. }
  229. static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  230. struct ieee80211_key_conf *keyconf,
  231. u8 sta_id)
  232. {
  233. unsigned long flags;
  234. __le16 key_flags = 0;
  235. int ret;
  236. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  237. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  238. if (sta_id == priv->hw_params.bcast_sta_id)
  239. key_flags |= STA_KEY_MULTICAST_MSK;
  240. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  241. keyconf->hw_key_idx = keyconf->keyidx;
  242. key_flags &= ~STA_KEY_FLG_INVALID;
  243. spin_lock_irqsave(&priv->sta_lock, flags);
  244. priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
  245. priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
  246. memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
  247. keyconf->keylen);
  248. memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
  249. keyconf->keylen);
  250. if ((priv->stations_39[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  251. == STA_KEY_FLG_NO_ENC)
  252. priv->stations_39[sta_id].sta.key.key_offset =
  253. iwl_get_free_ucode_key_index(priv);
  254. /* else, we are overriding an existing key => no need to allocated room
  255. * in uCode. */
  256. WARN(priv->stations_39[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  257. "no space for a new key");
  258. priv->stations_39[sta_id].sta.key.key_flags = key_flags;
  259. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  260. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  261. IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
  262. ret = iwl_send_add_sta(priv,
  263. (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, CMD_ASYNC);
  264. spin_unlock_irqrestore(&priv->sta_lock, flags);
  265. return ret;
  266. }
  267. static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  268. struct ieee80211_key_conf *keyconf,
  269. u8 sta_id)
  270. {
  271. return -EOPNOTSUPP;
  272. }
  273. static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
  274. struct ieee80211_key_conf *keyconf,
  275. u8 sta_id)
  276. {
  277. return -EOPNOTSUPP;
  278. }
  279. static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
  280. {
  281. unsigned long flags;
  282. spin_lock_irqsave(&priv->sta_lock, flags);
  283. memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
  284. memset(&priv->stations_39[sta_id].sta.key, 0,
  285. sizeof(struct iwl4965_keyinfo));
  286. priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  287. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  288. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  289. spin_unlock_irqrestore(&priv->sta_lock, flags);
  290. IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
  291. iwl_send_add_sta(priv,
  292. (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
  293. return 0;
  294. }
  295. static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
  296. struct ieee80211_key_conf *keyconf, u8 sta_id)
  297. {
  298. int ret = 0;
  299. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  300. switch (keyconf->alg) {
  301. case ALG_CCMP:
  302. ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  303. break;
  304. case ALG_TKIP:
  305. ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  306. break;
  307. case ALG_WEP:
  308. ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  309. break;
  310. default:
  311. IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
  312. ret = -EINVAL;
  313. }
  314. IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
  315. keyconf->alg, keyconf->keylen, keyconf->keyidx,
  316. sta_id, ret);
  317. return ret;
  318. }
  319. static int iwl3945_remove_static_key(struct iwl_priv *priv)
  320. {
  321. int ret = -EOPNOTSUPP;
  322. return ret;
  323. }
  324. static int iwl3945_set_static_key(struct iwl_priv *priv,
  325. struct ieee80211_key_conf *key)
  326. {
  327. if (key->alg == ALG_WEP)
  328. return -EOPNOTSUPP;
  329. IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
  330. return -EINVAL;
  331. }
  332. static void iwl3945_clear_free_frames(struct iwl_priv *priv)
  333. {
  334. struct list_head *element;
  335. IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
  336. priv->frames_count);
  337. while (!list_empty(&priv->free_frames)) {
  338. element = priv->free_frames.next;
  339. list_del(element);
  340. kfree(list_entry(element, struct iwl3945_frame, list));
  341. priv->frames_count--;
  342. }
  343. if (priv->frames_count) {
  344. IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
  345. priv->frames_count);
  346. priv->frames_count = 0;
  347. }
  348. }
  349. static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
  350. {
  351. struct iwl3945_frame *frame;
  352. struct list_head *element;
  353. if (list_empty(&priv->free_frames)) {
  354. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  355. if (!frame) {
  356. IWL_ERR(priv, "Could not allocate frame!\n");
  357. return NULL;
  358. }
  359. priv->frames_count++;
  360. return frame;
  361. }
  362. element = priv->free_frames.next;
  363. list_del(element);
  364. return list_entry(element, struct iwl3945_frame, list);
  365. }
  366. static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
  367. {
  368. memset(frame, 0, sizeof(*frame));
  369. list_add(&frame->list, &priv->free_frames);
  370. }
  371. unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
  372. struct ieee80211_hdr *hdr,
  373. int left)
  374. {
  375. if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
  376. ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
  377. (priv->iw_mode != NL80211_IFTYPE_AP)))
  378. return 0;
  379. if (priv->ibss_beacon->len > left)
  380. return 0;
  381. memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
  382. return priv->ibss_beacon->len;
  383. }
  384. static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
  385. {
  386. struct iwl3945_frame *frame;
  387. unsigned int frame_size;
  388. int rc;
  389. u8 rate;
  390. frame = iwl3945_get_free_frame(priv);
  391. if (!frame) {
  392. IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
  393. "command.\n");
  394. return -ENOMEM;
  395. }
  396. rate = iwl_rate_get_lowest_plcp(priv);
  397. frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
  398. rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  399. &frame->u.cmd[0]);
  400. iwl3945_free_frame(priv, frame);
  401. return rc;
  402. }
  403. static void iwl3945_unset_hw_params(struct iwl_priv *priv)
  404. {
  405. if (priv->shared_virt)
  406. pci_free_consistent(priv->pci_dev,
  407. sizeof(struct iwl3945_shared),
  408. priv->shared_virt,
  409. priv->shared_phys);
  410. }
  411. #define MAX_UCODE_BEACON_INTERVAL 1024
  412. #define INTEL_CONN_LISTEN_INTERVAL cpu_to_le16(0xA)
  413. static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
  414. {
  415. u16 new_val = 0;
  416. u16 beacon_factor = 0;
  417. beacon_factor =
  418. (beacon_val + MAX_UCODE_BEACON_INTERVAL)
  419. / MAX_UCODE_BEACON_INTERVAL;
  420. new_val = beacon_val / beacon_factor;
  421. return cpu_to_le16(new_val);
  422. }
  423. static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
  424. {
  425. u64 interval_tm_unit;
  426. u64 tsf, result;
  427. unsigned long flags;
  428. struct ieee80211_conf *conf = NULL;
  429. u16 beacon_int = 0;
  430. conf = ieee80211_get_hw_conf(priv->hw);
  431. spin_lock_irqsave(&priv->lock, flags);
  432. priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
  433. priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
  434. tsf = priv->timestamp;
  435. beacon_int = priv->beacon_int;
  436. spin_unlock_irqrestore(&priv->lock, flags);
  437. if (priv->iw_mode == NL80211_IFTYPE_STATION) {
  438. if (beacon_int == 0) {
  439. priv->rxon_timing.beacon_interval = cpu_to_le16(100);
  440. priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
  441. } else {
  442. priv->rxon_timing.beacon_interval =
  443. cpu_to_le16(beacon_int);
  444. priv->rxon_timing.beacon_interval =
  445. iwl3945_adjust_beacon_interval(
  446. le16_to_cpu(priv->rxon_timing.beacon_interval));
  447. }
  448. priv->rxon_timing.atim_window = 0;
  449. } else {
  450. priv->rxon_timing.beacon_interval =
  451. iwl3945_adjust_beacon_interval(conf->beacon_int);
  452. /* TODO: we need to get atim_window from upper stack
  453. * for now we set to 0 */
  454. priv->rxon_timing.atim_window = 0;
  455. }
  456. interval_tm_unit =
  457. (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
  458. result = do_div(tsf, interval_tm_unit);
  459. priv->rxon_timing.beacon_init_val =
  460. cpu_to_le32((u32) ((u64) interval_tm_unit - result));
  461. IWL_DEBUG_ASSOC(priv,
  462. "beacon interval %d beacon timer %d beacon tim %d\n",
  463. le16_to_cpu(priv->rxon_timing.beacon_interval),
  464. le32_to_cpu(priv->rxon_timing.beacon_init_val),
  465. le16_to_cpu(priv->rxon_timing.atim_window));
  466. }
  467. static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
  468. {
  469. if (mode == NL80211_IFTYPE_ADHOC) {
  470. const struct iwl_channel_info *ch_info;
  471. ch_info = iwl_get_channel_info(priv,
  472. priv->band,
  473. le16_to_cpu(priv->staging_rxon.channel));
  474. if (!ch_info || !is_channel_ibss(ch_info)) {
  475. IWL_ERR(priv, "channel %d not IBSS channel\n",
  476. le16_to_cpu(priv->staging_rxon.channel));
  477. return -EINVAL;
  478. }
  479. }
  480. iwl_connection_init_rx_config(priv, mode);
  481. iwl3945_clear_stations_table(priv);
  482. /* don't commit rxon if rf-kill is on*/
  483. if (!iwl_is_ready_rf(priv))
  484. return -EAGAIN;
  485. cancel_delayed_work(&priv->scan_check);
  486. if (iwl_scan_cancel_timeout(priv, 100)) {
  487. IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
  488. IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
  489. return -EAGAIN;
  490. }
  491. iwlcore_commit_rxon(priv);
  492. return 0;
  493. }
  494. static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
  495. struct ieee80211_tx_info *info,
  496. struct iwl_cmd *cmd,
  497. struct sk_buff *skb_frag,
  498. int sta_id)
  499. {
  500. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  501. struct iwl3945_hw_key *keyinfo =
  502. &priv->stations_39[sta_id].keyinfo;
  503. switch (keyinfo->alg) {
  504. case ALG_CCMP:
  505. tx->sec_ctl = TX_CMD_SEC_CCM;
  506. memcpy(tx->key, keyinfo->key, keyinfo->keylen);
  507. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  508. break;
  509. case ALG_TKIP:
  510. break;
  511. case ALG_WEP:
  512. tx->sec_ctl = TX_CMD_SEC_WEP |
  513. (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
  514. if (keyinfo->keylen == 13)
  515. tx->sec_ctl |= TX_CMD_SEC_KEY128;
  516. memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
  517. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  518. "with key %d\n", info->control.hw_key->hw_key_idx);
  519. break;
  520. default:
  521. IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
  522. break;
  523. }
  524. }
  525. /*
  526. * handle build REPLY_TX command notification.
  527. */
  528. static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
  529. struct iwl_cmd *cmd,
  530. struct ieee80211_tx_info *info,
  531. struct ieee80211_hdr *hdr, u8 std_id)
  532. {
  533. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  534. __le32 tx_flags = tx->tx_flags;
  535. __le16 fc = hdr->frame_control;
  536. u8 rc_flags = info->control.rates[0].flags;
  537. tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  538. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  539. tx_flags |= TX_CMD_FLG_ACK_MSK;
  540. if (ieee80211_is_mgmt(fc))
  541. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  542. if (ieee80211_is_probe_resp(fc) &&
  543. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  544. tx_flags |= TX_CMD_FLG_TSF_MSK;
  545. } else {
  546. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  547. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  548. }
  549. tx->sta_id = std_id;
  550. if (ieee80211_has_morefrags(fc))
  551. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  552. if (ieee80211_is_data_qos(fc)) {
  553. u8 *qc = ieee80211_get_qos_ctl(hdr);
  554. tx->tid_tspec = qc[0] & 0xf;
  555. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  556. } else {
  557. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  558. }
  559. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  560. tx_flags |= TX_CMD_FLG_RTS_MSK;
  561. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  562. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  563. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  564. tx_flags |= TX_CMD_FLG_CTS_MSK;
  565. }
  566. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  567. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  568. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  569. if (ieee80211_is_mgmt(fc)) {
  570. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  571. tx->timeout.pm_frame_timeout = cpu_to_le16(3);
  572. else
  573. tx->timeout.pm_frame_timeout = cpu_to_le16(2);
  574. } else {
  575. tx->timeout.pm_frame_timeout = 0;
  576. #ifdef CONFIG_IWLWIFI_LEDS
  577. priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
  578. #endif
  579. }
  580. tx->driver_txop = 0;
  581. tx->tx_flags = tx_flags;
  582. tx->next_frame_len = 0;
  583. }
  584. /**
  585. * iwl3945_get_sta_id - Find station's index within station table
  586. */
  587. static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  588. {
  589. int sta_id;
  590. u16 fc = le16_to_cpu(hdr->frame_control);
  591. /* If this frame is broadcast or management, use broadcast station id */
  592. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  593. is_multicast_ether_addr(hdr->addr1))
  594. return priv->hw_params.bcast_sta_id;
  595. switch (priv->iw_mode) {
  596. /* If we are a client station in a BSS network, use the special
  597. * AP station entry (that's the only station we communicate with) */
  598. case NL80211_IFTYPE_STATION:
  599. return IWL_AP_ID;
  600. /* If we are an AP, then find the station, or use BCAST */
  601. case NL80211_IFTYPE_AP:
  602. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  603. if (sta_id != IWL_INVALID_STATION)
  604. return sta_id;
  605. return priv->hw_params.bcast_sta_id;
  606. /* If this frame is going out to an IBSS network, find the station,
  607. * or create a new station table entry */
  608. case NL80211_IFTYPE_ADHOC: {
  609. /* Create new station table entry */
  610. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  611. if (sta_id != IWL_INVALID_STATION)
  612. return sta_id;
  613. sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
  614. if (sta_id != IWL_INVALID_STATION)
  615. return sta_id;
  616. IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
  617. "Defaulting to broadcast...\n",
  618. hdr->addr1);
  619. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  620. return priv->hw_params.bcast_sta_id;
  621. }
  622. /* If we are in monitor mode, use BCAST. This is required for
  623. * packet injection. */
  624. case NL80211_IFTYPE_MONITOR:
  625. return priv->hw_params.bcast_sta_id;
  626. default:
  627. IWL_WARN(priv, "Unknown mode of operation: %d\n",
  628. priv->iw_mode);
  629. return priv->hw_params.bcast_sta_id;
  630. }
  631. }
  632. /*
  633. * start REPLY_TX command process
  634. */
  635. static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  636. {
  637. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  638. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  639. struct iwl3945_tx_cmd *tx;
  640. struct iwl_tx_queue *txq = NULL;
  641. struct iwl_queue *q = NULL;
  642. struct iwl_cmd *out_cmd = NULL;
  643. dma_addr_t phys_addr;
  644. dma_addr_t txcmd_phys;
  645. int txq_id = skb_get_queue_mapping(skb);
  646. u16 len, idx, len_org, hdr_len;
  647. u8 id;
  648. u8 unicast;
  649. u8 sta_id;
  650. u8 tid = 0;
  651. u16 seq_number = 0;
  652. __le16 fc;
  653. u8 wait_write_ptr = 0;
  654. u8 *qc = NULL;
  655. unsigned long flags;
  656. int rc;
  657. spin_lock_irqsave(&priv->lock, flags);
  658. if (iwl_is_rfkill(priv)) {
  659. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  660. goto drop_unlock;
  661. }
  662. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
  663. IWL_ERR(priv, "ERROR: No TX rate available.\n");
  664. goto drop_unlock;
  665. }
  666. unicast = !is_multicast_ether_addr(hdr->addr1);
  667. id = 0;
  668. fc = hdr->frame_control;
  669. #ifdef CONFIG_IWLWIFI_DEBUG
  670. if (ieee80211_is_auth(fc))
  671. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  672. else if (ieee80211_is_assoc_req(fc))
  673. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  674. else if (ieee80211_is_reassoc_req(fc))
  675. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  676. #endif
  677. /* drop all data frame if we are not associated */
  678. if (ieee80211_is_data(fc) &&
  679. (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
  680. (!iwl_is_associated(priv) ||
  681. ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
  682. IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
  683. goto drop_unlock;
  684. }
  685. spin_unlock_irqrestore(&priv->lock, flags);
  686. hdr_len = ieee80211_hdrlen(fc);
  687. /* Find (or create) index into station table for destination station */
  688. sta_id = iwl3945_get_sta_id(priv, hdr);
  689. if (sta_id == IWL_INVALID_STATION) {
  690. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  691. hdr->addr1);
  692. goto drop;
  693. }
  694. IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
  695. if (ieee80211_is_data_qos(fc)) {
  696. qc = ieee80211_get_qos_ctl(hdr);
  697. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  698. seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
  699. IEEE80211_SCTL_SEQ;
  700. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  701. (hdr->seq_ctrl &
  702. cpu_to_le16(IEEE80211_SCTL_FRAG));
  703. seq_number += 0x10;
  704. }
  705. /* Descriptor for chosen Tx queue */
  706. txq = &priv->txq[txq_id];
  707. q = &txq->q;
  708. spin_lock_irqsave(&priv->lock, flags);
  709. idx = get_cmd_index(q, q->write_ptr, 0);
  710. /* Set up driver data for this TFD */
  711. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  712. txq->txb[q->write_ptr].skb[0] = skb;
  713. /* Init first empty entry in queue's array of Tx/cmd buffers */
  714. out_cmd = txq->cmd[idx];
  715. tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
  716. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  717. memset(tx, 0, sizeof(*tx));
  718. /*
  719. * Set up the Tx-command (not MAC!) header.
  720. * Store the chosen Tx queue and TFD index within the sequence field;
  721. * after Tx, uCode's Tx response will return this value so driver can
  722. * locate the frame within the tx queue and do post-tx processing.
  723. */
  724. out_cmd->hdr.cmd = REPLY_TX;
  725. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  726. INDEX_TO_SEQ(q->write_ptr)));
  727. /* Copy MAC header from skb into command buffer */
  728. memcpy(tx->hdr, hdr, hdr_len);
  729. /*
  730. * Use the first empty entry in this queue's command buffer array
  731. * to contain the Tx command and MAC header concatenated together
  732. * (payload data will be in another buffer).
  733. * Size of this varies, due to varying MAC header length.
  734. * If end is not dword aligned, we'll have 2 extra bytes at the end
  735. * of the MAC header (device reads on dword boundaries).
  736. * We'll tell device about this padding later.
  737. */
  738. len = sizeof(struct iwl3945_tx_cmd) +
  739. sizeof(struct iwl_cmd_header) + hdr_len;
  740. len_org = len;
  741. len = (len + 3) & ~3;
  742. if (len_org != len)
  743. len_org = 1;
  744. else
  745. len_org = 0;
  746. /* Physical address of this Tx command's header (not MAC header!),
  747. * within command buffer array. */
  748. txcmd_phys = pci_map_single(priv->pci_dev,
  749. out_cmd, sizeof(struct iwl_cmd),
  750. PCI_DMA_TODEVICE);
  751. pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
  752. pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
  753. /* Add buffer containing Tx command and MAC(!) header to TFD's
  754. * first entry */
  755. txcmd_phys += offsetof(struct iwl_cmd, hdr);
  756. /* Add buffer containing Tx command and MAC(!) header to TFD's
  757. * first entry */
  758. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  759. txcmd_phys, len, 1, 0);
  760. if (info->control.hw_key)
  761. iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
  762. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  763. * if any (802.11 null frames have no payload). */
  764. len = skb->len - hdr_len;
  765. if (len) {
  766. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  767. len, PCI_DMA_TODEVICE);
  768. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  769. phys_addr, len,
  770. 0, U32_PAD(len));
  771. }
  772. /* Total # bytes to be transmitted */
  773. len = (u16)skb->len;
  774. tx->len = cpu_to_le16(len);
  775. /* TODO need this for burst mode later on */
  776. iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
  777. /* set is_hcca to 0; it probably will never be implemented */
  778. iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
  779. tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  780. tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  781. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  782. txq->need_update = 1;
  783. if (qc)
  784. priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
  785. } else {
  786. wait_write_ptr = 1;
  787. txq->need_update = 0;
  788. }
  789. iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
  790. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
  791. ieee80211_hdrlen(fc));
  792. /* Tell device the write index *just past* this latest filled TFD */
  793. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  794. rc = iwl_txq_update_write_ptr(priv, txq);
  795. spin_unlock_irqrestore(&priv->lock, flags);
  796. if (rc)
  797. return rc;
  798. if ((iwl_queue_space(q) < q->high_mark)
  799. && priv->mac80211_registered) {
  800. if (wait_write_ptr) {
  801. spin_lock_irqsave(&priv->lock, flags);
  802. txq->need_update = 1;
  803. iwl_txq_update_write_ptr(priv, txq);
  804. spin_unlock_irqrestore(&priv->lock, flags);
  805. }
  806. iwl_stop_queue(priv, skb_get_queue_mapping(skb));
  807. }
  808. return 0;
  809. drop_unlock:
  810. spin_unlock_irqrestore(&priv->lock, flags);
  811. drop:
  812. return -1;
  813. }
  814. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  815. #include "iwl-spectrum.h"
  816. #define BEACON_TIME_MASK_LOW 0x00FFFFFF
  817. #define BEACON_TIME_MASK_HIGH 0xFF000000
  818. #define TIME_UNIT 1024
  819. /*
  820. * extended beacon time format
  821. * time in usec will be changed into a 32-bit value in 8:24 format
  822. * the high 1 byte is the beacon counts
  823. * the lower 3 bytes is the time in usec within one beacon interval
  824. */
  825. static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
  826. {
  827. u32 quot;
  828. u32 rem;
  829. u32 interval = beacon_interval * 1024;
  830. if (!interval || !usec)
  831. return 0;
  832. quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
  833. rem = (usec % interval) & BEACON_TIME_MASK_LOW;
  834. return (quot << 24) + rem;
  835. }
  836. /* base is usually what we get from ucode with each received frame,
  837. * the same as HW timer counter counting down
  838. */
  839. static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
  840. {
  841. u32 base_low = base & BEACON_TIME_MASK_LOW;
  842. u32 addon_low = addon & BEACON_TIME_MASK_LOW;
  843. u32 interval = beacon_interval * TIME_UNIT;
  844. u32 res = (base & BEACON_TIME_MASK_HIGH) +
  845. (addon & BEACON_TIME_MASK_HIGH);
  846. if (base_low > addon_low)
  847. res += base_low - addon_low;
  848. else if (base_low < addon_low) {
  849. res += interval + base_low - addon_low;
  850. res += (1 << 24);
  851. } else
  852. res += (1 << 24);
  853. return cpu_to_le32(res);
  854. }
  855. static int iwl3945_get_measurement(struct iwl_priv *priv,
  856. struct ieee80211_measurement_params *params,
  857. u8 type)
  858. {
  859. struct iwl_spectrum_cmd spectrum;
  860. struct iwl_rx_packet *res;
  861. struct iwl_host_cmd cmd = {
  862. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  863. .data = (void *)&spectrum,
  864. .meta.flags = CMD_WANT_SKB,
  865. };
  866. u32 add_time = le64_to_cpu(params->start_time);
  867. int rc;
  868. int spectrum_resp_status;
  869. int duration = le16_to_cpu(params->duration);
  870. if (iwl_is_associated(priv))
  871. add_time =
  872. iwl3945_usecs_to_beacons(
  873. le64_to_cpu(params->start_time) - priv->last_tsf,
  874. le16_to_cpu(priv->rxon_timing.beacon_interval));
  875. memset(&spectrum, 0, sizeof(spectrum));
  876. spectrum.channel_count = cpu_to_le16(1);
  877. spectrum.flags =
  878. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  879. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  880. cmd.len = sizeof(spectrum);
  881. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  882. if (iwl_is_associated(priv))
  883. spectrum.start_time =
  884. iwl3945_add_beacon_time(priv->last_beacon_time,
  885. add_time,
  886. le16_to_cpu(priv->rxon_timing.beacon_interval));
  887. else
  888. spectrum.start_time = 0;
  889. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  890. spectrum.channels[0].channel = params->channel;
  891. spectrum.channels[0].type = type;
  892. if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
  893. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  894. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  895. rc = iwl_send_cmd_sync(priv, &cmd);
  896. if (rc)
  897. return rc;
  898. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  899. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  900. IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
  901. rc = -EIO;
  902. }
  903. spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
  904. switch (spectrum_resp_status) {
  905. case 0: /* Command will be handled */
  906. if (res->u.spectrum.id != 0xff) {
  907. IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
  908. res->u.spectrum.id);
  909. priv->measurement_status &= ~MEASUREMENT_READY;
  910. }
  911. priv->measurement_status |= MEASUREMENT_ACTIVE;
  912. rc = 0;
  913. break;
  914. case 1: /* Command will not be handled */
  915. rc = -EAGAIN;
  916. break;
  917. }
  918. dev_kfree_skb_any(cmd.meta.u.skb);
  919. return rc;
  920. }
  921. #endif
  922. static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
  923. struct iwl_rx_mem_buffer *rxb)
  924. {
  925. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  926. struct iwl_alive_resp *palive;
  927. struct delayed_work *pwork;
  928. palive = &pkt->u.alive_frame;
  929. IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
  930. "0x%01X 0x%01X\n",
  931. palive->is_valid, palive->ver_type,
  932. palive->ver_subtype);
  933. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  934. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  935. memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
  936. sizeof(struct iwl_alive_resp));
  937. pwork = &priv->init_alive_start;
  938. } else {
  939. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  940. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  941. sizeof(struct iwl_alive_resp));
  942. pwork = &priv->alive_start;
  943. iwl3945_disable_events(priv);
  944. }
  945. /* We delay the ALIVE response by 5ms to
  946. * give the HW RF Kill time to activate... */
  947. if (palive->is_valid == UCODE_VALID_OK)
  948. queue_delayed_work(priv->workqueue, pwork,
  949. msecs_to_jiffies(5));
  950. else
  951. IWL_WARN(priv, "uCode did not respond OK.\n");
  952. }
  953. static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
  954. struct iwl_rx_mem_buffer *rxb)
  955. {
  956. #ifdef CONFIG_IWLWIFI_DEBUG
  957. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  958. #endif
  959. IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  960. return;
  961. }
  962. static void iwl3945_bg_beacon_update(struct work_struct *work)
  963. {
  964. struct iwl_priv *priv =
  965. container_of(work, struct iwl_priv, beacon_update);
  966. struct sk_buff *beacon;
  967. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  968. beacon = ieee80211_beacon_get(priv->hw, priv->vif);
  969. if (!beacon) {
  970. IWL_ERR(priv, "update beacon failed\n");
  971. return;
  972. }
  973. mutex_lock(&priv->mutex);
  974. /* new beacon skb is allocated every time; dispose previous.*/
  975. if (priv->ibss_beacon)
  976. dev_kfree_skb(priv->ibss_beacon);
  977. priv->ibss_beacon = beacon;
  978. mutex_unlock(&priv->mutex);
  979. iwl3945_send_beacon_cmd(priv);
  980. }
  981. static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
  982. struct iwl_rx_mem_buffer *rxb)
  983. {
  984. #ifdef CONFIG_IWLWIFI_DEBUG
  985. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  986. struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
  987. u8 rate = beacon->beacon_notify_hdr.rate;
  988. IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
  989. "tsf %d %d rate %d\n",
  990. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  991. beacon->beacon_notify_hdr.failure_frame,
  992. le32_to_cpu(beacon->ibss_mgr_status),
  993. le32_to_cpu(beacon->high_tsf),
  994. le32_to_cpu(beacon->low_tsf), rate);
  995. #endif
  996. if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
  997. (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
  998. queue_work(priv->workqueue, &priv->beacon_update);
  999. }
  1000. /* Handle notification from uCode that card's power state is changing
  1001. * due to software, hardware, or critical temperature RFKILL */
  1002. static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
  1003. struct iwl_rx_mem_buffer *rxb)
  1004. {
  1005. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1006. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  1007. unsigned long status = priv->status;
  1008. IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
  1009. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  1010. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  1011. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  1012. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  1013. if (flags & HW_CARD_DISABLED)
  1014. set_bit(STATUS_RF_KILL_HW, &priv->status);
  1015. else
  1016. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  1017. if (flags & SW_CARD_DISABLED)
  1018. set_bit(STATUS_RF_KILL_SW, &priv->status);
  1019. else
  1020. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  1021. iwl_scan_cancel(priv);
  1022. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  1023. test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
  1024. (test_bit(STATUS_RF_KILL_SW, &status) !=
  1025. test_bit(STATUS_RF_KILL_SW, &priv->status)))
  1026. queue_work(priv->workqueue, &priv->rf_kill);
  1027. else
  1028. wake_up_interruptible(&priv->wait_command_queue);
  1029. }
  1030. /**
  1031. * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
  1032. *
  1033. * Setup the RX handlers for each of the reply types sent from the uCode
  1034. * to the host.
  1035. *
  1036. * This function chains into the hardware specific files for them to setup
  1037. * any hardware specific handlers as well.
  1038. */
  1039. static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
  1040. {
  1041. priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
  1042. priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
  1043. priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
  1044. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
  1045. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
  1046. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  1047. iwl_rx_pm_debug_statistics_notif;
  1048. priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
  1049. /*
  1050. * The same handler is used for both the REPLY to a discrete
  1051. * statistics request from the host as well as for the periodic
  1052. * statistics notifications (after received beacons) from the uCode.
  1053. */
  1054. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
  1055. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
  1056. iwl_setup_spectrum_handlers(priv);
  1057. iwl_setup_rx_scan_handlers(priv);
  1058. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
  1059. /* Set up hardware specific Rx handlers */
  1060. iwl3945_hw_rx_handler_setup(priv);
  1061. }
  1062. /************************** RX-FUNCTIONS ****************************/
  1063. /*
  1064. * Rx theory of operation
  1065. *
  1066. * The host allocates 32 DMA target addresses and passes the host address
  1067. * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  1068. * 0 to 31
  1069. *
  1070. * Rx Queue Indexes
  1071. * The host/firmware share two index registers for managing the Rx buffers.
  1072. *
  1073. * The READ index maps to the first position that the firmware may be writing
  1074. * to -- the driver can read up to (but not including) this position and get
  1075. * good data.
  1076. * The READ index is managed by the firmware once the card is enabled.
  1077. *
  1078. * The WRITE index maps to the last position the driver has read from -- the
  1079. * position preceding WRITE is the last slot the firmware can place a packet.
  1080. *
  1081. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  1082. * WRITE = READ.
  1083. *
  1084. * During initialization, the host sets up the READ queue position to the first
  1085. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  1086. *
  1087. * When the firmware places a packet in a buffer, it will advance the READ index
  1088. * and fire the RX interrupt. The driver can then query the READ index and
  1089. * process as many packets as possible, moving the WRITE index forward as it
  1090. * resets the Rx queue buffers with new memory.
  1091. *
  1092. * The management in the driver is as follows:
  1093. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  1094. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  1095. * to replenish the iwl->rxq->rx_free.
  1096. * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
  1097. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  1098. * 'processed' and 'read' driver indexes as well)
  1099. * + A received packet is processed and handed to the kernel network stack,
  1100. * detached from the iwl->rxq. The driver 'processed' index is updated.
  1101. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  1102. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  1103. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  1104. * were enough free buffers and RX_STALLED is set it is cleared.
  1105. *
  1106. *
  1107. * Driver sequence:
  1108. *
  1109. * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
  1110. * iwl3945_rx_queue_restock
  1111. * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
  1112. * queue, updates firmware pointers, and updates
  1113. * the WRITE index. If insufficient rx_free buffers
  1114. * are available, schedules iwl3945_rx_replenish
  1115. *
  1116. * -- enable interrupts --
  1117. * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
  1118. * READ INDEX, detaching the SKB from the pool.
  1119. * Moves the packet buffer from queue to rx_used.
  1120. * Calls iwl3945_rx_queue_restock to refill any empty
  1121. * slots.
  1122. * ...
  1123. *
  1124. */
  1125. /**
  1126. * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  1127. */
  1128. static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
  1129. dma_addr_t dma_addr)
  1130. {
  1131. return cpu_to_le32((u32)dma_addr);
  1132. }
  1133. /**
  1134. * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
  1135. *
  1136. * If there are slots in the RX queue that need to be restocked,
  1137. * and we have free pre-allocated buffers, fill the ranks as much
  1138. * as we can, pulling from rx_free.
  1139. *
  1140. * This moves the 'write' index forward to catch up with 'processed', and
  1141. * also updates the memory address in the firmware to reference the new
  1142. * target buffer.
  1143. */
  1144. static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
  1145. {
  1146. struct iwl_rx_queue *rxq = &priv->rxq;
  1147. struct list_head *element;
  1148. struct iwl_rx_mem_buffer *rxb;
  1149. unsigned long flags;
  1150. int write, rc;
  1151. spin_lock_irqsave(&rxq->lock, flags);
  1152. write = rxq->write & ~0x7;
  1153. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  1154. /* Get next free Rx buffer, remove from free list */
  1155. element = rxq->rx_free.next;
  1156. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  1157. list_del(element);
  1158. /* Point to Rx buffer via next RBD in circular buffer */
  1159. rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
  1160. rxq->queue[rxq->write] = rxb;
  1161. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  1162. rxq->free_count--;
  1163. }
  1164. spin_unlock_irqrestore(&rxq->lock, flags);
  1165. /* If the pre-allocated buffer pool is dropping low, schedule to
  1166. * refill it */
  1167. if (rxq->free_count <= RX_LOW_WATERMARK)
  1168. queue_work(priv->workqueue, &priv->rx_replenish);
  1169. /* If we've added more space for the firmware to place data, tell it.
  1170. * Increment device's write pointer in multiples of 8. */
  1171. if ((write != (rxq->write & ~0x7))
  1172. || (abs(rxq->write - rxq->read) > 7)) {
  1173. spin_lock_irqsave(&rxq->lock, flags);
  1174. rxq->need_update = 1;
  1175. spin_unlock_irqrestore(&rxq->lock, flags);
  1176. rc = iwl_rx_queue_update_write_ptr(priv, rxq);
  1177. if (rc)
  1178. return rc;
  1179. }
  1180. return 0;
  1181. }
  1182. /**
  1183. * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
  1184. *
  1185. * When moving to rx_free an SKB is allocated for the slot.
  1186. *
  1187. * Also restock the Rx queue via iwl3945_rx_queue_restock.
  1188. * This is called as a scheduled work item (except for during initialization)
  1189. */
  1190. static void iwl3945_rx_allocate(struct iwl_priv *priv)
  1191. {
  1192. struct iwl_rx_queue *rxq = &priv->rxq;
  1193. struct list_head *element;
  1194. struct iwl_rx_mem_buffer *rxb;
  1195. unsigned long flags;
  1196. spin_lock_irqsave(&rxq->lock, flags);
  1197. while (!list_empty(&rxq->rx_used)) {
  1198. element = rxq->rx_used.next;
  1199. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  1200. /* Alloc a new receive buffer */
  1201. rxb->skb =
  1202. alloc_skb(priv->hw_params.rx_buf_size,
  1203. __GFP_NOWARN | GFP_ATOMIC);
  1204. if (!rxb->skb) {
  1205. if (net_ratelimit())
  1206. IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
  1207. /* We don't reschedule replenish work here -- we will
  1208. * call the restock method and if it still needs
  1209. * more buffers it will schedule replenish */
  1210. break;
  1211. }
  1212. /* If radiotap head is required, reserve some headroom here.
  1213. * The physical head count is a variable rx_stats->phy_count.
  1214. * We reserve 4 bytes here. Plus these extra bytes, the
  1215. * headroom of the physical head should be enough for the
  1216. * radiotap head that iwl3945 supported. See iwl3945_rt.
  1217. */
  1218. skb_reserve(rxb->skb, 4);
  1219. priv->alloc_rxb_skb++;
  1220. list_del(element);
  1221. /* Get physical address of RB/SKB */
  1222. rxb->real_dma_addr = pci_map_single(priv->pci_dev,
  1223. rxb->skb->data,
  1224. priv->hw_params.rx_buf_size,
  1225. PCI_DMA_FROMDEVICE);
  1226. list_add_tail(&rxb->list, &rxq->rx_free);
  1227. rxq->free_count++;
  1228. }
  1229. spin_unlock_irqrestore(&rxq->lock, flags);
  1230. }
  1231. /*
  1232. * this should be called while priv->lock is locked
  1233. */
  1234. static void __iwl3945_rx_replenish(void *data)
  1235. {
  1236. struct iwl_priv *priv = data;
  1237. iwl3945_rx_allocate(priv);
  1238. iwl3945_rx_queue_restock(priv);
  1239. }
  1240. void iwl3945_rx_replenish(void *data)
  1241. {
  1242. struct iwl_priv *priv = data;
  1243. unsigned long flags;
  1244. iwl3945_rx_allocate(priv);
  1245. spin_lock_irqsave(&priv->lock, flags);
  1246. iwl3945_rx_queue_restock(priv);
  1247. spin_unlock_irqrestore(&priv->lock, flags);
  1248. }
  1249. /* Convert linear signal-to-noise ratio into dB */
  1250. static u8 ratio2dB[100] = {
  1251. /* 0 1 2 3 4 5 6 7 8 9 */
  1252. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  1253. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  1254. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  1255. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  1256. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  1257. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  1258. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  1259. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  1260. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  1261. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  1262. };
  1263. /* Calculates a relative dB value from a ratio of linear
  1264. * (i.e. not dB) signal levels.
  1265. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  1266. int iwl3945_calc_db_from_ratio(int sig_ratio)
  1267. {
  1268. /* 1000:1 or higher just report as 60 dB */
  1269. if (sig_ratio >= 1000)
  1270. return 60;
  1271. /* 100:1 or higher, divide by 10 and use table,
  1272. * add 20 dB to make up for divide by 10 */
  1273. if (sig_ratio >= 100)
  1274. return 20 + (int)ratio2dB[sig_ratio/10];
  1275. /* We shouldn't see this */
  1276. if (sig_ratio < 1)
  1277. return 0;
  1278. /* Use table for ratios 1:1 - 99:1 */
  1279. return (int)ratio2dB[sig_ratio];
  1280. }
  1281. #define PERFECT_RSSI (-20) /* dBm */
  1282. #define WORST_RSSI (-95) /* dBm */
  1283. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  1284. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  1285. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  1286. * about formulas used below. */
  1287. int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
  1288. {
  1289. int sig_qual;
  1290. int degradation = PERFECT_RSSI - rssi_dbm;
  1291. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  1292. * as indicator; formula is (signal dbm - noise dbm).
  1293. * SNR at or above 40 is a great signal (100%).
  1294. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  1295. * Weakest usable signal is usually 10 - 15 dB SNR. */
  1296. if (noise_dbm) {
  1297. if (rssi_dbm - noise_dbm >= 40)
  1298. return 100;
  1299. else if (rssi_dbm < noise_dbm)
  1300. return 0;
  1301. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  1302. /* Else use just the signal level.
  1303. * This formula is a least squares fit of data points collected and
  1304. * compared with a reference system that had a percentage (%) display
  1305. * for signal quality. */
  1306. } else
  1307. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  1308. (15 * RSSI_RANGE + 62 * degradation)) /
  1309. (RSSI_RANGE * RSSI_RANGE);
  1310. if (sig_qual > 100)
  1311. sig_qual = 100;
  1312. else if (sig_qual < 1)
  1313. sig_qual = 0;
  1314. return sig_qual;
  1315. }
  1316. /**
  1317. * iwl3945_rx_handle - Main entry function for receiving responses from uCode
  1318. *
  1319. * Uses the priv->rx_handlers callback function array to invoke
  1320. * the appropriate handlers, including command responses,
  1321. * frame-received notifications, and other notifications.
  1322. */
  1323. static void iwl3945_rx_handle(struct iwl_priv *priv)
  1324. {
  1325. struct iwl_rx_mem_buffer *rxb;
  1326. struct iwl_rx_packet *pkt;
  1327. struct iwl_rx_queue *rxq = &priv->rxq;
  1328. u32 r, i;
  1329. int reclaim;
  1330. unsigned long flags;
  1331. u8 fill_rx = 0;
  1332. u32 count = 8;
  1333. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  1334. * buffer that the driver may process (last buffer filled by ucode). */
  1335. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  1336. i = rxq->read;
  1337. if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
  1338. fill_rx = 1;
  1339. /* Rx interrupt, but nothing sent from uCode */
  1340. if (i == r)
  1341. IWL_DEBUG(priv, IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
  1342. while (i != r) {
  1343. rxb = rxq->queue[i];
  1344. /* If an RXB doesn't have a Rx queue slot associated with it,
  1345. * then a bug has been introduced in the queue refilling
  1346. * routines -- catch it here */
  1347. BUG_ON(rxb == NULL);
  1348. rxq->queue[i] = NULL;
  1349. pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
  1350. priv->hw_params.rx_buf_size,
  1351. PCI_DMA_FROMDEVICE);
  1352. pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1353. /* Reclaim a command buffer only if this packet is a response
  1354. * to a (driver-originated) command.
  1355. * If the packet (e.g. Rx frame) originated from uCode,
  1356. * there is no command buffer to reclaim.
  1357. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  1358. * but apparently a few don't get set; catch them here. */
  1359. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  1360. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  1361. (pkt->hdr.cmd != REPLY_TX);
  1362. /* Based on type of command response or notification,
  1363. * handle those that need handling via function in
  1364. * rx_handlers table. See iwl3945_setup_rx_handlers() */
  1365. if (priv->rx_handlers[pkt->hdr.cmd]) {
  1366. IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  1367. "r = %d, i = %d, %s, 0x%02x\n", r, i,
  1368. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  1369. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  1370. } else {
  1371. /* No handling needed */
  1372. IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  1373. "r %d i %d No handler needed for %s, 0x%02x\n",
  1374. r, i, get_cmd_string(pkt->hdr.cmd),
  1375. pkt->hdr.cmd);
  1376. }
  1377. if (reclaim) {
  1378. /* Invoke any callbacks, transfer the skb to caller, and
  1379. * fire off the (possibly) blocking iwl_send_cmd()
  1380. * as we reclaim the driver command queue */
  1381. if (rxb && rxb->skb)
  1382. iwl_tx_cmd_complete(priv, rxb);
  1383. else
  1384. IWL_WARN(priv, "Claim null rxb?\n");
  1385. }
  1386. /* For now we just don't re-use anything. We can tweak this
  1387. * later to try and re-use notification packets and SKBs that
  1388. * fail to Rx correctly */
  1389. if (rxb->skb != NULL) {
  1390. priv->alloc_rxb_skb--;
  1391. dev_kfree_skb_any(rxb->skb);
  1392. rxb->skb = NULL;
  1393. }
  1394. pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
  1395. priv->hw_params.rx_buf_size,
  1396. PCI_DMA_FROMDEVICE);
  1397. spin_lock_irqsave(&rxq->lock, flags);
  1398. list_add_tail(&rxb->list, &priv->rxq.rx_used);
  1399. spin_unlock_irqrestore(&rxq->lock, flags);
  1400. i = (i + 1) & RX_QUEUE_MASK;
  1401. /* If there are a lot of unused frames,
  1402. * restock the Rx queue so ucode won't assert. */
  1403. if (fill_rx) {
  1404. count++;
  1405. if (count >= 8) {
  1406. priv->rxq.read = i;
  1407. __iwl3945_rx_replenish(priv);
  1408. count = 0;
  1409. }
  1410. }
  1411. }
  1412. /* Backtrack one entry */
  1413. priv->rxq.read = i;
  1414. iwl3945_rx_queue_restock(priv);
  1415. }
  1416. /* call this function to flush any scheduled tasklet */
  1417. static inline void iwl_synchronize_irq(struct iwl_priv *priv)
  1418. {
  1419. /* wait to make sure we flush pending tasklet*/
  1420. synchronize_irq(priv->pci_dev->irq);
  1421. tasklet_kill(&priv->irq_tasklet);
  1422. }
  1423. static const char *desc_lookup(int i)
  1424. {
  1425. switch (i) {
  1426. case 1:
  1427. return "FAIL";
  1428. case 2:
  1429. return "BAD_PARAM";
  1430. case 3:
  1431. return "BAD_CHECKSUM";
  1432. case 4:
  1433. return "NMI_INTERRUPT";
  1434. case 5:
  1435. return "SYSASSERT";
  1436. case 6:
  1437. return "FATAL_ERROR";
  1438. }
  1439. return "UNKNOWN";
  1440. }
  1441. #define ERROR_START_OFFSET (1 * sizeof(u32))
  1442. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  1443. static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
  1444. {
  1445. u32 i;
  1446. u32 desc, time, count, base, data1;
  1447. u32 blink1, blink2, ilink1, ilink2;
  1448. int rc;
  1449. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  1450. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  1451. IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
  1452. return;
  1453. }
  1454. rc = iwl_grab_nic_access(priv);
  1455. if (rc) {
  1456. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  1457. return;
  1458. }
  1459. count = iwl_read_targ_mem(priv, base);
  1460. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  1461. IWL_ERR(priv, "Start IWL Error Log Dump:\n");
  1462. IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
  1463. priv->status, count);
  1464. }
  1465. IWL_ERR(priv, "Desc Time asrtPC blink2 "
  1466. "ilink1 nmiPC Line\n");
  1467. for (i = ERROR_START_OFFSET;
  1468. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  1469. i += ERROR_ELEM_SIZE) {
  1470. desc = iwl_read_targ_mem(priv, base + i);
  1471. time =
  1472. iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
  1473. blink1 =
  1474. iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
  1475. blink2 =
  1476. iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
  1477. ilink1 =
  1478. iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
  1479. ilink2 =
  1480. iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
  1481. data1 =
  1482. iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
  1483. IWL_ERR(priv,
  1484. "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  1485. desc_lookup(desc), desc, time, blink1, blink2,
  1486. ilink1, ilink2, data1);
  1487. }
  1488. iwl_release_nic_access(priv);
  1489. }
  1490. #define EVENT_START_OFFSET (6 * sizeof(u32))
  1491. /**
  1492. * iwl3945_print_event_log - Dump error event log to syslog
  1493. *
  1494. * NOTE: Must be called with iwl_grab_nic_access() already obtained!
  1495. */
  1496. static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
  1497. u32 num_events, u32 mode)
  1498. {
  1499. u32 i;
  1500. u32 base; /* SRAM byte address of event log header */
  1501. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  1502. u32 ptr; /* SRAM byte address of log data */
  1503. u32 ev, time, data; /* event log data */
  1504. if (num_events == 0)
  1505. return;
  1506. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1507. if (mode == 0)
  1508. event_size = 2 * sizeof(u32);
  1509. else
  1510. event_size = 3 * sizeof(u32);
  1511. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  1512. /* "time" is actually "data" for mode 0 (no timestamp).
  1513. * place event id # at far right for easier visual parsing. */
  1514. for (i = 0; i < num_events; i++) {
  1515. ev = iwl_read_targ_mem(priv, ptr);
  1516. ptr += sizeof(u32);
  1517. time = iwl_read_targ_mem(priv, ptr);
  1518. ptr += sizeof(u32);
  1519. if (mode == 0) {
  1520. /* data, ev */
  1521. IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
  1522. } else {
  1523. data = iwl_read_targ_mem(priv, ptr);
  1524. ptr += sizeof(u32);
  1525. IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
  1526. }
  1527. }
  1528. }
  1529. static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
  1530. {
  1531. int rc;
  1532. u32 base; /* SRAM byte address of event log header */
  1533. u32 capacity; /* event log capacity in # entries */
  1534. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  1535. u32 num_wraps; /* # times uCode wrapped to top of log */
  1536. u32 next_entry; /* index of next entry to be written by uCode */
  1537. u32 size; /* # entries that we'll print */
  1538. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1539. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  1540. IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
  1541. return;
  1542. }
  1543. rc = iwl_grab_nic_access(priv);
  1544. if (rc) {
  1545. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  1546. return;
  1547. }
  1548. /* event log header */
  1549. capacity = iwl_read_targ_mem(priv, base);
  1550. mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
  1551. num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
  1552. next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
  1553. size = num_wraps ? capacity : next_entry;
  1554. /* bail out if nothing in log */
  1555. if (size == 0) {
  1556. IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
  1557. iwl_release_nic_access(priv);
  1558. return;
  1559. }
  1560. IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
  1561. size, num_wraps);
  1562. /* if uCode has wrapped back to top of log, start at the oldest entry,
  1563. * i.e the next one that uCode would fill. */
  1564. if (num_wraps)
  1565. iwl3945_print_event_log(priv, next_entry,
  1566. capacity - next_entry, mode);
  1567. /* (then/else) start at top of log */
  1568. iwl3945_print_event_log(priv, 0, next_entry, mode);
  1569. iwl_release_nic_access(priv);
  1570. }
  1571. static void iwl3945_error_recovery(struct iwl_priv *priv)
  1572. {
  1573. unsigned long flags;
  1574. memcpy(&priv->staging_rxon, &priv->recovery_rxon,
  1575. sizeof(priv->staging_rxon));
  1576. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1577. iwlcore_commit_rxon(priv);
  1578. iwl3945_add_station(priv, priv->bssid, 1, 0);
  1579. spin_lock_irqsave(&priv->lock, flags);
  1580. priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
  1581. priv->error_recovering = 0;
  1582. spin_unlock_irqrestore(&priv->lock, flags);
  1583. }
  1584. static void iwl3945_irq_tasklet(struct iwl_priv *priv)
  1585. {
  1586. u32 inta, handled = 0;
  1587. u32 inta_fh;
  1588. unsigned long flags;
  1589. #ifdef CONFIG_IWLWIFI_DEBUG
  1590. u32 inta_mask;
  1591. #endif
  1592. spin_lock_irqsave(&priv->lock, flags);
  1593. /* Ack/clear/reset pending uCode interrupts.
  1594. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  1595. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  1596. inta = iwl_read32(priv, CSR_INT);
  1597. iwl_write32(priv, CSR_INT, inta);
  1598. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  1599. * Any new interrupts that happen after this, either while we're
  1600. * in this tasklet, or later, will show up in next ISR/tasklet. */
  1601. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1602. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  1603. #ifdef CONFIG_IWLWIFI_DEBUG
  1604. if (priv->debug_level & IWL_DL_ISR) {
  1605. /* just for debug */
  1606. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1607. IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  1608. inta, inta_mask, inta_fh);
  1609. }
  1610. #endif
  1611. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  1612. * atomic, make sure that inta covers all the interrupts that
  1613. * we've discovered, even if FH interrupt came in just after
  1614. * reading CSR_INT. */
  1615. if (inta_fh & CSR39_FH_INT_RX_MASK)
  1616. inta |= CSR_INT_BIT_FH_RX;
  1617. if (inta_fh & CSR39_FH_INT_TX_MASK)
  1618. inta |= CSR_INT_BIT_FH_TX;
  1619. /* Now service all interrupt bits discovered above. */
  1620. if (inta & CSR_INT_BIT_HW_ERR) {
  1621. IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
  1622. /* Tell the device to stop sending interrupts */
  1623. iwl_disable_interrupts(priv);
  1624. iwl_irq_handle_error(priv);
  1625. handled |= CSR_INT_BIT_HW_ERR;
  1626. spin_unlock_irqrestore(&priv->lock, flags);
  1627. return;
  1628. }
  1629. #ifdef CONFIG_IWLWIFI_DEBUG
  1630. if (priv->debug_level & (IWL_DL_ISR)) {
  1631. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  1632. if (inta & CSR_INT_BIT_SCD)
  1633. IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
  1634. "the frame/frames.\n");
  1635. /* Alive notification via Rx interrupt will do the real work */
  1636. if (inta & CSR_INT_BIT_ALIVE)
  1637. IWL_DEBUG_ISR(priv, "Alive interrupt\n");
  1638. }
  1639. #endif
  1640. /* Safely ignore these bits for debug checks below */
  1641. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  1642. /* Error detected by uCode */
  1643. if (inta & CSR_INT_BIT_SW_ERR) {
  1644. IWL_ERR(priv, "Microcode SW error detected. "
  1645. "Restarting 0x%X.\n", inta);
  1646. iwl_irq_handle_error(priv);
  1647. handled |= CSR_INT_BIT_SW_ERR;
  1648. }
  1649. /* uCode wakes up after power-down sleep */
  1650. if (inta & CSR_INT_BIT_WAKEUP) {
  1651. IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
  1652. iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
  1653. iwl_txq_update_write_ptr(priv, &priv->txq[0]);
  1654. iwl_txq_update_write_ptr(priv, &priv->txq[1]);
  1655. iwl_txq_update_write_ptr(priv, &priv->txq[2]);
  1656. iwl_txq_update_write_ptr(priv, &priv->txq[3]);
  1657. iwl_txq_update_write_ptr(priv, &priv->txq[4]);
  1658. iwl_txq_update_write_ptr(priv, &priv->txq[5]);
  1659. handled |= CSR_INT_BIT_WAKEUP;
  1660. }
  1661. /* All uCode command responses, including Tx command responses,
  1662. * Rx "responses" (frame-received notification), and other
  1663. * notifications from uCode come through here*/
  1664. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  1665. iwl3945_rx_handle(priv);
  1666. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  1667. }
  1668. if (inta & CSR_INT_BIT_FH_TX) {
  1669. IWL_DEBUG_ISR(priv, "Tx interrupt\n");
  1670. iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
  1671. if (!iwl_grab_nic_access(priv)) {
  1672. iwl_write_direct32(priv, FH39_TCSR_CREDIT
  1673. (FH39_SRVC_CHNL), 0x0);
  1674. iwl_release_nic_access(priv);
  1675. }
  1676. handled |= CSR_INT_BIT_FH_TX;
  1677. }
  1678. if (inta & ~handled)
  1679. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  1680. if (inta & ~CSR_INI_SET_MASK) {
  1681. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  1682. inta & ~CSR_INI_SET_MASK);
  1683. IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
  1684. }
  1685. /* Re-enable all interrupts */
  1686. /* only Re-enable if disabled by irq */
  1687. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  1688. iwl_enable_interrupts(priv);
  1689. #ifdef CONFIG_IWLWIFI_DEBUG
  1690. if (priv->debug_level & (IWL_DL_ISR)) {
  1691. inta = iwl_read32(priv, CSR_INT);
  1692. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1693. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1694. IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  1695. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  1696. }
  1697. #endif
  1698. spin_unlock_irqrestore(&priv->lock, flags);
  1699. }
  1700. static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
  1701. enum ieee80211_band band,
  1702. u8 is_active, u8 n_probes,
  1703. struct iwl3945_scan_channel *scan_ch)
  1704. {
  1705. const struct ieee80211_channel *channels = NULL;
  1706. const struct ieee80211_supported_band *sband;
  1707. const struct iwl_channel_info *ch_info;
  1708. u16 passive_dwell = 0;
  1709. u16 active_dwell = 0;
  1710. int added, i;
  1711. sband = iwl_get_hw_mode(priv, band);
  1712. if (!sband)
  1713. return 0;
  1714. channels = sband->channels;
  1715. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  1716. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  1717. if (passive_dwell <= active_dwell)
  1718. passive_dwell = active_dwell + 1;
  1719. for (i = 0, added = 0; i < sband->n_channels; i++) {
  1720. if (channels[i].flags & IEEE80211_CHAN_DISABLED)
  1721. continue;
  1722. scan_ch->channel = channels[i].hw_value;
  1723. ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
  1724. if (!is_channel_valid(ch_info)) {
  1725. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  1726. scan_ch->channel);
  1727. continue;
  1728. }
  1729. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  1730. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1731. /* If passive , set up for auto-switch
  1732. * and use long active_dwell time.
  1733. */
  1734. if (!is_active || is_channel_passive(ch_info) ||
  1735. (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
  1736. scan_ch->type = 0; /* passive */
  1737. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  1738. scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
  1739. } else {
  1740. scan_ch->type = 1; /* active */
  1741. }
  1742. /* Set direct probe bits. These may be used both for active
  1743. * scan channels (probes gets sent right away),
  1744. * or for passive channels (probes get se sent only after
  1745. * hearing clear Rx packet).*/
  1746. if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
  1747. if (n_probes)
  1748. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  1749. } else {
  1750. /* uCode v1 does not allow setting direct probe bits on
  1751. * passive channel. */
  1752. if ((scan_ch->type & 1) && n_probes)
  1753. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  1754. }
  1755. /* Set txpower levels to defaults */
  1756. scan_ch->tpc.dsp_atten = 110;
  1757. /* scan_pwr_info->tpc.dsp_atten; */
  1758. /*scan_pwr_info->tpc.tx_gain; */
  1759. if (band == IEEE80211_BAND_5GHZ)
  1760. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1761. else {
  1762. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  1763. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  1764. * power level:
  1765. * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
  1766. */
  1767. }
  1768. IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
  1769. scan_ch->channel,
  1770. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  1771. (scan_ch->type & 1) ?
  1772. active_dwell : passive_dwell);
  1773. scan_ch++;
  1774. added++;
  1775. }
  1776. IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
  1777. return added;
  1778. }
  1779. static void iwl3945_init_hw_rates(struct iwl_priv *priv,
  1780. struct ieee80211_rate *rates)
  1781. {
  1782. int i;
  1783. for (i = 0; i < IWL_RATE_COUNT; i++) {
  1784. rates[i].bitrate = iwl3945_rates[i].ieee * 5;
  1785. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  1786. rates[i].hw_value_short = i;
  1787. rates[i].flags = 0;
  1788. if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  1789. /*
  1790. * If CCK != 1M then set short preamble rate flag.
  1791. */
  1792. rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
  1793. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  1794. }
  1795. }
  1796. }
  1797. /******************************************************************************
  1798. *
  1799. * uCode download functions
  1800. *
  1801. ******************************************************************************/
  1802. static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
  1803. {
  1804. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
  1805. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
  1806. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  1807. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
  1808. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  1809. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
  1810. }
  1811. /**
  1812. * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
  1813. * looking at all data.
  1814. */
  1815. static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
  1816. {
  1817. u32 val;
  1818. u32 save_len = len;
  1819. int rc = 0;
  1820. u32 errcnt;
  1821. IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
  1822. rc = iwl_grab_nic_access(priv);
  1823. if (rc)
  1824. return rc;
  1825. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  1826. IWL39_RTC_INST_LOWER_BOUND);
  1827. errcnt = 0;
  1828. for (; len > 0; len -= sizeof(u32), image++) {
  1829. /* read data comes through single port, auto-incr addr */
  1830. /* NOTE: Use the debugless read so we don't flood kernel log
  1831. * if IWL_DL_IO is set */
  1832. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  1833. if (val != le32_to_cpu(*image)) {
  1834. IWL_ERR(priv, "uCode INST section is invalid at "
  1835. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  1836. save_len - len, val, le32_to_cpu(*image));
  1837. rc = -EIO;
  1838. errcnt++;
  1839. if (errcnt >= 20)
  1840. break;
  1841. }
  1842. }
  1843. iwl_release_nic_access(priv);
  1844. if (!errcnt)
  1845. IWL_DEBUG_INFO(priv,
  1846. "ucode image in INSTRUCTION memory is good\n");
  1847. return rc;
  1848. }
  1849. /**
  1850. * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
  1851. * using sample data 100 bytes apart. If these sample points are good,
  1852. * it's a pretty good bet that everything between them is good, too.
  1853. */
  1854. static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  1855. {
  1856. u32 val;
  1857. int rc = 0;
  1858. u32 errcnt = 0;
  1859. u32 i;
  1860. IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
  1861. rc = iwl_grab_nic_access(priv);
  1862. if (rc)
  1863. return rc;
  1864. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  1865. /* read data comes through single port, auto-incr addr */
  1866. /* NOTE: Use the debugless read so we don't flood kernel log
  1867. * if IWL_DL_IO is set */
  1868. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  1869. i + IWL39_RTC_INST_LOWER_BOUND);
  1870. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  1871. if (val != le32_to_cpu(*image)) {
  1872. #if 0 /* Enable this if you want to see details */
  1873. IWL_ERR(priv, "uCode INST section is invalid at "
  1874. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  1875. i, val, *image);
  1876. #endif
  1877. rc = -EIO;
  1878. errcnt++;
  1879. if (errcnt >= 3)
  1880. break;
  1881. }
  1882. }
  1883. iwl_release_nic_access(priv);
  1884. return rc;
  1885. }
  1886. /**
  1887. * iwl3945_verify_ucode - determine which instruction image is in SRAM,
  1888. * and verify its contents
  1889. */
  1890. static int iwl3945_verify_ucode(struct iwl_priv *priv)
  1891. {
  1892. __le32 *image;
  1893. u32 len;
  1894. int rc = 0;
  1895. /* Try bootstrap */
  1896. image = (__le32 *)priv->ucode_boot.v_addr;
  1897. len = priv->ucode_boot.len;
  1898. rc = iwl3945_verify_inst_sparse(priv, image, len);
  1899. if (rc == 0) {
  1900. IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
  1901. return 0;
  1902. }
  1903. /* Try initialize */
  1904. image = (__le32 *)priv->ucode_init.v_addr;
  1905. len = priv->ucode_init.len;
  1906. rc = iwl3945_verify_inst_sparse(priv, image, len);
  1907. if (rc == 0) {
  1908. IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
  1909. return 0;
  1910. }
  1911. /* Try runtime/protocol */
  1912. image = (__le32 *)priv->ucode_code.v_addr;
  1913. len = priv->ucode_code.len;
  1914. rc = iwl3945_verify_inst_sparse(priv, image, len);
  1915. if (rc == 0) {
  1916. IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
  1917. return 0;
  1918. }
  1919. IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  1920. /* Since nothing seems to match, show first several data entries in
  1921. * instruction SRAM, so maybe visual inspection will give a clue.
  1922. * Selection of bootstrap image (vs. other images) is arbitrary. */
  1923. image = (__le32 *)priv->ucode_boot.v_addr;
  1924. len = priv->ucode_boot.len;
  1925. rc = iwl3945_verify_inst_full(priv, image, len);
  1926. return rc;
  1927. }
  1928. static void iwl3945_nic_start(struct iwl_priv *priv)
  1929. {
  1930. /* Remove all resets to allow NIC to operate */
  1931. iwl_write32(priv, CSR_RESET, 0);
  1932. }
  1933. /**
  1934. * iwl3945_read_ucode - Read uCode images from disk file.
  1935. *
  1936. * Copy into buffers for card to fetch via bus-mastering
  1937. */
  1938. static int iwl3945_read_ucode(struct iwl_priv *priv)
  1939. {
  1940. struct iwl_ucode *ucode;
  1941. int ret = -EINVAL, index;
  1942. const struct firmware *ucode_raw;
  1943. /* firmware file name contains uCode/driver compatibility version */
  1944. const char *name_pre = priv->cfg->fw_name_pre;
  1945. const unsigned int api_max = priv->cfg->ucode_api_max;
  1946. const unsigned int api_min = priv->cfg->ucode_api_min;
  1947. char buf[25];
  1948. u8 *src;
  1949. size_t len;
  1950. u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
  1951. /* Ask kernel firmware_class module to get the boot firmware off disk.
  1952. * request_firmware() is synchronous, file is in memory on return. */
  1953. for (index = api_max; index >= api_min; index--) {
  1954. sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
  1955. ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
  1956. if (ret < 0) {
  1957. IWL_ERR(priv, "%s firmware file req failed: %d\n",
  1958. buf, ret);
  1959. if (ret == -ENOENT)
  1960. continue;
  1961. else
  1962. goto error;
  1963. } else {
  1964. if (index < api_max)
  1965. IWL_ERR(priv, "Loaded firmware %s, "
  1966. "which is deprecated. "
  1967. " Please use API v%u instead.\n",
  1968. buf, api_max);
  1969. IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
  1970. "(%zd bytes) from disk\n",
  1971. buf, ucode_raw->size);
  1972. break;
  1973. }
  1974. }
  1975. if (ret < 0)
  1976. goto error;
  1977. /* Make sure that we got at least our header! */
  1978. if (ucode_raw->size < sizeof(*ucode)) {
  1979. IWL_ERR(priv, "File size way too small!\n");
  1980. ret = -EINVAL;
  1981. goto err_release;
  1982. }
  1983. /* Data from ucode file: header followed by uCode images */
  1984. ucode = (void *)ucode_raw->data;
  1985. priv->ucode_ver = le32_to_cpu(ucode->ver);
  1986. api_ver = IWL_UCODE_API(priv->ucode_ver);
  1987. inst_size = le32_to_cpu(ucode->inst_size);
  1988. data_size = le32_to_cpu(ucode->data_size);
  1989. init_size = le32_to_cpu(ucode->init_size);
  1990. init_data_size = le32_to_cpu(ucode->init_data_size);
  1991. boot_size = le32_to_cpu(ucode->boot_size);
  1992. /* api_ver should match the api version forming part of the
  1993. * firmware filename ... but we don't check for that and only rely
  1994. * on the API version read from firmware header from here on forward */
  1995. if (api_ver < api_min || api_ver > api_max) {
  1996. IWL_ERR(priv, "Driver unable to support your firmware API. "
  1997. "Driver supports v%u, firmware is v%u.\n",
  1998. api_max, api_ver);
  1999. priv->ucode_ver = 0;
  2000. ret = -EINVAL;
  2001. goto err_release;
  2002. }
  2003. if (api_ver != api_max)
  2004. IWL_ERR(priv, "Firmware has old API version. Expected %u, "
  2005. "got %u. New firmware can be obtained "
  2006. "from http://www.intellinuxwireless.org.\n",
  2007. api_max, api_ver);
  2008. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
  2009. IWL_UCODE_MAJOR(priv->ucode_ver),
  2010. IWL_UCODE_MINOR(priv->ucode_ver),
  2011. IWL_UCODE_API(priv->ucode_ver),
  2012. IWL_UCODE_SERIAL(priv->ucode_ver));
  2013. IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
  2014. priv->ucode_ver);
  2015. IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
  2016. inst_size);
  2017. IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
  2018. data_size);
  2019. IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
  2020. init_size);
  2021. IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
  2022. init_data_size);
  2023. IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
  2024. boot_size);
  2025. /* Verify size of file vs. image size info in file's header */
  2026. if (ucode_raw->size < sizeof(*ucode) +
  2027. inst_size + data_size + init_size +
  2028. init_data_size + boot_size) {
  2029. IWL_DEBUG_INFO(priv, "uCode file size %zd too small\n",
  2030. ucode_raw->size);
  2031. ret = -EINVAL;
  2032. goto err_release;
  2033. }
  2034. /* Verify that uCode images will fit in card's SRAM */
  2035. if (inst_size > IWL39_MAX_INST_SIZE) {
  2036. IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
  2037. inst_size);
  2038. ret = -EINVAL;
  2039. goto err_release;
  2040. }
  2041. if (data_size > IWL39_MAX_DATA_SIZE) {
  2042. IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
  2043. data_size);
  2044. ret = -EINVAL;
  2045. goto err_release;
  2046. }
  2047. if (init_size > IWL39_MAX_INST_SIZE) {
  2048. IWL_DEBUG_INFO(priv,
  2049. "uCode init instr len %d too large to fit in\n",
  2050. init_size);
  2051. ret = -EINVAL;
  2052. goto err_release;
  2053. }
  2054. if (init_data_size > IWL39_MAX_DATA_SIZE) {
  2055. IWL_DEBUG_INFO(priv,
  2056. "uCode init data len %d too large to fit in\n",
  2057. init_data_size);
  2058. ret = -EINVAL;
  2059. goto err_release;
  2060. }
  2061. if (boot_size > IWL39_MAX_BSM_SIZE) {
  2062. IWL_DEBUG_INFO(priv,
  2063. "uCode boot instr len %d too large to fit in\n",
  2064. boot_size);
  2065. ret = -EINVAL;
  2066. goto err_release;
  2067. }
  2068. /* Allocate ucode buffers for card's bus-master loading ... */
  2069. /* Runtime instructions and 2 copies of data:
  2070. * 1) unmodified from disk
  2071. * 2) backup cache for save/restore during power-downs */
  2072. priv->ucode_code.len = inst_size;
  2073. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
  2074. priv->ucode_data.len = data_size;
  2075. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
  2076. priv->ucode_data_backup.len = data_size;
  2077. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  2078. if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
  2079. !priv->ucode_data_backup.v_addr)
  2080. goto err_pci_alloc;
  2081. /* Initialization instructions and data */
  2082. if (init_size && init_data_size) {
  2083. priv->ucode_init.len = init_size;
  2084. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
  2085. priv->ucode_init_data.len = init_data_size;
  2086. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  2087. if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
  2088. goto err_pci_alloc;
  2089. }
  2090. /* Bootstrap (instructions only, no data) */
  2091. if (boot_size) {
  2092. priv->ucode_boot.len = boot_size;
  2093. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
  2094. if (!priv->ucode_boot.v_addr)
  2095. goto err_pci_alloc;
  2096. }
  2097. /* Copy images into buffers for card's bus-master reads ... */
  2098. /* Runtime instructions (first block of data in file) */
  2099. src = &ucode->data[0];
  2100. len = priv->ucode_code.len;
  2101. IWL_DEBUG_INFO(priv,
  2102. "Copying (but not loading) uCode instr len %zd\n", len);
  2103. memcpy(priv->ucode_code.v_addr, src, len);
  2104. IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  2105. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  2106. /* Runtime data (2nd block)
  2107. * NOTE: Copy into backup buffer will be done in iwl3945_up() */
  2108. src = &ucode->data[inst_size];
  2109. len = priv->ucode_data.len;
  2110. IWL_DEBUG_INFO(priv,
  2111. "Copying (but not loading) uCode data len %zd\n", len);
  2112. memcpy(priv->ucode_data.v_addr, src, len);
  2113. memcpy(priv->ucode_data_backup.v_addr, src, len);
  2114. /* Initialization instructions (3rd block) */
  2115. if (init_size) {
  2116. src = &ucode->data[inst_size + data_size];
  2117. len = priv->ucode_init.len;
  2118. IWL_DEBUG_INFO(priv,
  2119. "Copying (but not loading) init instr len %zd\n", len);
  2120. memcpy(priv->ucode_init.v_addr, src, len);
  2121. }
  2122. /* Initialization data (4th block) */
  2123. if (init_data_size) {
  2124. src = &ucode->data[inst_size + data_size + init_size];
  2125. len = priv->ucode_init_data.len;
  2126. IWL_DEBUG_INFO(priv,
  2127. "Copying (but not loading) init data len %zd\n", len);
  2128. memcpy(priv->ucode_init_data.v_addr, src, len);
  2129. }
  2130. /* Bootstrap instructions (5th block) */
  2131. src = &ucode->data[inst_size + data_size + init_size + init_data_size];
  2132. len = priv->ucode_boot.len;
  2133. IWL_DEBUG_INFO(priv,
  2134. "Copying (but not loading) boot instr len %zd\n", len);
  2135. memcpy(priv->ucode_boot.v_addr, src, len);
  2136. /* We have our copies now, allow OS release its copies */
  2137. release_firmware(ucode_raw);
  2138. return 0;
  2139. err_pci_alloc:
  2140. IWL_ERR(priv, "failed to allocate pci memory\n");
  2141. ret = -ENOMEM;
  2142. iwl3945_dealloc_ucode_pci(priv);
  2143. err_release:
  2144. release_firmware(ucode_raw);
  2145. error:
  2146. return ret;
  2147. }
  2148. /**
  2149. * iwl3945_set_ucode_ptrs - Set uCode address location
  2150. *
  2151. * Tell initialization uCode where to find runtime uCode.
  2152. *
  2153. * BSM registers initially contain pointers to initialization uCode.
  2154. * We need to replace them to load runtime uCode inst and data,
  2155. * and to save runtime data when powering down.
  2156. */
  2157. static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
  2158. {
  2159. dma_addr_t pinst;
  2160. dma_addr_t pdata;
  2161. int rc = 0;
  2162. unsigned long flags;
  2163. /* bits 31:0 for 3945 */
  2164. pinst = priv->ucode_code.p_addr;
  2165. pdata = priv->ucode_data_backup.p_addr;
  2166. spin_lock_irqsave(&priv->lock, flags);
  2167. rc = iwl_grab_nic_access(priv);
  2168. if (rc) {
  2169. spin_unlock_irqrestore(&priv->lock, flags);
  2170. return rc;
  2171. }
  2172. /* Tell bootstrap uCode where to find image to load */
  2173. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  2174. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  2175. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  2176. priv->ucode_data.len);
  2177. /* Inst byte count must be last to set up, bit 31 signals uCode
  2178. * that all new ptr/size info is in place */
  2179. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  2180. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  2181. iwl_release_nic_access(priv);
  2182. spin_unlock_irqrestore(&priv->lock, flags);
  2183. IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
  2184. return rc;
  2185. }
  2186. /**
  2187. * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
  2188. *
  2189. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  2190. *
  2191. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  2192. */
  2193. static void iwl3945_init_alive_start(struct iwl_priv *priv)
  2194. {
  2195. /* Check alive response for "valid" sign from uCode */
  2196. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  2197. /* We had an error bringing up the hardware, so take it
  2198. * all the way back down so we can try again */
  2199. IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
  2200. goto restart;
  2201. }
  2202. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  2203. * This is a paranoid check, because we would not have gotten the
  2204. * "initialize" alive if code weren't properly loaded. */
  2205. if (iwl3945_verify_ucode(priv)) {
  2206. /* Runtime instruction load was bad;
  2207. * take it all the way back down so we can try again */
  2208. IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
  2209. goto restart;
  2210. }
  2211. /* Send pointers to protocol/runtime uCode image ... init code will
  2212. * load and launch runtime uCode, which will send us another "Alive"
  2213. * notification. */
  2214. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  2215. if (iwl3945_set_ucode_ptrs(priv)) {
  2216. /* Runtime instruction load won't happen;
  2217. * take it all the way back down so we can try again */
  2218. IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
  2219. goto restart;
  2220. }
  2221. return;
  2222. restart:
  2223. queue_work(priv->workqueue, &priv->restart);
  2224. }
  2225. /**
  2226. * iwl3945_alive_start - called after REPLY_ALIVE notification received
  2227. * from protocol/runtime uCode (initialization uCode's
  2228. * Alive gets handled by iwl3945_init_alive_start()).
  2229. */
  2230. static void iwl3945_alive_start(struct iwl_priv *priv)
  2231. {
  2232. int rc = 0;
  2233. int thermal_spin = 0;
  2234. u32 rfkill;
  2235. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  2236. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  2237. /* We had an error bringing up the hardware, so take it
  2238. * all the way back down so we can try again */
  2239. IWL_DEBUG_INFO(priv, "Alive failed.\n");
  2240. goto restart;
  2241. }
  2242. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  2243. * This is a paranoid check, because we would not have gotten the
  2244. * "runtime" alive if code weren't properly loaded. */
  2245. if (iwl3945_verify_ucode(priv)) {
  2246. /* Runtime instruction load was bad;
  2247. * take it all the way back down so we can try again */
  2248. IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
  2249. goto restart;
  2250. }
  2251. iwl3945_clear_stations_table(priv);
  2252. rc = iwl_grab_nic_access(priv);
  2253. if (rc) {
  2254. IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
  2255. return;
  2256. }
  2257. rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
  2258. IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
  2259. iwl_release_nic_access(priv);
  2260. if (rfkill & 0x1) {
  2261. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2262. /* if RFKILL is not on, then wait for thermal
  2263. * sensor in adapter to kick in */
  2264. while (iwl3945_hw_get_temperature(priv) == 0) {
  2265. thermal_spin++;
  2266. udelay(10);
  2267. }
  2268. if (thermal_spin)
  2269. IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
  2270. thermal_spin * 10);
  2271. } else
  2272. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2273. /* After the ALIVE response, we can send commands to 3945 uCode */
  2274. set_bit(STATUS_ALIVE, &priv->status);
  2275. /* Clear out the uCode error bit if it is set */
  2276. clear_bit(STATUS_FW_ERROR, &priv->status);
  2277. if (iwl_is_rfkill(priv))
  2278. return;
  2279. ieee80211_wake_queues(priv->hw);
  2280. priv->active_rate = priv->rates_mask;
  2281. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  2282. iwl_power_update_mode(priv, false);
  2283. if (iwl_is_associated(priv)) {
  2284. struct iwl3945_rxon_cmd *active_rxon =
  2285. (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
  2286. memcpy(&priv->staging_rxon, &priv->active_rxon,
  2287. sizeof(priv->staging_rxon));
  2288. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2289. } else {
  2290. /* Initialize our rx_config data */
  2291. iwl_connection_init_rx_config(priv, priv->iw_mode);
  2292. }
  2293. /* Configure Bluetooth device coexistence support */
  2294. iwl_send_bt_config(priv);
  2295. /* Configure the adapter for unassociated operation */
  2296. iwlcore_commit_rxon(priv);
  2297. iwl3945_reg_txpower_periodic(priv);
  2298. iwl3945_led_register(priv);
  2299. IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
  2300. set_bit(STATUS_READY, &priv->status);
  2301. wake_up_interruptible(&priv->wait_command_queue);
  2302. if (priv->error_recovering)
  2303. iwl3945_error_recovery(priv);
  2304. /* reassociate for ADHOC mode */
  2305. if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
  2306. struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
  2307. priv->vif);
  2308. if (beacon)
  2309. iwl_mac_beacon_update(priv->hw, beacon);
  2310. }
  2311. return;
  2312. restart:
  2313. queue_work(priv->workqueue, &priv->restart);
  2314. }
  2315. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
  2316. static void __iwl3945_down(struct iwl_priv *priv)
  2317. {
  2318. unsigned long flags;
  2319. int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
  2320. struct ieee80211_conf *conf = NULL;
  2321. IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
  2322. conf = ieee80211_get_hw_conf(priv->hw);
  2323. if (!exit_pending)
  2324. set_bit(STATUS_EXIT_PENDING, &priv->status);
  2325. iwl3945_led_unregister(priv);
  2326. iwl3945_clear_stations_table(priv);
  2327. /* Unblock any waiting calls */
  2328. wake_up_interruptible_all(&priv->wait_command_queue);
  2329. /* Wipe out the EXIT_PENDING status bit if we are not actually
  2330. * exiting the module */
  2331. if (!exit_pending)
  2332. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  2333. /* stop and reset the on-board processor */
  2334. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  2335. /* tell the device to stop sending interrupts */
  2336. spin_lock_irqsave(&priv->lock, flags);
  2337. iwl_disable_interrupts(priv);
  2338. spin_unlock_irqrestore(&priv->lock, flags);
  2339. iwl_synchronize_irq(priv);
  2340. if (priv->mac80211_registered)
  2341. ieee80211_stop_queues(priv->hw);
  2342. /* If we have not previously called iwl3945_init() then
  2343. * clear all bits but the RF Kill bits and return */
  2344. if (!iwl_is_init(priv)) {
  2345. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  2346. STATUS_RF_KILL_HW |
  2347. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  2348. STATUS_RF_KILL_SW |
  2349. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  2350. STATUS_GEO_CONFIGURED |
  2351. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  2352. STATUS_EXIT_PENDING;
  2353. goto exit;
  2354. }
  2355. /* ...otherwise clear out all the status bits but the RF Kill
  2356. * bits and continue taking the NIC down. */
  2357. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  2358. STATUS_RF_KILL_HW |
  2359. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  2360. STATUS_RF_KILL_SW |
  2361. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  2362. STATUS_GEO_CONFIGURED |
  2363. test_bit(STATUS_FW_ERROR, &priv->status) <<
  2364. STATUS_FW_ERROR |
  2365. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  2366. STATUS_EXIT_PENDING;
  2367. priv->cfg->ops->lib->apm_ops.reset(priv);
  2368. spin_lock_irqsave(&priv->lock, flags);
  2369. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2370. spin_unlock_irqrestore(&priv->lock, flags);
  2371. iwl3945_hw_txq_ctx_stop(priv);
  2372. iwl3945_hw_rxq_stop(priv);
  2373. spin_lock_irqsave(&priv->lock, flags);
  2374. if (!iwl_grab_nic_access(priv)) {
  2375. iwl_write_prph(priv, APMG_CLK_DIS_REG,
  2376. APMG_CLK_VAL_DMA_CLK_RQT);
  2377. iwl_release_nic_access(priv);
  2378. }
  2379. spin_unlock_irqrestore(&priv->lock, flags);
  2380. udelay(5);
  2381. if (exit_pending)
  2382. priv->cfg->ops->lib->apm_ops.stop(priv);
  2383. else
  2384. priv->cfg->ops->lib->apm_ops.reset(priv);
  2385. exit:
  2386. memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
  2387. if (priv->ibss_beacon)
  2388. dev_kfree_skb(priv->ibss_beacon);
  2389. priv->ibss_beacon = NULL;
  2390. /* clear out any free frames */
  2391. iwl3945_clear_free_frames(priv);
  2392. }
  2393. static void iwl3945_down(struct iwl_priv *priv)
  2394. {
  2395. mutex_lock(&priv->mutex);
  2396. __iwl3945_down(priv);
  2397. mutex_unlock(&priv->mutex);
  2398. iwl3945_cancel_deferred_work(priv);
  2399. }
  2400. #define MAX_HW_RESTARTS 5
  2401. static int __iwl3945_up(struct iwl_priv *priv)
  2402. {
  2403. int rc, i;
  2404. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  2405. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  2406. return -EIO;
  2407. }
  2408. if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  2409. IWL_WARN(priv, "Radio disabled by SW RF kill (module "
  2410. "parameter)\n");
  2411. return -ENODEV;
  2412. }
  2413. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  2414. IWL_ERR(priv, "ucode not available for device bring up\n");
  2415. return -EIO;
  2416. }
  2417. /* If platform's RF_KILL switch is NOT set to KILL */
  2418. if (iwl_read32(priv, CSR_GP_CNTRL) &
  2419. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2420. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2421. else {
  2422. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2423. IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
  2424. return -ENODEV;
  2425. }
  2426. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2427. rc = iwl3945_hw_nic_init(priv);
  2428. if (rc) {
  2429. IWL_ERR(priv, "Unable to int nic\n");
  2430. return rc;
  2431. }
  2432. /* make sure rfkill handshake bits are cleared */
  2433. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2434. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  2435. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2436. /* clear (again), then enable host interrupts */
  2437. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2438. iwl_enable_interrupts(priv);
  2439. /* really make sure rfkill handshake bits are cleared */
  2440. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2441. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2442. /* Copy original ucode data image from disk into backup cache.
  2443. * This will be used to initialize the on-board processor's
  2444. * data SRAM for a clean start when the runtime program first loads. */
  2445. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  2446. priv->ucode_data.len);
  2447. /* We return success when we resume from suspend and rf_kill is on. */
  2448. if (test_bit(STATUS_RF_KILL_HW, &priv->status))
  2449. return 0;
  2450. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  2451. iwl3945_clear_stations_table(priv);
  2452. /* load bootstrap state machine,
  2453. * load bootstrap program into processor's memory,
  2454. * prepare to load the "initialize" uCode */
  2455. priv->cfg->ops->lib->load_ucode(priv);
  2456. if (rc) {
  2457. IWL_ERR(priv,
  2458. "Unable to set up bootstrap uCode: %d\n", rc);
  2459. continue;
  2460. }
  2461. /* start card; "initialize" will load runtime ucode */
  2462. iwl3945_nic_start(priv);
  2463. IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
  2464. return 0;
  2465. }
  2466. set_bit(STATUS_EXIT_PENDING, &priv->status);
  2467. __iwl3945_down(priv);
  2468. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  2469. /* tried to restart and config the device for as long as our
  2470. * patience could withstand */
  2471. IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
  2472. return -EIO;
  2473. }
  2474. /*****************************************************************************
  2475. *
  2476. * Workqueue callbacks
  2477. *
  2478. *****************************************************************************/
  2479. static void iwl3945_bg_init_alive_start(struct work_struct *data)
  2480. {
  2481. struct iwl_priv *priv =
  2482. container_of(data, struct iwl_priv, init_alive_start.work);
  2483. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2484. return;
  2485. mutex_lock(&priv->mutex);
  2486. iwl3945_init_alive_start(priv);
  2487. mutex_unlock(&priv->mutex);
  2488. }
  2489. static void iwl3945_bg_alive_start(struct work_struct *data)
  2490. {
  2491. struct iwl_priv *priv =
  2492. container_of(data, struct iwl_priv, alive_start.work);
  2493. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2494. return;
  2495. mutex_lock(&priv->mutex);
  2496. iwl3945_alive_start(priv);
  2497. mutex_unlock(&priv->mutex);
  2498. }
  2499. static void iwl3945_rfkill_poll(struct work_struct *data)
  2500. {
  2501. struct iwl_priv *priv =
  2502. container_of(data, struct iwl_priv, rfkill_poll.work);
  2503. unsigned long status = priv->status;
  2504. if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2505. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2506. else
  2507. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2508. if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
  2509. queue_work(priv->workqueue, &priv->rf_kill);
  2510. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  2511. round_jiffies_relative(2 * HZ));
  2512. }
  2513. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  2514. static void iwl3945_bg_request_scan(struct work_struct *data)
  2515. {
  2516. struct iwl_priv *priv =
  2517. container_of(data, struct iwl_priv, request_scan);
  2518. struct iwl_host_cmd cmd = {
  2519. .id = REPLY_SCAN_CMD,
  2520. .len = sizeof(struct iwl3945_scan_cmd),
  2521. .meta.flags = CMD_SIZE_HUGE,
  2522. };
  2523. int rc = 0;
  2524. struct iwl3945_scan_cmd *scan;
  2525. struct ieee80211_conf *conf = NULL;
  2526. u8 n_probes = 2;
  2527. enum ieee80211_band band;
  2528. DECLARE_SSID_BUF(ssid);
  2529. conf = ieee80211_get_hw_conf(priv->hw);
  2530. mutex_lock(&priv->mutex);
  2531. if (!iwl_is_ready(priv)) {
  2532. IWL_WARN(priv, "request scan called when driver not ready.\n");
  2533. goto done;
  2534. }
  2535. /* Make sure the scan wasn't canceled before this queued work
  2536. * was given the chance to run... */
  2537. if (!test_bit(STATUS_SCANNING, &priv->status))
  2538. goto done;
  2539. /* This should never be called or scheduled if there is currently
  2540. * a scan active in the hardware. */
  2541. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  2542. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
  2543. "Ignoring second request.\n");
  2544. rc = -EIO;
  2545. goto done;
  2546. }
  2547. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  2548. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  2549. goto done;
  2550. }
  2551. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  2552. IWL_DEBUG_HC(priv,
  2553. "Scan request while abort pending. Queuing.\n");
  2554. goto done;
  2555. }
  2556. if (iwl_is_rfkill(priv)) {
  2557. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  2558. goto done;
  2559. }
  2560. if (!test_bit(STATUS_READY, &priv->status)) {
  2561. IWL_DEBUG_HC(priv,
  2562. "Scan request while uninitialized. Queuing.\n");
  2563. goto done;
  2564. }
  2565. if (!priv->scan_bands) {
  2566. IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
  2567. goto done;
  2568. }
  2569. if (!priv->scan) {
  2570. priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
  2571. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  2572. if (!priv->scan) {
  2573. rc = -ENOMEM;
  2574. goto done;
  2575. }
  2576. }
  2577. scan = priv->scan;
  2578. memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
  2579. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  2580. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  2581. if (iwl_is_associated(priv)) {
  2582. u16 interval = 0;
  2583. u32 extra;
  2584. u32 suspend_time = 100;
  2585. u32 scan_suspend_time = 100;
  2586. unsigned long flags;
  2587. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  2588. spin_lock_irqsave(&priv->lock, flags);
  2589. interval = priv->beacon_int;
  2590. spin_unlock_irqrestore(&priv->lock, flags);
  2591. scan->suspend_time = 0;
  2592. scan->max_out_time = cpu_to_le32(200 * 1024);
  2593. if (!interval)
  2594. interval = suspend_time;
  2595. /*
  2596. * suspend time format:
  2597. * 0-19: beacon interval in usec (time before exec.)
  2598. * 20-23: 0
  2599. * 24-31: number of beacons (suspend between channels)
  2600. */
  2601. extra = (suspend_time / interval) << 24;
  2602. scan_suspend_time = 0xFF0FFFFF &
  2603. (extra | ((suspend_time % interval) * 1024));
  2604. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  2605. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  2606. scan_suspend_time, interval);
  2607. }
  2608. /* We should add the ability for user to lock to PASSIVE ONLY */
  2609. if (priv->one_direct_scan) {
  2610. IWL_DEBUG_SCAN(priv, "Kicking off one direct scan for '%s'\n",
  2611. print_ssid(ssid, priv->direct_ssid,
  2612. priv->direct_ssid_len));
  2613. scan->direct_scan[0].id = WLAN_EID_SSID;
  2614. scan->direct_scan[0].len = priv->direct_ssid_len;
  2615. memcpy(scan->direct_scan[0].ssid,
  2616. priv->direct_ssid, priv->direct_ssid_len);
  2617. n_probes++;
  2618. } else
  2619. IWL_DEBUG_SCAN(priv, "Kicking off one indirect scan.\n");
  2620. /* We don't build a direct scan probe request; the uCode will do
  2621. * that based on the direct_mask added to each channel entry */
  2622. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  2623. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  2624. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2625. /* flags + rate selection */
  2626. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
  2627. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  2628. scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
  2629. scan->good_CRC_th = 0;
  2630. band = IEEE80211_BAND_2GHZ;
  2631. } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
  2632. scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
  2633. scan->good_CRC_th = IWL_GOOD_CRC_TH;
  2634. band = IEEE80211_BAND_5GHZ;
  2635. } else {
  2636. IWL_WARN(priv, "Invalid scan band count\n");
  2637. goto done;
  2638. }
  2639. scan->tx_cmd.len = cpu_to_le16(
  2640. iwl_fill_probe_req(priv, band,
  2641. (struct ieee80211_mgmt *)scan->data,
  2642. IWL_MAX_SCAN_SIZE - sizeof(*scan)));
  2643. /* select Rx antennas */
  2644. scan->flags |= iwl3945_get_antenna_flags(priv);
  2645. if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
  2646. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  2647. scan->channel_count =
  2648. iwl3945_get_channels_for_scan(priv, band, 1, /* active */
  2649. n_probes,
  2650. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  2651. if (scan->channel_count == 0) {
  2652. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  2653. goto done;
  2654. }
  2655. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  2656. scan->channel_count * sizeof(struct iwl3945_scan_channel);
  2657. cmd.data = scan;
  2658. scan->len = cpu_to_le16(cmd.len);
  2659. set_bit(STATUS_SCAN_HW, &priv->status);
  2660. rc = iwl_send_cmd_sync(priv, &cmd);
  2661. if (rc)
  2662. goto done;
  2663. queue_delayed_work(priv->workqueue, &priv->scan_check,
  2664. IWL_SCAN_CHECK_WATCHDOG);
  2665. mutex_unlock(&priv->mutex);
  2666. return;
  2667. done:
  2668. /* can not perform scan make sure we clear scanning
  2669. * bits from status so next scan request can be performed.
  2670. * if we dont clear scanning status bit here all next scan
  2671. * will fail
  2672. */
  2673. clear_bit(STATUS_SCAN_HW, &priv->status);
  2674. clear_bit(STATUS_SCANNING, &priv->status);
  2675. /* inform mac80211 scan aborted */
  2676. queue_work(priv->workqueue, &priv->scan_completed);
  2677. mutex_unlock(&priv->mutex);
  2678. }
  2679. static void iwl3945_bg_up(struct work_struct *data)
  2680. {
  2681. struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
  2682. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2683. return;
  2684. mutex_lock(&priv->mutex);
  2685. __iwl3945_up(priv);
  2686. mutex_unlock(&priv->mutex);
  2687. iwl_rfkill_set_hw_state(priv);
  2688. }
  2689. static void iwl3945_bg_restart(struct work_struct *data)
  2690. {
  2691. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  2692. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2693. return;
  2694. iwl3945_down(priv);
  2695. queue_work(priv->workqueue, &priv->up);
  2696. }
  2697. static void iwl3945_bg_rx_replenish(struct work_struct *data)
  2698. {
  2699. struct iwl_priv *priv =
  2700. container_of(data, struct iwl_priv, rx_replenish);
  2701. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2702. return;
  2703. mutex_lock(&priv->mutex);
  2704. iwl3945_rx_replenish(priv);
  2705. mutex_unlock(&priv->mutex);
  2706. }
  2707. static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
  2708. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  2709. void iwl3945_post_associate(struct iwl_priv *priv)
  2710. {
  2711. int rc = 0;
  2712. struct ieee80211_conf *conf = NULL;
  2713. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  2714. IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
  2715. return;
  2716. }
  2717. IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
  2718. priv->assoc_id, priv->active_rxon.bssid_addr);
  2719. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2720. return;
  2721. if (!priv->vif || !priv->is_open)
  2722. return;
  2723. iwl_scan_cancel_timeout(priv, 200);
  2724. conf = ieee80211_get_hw_conf(priv->hw);
  2725. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2726. iwlcore_commit_rxon(priv);
  2727. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  2728. iwl3945_setup_rxon_timing(priv);
  2729. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  2730. sizeof(priv->rxon_timing), &priv->rxon_timing);
  2731. if (rc)
  2732. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  2733. "Attempting to continue.\n");
  2734. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  2735. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  2736. IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
  2737. priv->assoc_id, priv->beacon_int);
  2738. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  2739. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  2740. else
  2741. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  2742. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  2743. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  2744. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  2745. else
  2746. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2747. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  2748. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2749. }
  2750. iwlcore_commit_rxon(priv);
  2751. switch (priv->iw_mode) {
  2752. case NL80211_IFTYPE_STATION:
  2753. iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
  2754. break;
  2755. case NL80211_IFTYPE_ADHOC:
  2756. priv->assoc_id = 1;
  2757. iwl3945_add_station(priv, priv->bssid, 0, 0);
  2758. iwl3945_sync_sta(priv, IWL_STA_ID,
  2759. (priv->band == IEEE80211_BAND_5GHZ) ?
  2760. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
  2761. CMD_ASYNC);
  2762. iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
  2763. iwl3945_send_beacon_cmd(priv);
  2764. break;
  2765. default:
  2766. IWL_ERR(priv, "%s Should not be called in %d mode\n",
  2767. __func__, priv->iw_mode);
  2768. break;
  2769. }
  2770. iwl_activate_qos(priv, 0);
  2771. /* we have just associated, don't start scan too early */
  2772. priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
  2773. }
  2774. /*****************************************************************************
  2775. *
  2776. * mac80211 entry point functions
  2777. *
  2778. *****************************************************************************/
  2779. #define UCODE_READY_TIMEOUT (2 * HZ)
  2780. static int iwl3945_mac_start(struct ieee80211_hw *hw)
  2781. {
  2782. struct iwl_priv *priv = hw->priv;
  2783. int ret;
  2784. IWL_DEBUG_MAC80211(priv, "enter\n");
  2785. /* we should be verifying the device is ready to be opened */
  2786. mutex_lock(&priv->mutex);
  2787. memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
  2788. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  2789. * ucode filename and max sizes are card-specific. */
  2790. if (!priv->ucode_code.len) {
  2791. ret = iwl3945_read_ucode(priv);
  2792. if (ret) {
  2793. IWL_ERR(priv, "Could not read microcode: %d\n", ret);
  2794. mutex_unlock(&priv->mutex);
  2795. goto out_release_irq;
  2796. }
  2797. }
  2798. ret = __iwl3945_up(priv);
  2799. mutex_unlock(&priv->mutex);
  2800. iwl_rfkill_set_hw_state(priv);
  2801. if (ret)
  2802. goto out_release_irq;
  2803. IWL_DEBUG_INFO(priv, "Start UP work.\n");
  2804. /* Wait for START_ALIVE from ucode. Otherwise callbacks from
  2805. * mac80211 will not be run successfully. */
  2806. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  2807. test_bit(STATUS_READY, &priv->status),
  2808. UCODE_READY_TIMEOUT);
  2809. if (!ret) {
  2810. if (!test_bit(STATUS_READY, &priv->status)) {
  2811. IWL_ERR(priv,
  2812. "Wait for START_ALIVE timeout after %dms.\n",
  2813. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  2814. ret = -ETIMEDOUT;
  2815. goto out_release_irq;
  2816. }
  2817. }
  2818. /* ucode is running and will send rfkill notifications,
  2819. * no need to poll the killswitch state anymore */
  2820. cancel_delayed_work(&priv->rfkill_poll);
  2821. priv->is_open = 1;
  2822. IWL_DEBUG_MAC80211(priv, "leave\n");
  2823. return 0;
  2824. out_release_irq:
  2825. priv->is_open = 0;
  2826. IWL_DEBUG_MAC80211(priv, "leave - failed\n");
  2827. return ret;
  2828. }
  2829. static void iwl3945_mac_stop(struct ieee80211_hw *hw)
  2830. {
  2831. struct iwl_priv *priv = hw->priv;
  2832. IWL_DEBUG_MAC80211(priv, "enter\n");
  2833. if (!priv->is_open) {
  2834. IWL_DEBUG_MAC80211(priv, "leave - skip\n");
  2835. return;
  2836. }
  2837. priv->is_open = 0;
  2838. if (iwl_is_ready_rf(priv)) {
  2839. /* stop mac, cancel any scan request and clear
  2840. * RXON_FILTER_ASSOC_MSK BIT
  2841. */
  2842. mutex_lock(&priv->mutex);
  2843. iwl_scan_cancel_timeout(priv, 100);
  2844. mutex_unlock(&priv->mutex);
  2845. }
  2846. iwl3945_down(priv);
  2847. flush_workqueue(priv->workqueue);
  2848. /* start polling the killswitch state again */
  2849. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  2850. round_jiffies_relative(2 * HZ));
  2851. IWL_DEBUG_MAC80211(priv, "leave\n");
  2852. }
  2853. static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2854. {
  2855. struct iwl_priv *priv = hw->priv;
  2856. IWL_DEBUG_MAC80211(priv, "enter\n");
  2857. IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  2858. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  2859. if (iwl3945_tx_skb(priv, skb))
  2860. dev_kfree_skb_any(skb);
  2861. IWL_DEBUG_MAC80211(priv, "leave\n");
  2862. return NETDEV_TX_OK;
  2863. }
  2864. static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
  2865. struct ieee80211_if_init_conf *conf)
  2866. {
  2867. struct iwl_priv *priv = hw->priv;
  2868. unsigned long flags;
  2869. IWL_DEBUG_MAC80211(priv, "enter: type %d\n", conf->type);
  2870. if (priv->vif) {
  2871. IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
  2872. return -EOPNOTSUPP;
  2873. }
  2874. spin_lock_irqsave(&priv->lock, flags);
  2875. priv->vif = conf->vif;
  2876. priv->iw_mode = conf->type;
  2877. spin_unlock_irqrestore(&priv->lock, flags);
  2878. mutex_lock(&priv->mutex);
  2879. if (conf->mac_addr) {
  2880. IWL_DEBUG_MAC80211(priv, "Set: %pM\n", conf->mac_addr);
  2881. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  2882. }
  2883. if (iwl_is_ready(priv))
  2884. iwl3945_set_mode(priv, conf->type);
  2885. mutex_unlock(&priv->mutex);
  2886. IWL_DEBUG_MAC80211(priv, "leave\n");
  2887. return 0;
  2888. }
  2889. /**
  2890. * iwl3945_mac_config - mac80211 config callback
  2891. *
  2892. * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
  2893. * be set inappropriately and the driver currently sets the hardware up to
  2894. * use it whenever needed.
  2895. */
  2896. static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
  2897. {
  2898. struct iwl_priv *priv = hw->priv;
  2899. const struct iwl_channel_info *ch_info;
  2900. struct ieee80211_conf *conf = &hw->conf;
  2901. unsigned long flags;
  2902. int ret = 0;
  2903. mutex_lock(&priv->mutex);
  2904. IWL_DEBUG_MAC80211(priv, "enter to channel %d\n",
  2905. conf->channel->hw_value);
  2906. if (!iwl_is_ready(priv)) {
  2907. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  2908. ret = -EIO;
  2909. goto out;
  2910. }
  2911. if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
  2912. test_bit(STATUS_SCANNING, &priv->status))) {
  2913. IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
  2914. set_bit(STATUS_CONF_PENDING, &priv->status);
  2915. mutex_unlock(&priv->mutex);
  2916. return 0;
  2917. }
  2918. spin_lock_irqsave(&priv->lock, flags);
  2919. ch_info = iwl_get_channel_info(priv, conf->channel->band,
  2920. conf->channel->hw_value);
  2921. if (!is_channel_valid(ch_info)) {
  2922. IWL_DEBUG_SCAN(priv,
  2923. "Channel %d [%d] is INVALID for this band.\n",
  2924. conf->channel->hw_value, conf->channel->band);
  2925. IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
  2926. spin_unlock_irqrestore(&priv->lock, flags);
  2927. ret = -EINVAL;
  2928. goto out;
  2929. }
  2930. iwl_set_rxon_channel(priv, conf->channel);
  2931. iwl_set_flags_for_band(priv, conf->channel->band);
  2932. /* The list of supported rates and rate mask can be different
  2933. * for each phymode; since the phymode may have changed, reset
  2934. * the rate mask to what mac80211 lists */
  2935. iwl_set_rate(priv);
  2936. spin_unlock_irqrestore(&priv->lock, flags);
  2937. #ifdef IEEE80211_CONF_CHANNEL_SWITCH
  2938. if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
  2939. iwl3945_hw_channel_switch(priv, conf->channel);
  2940. goto out;
  2941. }
  2942. #endif
  2943. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  2944. if (conf->radio_enabled &&
  2945. iwl_radio_kill_sw_enable_radio(priv)) {
  2946. IWL_DEBUG_MAC80211(priv, "leave - RF-KILL - "
  2947. "waiting for uCode\n");
  2948. goto out;
  2949. }
  2950. if (!conf->radio_enabled) {
  2951. iwl_radio_kill_sw_disable_radio(priv);
  2952. IWL_DEBUG_MAC80211(priv, "leave - radio disabled\n");
  2953. goto out;
  2954. }
  2955. }
  2956. if (iwl_is_rfkill(priv)) {
  2957. IWL_DEBUG_MAC80211(priv, "leave - RF kill\n");
  2958. ret = -EIO;
  2959. goto out;
  2960. }
  2961. iwl_set_rate(priv);
  2962. if (memcmp(&priv->active_rxon,
  2963. &priv->staging_rxon, sizeof(priv->staging_rxon)))
  2964. iwlcore_commit_rxon(priv);
  2965. else
  2966. IWL_DEBUG_INFO(priv, "Not re-sending same RXON configuration\n");
  2967. IWL_DEBUG_MAC80211(priv, "leave\n");
  2968. out:
  2969. clear_bit(STATUS_CONF_PENDING, &priv->status);
  2970. mutex_unlock(&priv->mutex);
  2971. return ret;
  2972. }
  2973. static void iwl3945_config_ap(struct iwl_priv *priv)
  2974. {
  2975. int rc = 0;
  2976. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2977. return;
  2978. /* The following should be done only at AP bring up */
  2979. if (!(iwl_is_associated(priv))) {
  2980. /* RXON - unassoc (to set timing command) */
  2981. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2982. iwlcore_commit_rxon(priv);
  2983. /* RXON Timing */
  2984. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  2985. iwl3945_setup_rxon_timing(priv);
  2986. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  2987. sizeof(priv->rxon_timing),
  2988. &priv->rxon_timing);
  2989. if (rc)
  2990. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  2991. "Attempting to continue.\n");
  2992. /* FIXME: what should be the assoc_id for AP? */
  2993. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  2994. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  2995. priv->staging_rxon.flags |=
  2996. RXON_FLG_SHORT_PREAMBLE_MSK;
  2997. else
  2998. priv->staging_rxon.flags &=
  2999. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  3000. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  3001. if (priv->assoc_capability &
  3002. WLAN_CAPABILITY_SHORT_SLOT_TIME)
  3003. priv->staging_rxon.flags |=
  3004. RXON_FLG_SHORT_SLOT_MSK;
  3005. else
  3006. priv->staging_rxon.flags &=
  3007. ~RXON_FLG_SHORT_SLOT_MSK;
  3008. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  3009. priv->staging_rxon.flags &=
  3010. ~RXON_FLG_SHORT_SLOT_MSK;
  3011. }
  3012. /* restore RXON assoc */
  3013. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  3014. iwlcore_commit_rxon(priv);
  3015. iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
  3016. }
  3017. iwl3945_send_beacon_cmd(priv);
  3018. /* FIXME - we need to add code here to detect a totally new
  3019. * configuration, reset the AP, unassoc, rxon timing, assoc,
  3020. * clear sta table, add BCAST sta... */
  3021. }
  3022. static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
  3023. struct ieee80211_vif *vif,
  3024. struct ieee80211_if_conf *conf)
  3025. {
  3026. struct iwl_priv *priv = hw->priv;
  3027. int rc;
  3028. if (conf == NULL)
  3029. return -EIO;
  3030. if (priv->vif != vif) {
  3031. IWL_DEBUG_MAC80211(priv, "leave - priv->vif != vif\n");
  3032. return 0;
  3033. }
  3034. /* handle this temporarily here */
  3035. if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
  3036. conf->changed & IEEE80211_IFCC_BEACON) {
  3037. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  3038. if (!beacon)
  3039. return -ENOMEM;
  3040. mutex_lock(&priv->mutex);
  3041. rc = iwl_mac_beacon_update(hw, beacon);
  3042. mutex_unlock(&priv->mutex);
  3043. if (rc)
  3044. return rc;
  3045. }
  3046. if (!iwl_is_alive(priv))
  3047. return -EAGAIN;
  3048. mutex_lock(&priv->mutex);
  3049. if (conf->bssid)
  3050. IWL_DEBUG_MAC80211(priv, "bssid: %pM\n", conf->bssid);
  3051. /*
  3052. * very dubious code was here; the probe filtering flag is never set:
  3053. *
  3054. if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
  3055. !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
  3056. */
  3057. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  3058. if (!conf->bssid) {
  3059. conf->bssid = priv->mac_addr;
  3060. memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
  3061. IWL_DEBUG_MAC80211(priv, "bssid was set to: %pM\n",
  3062. conf->bssid);
  3063. }
  3064. if (priv->ibss_beacon)
  3065. dev_kfree_skb(priv->ibss_beacon);
  3066. priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
  3067. }
  3068. if (iwl_is_rfkill(priv))
  3069. goto done;
  3070. if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
  3071. !is_multicast_ether_addr(conf->bssid)) {
  3072. /* If there is currently a HW scan going on in the background
  3073. * then we need to cancel it else the RXON below will fail. */
  3074. if (iwl_scan_cancel_timeout(priv, 100)) {
  3075. IWL_WARN(priv, "Aborted scan still in progress "
  3076. "after 100ms\n");
  3077. IWL_DEBUG_MAC80211(priv, "leaving:scan abort failed\n");
  3078. mutex_unlock(&priv->mutex);
  3079. return -EAGAIN;
  3080. }
  3081. memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
  3082. /* TODO: Audit driver for usage of these members and see
  3083. * if mac80211 deprecates them (priv->bssid looks like it
  3084. * shouldn't be there, but I haven't scanned the IBSS code
  3085. * to verify) - jpk */
  3086. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  3087. if (priv->iw_mode == NL80211_IFTYPE_AP)
  3088. iwl3945_config_ap(priv);
  3089. else {
  3090. rc = iwlcore_commit_rxon(priv);
  3091. if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
  3092. iwl3945_add_station(priv,
  3093. priv->active_rxon.bssid_addr, 1, 0);
  3094. }
  3095. } else {
  3096. iwl_scan_cancel_timeout(priv, 100);
  3097. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3098. iwlcore_commit_rxon(priv);
  3099. }
  3100. done:
  3101. IWL_DEBUG_MAC80211(priv, "leave\n");
  3102. mutex_unlock(&priv->mutex);
  3103. return 0;
  3104. }
  3105. static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
  3106. struct ieee80211_if_init_conf *conf)
  3107. {
  3108. struct iwl_priv *priv = hw->priv;
  3109. IWL_DEBUG_MAC80211(priv, "enter\n");
  3110. mutex_lock(&priv->mutex);
  3111. if (iwl_is_ready_rf(priv)) {
  3112. iwl_scan_cancel_timeout(priv, 100);
  3113. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3114. iwlcore_commit_rxon(priv);
  3115. }
  3116. if (priv->vif == conf->vif) {
  3117. priv->vif = NULL;
  3118. memset(priv->bssid, 0, ETH_ALEN);
  3119. }
  3120. mutex_unlock(&priv->mutex);
  3121. IWL_DEBUG_MAC80211(priv, "leave\n");
  3122. }
  3123. static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  3124. struct ieee80211_vif *vif,
  3125. struct ieee80211_sta *sta,
  3126. struct ieee80211_key_conf *key)
  3127. {
  3128. struct iwl_priv *priv = hw->priv;
  3129. const u8 *addr;
  3130. int ret = 0;
  3131. u8 sta_id = IWL_INVALID_STATION;
  3132. u8 static_key;
  3133. IWL_DEBUG_MAC80211(priv, "enter\n");
  3134. if (iwl3945_mod_params.sw_crypto) {
  3135. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  3136. return -EOPNOTSUPP;
  3137. }
  3138. addr = sta ? sta->addr : iwl_bcast_addr;
  3139. static_key = !iwl_is_associated(priv);
  3140. if (!static_key) {
  3141. sta_id = iwl3945_hw_find_station(priv, addr);
  3142. if (sta_id == IWL_INVALID_STATION) {
  3143. IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
  3144. addr);
  3145. return -EINVAL;
  3146. }
  3147. }
  3148. mutex_lock(&priv->mutex);
  3149. iwl_scan_cancel_timeout(priv, 100);
  3150. mutex_unlock(&priv->mutex);
  3151. switch (cmd) {
  3152. case SET_KEY:
  3153. if (static_key)
  3154. ret = iwl3945_set_static_key(priv, key);
  3155. else
  3156. ret = iwl3945_set_dynamic_key(priv, key, sta_id);
  3157. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  3158. break;
  3159. case DISABLE_KEY:
  3160. if (static_key)
  3161. ret = iwl3945_remove_static_key(priv);
  3162. else
  3163. ret = iwl3945_clear_sta_key_info(priv, sta_id);
  3164. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  3165. break;
  3166. default:
  3167. ret = -EINVAL;
  3168. }
  3169. IWL_DEBUG_MAC80211(priv, "leave\n");
  3170. return ret;
  3171. }
  3172. static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
  3173. struct ieee80211_tx_queue_stats *stats)
  3174. {
  3175. struct iwl_priv *priv = hw->priv;
  3176. int i, avail;
  3177. struct iwl_tx_queue *txq;
  3178. struct iwl_queue *q;
  3179. unsigned long flags;
  3180. IWL_DEBUG_MAC80211(priv, "enter\n");
  3181. if (!iwl_is_ready_rf(priv)) {
  3182. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  3183. return -EIO;
  3184. }
  3185. spin_lock_irqsave(&priv->lock, flags);
  3186. for (i = 0; i < AC_NUM; i++) {
  3187. txq = &priv->txq[i];
  3188. q = &txq->q;
  3189. avail = iwl_queue_space(q);
  3190. stats[i].len = q->n_window - avail;
  3191. stats[i].limit = q->n_window - q->high_mark;
  3192. stats[i].count = q->n_window;
  3193. }
  3194. spin_unlock_irqrestore(&priv->lock, flags);
  3195. IWL_DEBUG_MAC80211(priv, "leave\n");
  3196. return 0;
  3197. }
  3198. static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
  3199. {
  3200. struct iwl_priv *priv = hw->priv;
  3201. unsigned long flags;
  3202. mutex_lock(&priv->mutex);
  3203. IWL_DEBUG_MAC80211(priv, "enter\n");
  3204. iwl_reset_qos(priv);
  3205. spin_lock_irqsave(&priv->lock, flags);
  3206. priv->assoc_id = 0;
  3207. priv->assoc_capability = 0;
  3208. /* new association get rid of ibss beacon skb */
  3209. if (priv->ibss_beacon)
  3210. dev_kfree_skb(priv->ibss_beacon);
  3211. priv->ibss_beacon = NULL;
  3212. priv->beacon_int = priv->hw->conf.beacon_int;
  3213. priv->timestamp = 0;
  3214. if ((priv->iw_mode == NL80211_IFTYPE_STATION))
  3215. priv->beacon_int = 0;
  3216. spin_unlock_irqrestore(&priv->lock, flags);
  3217. if (!iwl_is_ready_rf(priv)) {
  3218. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  3219. mutex_unlock(&priv->mutex);
  3220. return;
  3221. }
  3222. /* we are restarting association process
  3223. * clear RXON_FILTER_ASSOC_MSK bit
  3224. */
  3225. if (priv->iw_mode != NL80211_IFTYPE_AP) {
  3226. iwl_scan_cancel_timeout(priv, 100);
  3227. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3228. iwlcore_commit_rxon(priv);
  3229. }
  3230. /* Per mac80211.h: This is only used in IBSS mode... */
  3231. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  3232. IWL_DEBUG_MAC80211(priv, "leave - not in IBSS\n");
  3233. mutex_unlock(&priv->mutex);
  3234. return;
  3235. }
  3236. iwl_set_rate(priv);
  3237. mutex_unlock(&priv->mutex);
  3238. IWL_DEBUG_MAC80211(priv, "leave\n");
  3239. }
  3240. /*****************************************************************************
  3241. *
  3242. * sysfs attributes
  3243. *
  3244. *****************************************************************************/
  3245. #ifdef CONFIG_IWLWIFI_DEBUG
  3246. /*
  3247. * The following adds a new attribute to the sysfs representation
  3248. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  3249. * used for controlling the debug level.
  3250. *
  3251. * See the level definitions in iwl for details.
  3252. */
  3253. static ssize_t show_debug_level(struct device *d,
  3254. struct device_attribute *attr, char *buf)
  3255. {
  3256. struct iwl_priv *priv = d->driver_data;
  3257. return sprintf(buf, "0x%08X\n", priv->debug_level);
  3258. }
  3259. static ssize_t store_debug_level(struct device *d,
  3260. struct device_attribute *attr,
  3261. const char *buf, size_t count)
  3262. {
  3263. struct iwl_priv *priv = d->driver_data;
  3264. unsigned long val;
  3265. int ret;
  3266. ret = strict_strtoul(buf, 0, &val);
  3267. if (ret)
  3268. IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
  3269. else
  3270. priv->debug_level = val;
  3271. return strnlen(buf, count);
  3272. }
  3273. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  3274. show_debug_level, store_debug_level);
  3275. #endif /* CONFIG_IWLWIFI_DEBUG */
  3276. static ssize_t show_temperature(struct device *d,
  3277. struct device_attribute *attr, char *buf)
  3278. {
  3279. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3280. if (!iwl_is_alive(priv))
  3281. return -EAGAIN;
  3282. return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
  3283. }
  3284. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  3285. static ssize_t show_tx_power(struct device *d,
  3286. struct device_attribute *attr, char *buf)
  3287. {
  3288. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3289. return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
  3290. }
  3291. static ssize_t store_tx_power(struct device *d,
  3292. struct device_attribute *attr,
  3293. const char *buf, size_t count)
  3294. {
  3295. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3296. char *p = (char *)buf;
  3297. u32 val;
  3298. val = simple_strtoul(p, &p, 10);
  3299. if (p == buf)
  3300. IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
  3301. else
  3302. iwl3945_hw_reg_set_txpower(priv, val);
  3303. return count;
  3304. }
  3305. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  3306. static ssize_t show_flags(struct device *d,
  3307. struct device_attribute *attr, char *buf)
  3308. {
  3309. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3310. return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
  3311. }
  3312. static ssize_t store_flags(struct device *d,
  3313. struct device_attribute *attr,
  3314. const char *buf, size_t count)
  3315. {
  3316. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3317. u32 flags = simple_strtoul(buf, NULL, 0);
  3318. mutex_lock(&priv->mutex);
  3319. if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
  3320. /* Cancel any currently running scans... */
  3321. if (iwl_scan_cancel_timeout(priv, 100))
  3322. IWL_WARN(priv, "Could not cancel scan.\n");
  3323. else {
  3324. IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
  3325. flags);
  3326. priv->staging_rxon.flags = cpu_to_le32(flags);
  3327. iwlcore_commit_rxon(priv);
  3328. }
  3329. }
  3330. mutex_unlock(&priv->mutex);
  3331. return count;
  3332. }
  3333. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
  3334. static ssize_t show_filter_flags(struct device *d,
  3335. struct device_attribute *attr, char *buf)
  3336. {
  3337. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3338. return sprintf(buf, "0x%04X\n",
  3339. le32_to_cpu(priv->active_rxon.filter_flags));
  3340. }
  3341. static ssize_t store_filter_flags(struct device *d,
  3342. struct device_attribute *attr,
  3343. const char *buf, size_t count)
  3344. {
  3345. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3346. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  3347. mutex_lock(&priv->mutex);
  3348. if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
  3349. /* Cancel any currently running scans... */
  3350. if (iwl_scan_cancel_timeout(priv, 100))
  3351. IWL_WARN(priv, "Could not cancel scan.\n");
  3352. else {
  3353. IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
  3354. "0x%04X\n", filter_flags);
  3355. priv->staging_rxon.filter_flags =
  3356. cpu_to_le32(filter_flags);
  3357. iwlcore_commit_rxon(priv);
  3358. }
  3359. }
  3360. mutex_unlock(&priv->mutex);
  3361. return count;
  3362. }
  3363. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
  3364. store_filter_flags);
  3365. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  3366. static ssize_t show_measurement(struct device *d,
  3367. struct device_attribute *attr, char *buf)
  3368. {
  3369. struct iwl_priv *priv = dev_get_drvdata(d);
  3370. struct iwl_spectrum_notification measure_report;
  3371. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  3372. u8 *data = (u8 *)&measure_report;
  3373. unsigned long flags;
  3374. spin_lock_irqsave(&priv->lock, flags);
  3375. if (!(priv->measurement_status & MEASUREMENT_READY)) {
  3376. spin_unlock_irqrestore(&priv->lock, flags);
  3377. return 0;
  3378. }
  3379. memcpy(&measure_report, &priv->measure_report, size);
  3380. priv->measurement_status = 0;
  3381. spin_unlock_irqrestore(&priv->lock, flags);
  3382. while (size && (PAGE_SIZE - len)) {
  3383. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  3384. PAGE_SIZE - len, 1);
  3385. len = strlen(buf);
  3386. if (PAGE_SIZE - len)
  3387. buf[len++] = '\n';
  3388. ofs += 16;
  3389. size -= min(size, 16U);
  3390. }
  3391. return len;
  3392. }
  3393. static ssize_t store_measurement(struct device *d,
  3394. struct device_attribute *attr,
  3395. const char *buf, size_t count)
  3396. {
  3397. struct iwl_priv *priv = dev_get_drvdata(d);
  3398. struct ieee80211_measurement_params params = {
  3399. .channel = le16_to_cpu(priv->active_rxon.channel),
  3400. .start_time = cpu_to_le64(priv->last_tsf),
  3401. .duration = cpu_to_le16(1),
  3402. };
  3403. u8 type = IWL_MEASURE_BASIC;
  3404. u8 buffer[32];
  3405. u8 channel;
  3406. if (count) {
  3407. char *p = buffer;
  3408. strncpy(buffer, buf, min(sizeof(buffer), count));
  3409. channel = simple_strtoul(p, NULL, 0);
  3410. if (channel)
  3411. params.channel = channel;
  3412. p = buffer;
  3413. while (*p && *p != ' ')
  3414. p++;
  3415. if (*p)
  3416. type = simple_strtoul(p + 1, NULL, 0);
  3417. }
  3418. IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
  3419. "channel %d (for '%s')\n", type, params.channel, buf);
  3420. iwl3945_get_measurement(priv, &params, type);
  3421. return count;
  3422. }
  3423. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
  3424. show_measurement, store_measurement);
  3425. #endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
  3426. static ssize_t store_retry_rate(struct device *d,
  3427. struct device_attribute *attr,
  3428. const char *buf, size_t count)
  3429. {
  3430. struct iwl_priv *priv = dev_get_drvdata(d);
  3431. priv->retry_rate = simple_strtoul(buf, NULL, 0);
  3432. if (priv->retry_rate <= 0)
  3433. priv->retry_rate = 1;
  3434. return count;
  3435. }
  3436. static ssize_t show_retry_rate(struct device *d,
  3437. struct device_attribute *attr, char *buf)
  3438. {
  3439. struct iwl_priv *priv = dev_get_drvdata(d);
  3440. return sprintf(buf, "%d", priv->retry_rate);
  3441. }
  3442. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
  3443. store_retry_rate);
  3444. static ssize_t store_power_level(struct device *d,
  3445. struct device_attribute *attr,
  3446. const char *buf, size_t count)
  3447. {
  3448. struct iwl_priv *priv = dev_get_drvdata(d);
  3449. int ret;
  3450. unsigned long mode;
  3451. mutex_lock(&priv->mutex);
  3452. ret = strict_strtoul(buf, 10, &mode);
  3453. if (ret)
  3454. goto out;
  3455. ret = iwl_power_set_user_mode(priv, mode);
  3456. if (ret) {
  3457. IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
  3458. goto out;
  3459. }
  3460. ret = count;
  3461. out:
  3462. mutex_unlock(&priv->mutex);
  3463. return ret;
  3464. }
  3465. static ssize_t show_power_level(struct device *d,
  3466. struct device_attribute *attr, char *buf)
  3467. {
  3468. struct iwl_priv *priv = dev_get_drvdata(d);
  3469. int mode = priv->power_data.user_power_setting;
  3470. int system = priv->power_data.system_power_setting;
  3471. int level = priv->power_data.power_mode;
  3472. char *p = buf;
  3473. switch (system) {
  3474. case IWL_POWER_SYS_AUTO:
  3475. p += sprintf(p, "SYSTEM:auto");
  3476. break;
  3477. case IWL_POWER_SYS_AC:
  3478. p += sprintf(p, "SYSTEM:ac");
  3479. break;
  3480. case IWL_POWER_SYS_BATTERY:
  3481. p += sprintf(p, "SYSTEM:battery");
  3482. break;
  3483. }
  3484. p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
  3485. "fixed" : "auto");
  3486. p += sprintf(p, "\tINDEX:%d", level);
  3487. p += sprintf(p, "\n");
  3488. return p - buf + 1;
  3489. }
  3490. static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR,
  3491. show_power_level, store_power_level);
  3492. #define MAX_WX_STRING 80
  3493. /* Values are in microsecond */
  3494. static const s32 timeout_duration[] = {
  3495. 350000,
  3496. 250000,
  3497. 75000,
  3498. 37000,
  3499. 25000,
  3500. };
  3501. static const s32 period_duration[] = {
  3502. 400000,
  3503. 700000,
  3504. 1000000,
  3505. 1000000,
  3506. 1000000
  3507. };
  3508. static ssize_t show_channels(struct device *d,
  3509. struct device_attribute *attr, char *buf)
  3510. {
  3511. /* all this shit doesn't belong into sysfs anyway */
  3512. return 0;
  3513. }
  3514. static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
  3515. static ssize_t show_statistics(struct device *d,
  3516. struct device_attribute *attr, char *buf)
  3517. {
  3518. struct iwl_priv *priv = dev_get_drvdata(d);
  3519. u32 size = sizeof(struct iwl3945_notif_statistics);
  3520. u32 len = 0, ofs = 0;
  3521. u8 *data = (u8 *)&priv->statistics_39;
  3522. int rc = 0;
  3523. if (!iwl_is_alive(priv))
  3524. return -EAGAIN;
  3525. mutex_lock(&priv->mutex);
  3526. rc = iwl_send_statistics_request(priv, 0);
  3527. mutex_unlock(&priv->mutex);
  3528. if (rc) {
  3529. len = sprintf(buf,
  3530. "Error sending statistics request: 0x%08X\n", rc);
  3531. return len;
  3532. }
  3533. while (size && (PAGE_SIZE - len)) {
  3534. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  3535. PAGE_SIZE - len, 1);
  3536. len = strlen(buf);
  3537. if (PAGE_SIZE - len)
  3538. buf[len++] = '\n';
  3539. ofs += 16;
  3540. size -= min(size, 16U);
  3541. }
  3542. return len;
  3543. }
  3544. static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
  3545. static ssize_t show_antenna(struct device *d,
  3546. struct device_attribute *attr, char *buf)
  3547. {
  3548. struct iwl_priv *priv = dev_get_drvdata(d);
  3549. if (!iwl_is_alive(priv))
  3550. return -EAGAIN;
  3551. return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
  3552. }
  3553. static ssize_t store_antenna(struct device *d,
  3554. struct device_attribute *attr,
  3555. const char *buf, size_t count)
  3556. {
  3557. struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
  3558. int ant;
  3559. if (count == 0)
  3560. return 0;
  3561. if (sscanf(buf, "%1i", &ant) != 1) {
  3562. IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
  3563. return count;
  3564. }
  3565. if ((ant >= 0) && (ant <= 2)) {
  3566. IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
  3567. iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
  3568. } else
  3569. IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
  3570. return count;
  3571. }
  3572. static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
  3573. static ssize_t show_status(struct device *d,
  3574. struct device_attribute *attr, char *buf)
  3575. {
  3576. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3577. if (!iwl_is_alive(priv))
  3578. return -EAGAIN;
  3579. return sprintf(buf, "0x%08x\n", (int)priv->status);
  3580. }
  3581. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  3582. static ssize_t dump_error_log(struct device *d,
  3583. struct device_attribute *attr,
  3584. const char *buf, size_t count)
  3585. {
  3586. char *p = (char *)buf;
  3587. if (p[0] == '1')
  3588. iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
  3589. return strnlen(buf, count);
  3590. }
  3591. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
  3592. static ssize_t dump_event_log(struct device *d,
  3593. struct device_attribute *attr,
  3594. const char *buf, size_t count)
  3595. {
  3596. char *p = (char *)buf;
  3597. if (p[0] == '1')
  3598. iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
  3599. return strnlen(buf, count);
  3600. }
  3601. static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
  3602. /*****************************************************************************
  3603. *
  3604. * driver setup and tear down
  3605. *
  3606. *****************************************************************************/
  3607. static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
  3608. {
  3609. priv->workqueue = create_singlethread_workqueue(DRV_NAME);
  3610. init_waitqueue_head(&priv->wait_command_queue);
  3611. INIT_WORK(&priv->up, iwl3945_bg_up);
  3612. INIT_WORK(&priv->restart, iwl3945_bg_restart);
  3613. INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
  3614. INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
  3615. INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
  3616. INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
  3617. INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
  3618. INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
  3619. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  3620. INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
  3621. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  3622. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  3623. iwl3945_hw_setup_deferred_work(priv);
  3624. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  3625. iwl3945_irq_tasklet, (unsigned long)priv);
  3626. }
  3627. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
  3628. {
  3629. iwl3945_hw_cancel_deferred_work(priv);
  3630. cancel_delayed_work_sync(&priv->init_alive_start);
  3631. cancel_delayed_work(&priv->scan_check);
  3632. cancel_delayed_work(&priv->alive_start);
  3633. cancel_work_sync(&priv->beacon_update);
  3634. }
  3635. static struct attribute *iwl3945_sysfs_entries[] = {
  3636. &dev_attr_antenna.attr,
  3637. &dev_attr_channels.attr,
  3638. &dev_attr_dump_errors.attr,
  3639. &dev_attr_dump_events.attr,
  3640. &dev_attr_flags.attr,
  3641. &dev_attr_filter_flags.attr,
  3642. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  3643. &dev_attr_measurement.attr,
  3644. #endif
  3645. &dev_attr_power_level.attr,
  3646. &dev_attr_retry_rate.attr,
  3647. &dev_attr_statistics.attr,
  3648. &dev_attr_status.attr,
  3649. &dev_attr_temperature.attr,
  3650. &dev_attr_tx_power.attr,
  3651. #ifdef CONFIG_IWLWIFI_DEBUG
  3652. &dev_attr_debug_level.attr,
  3653. #endif
  3654. NULL
  3655. };
  3656. static struct attribute_group iwl3945_attribute_group = {
  3657. .name = NULL, /* put in device directory */
  3658. .attrs = iwl3945_sysfs_entries,
  3659. };
  3660. static struct ieee80211_ops iwl3945_hw_ops = {
  3661. .tx = iwl3945_mac_tx,
  3662. .start = iwl3945_mac_start,
  3663. .stop = iwl3945_mac_stop,
  3664. .add_interface = iwl3945_mac_add_interface,
  3665. .remove_interface = iwl3945_mac_remove_interface,
  3666. .config = iwl3945_mac_config,
  3667. .config_interface = iwl3945_mac_config_interface,
  3668. .configure_filter = iwl_configure_filter,
  3669. .set_key = iwl3945_mac_set_key,
  3670. .get_tx_stats = iwl3945_mac_get_tx_stats,
  3671. .conf_tx = iwl_mac_conf_tx,
  3672. .reset_tsf = iwl3945_mac_reset_tsf,
  3673. .bss_info_changed = iwl_bss_info_changed,
  3674. .hw_scan = iwl_mac_hw_scan
  3675. };
  3676. static int iwl3945_init_drv(struct iwl_priv *priv)
  3677. {
  3678. int ret;
  3679. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  3680. priv->retry_rate = 1;
  3681. priv->ibss_beacon = NULL;
  3682. spin_lock_init(&priv->lock);
  3683. spin_lock_init(&priv->power_data.lock);
  3684. spin_lock_init(&priv->sta_lock);
  3685. spin_lock_init(&priv->hcmd_lock);
  3686. INIT_LIST_HEAD(&priv->free_frames);
  3687. mutex_init(&priv->mutex);
  3688. /* Clear the driver's (not device's) station table */
  3689. iwl3945_clear_stations_table(priv);
  3690. priv->data_retry_limit = -1;
  3691. priv->ieee_channels = NULL;
  3692. priv->ieee_rates = NULL;
  3693. priv->band = IEEE80211_BAND_2GHZ;
  3694. priv->iw_mode = NL80211_IFTYPE_STATION;
  3695. iwl_reset_qos(priv);
  3696. priv->qos_data.qos_active = 0;
  3697. priv->qos_data.qos_cap.val = 0;
  3698. priv->rates_mask = IWL_RATES_MASK;
  3699. /* If power management is turned on, default to CAM mode */
  3700. priv->power_mode = IWL_POWER_MODE_CAM;
  3701. priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
  3702. if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
  3703. IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
  3704. eeprom->version);
  3705. ret = -EINVAL;
  3706. goto err;
  3707. }
  3708. ret = iwl_init_channel_map(priv);
  3709. if (ret) {
  3710. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  3711. goto err;
  3712. }
  3713. /* Set up txpower settings in driver for all channels */
  3714. if (iwl3945_txpower_set_from_eeprom(priv)) {
  3715. ret = -EIO;
  3716. goto err_free_channel_map;
  3717. }
  3718. ret = iwlcore_init_geos(priv);
  3719. if (ret) {
  3720. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  3721. goto err_free_channel_map;
  3722. }
  3723. iwl3945_init_hw_rates(priv, priv->ieee_rates);
  3724. return 0;
  3725. err_free_channel_map:
  3726. iwl_free_channel_map(priv);
  3727. err:
  3728. return ret;
  3729. }
  3730. static int iwl3945_setup_mac(struct iwl_priv *priv)
  3731. {
  3732. int ret;
  3733. struct ieee80211_hw *hw = priv->hw;
  3734. hw->rate_control_algorithm = "iwl-3945-rs";
  3735. hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
  3736. /* Tell mac80211 our characteristics */
  3737. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  3738. IEEE80211_HW_NOISE_DBM |
  3739. IEEE80211_HW_SPECTRUM_MGMT;
  3740. hw->wiphy->interface_modes =
  3741. BIT(NL80211_IFTYPE_STATION) |
  3742. BIT(NL80211_IFTYPE_ADHOC);
  3743. hw->wiphy->custom_regulatory = true;
  3744. hw->wiphy->max_scan_ssids = 1; /* WILL FIX */
  3745. /* Default value; 4 EDCA QOS priorities */
  3746. hw->queues = 4;
  3747. hw->conf.beacon_int = 100;
  3748. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  3749. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3750. &priv->bands[IEEE80211_BAND_2GHZ];
  3751. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  3752. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  3753. &priv->bands[IEEE80211_BAND_5GHZ];
  3754. ret = ieee80211_register_hw(priv->hw);
  3755. if (ret) {
  3756. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  3757. return ret;
  3758. }
  3759. priv->mac80211_registered = 1;
  3760. return 0;
  3761. }
  3762. static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3763. {
  3764. int err = 0;
  3765. struct iwl_priv *priv;
  3766. struct ieee80211_hw *hw;
  3767. struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
  3768. struct iwl3945_eeprom *eeprom;
  3769. unsigned long flags;
  3770. /***********************
  3771. * 1. Allocating HW data
  3772. * ********************/
  3773. /* mac80211 allocates memory for this device instance, including
  3774. * space for this driver's private structure */
  3775. hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
  3776. if (hw == NULL) {
  3777. printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
  3778. err = -ENOMEM;
  3779. goto out;
  3780. }
  3781. priv = hw->priv;
  3782. SET_IEEE80211_DEV(hw, &pdev->dev);
  3783. if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
  3784. (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
  3785. IWL_ERR(priv,
  3786. "invalid queues_num, should be between %d and %d\n",
  3787. IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
  3788. err = -EINVAL;
  3789. goto out_ieee80211_free_hw;
  3790. }
  3791. /*
  3792. * Disabling hardware scan means that mac80211 will perform scans
  3793. * "the hard way", rather than using device's scan.
  3794. */
  3795. if (iwl3945_mod_params.disable_hw_scan) {
  3796. IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
  3797. iwl3945_hw_ops.hw_scan = NULL;
  3798. }
  3799. IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
  3800. priv->cfg = cfg;
  3801. priv->pci_dev = pdev;
  3802. #ifdef CONFIG_IWLWIFI_DEBUG
  3803. priv->debug_level = iwl3945_mod_params.debug;
  3804. atomic_set(&priv->restrict_refcnt, 0);
  3805. #endif
  3806. /***************************
  3807. * 2. Initializing PCI bus
  3808. * *************************/
  3809. if (pci_enable_device(pdev)) {
  3810. err = -ENODEV;
  3811. goto out_ieee80211_free_hw;
  3812. }
  3813. pci_set_master(pdev);
  3814. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3815. if (!err)
  3816. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3817. if (err) {
  3818. IWL_WARN(priv, "No suitable DMA available.\n");
  3819. goto out_pci_disable_device;
  3820. }
  3821. pci_set_drvdata(pdev, priv);
  3822. err = pci_request_regions(pdev, DRV_NAME);
  3823. if (err)
  3824. goto out_pci_disable_device;
  3825. /***********************
  3826. * 3. Read REV Register
  3827. * ********************/
  3828. priv->hw_base = pci_iomap(pdev, 0, 0);
  3829. if (!priv->hw_base) {
  3830. err = -ENODEV;
  3831. goto out_pci_release_regions;
  3832. }
  3833. IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
  3834. (unsigned long long) pci_resource_len(pdev, 0));
  3835. IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
  3836. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  3837. * PCI Tx retries from interfering with C3 CPU state */
  3838. pci_write_config_byte(pdev, 0x41, 0x00);
  3839. /* amp init */
  3840. err = priv->cfg->ops->lib->apm_ops.init(priv);
  3841. if (err < 0) {
  3842. IWL_DEBUG_INFO(priv, "Failed to init the card\n");
  3843. goto out_iounmap;
  3844. }
  3845. /***********************
  3846. * 4. Read EEPROM
  3847. * ********************/
  3848. /* Read the EEPROM */
  3849. err = iwl_eeprom_init(priv);
  3850. if (err) {
  3851. IWL_ERR(priv, "Unable to init EEPROM\n");
  3852. goto out_iounmap;
  3853. }
  3854. /* MAC Address location in EEPROM same for 3945/4965 */
  3855. eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  3856. memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
  3857. IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
  3858. SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
  3859. /***********************
  3860. * 5. Setup HW Constants
  3861. * ********************/
  3862. /* Device-specific setup */
  3863. if (iwl3945_hw_set_hw_params(priv)) {
  3864. IWL_ERR(priv, "failed to set hw settings\n");
  3865. goto out_eeprom_free;
  3866. }
  3867. /***********************
  3868. * 6. Setup priv
  3869. * ********************/
  3870. err = iwl3945_init_drv(priv);
  3871. if (err) {
  3872. IWL_ERR(priv, "initializing driver failed\n");
  3873. goto out_unset_hw_params;
  3874. }
  3875. IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
  3876. priv->cfg->name);
  3877. /***********************************
  3878. * 7. Initialize Module Parameters
  3879. * **********************************/
  3880. /* Initialize module parameter values here */
  3881. /* Disable radio (SW RF KILL) via parameter when loading driver */
  3882. if (iwl3945_mod_params.disable) {
  3883. set_bit(STATUS_RF_KILL_SW, &priv->status);
  3884. IWL_DEBUG_INFO(priv, "Radio disabled.\n");
  3885. }
  3886. /***********************
  3887. * 8. Setup Services
  3888. * ********************/
  3889. spin_lock_irqsave(&priv->lock, flags);
  3890. iwl_disable_interrupts(priv);
  3891. spin_unlock_irqrestore(&priv->lock, flags);
  3892. pci_enable_msi(priv->pci_dev);
  3893. err = request_irq(priv->pci_dev->irq, iwl_isr, IRQF_SHARED,
  3894. DRV_NAME, priv);
  3895. if (err) {
  3896. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
  3897. goto out_disable_msi;
  3898. }
  3899. err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  3900. if (err) {
  3901. IWL_ERR(priv, "failed to create sysfs device attributes\n");
  3902. goto out_release_irq;
  3903. }
  3904. iwl_set_rxon_channel(priv,
  3905. &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
  3906. iwl3945_setup_deferred_work(priv);
  3907. iwl3945_setup_rx_handlers(priv);
  3908. /*********************************
  3909. * 9. Setup and Register mac80211
  3910. * *******************************/
  3911. iwl_enable_interrupts(priv);
  3912. err = iwl3945_setup_mac(priv);
  3913. if (err)
  3914. goto out_remove_sysfs;
  3915. err = iwl_rfkill_init(priv);
  3916. if (err)
  3917. IWL_ERR(priv, "Unable to initialize RFKILL system. "
  3918. "Ignoring error: %d\n", err);
  3919. else
  3920. iwl_rfkill_set_hw_state(priv);
  3921. /* Start monitoring the killswitch */
  3922. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  3923. 2 * HZ);
  3924. return 0;
  3925. out_remove_sysfs:
  3926. destroy_workqueue(priv->workqueue);
  3927. priv->workqueue = NULL;
  3928. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  3929. out_release_irq:
  3930. free_irq(priv->pci_dev->irq, priv);
  3931. out_disable_msi:
  3932. pci_disable_msi(priv->pci_dev);
  3933. iwlcore_free_geos(priv);
  3934. iwl_free_channel_map(priv);
  3935. out_unset_hw_params:
  3936. iwl3945_unset_hw_params(priv);
  3937. out_eeprom_free:
  3938. iwl_eeprom_free(priv);
  3939. out_iounmap:
  3940. pci_iounmap(pdev, priv->hw_base);
  3941. out_pci_release_regions:
  3942. pci_release_regions(pdev);
  3943. out_pci_disable_device:
  3944. pci_set_drvdata(pdev, NULL);
  3945. pci_disable_device(pdev);
  3946. out_ieee80211_free_hw:
  3947. ieee80211_free_hw(priv->hw);
  3948. out:
  3949. return err;
  3950. }
  3951. static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
  3952. {
  3953. struct iwl_priv *priv = pci_get_drvdata(pdev);
  3954. unsigned long flags;
  3955. if (!priv)
  3956. return;
  3957. IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
  3958. set_bit(STATUS_EXIT_PENDING, &priv->status);
  3959. if (priv->mac80211_registered) {
  3960. ieee80211_unregister_hw(priv->hw);
  3961. priv->mac80211_registered = 0;
  3962. } else {
  3963. iwl3945_down(priv);
  3964. }
  3965. /* make sure we flush any pending irq or
  3966. * tasklet for the driver
  3967. */
  3968. spin_lock_irqsave(&priv->lock, flags);
  3969. iwl_disable_interrupts(priv);
  3970. spin_unlock_irqrestore(&priv->lock, flags);
  3971. iwl_synchronize_irq(priv);
  3972. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  3973. iwl_rfkill_unregister(priv);
  3974. cancel_delayed_work(&priv->rfkill_poll);
  3975. iwl3945_dealloc_ucode_pci(priv);
  3976. if (priv->rxq.bd)
  3977. iwl_rx_queue_free(priv, &priv->rxq);
  3978. iwl3945_hw_txq_ctx_free(priv);
  3979. iwl3945_unset_hw_params(priv);
  3980. iwl3945_clear_stations_table(priv);
  3981. /*netif_stop_queue(dev); */
  3982. flush_workqueue(priv->workqueue);
  3983. /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
  3984. * priv->workqueue... so we can't take down the workqueue
  3985. * until now... */
  3986. destroy_workqueue(priv->workqueue);
  3987. priv->workqueue = NULL;
  3988. free_irq(pdev->irq, priv);
  3989. pci_disable_msi(pdev);
  3990. pci_iounmap(pdev, priv->hw_base);
  3991. pci_release_regions(pdev);
  3992. pci_disable_device(pdev);
  3993. pci_set_drvdata(pdev, NULL);
  3994. iwl_free_channel_map(priv);
  3995. iwlcore_free_geos(priv);
  3996. kfree(priv->scan);
  3997. if (priv->ibss_beacon)
  3998. dev_kfree_skb(priv->ibss_beacon);
  3999. ieee80211_free_hw(priv->hw);
  4000. }
  4001. /*****************************************************************************
  4002. *
  4003. * driver and module entry point
  4004. *
  4005. *****************************************************************************/
  4006. static struct pci_driver iwl3945_driver = {
  4007. .name = DRV_NAME,
  4008. .id_table = iwl3945_hw_card_ids,
  4009. .probe = iwl3945_pci_probe,
  4010. .remove = __devexit_p(iwl3945_pci_remove),
  4011. #ifdef CONFIG_PM
  4012. .suspend = iwl_pci_suspend,
  4013. .resume = iwl_pci_resume,
  4014. #endif
  4015. };
  4016. static int __init iwl3945_init(void)
  4017. {
  4018. int ret;
  4019. printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
  4020. printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
  4021. ret = iwl3945_rate_control_register();
  4022. if (ret) {
  4023. printk(KERN_ERR DRV_NAME
  4024. "Unable to register rate control algorithm: %d\n", ret);
  4025. return ret;
  4026. }
  4027. ret = pci_register_driver(&iwl3945_driver);
  4028. if (ret) {
  4029. printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
  4030. goto error_register;
  4031. }
  4032. return ret;
  4033. error_register:
  4034. iwl3945_rate_control_unregister();
  4035. return ret;
  4036. }
  4037. static void __exit iwl3945_exit(void)
  4038. {
  4039. pci_unregister_driver(&iwl3945_driver);
  4040. iwl3945_rate_control_unregister();
  4041. }
  4042. MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
  4043. module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
  4044. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  4045. module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
  4046. MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
  4047. module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
  4048. MODULE_PARM_DESC(swcrypto,
  4049. "using software crypto (default 1 [software])\n");
  4050. module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
  4051. MODULE_PARM_DESC(debug, "debug output mask");
  4052. module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
  4053. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
  4054. module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
  4055. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  4056. module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
  4057. MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
  4058. module_exit(iwl3945_exit);
  4059. module_init(iwl3945_init);