iwl3945-base.c 148 KB

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