iwl3945-base.c 149 KB

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