iwl3945-base.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci-aspm.h>
  35. #include <linux/slab.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/delay.h>
  38. #include <linux/sched.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/firmware.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/if_arp.h>
  44. #include <net/ieee80211_radiotap.h>
  45. #include <net/mac80211.h>
  46. #include <asm/div64.h>
  47. #define DRV_NAME "iwl3945"
  48. #include "iwl-fh.h"
  49. #include "iwl-3945-fh.h"
  50. #include "iwl-commands.h"
  51. #include "iwl-sta.h"
  52. #include "iwl-3945.h"
  53. #include "iwl-core.h"
  54. #include "iwl-helpers.h"
  55. #include "iwl-dev.h"
  56. #include "iwl-spectrum.h"
  57. /*
  58. * module name, copyright, version, etc.
  59. */
  60. #define DRV_DESCRIPTION \
  61. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  62. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  63. #define VD "d"
  64. #else
  65. #define VD
  66. #endif
  67. /*
  68. * add "s" to indicate spectrum measurement included.
  69. * we add it here to be consistent with previous releases in which
  70. * this was configurable.
  71. */
  72. #define DRV_VERSION IWLWIFI_VERSION VD "s"
  73. #define DRV_COPYRIGHT "Copyright(c) 2003-2011 Intel Corporation"
  74. #define DRV_AUTHOR "<ilw@linux.intel.com>"
  75. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  76. MODULE_VERSION(DRV_VERSION);
  77. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  78. MODULE_LICENSE("GPL");
  79. /* module parameters */
  80. struct iwl_mod_params iwl3945_mod_params = {
  81. .sw_crypto = 1,
  82. .restart_fw = 1,
  83. .disable_hw_scan = 1,
  84. /* the rest are 0 by default */
  85. };
  86. /**
  87. * iwl3945_get_antenna_flags - Get antenna flags for RXON command
  88. * @priv: eeprom and antenna fields are used to determine antenna flags
  89. *
  90. * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
  91. * iwl3945_mod_params.antenna specifies the antenna diversity mode:
  92. *
  93. * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
  94. * IWL_ANTENNA_MAIN - Force MAIN antenna
  95. * IWL_ANTENNA_AUX - Force AUX antenna
  96. */
  97. __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
  98. {
  99. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  100. switch (iwl3945_mod_params.antenna) {
  101. case IWL_ANTENNA_DIVERSITY:
  102. return 0;
  103. case IWL_ANTENNA_MAIN:
  104. if (eeprom->antenna_switch_type)
  105. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  106. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  107. case IWL_ANTENNA_AUX:
  108. if (eeprom->antenna_switch_type)
  109. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  110. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  111. }
  112. /* bad antenna selector value */
  113. IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
  114. iwl3945_mod_params.antenna);
  115. return 0; /* "diversity" is default if error */
  116. }
  117. static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  118. struct ieee80211_key_conf *keyconf,
  119. u8 sta_id)
  120. {
  121. unsigned long flags;
  122. __le16 key_flags = 0;
  123. int ret;
  124. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  125. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  126. if (sta_id == priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id)
  127. key_flags |= STA_KEY_MULTICAST_MSK;
  128. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  129. keyconf->hw_key_idx = keyconf->keyidx;
  130. key_flags &= ~STA_KEY_FLG_INVALID;
  131. spin_lock_irqsave(&priv->sta_lock, flags);
  132. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  133. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  134. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  135. keyconf->keylen);
  136. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  137. keyconf->keylen);
  138. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  139. == STA_KEY_FLG_NO_ENC)
  140. priv->stations[sta_id].sta.key.key_offset =
  141. iwl_legacy_get_free_ucode_key_index(priv);
  142. /* else, we are overriding an existing key => no need to allocated room
  143. * in uCode. */
  144. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  145. "no space for a new key");
  146. priv->stations[sta_id].sta.key.key_flags = key_flags;
  147. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  148. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  149. IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
  150. ret = iwl_legacy_send_add_sta(priv,
  151. &priv->stations[sta_id].sta, CMD_ASYNC);
  152. spin_unlock_irqrestore(&priv->sta_lock, flags);
  153. return ret;
  154. }
  155. static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  156. struct ieee80211_key_conf *keyconf,
  157. u8 sta_id)
  158. {
  159. return -EOPNOTSUPP;
  160. }
  161. static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
  162. struct ieee80211_key_conf *keyconf,
  163. u8 sta_id)
  164. {
  165. return -EOPNOTSUPP;
  166. }
  167. static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
  168. {
  169. unsigned long flags;
  170. struct iwl_legacy_addsta_cmd sta_cmd;
  171. spin_lock_irqsave(&priv->sta_lock, flags);
  172. memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
  173. memset(&priv->stations[sta_id].sta.key, 0,
  174. sizeof(struct iwl4965_keyinfo));
  175. priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  176. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  177. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  178. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_legacy_addsta_cmd));
  179. spin_unlock_irqrestore(&priv->sta_lock, flags);
  180. IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
  181. return iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  182. }
  183. static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
  184. struct ieee80211_key_conf *keyconf, u8 sta_id)
  185. {
  186. int ret = 0;
  187. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  188. switch (keyconf->cipher) {
  189. case WLAN_CIPHER_SUITE_CCMP:
  190. ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  191. break;
  192. case WLAN_CIPHER_SUITE_TKIP:
  193. ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  194. break;
  195. case WLAN_CIPHER_SUITE_WEP40:
  196. case WLAN_CIPHER_SUITE_WEP104:
  197. ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  198. break;
  199. default:
  200. IWL_ERR(priv, "Unknown alg: %s alg=%x\n", __func__,
  201. keyconf->cipher);
  202. ret = -EINVAL;
  203. }
  204. IWL_DEBUG_WEP(priv, "Set dynamic key: alg=%x len=%d idx=%d sta=%d ret=%d\n",
  205. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  206. sta_id, ret);
  207. return ret;
  208. }
  209. static int iwl3945_remove_static_key(struct iwl_priv *priv)
  210. {
  211. int ret = -EOPNOTSUPP;
  212. return ret;
  213. }
  214. static int iwl3945_set_static_key(struct iwl_priv *priv,
  215. struct ieee80211_key_conf *key)
  216. {
  217. if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  218. key->cipher == WLAN_CIPHER_SUITE_WEP104)
  219. return -EOPNOTSUPP;
  220. IWL_ERR(priv, "Static key invalid: cipher %x\n", key->cipher);
  221. return -EINVAL;
  222. }
  223. static void iwl3945_clear_free_frames(struct iwl_priv *priv)
  224. {
  225. struct list_head *element;
  226. IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
  227. priv->frames_count);
  228. while (!list_empty(&priv->free_frames)) {
  229. element = priv->free_frames.next;
  230. list_del(element);
  231. kfree(list_entry(element, struct iwl3945_frame, list));
  232. priv->frames_count--;
  233. }
  234. if (priv->frames_count) {
  235. IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
  236. priv->frames_count);
  237. priv->frames_count = 0;
  238. }
  239. }
  240. static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
  241. {
  242. struct iwl3945_frame *frame;
  243. struct list_head *element;
  244. if (list_empty(&priv->free_frames)) {
  245. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  246. if (!frame) {
  247. IWL_ERR(priv, "Could not allocate frame!\n");
  248. return NULL;
  249. }
  250. priv->frames_count++;
  251. return frame;
  252. }
  253. element = priv->free_frames.next;
  254. list_del(element);
  255. return list_entry(element, struct iwl3945_frame, list);
  256. }
  257. static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
  258. {
  259. memset(frame, 0, sizeof(*frame));
  260. list_add(&frame->list, &priv->free_frames);
  261. }
  262. unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
  263. struct ieee80211_hdr *hdr,
  264. int left)
  265. {
  266. if (!iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS) || !priv->beacon_skb)
  267. return 0;
  268. if (priv->beacon_skb->len > left)
  269. return 0;
  270. memcpy(hdr, priv->beacon_skb->data, priv->beacon_skb->len);
  271. return priv->beacon_skb->len;
  272. }
  273. static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
  274. {
  275. struct iwl3945_frame *frame;
  276. unsigned int frame_size;
  277. int rc;
  278. u8 rate;
  279. frame = iwl3945_get_free_frame(priv);
  280. if (!frame) {
  281. IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
  282. "command.\n");
  283. return -ENOMEM;
  284. }
  285. rate = iwl_legacy_get_lowest_plcp(priv,
  286. &priv->contexts[IWL_RXON_CTX_BSS]);
  287. frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
  288. rc = iwl_legacy_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  289. &frame->u.cmd[0]);
  290. iwl3945_free_frame(priv, frame);
  291. return rc;
  292. }
  293. static void iwl3945_unset_hw_params(struct iwl_priv *priv)
  294. {
  295. if (priv->_3945.shared_virt)
  296. dma_free_coherent(&priv->pci_dev->dev,
  297. sizeof(struct iwl3945_shared),
  298. priv->_3945.shared_virt,
  299. priv->_3945.shared_phys);
  300. }
  301. static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
  302. struct ieee80211_tx_info *info,
  303. struct iwl_device_cmd *cmd,
  304. struct sk_buff *skb_frag,
  305. int sta_id)
  306. {
  307. struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  308. struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
  309. tx_cmd->sec_ctl = 0;
  310. switch (keyinfo->cipher) {
  311. case WLAN_CIPHER_SUITE_CCMP:
  312. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  313. memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
  314. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  315. break;
  316. case WLAN_CIPHER_SUITE_TKIP:
  317. break;
  318. case WLAN_CIPHER_SUITE_WEP104:
  319. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  320. /* fall through */
  321. case WLAN_CIPHER_SUITE_WEP40:
  322. tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
  323. (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
  324. memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
  325. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  326. "with key %d\n", info->control.hw_key->hw_key_idx);
  327. break;
  328. default:
  329. IWL_ERR(priv, "Unknown encode cipher %x\n", keyinfo->cipher);
  330. break;
  331. }
  332. }
  333. /*
  334. * handle build REPLY_TX command notification.
  335. */
  336. static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
  337. struct iwl_device_cmd *cmd,
  338. struct ieee80211_tx_info *info,
  339. struct ieee80211_hdr *hdr, u8 std_id)
  340. {
  341. struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  342. __le32 tx_flags = tx_cmd->tx_flags;
  343. __le16 fc = hdr->frame_control;
  344. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  345. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  346. tx_flags |= TX_CMD_FLG_ACK_MSK;
  347. if (ieee80211_is_mgmt(fc))
  348. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  349. if (ieee80211_is_probe_resp(fc) &&
  350. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  351. tx_flags |= TX_CMD_FLG_TSF_MSK;
  352. } else {
  353. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  354. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  355. }
  356. tx_cmd->sta_id = std_id;
  357. if (ieee80211_has_morefrags(fc))
  358. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  359. if (ieee80211_is_data_qos(fc)) {
  360. u8 *qc = ieee80211_get_qos_ctl(hdr);
  361. tx_cmd->tid_tspec = qc[0] & 0xf;
  362. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  363. } else {
  364. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  365. }
  366. iwl_legacy_tx_cmd_protection(priv, info, fc, &tx_flags);
  367. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  368. if (ieee80211_is_mgmt(fc)) {
  369. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  370. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  371. else
  372. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  373. } else {
  374. tx_cmd->timeout.pm_frame_timeout = 0;
  375. }
  376. tx_cmd->driver_txop = 0;
  377. tx_cmd->tx_flags = tx_flags;
  378. tx_cmd->next_frame_len = 0;
  379. }
  380. /*
  381. * start REPLY_TX command process
  382. */
  383. static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  384. {
  385. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  386. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  387. struct iwl3945_tx_cmd *tx_cmd;
  388. struct iwl_tx_queue *txq = NULL;
  389. struct iwl_queue *q = NULL;
  390. struct iwl_device_cmd *out_cmd;
  391. struct iwl_cmd_meta *out_meta;
  392. dma_addr_t phys_addr;
  393. dma_addr_t txcmd_phys;
  394. int txq_id = skb_get_queue_mapping(skb);
  395. u16 len, idx, hdr_len;
  396. u8 id;
  397. u8 unicast;
  398. u8 sta_id;
  399. u8 tid = 0;
  400. __le16 fc;
  401. u8 wait_write_ptr = 0;
  402. unsigned long flags;
  403. spin_lock_irqsave(&priv->lock, flags);
  404. if (iwl_legacy_is_rfkill(priv)) {
  405. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  406. goto drop_unlock;
  407. }
  408. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
  409. IWL_ERR(priv, "ERROR: No TX rate available.\n");
  410. goto drop_unlock;
  411. }
  412. unicast = !is_multicast_ether_addr(hdr->addr1);
  413. id = 0;
  414. fc = hdr->frame_control;
  415. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  416. if (ieee80211_is_auth(fc))
  417. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  418. else if (ieee80211_is_assoc_req(fc))
  419. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  420. else if (ieee80211_is_reassoc_req(fc))
  421. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  422. #endif
  423. spin_unlock_irqrestore(&priv->lock, flags);
  424. hdr_len = ieee80211_hdrlen(fc);
  425. /* Find index into station table for destination station */
  426. sta_id = iwl_legacy_sta_id_or_broadcast(
  427. priv, &priv->contexts[IWL_RXON_CTX_BSS],
  428. info->control.sta);
  429. if (sta_id == IWL_INVALID_STATION) {
  430. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  431. hdr->addr1);
  432. goto drop;
  433. }
  434. IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
  435. if (ieee80211_is_data_qos(fc)) {
  436. u8 *qc = ieee80211_get_qos_ctl(hdr);
  437. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  438. if (unlikely(tid >= MAX_TID_COUNT))
  439. goto drop;
  440. }
  441. /* Descriptor for chosen Tx queue */
  442. txq = &priv->txq[txq_id];
  443. q = &txq->q;
  444. if ((iwl_legacy_queue_space(q) < q->high_mark))
  445. goto drop;
  446. spin_lock_irqsave(&priv->lock, flags);
  447. idx = iwl_legacy_get_cmd_index(q, q->write_ptr, 0);
  448. /* Set up driver data for this TFD */
  449. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  450. txq->txb[q->write_ptr].skb = skb;
  451. txq->txb[q->write_ptr].ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  452. /* Init first empty entry in queue's array of Tx/cmd buffers */
  453. out_cmd = txq->cmd[idx];
  454. out_meta = &txq->meta[idx];
  455. tx_cmd = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
  456. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  457. memset(tx_cmd, 0, sizeof(*tx_cmd));
  458. /*
  459. * Set up the Tx-command (not MAC!) header.
  460. * Store the chosen Tx queue and TFD index within the sequence field;
  461. * after Tx, uCode's Tx response will return this value so driver can
  462. * locate the frame within the tx queue and do post-tx processing.
  463. */
  464. out_cmd->hdr.cmd = REPLY_TX;
  465. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  466. INDEX_TO_SEQ(q->write_ptr)));
  467. /* Copy MAC header from skb into command buffer */
  468. memcpy(tx_cmd->hdr, hdr, hdr_len);
  469. if (info->control.hw_key)
  470. iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
  471. /* TODO need this for burst mode later on */
  472. iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
  473. /* set is_hcca to 0; it probably will never be implemented */
  474. iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
  475. /* Total # bytes to be transmitted */
  476. len = (u16)skb->len;
  477. tx_cmd->len = cpu_to_le16(len);
  478. iwl_legacy_dbg_log_tx_data_frame(priv, len, hdr);
  479. iwl_legacy_update_stats(priv, true, fc, len);
  480. tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  481. tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  482. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  483. txq->need_update = 1;
  484. } else {
  485. wait_write_ptr = 1;
  486. txq->need_update = 0;
  487. }
  488. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  489. le16_to_cpu(out_cmd->hdr.sequence));
  490. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  491. iwl_print_hex_dump(priv, IWL_DL_TX, tx_cmd, sizeof(*tx_cmd));
  492. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr,
  493. ieee80211_hdrlen(fc));
  494. /*
  495. * Use the first empty entry in this queue's command buffer array
  496. * to contain the Tx command and MAC header concatenated together
  497. * (payload data will be in another buffer).
  498. * Size of this varies, due to varying MAC header length.
  499. * If end is not dword aligned, we'll have 2 extra bytes at the end
  500. * of the MAC header (device reads on dword boundaries).
  501. * We'll tell device about this padding later.
  502. */
  503. len = sizeof(struct iwl3945_tx_cmd) +
  504. sizeof(struct iwl_cmd_header) + hdr_len;
  505. len = (len + 3) & ~3;
  506. /* Physical address of this Tx command's header (not MAC header!),
  507. * within command buffer array. */
  508. txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
  509. len, PCI_DMA_TODEVICE);
  510. /* we do not map meta data ... so we can safely access address to
  511. * provide to unmap command*/
  512. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  513. dma_unmap_len_set(out_meta, len, len);
  514. /* Add buffer containing Tx command and MAC(!) header to TFD's
  515. * first entry */
  516. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  517. txcmd_phys, len, 1, 0);
  518. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  519. * if any (802.11 null frames have no payload). */
  520. len = skb->len - hdr_len;
  521. if (len) {
  522. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  523. len, PCI_DMA_TODEVICE);
  524. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  525. phys_addr, len,
  526. 0, U32_PAD(len));
  527. }
  528. /* Tell device the write index *just past* this latest filled TFD */
  529. q->write_ptr = iwl_legacy_queue_inc_wrap(q->write_ptr, q->n_bd);
  530. iwl_legacy_txq_update_write_ptr(priv, txq);
  531. spin_unlock_irqrestore(&priv->lock, flags);
  532. if ((iwl_legacy_queue_space(q) < q->high_mark)
  533. && priv->mac80211_registered) {
  534. if (wait_write_ptr) {
  535. spin_lock_irqsave(&priv->lock, flags);
  536. txq->need_update = 1;
  537. iwl_legacy_txq_update_write_ptr(priv, txq);
  538. spin_unlock_irqrestore(&priv->lock, flags);
  539. }
  540. iwl_legacy_stop_queue(priv, txq);
  541. }
  542. return 0;
  543. drop_unlock:
  544. spin_unlock_irqrestore(&priv->lock, flags);
  545. drop:
  546. return -1;
  547. }
  548. static int iwl3945_get_measurement(struct iwl_priv *priv,
  549. struct ieee80211_measurement_params *params,
  550. u8 type)
  551. {
  552. struct iwl_spectrum_cmd spectrum;
  553. struct iwl_rx_packet *pkt;
  554. struct iwl_host_cmd cmd = {
  555. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  556. .data = (void *)&spectrum,
  557. .flags = CMD_WANT_SKB,
  558. };
  559. u32 add_time = le64_to_cpu(params->start_time);
  560. int rc;
  561. int spectrum_resp_status;
  562. int duration = le16_to_cpu(params->duration);
  563. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  564. if (iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS))
  565. add_time = iwl_legacy_usecs_to_beacons(priv,
  566. le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
  567. le16_to_cpu(ctx->timing.beacon_interval));
  568. memset(&spectrum, 0, sizeof(spectrum));
  569. spectrum.channel_count = cpu_to_le16(1);
  570. spectrum.flags =
  571. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  572. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  573. cmd.len = sizeof(spectrum);
  574. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  575. if (iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS))
  576. spectrum.start_time =
  577. iwl_legacy_add_beacon_time(priv,
  578. priv->_3945.last_beacon_time, add_time,
  579. le16_to_cpu(ctx->timing.beacon_interval));
  580. else
  581. spectrum.start_time = 0;
  582. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  583. spectrum.channels[0].channel = params->channel;
  584. spectrum.channels[0].type = type;
  585. if (ctx->active.flags & RXON_FLG_BAND_24G_MSK)
  586. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  587. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  588. rc = iwl_legacy_send_cmd_sync(priv, &cmd);
  589. if (rc)
  590. return rc;
  591. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  592. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  593. IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
  594. rc = -EIO;
  595. }
  596. spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
  597. switch (spectrum_resp_status) {
  598. case 0: /* Command will be handled */
  599. if (pkt->u.spectrum.id != 0xff) {
  600. IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
  601. pkt->u.spectrum.id);
  602. priv->measurement_status &= ~MEASUREMENT_READY;
  603. }
  604. priv->measurement_status |= MEASUREMENT_ACTIVE;
  605. rc = 0;
  606. break;
  607. case 1: /* Command will not be handled */
  608. rc = -EAGAIN;
  609. break;
  610. }
  611. iwl_legacy_free_pages(priv, cmd.reply_page);
  612. return rc;
  613. }
  614. static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
  615. struct iwl_rx_mem_buffer *rxb)
  616. {
  617. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  618. struct iwl_alive_resp *palive;
  619. struct delayed_work *pwork;
  620. palive = &pkt->u.alive_frame;
  621. IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
  622. "0x%01X 0x%01X\n",
  623. palive->is_valid, palive->ver_type,
  624. palive->ver_subtype);
  625. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  626. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  627. memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
  628. sizeof(struct iwl_alive_resp));
  629. pwork = &priv->init_alive_start;
  630. } else {
  631. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  632. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  633. sizeof(struct iwl_alive_resp));
  634. pwork = &priv->alive_start;
  635. iwl3945_disable_events(priv);
  636. }
  637. /* We delay the ALIVE response by 5ms to
  638. * give the HW RF Kill time to activate... */
  639. if (palive->is_valid == UCODE_VALID_OK)
  640. queue_delayed_work(priv->workqueue, pwork,
  641. msecs_to_jiffies(5));
  642. else
  643. IWL_WARN(priv, "uCode did not respond OK.\n");
  644. }
  645. static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
  646. struct iwl_rx_mem_buffer *rxb)
  647. {
  648. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  649. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  650. #endif
  651. IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  652. }
  653. static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
  654. struct iwl_rx_mem_buffer *rxb)
  655. {
  656. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  657. struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
  658. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  659. u8 rate = beacon->beacon_notify_hdr.rate;
  660. IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
  661. "tsf %d %d rate %d\n",
  662. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  663. beacon->beacon_notify_hdr.failure_frame,
  664. le32_to_cpu(beacon->ibss_mgr_status),
  665. le32_to_cpu(beacon->high_tsf),
  666. le32_to_cpu(beacon->low_tsf), rate);
  667. #endif
  668. priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
  669. }
  670. /* Handle notification from uCode that card's power state is changing
  671. * due to software, hardware, or critical temperature RFKILL */
  672. static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
  673. struct iwl_rx_mem_buffer *rxb)
  674. {
  675. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  676. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  677. unsigned long status = priv->status;
  678. IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
  679. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  680. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  681. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  682. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  683. if (flags & HW_CARD_DISABLED)
  684. set_bit(STATUS_RF_KILL_HW, &priv->status);
  685. else
  686. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  687. iwl_legacy_scan_cancel(priv);
  688. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  689. test_bit(STATUS_RF_KILL_HW, &priv->status)))
  690. wiphy_rfkill_set_hw_state(priv->hw->wiphy,
  691. test_bit(STATUS_RF_KILL_HW, &priv->status));
  692. else
  693. wake_up(&priv->wait_command_queue);
  694. }
  695. /**
  696. * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
  697. *
  698. * Setup the RX handlers for each of the reply types sent from the uCode
  699. * to the host.
  700. *
  701. * This function chains into the hardware specific files for them to setup
  702. * any hardware specific handlers as well.
  703. */
  704. static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
  705. {
  706. priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
  707. priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
  708. priv->rx_handlers[REPLY_ERROR] = iwl_legacy_rx_reply_error;
  709. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_legacy_rx_csa;
  710. priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  711. iwl_legacy_rx_spectrum_measure_notif;
  712. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_legacy_rx_pm_sleep_notif;
  713. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  714. iwl_legacy_rx_pm_debug_statistics_notif;
  715. priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
  716. /*
  717. * The same handler is used for both the REPLY to a discrete
  718. * statistics request from the host as well as for the periodic
  719. * statistics notifications (after received beacons) from the uCode.
  720. */
  721. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
  722. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
  723. iwl_legacy_setup_rx_scan_handlers(priv);
  724. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
  725. /* Set up hardware specific Rx handlers */
  726. iwl3945_hw_rx_handler_setup(priv);
  727. }
  728. /************************** RX-FUNCTIONS ****************************/
  729. /*
  730. * Rx theory of operation
  731. *
  732. * The host allocates 32 DMA target addresses and passes the host address
  733. * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  734. * 0 to 31
  735. *
  736. * Rx Queue Indexes
  737. * The host/firmware share two index registers for managing the Rx buffers.
  738. *
  739. * The READ index maps to the first position that the firmware may be writing
  740. * to -- the driver can read up to (but not including) this position and get
  741. * good data.
  742. * The READ index is managed by the firmware once the card is enabled.
  743. *
  744. * The WRITE index maps to the last position the driver has read from -- the
  745. * position preceding WRITE is the last slot the firmware can place a packet.
  746. *
  747. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  748. * WRITE = READ.
  749. *
  750. * During initialization, the host sets up the READ queue position to the first
  751. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  752. *
  753. * When the firmware places a packet in a buffer, it will advance the READ index
  754. * and fire the RX interrupt. The driver can then query the READ index and
  755. * process as many packets as possible, moving the WRITE index forward as it
  756. * resets the Rx queue buffers with new memory.
  757. *
  758. * The management in the driver is as follows:
  759. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  760. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  761. * to replenish the iwl->rxq->rx_free.
  762. * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
  763. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  764. * 'processed' and 'read' driver indexes as well)
  765. * + A received packet is processed and handed to the kernel network stack,
  766. * detached from the iwl->rxq. The driver 'processed' index is updated.
  767. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  768. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  769. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  770. * were enough free buffers and RX_STALLED is set it is cleared.
  771. *
  772. *
  773. * Driver sequence:
  774. *
  775. * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
  776. * iwl3945_rx_queue_restock
  777. * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
  778. * queue, updates firmware pointers, and updates
  779. * the WRITE index. If insufficient rx_free buffers
  780. * are available, schedules iwl3945_rx_replenish
  781. *
  782. * -- enable interrupts --
  783. * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
  784. * READ INDEX, detaching the SKB from the pool.
  785. * Moves the packet buffer from queue to rx_used.
  786. * Calls iwl3945_rx_queue_restock to refill any empty
  787. * slots.
  788. * ...
  789. *
  790. */
  791. /**
  792. * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  793. */
  794. static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
  795. dma_addr_t dma_addr)
  796. {
  797. return cpu_to_le32((u32)dma_addr);
  798. }
  799. /**
  800. * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
  801. *
  802. * If there are slots in the RX queue that need to be restocked,
  803. * and we have free pre-allocated buffers, fill the ranks as much
  804. * as we can, pulling from rx_free.
  805. *
  806. * This moves the 'write' index forward to catch up with 'processed', and
  807. * also updates the memory address in the firmware to reference the new
  808. * target buffer.
  809. */
  810. static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
  811. {
  812. struct iwl_rx_queue *rxq = &priv->rxq;
  813. struct list_head *element;
  814. struct iwl_rx_mem_buffer *rxb;
  815. unsigned long flags;
  816. int write;
  817. spin_lock_irqsave(&rxq->lock, flags);
  818. write = rxq->write & ~0x7;
  819. while ((iwl_legacy_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  820. /* Get next free Rx buffer, remove from free list */
  821. element = rxq->rx_free.next;
  822. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  823. list_del(element);
  824. /* Point to Rx buffer via next RBD in circular buffer */
  825. rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
  826. rxq->queue[rxq->write] = rxb;
  827. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  828. rxq->free_count--;
  829. }
  830. spin_unlock_irqrestore(&rxq->lock, flags);
  831. /* If the pre-allocated buffer pool is dropping low, schedule to
  832. * refill it */
  833. if (rxq->free_count <= RX_LOW_WATERMARK)
  834. queue_work(priv->workqueue, &priv->rx_replenish);
  835. /* If we've added more space for the firmware to place data, tell it.
  836. * Increment device's write pointer in multiples of 8. */
  837. if ((rxq->write_actual != (rxq->write & ~0x7))
  838. || (abs(rxq->write - rxq->read) > 7)) {
  839. spin_lock_irqsave(&rxq->lock, flags);
  840. rxq->need_update = 1;
  841. spin_unlock_irqrestore(&rxq->lock, flags);
  842. iwl_legacy_rx_queue_update_write_ptr(priv, rxq);
  843. }
  844. }
  845. /**
  846. * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
  847. *
  848. * When moving to rx_free an SKB is allocated for the slot.
  849. *
  850. * Also restock the Rx queue via iwl3945_rx_queue_restock.
  851. * This is called as a scheduled work item (except for during initialization)
  852. */
  853. static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  854. {
  855. struct iwl_rx_queue *rxq = &priv->rxq;
  856. struct list_head *element;
  857. struct iwl_rx_mem_buffer *rxb;
  858. struct page *page;
  859. unsigned long flags;
  860. gfp_t gfp_mask = priority;
  861. while (1) {
  862. spin_lock_irqsave(&rxq->lock, flags);
  863. if (list_empty(&rxq->rx_used)) {
  864. spin_unlock_irqrestore(&rxq->lock, flags);
  865. return;
  866. }
  867. spin_unlock_irqrestore(&rxq->lock, flags);
  868. if (rxq->free_count > RX_LOW_WATERMARK)
  869. gfp_mask |= __GFP_NOWARN;
  870. if (priv->hw_params.rx_page_order > 0)
  871. gfp_mask |= __GFP_COMP;
  872. /* Alloc a new receive buffer */
  873. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  874. if (!page) {
  875. if (net_ratelimit())
  876. IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
  877. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  878. net_ratelimit())
  879. IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
  880. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  881. rxq->free_count);
  882. /* We don't reschedule replenish work here -- we will
  883. * call the restock method and if it still needs
  884. * more buffers it will schedule replenish */
  885. break;
  886. }
  887. spin_lock_irqsave(&rxq->lock, flags);
  888. if (list_empty(&rxq->rx_used)) {
  889. spin_unlock_irqrestore(&rxq->lock, flags);
  890. __free_pages(page, priv->hw_params.rx_page_order);
  891. return;
  892. }
  893. element = rxq->rx_used.next;
  894. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  895. list_del(element);
  896. spin_unlock_irqrestore(&rxq->lock, flags);
  897. rxb->page = page;
  898. /* Get physical address of RB/SKB */
  899. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  900. PAGE_SIZE << priv->hw_params.rx_page_order,
  901. PCI_DMA_FROMDEVICE);
  902. spin_lock_irqsave(&rxq->lock, flags);
  903. list_add_tail(&rxb->list, &rxq->rx_free);
  904. rxq->free_count++;
  905. priv->alloc_rxb_page++;
  906. spin_unlock_irqrestore(&rxq->lock, flags);
  907. }
  908. }
  909. void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  910. {
  911. unsigned long flags;
  912. int i;
  913. spin_lock_irqsave(&rxq->lock, flags);
  914. INIT_LIST_HEAD(&rxq->rx_free);
  915. INIT_LIST_HEAD(&rxq->rx_used);
  916. /* Fill the rx_used queue with _all_ of the Rx buffers */
  917. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  918. /* In the reset function, these buffers may have been allocated
  919. * to an SKB, so we need to unmap and free potential storage */
  920. if (rxq->pool[i].page != NULL) {
  921. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  922. PAGE_SIZE << priv->hw_params.rx_page_order,
  923. PCI_DMA_FROMDEVICE);
  924. __iwl_legacy_free_pages(priv, rxq->pool[i].page);
  925. rxq->pool[i].page = NULL;
  926. }
  927. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  928. }
  929. /* Set us so that we have processed and used all buffers, but have
  930. * not restocked the Rx queue with fresh buffers */
  931. rxq->read = rxq->write = 0;
  932. rxq->write_actual = 0;
  933. rxq->free_count = 0;
  934. spin_unlock_irqrestore(&rxq->lock, flags);
  935. }
  936. void iwl3945_rx_replenish(void *data)
  937. {
  938. struct iwl_priv *priv = data;
  939. unsigned long flags;
  940. iwl3945_rx_allocate(priv, GFP_KERNEL);
  941. spin_lock_irqsave(&priv->lock, flags);
  942. iwl3945_rx_queue_restock(priv);
  943. spin_unlock_irqrestore(&priv->lock, flags);
  944. }
  945. static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
  946. {
  947. iwl3945_rx_allocate(priv, GFP_ATOMIC);
  948. iwl3945_rx_queue_restock(priv);
  949. }
  950. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  951. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  952. * This free routine walks the list of POOL entries and if SKB is set to
  953. * non NULL it is unmapped and freed
  954. */
  955. static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  956. {
  957. int i;
  958. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  959. if (rxq->pool[i].page != NULL) {
  960. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  961. PAGE_SIZE << priv->hw_params.rx_page_order,
  962. PCI_DMA_FROMDEVICE);
  963. __iwl_legacy_free_pages(priv, rxq->pool[i].page);
  964. rxq->pool[i].page = NULL;
  965. }
  966. }
  967. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  968. rxq->bd_dma);
  969. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  970. rxq->rb_stts, rxq->rb_stts_dma);
  971. rxq->bd = NULL;
  972. rxq->rb_stts = NULL;
  973. }
  974. /* Convert linear signal-to-noise ratio into dB */
  975. static u8 ratio2dB[100] = {
  976. /* 0 1 2 3 4 5 6 7 8 9 */
  977. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  978. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  979. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  980. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  981. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  982. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  983. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  984. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  985. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  986. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  987. };
  988. /* Calculates a relative dB value from a ratio of linear
  989. * (i.e. not dB) signal levels.
  990. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  991. int iwl3945_calc_db_from_ratio(int sig_ratio)
  992. {
  993. /* 1000:1 or higher just report as 60 dB */
  994. if (sig_ratio >= 1000)
  995. return 60;
  996. /* 100:1 or higher, divide by 10 and use table,
  997. * add 20 dB to make up for divide by 10 */
  998. if (sig_ratio >= 100)
  999. return 20 + (int)ratio2dB[sig_ratio/10];
  1000. /* We shouldn't see this */
  1001. if (sig_ratio < 1)
  1002. return 0;
  1003. /* Use table for ratios 1:1 - 99:1 */
  1004. return (int)ratio2dB[sig_ratio];
  1005. }
  1006. /**
  1007. * iwl3945_rx_handle - Main entry function for receiving responses from uCode
  1008. *
  1009. * Uses the priv->rx_handlers callback function array to invoke
  1010. * the appropriate handlers, including command responses,
  1011. * frame-received notifications, and other notifications.
  1012. */
  1013. static void iwl3945_rx_handle(struct iwl_priv *priv)
  1014. {
  1015. struct iwl_rx_mem_buffer *rxb;
  1016. struct iwl_rx_packet *pkt;
  1017. struct iwl_rx_queue *rxq = &priv->rxq;
  1018. u32 r, i;
  1019. int reclaim;
  1020. unsigned long flags;
  1021. u8 fill_rx = 0;
  1022. u32 count = 8;
  1023. int total_empty = 0;
  1024. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  1025. * buffer that the driver may process (last buffer filled by ucode). */
  1026. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  1027. i = rxq->read;
  1028. /* calculate total frames need to be restock after handling RX */
  1029. total_empty = r - rxq->write_actual;
  1030. if (total_empty < 0)
  1031. total_empty += RX_QUEUE_SIZE;
  1032. if (total_empty > (RX_QUEUE_SIZE / 2))
  1033. fill_rx = 1;
  1034. /* Rx interrupt, but nothing sent from uCode */
  1035. if (i == r)
  1036. IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
  1037. while (i != r) {
  1038. int len;
  1039. rxb = rxq->queue[i];
  1040. /* If an RXB doesn't have a Rx queue slot associated with it,
  1041. * then a bug has been introduced in the queue refilling
  1042. * routines -- catch it here */
  1043. BUG_ON(rxb == NULL);
  1044. rxq->queue[i] = NULL;
  1045. pci_unmap_page(priv->pci_dev, rxb->page_dma,
  1046. PAGE_SIZE << priv->hw_params.rx_page_order,
  1047. PCI_DMA_FROMDEVICE);
  1048. pkt = rxb_addr(rxb);
  1049. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  1050. len += sizeof(u32); /* account for status word */
  1051. trace_iwlwifi_legacy_dev_rx(priv, pkt, len);
  1052. /* Reclaim a command buffer only if this packet is a response
  1053. * to a (driver-originated) command.
  1054. * If the packet (e.g. Rx frame) originated from uCode,
  1055. * there is no command buffer to reclaim.
  1056. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  1057. * but apparently a few don't get set; catch them here. */
  1058. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  1059. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  1060. (pkt->hdr.cmd != REPLY_TX);
  1061. /* Based on type of command response or notification,
  1062. * handle those that need handling via function in
  1063. * rx_handlers table. See iwl3945_setup_rx_handlers() */
  1064. if (priv->rx_handlers[pkt->hdr.cmd]) {
  1065. IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
  1066. iwl_legacy_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  1067. priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
  1068. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  1069. } else {
  1070. /* No handling needed */
  1071. IWL_DEBUG_RX(priv,
  1072. "r %d i %d No handler needed for %s, 0x%02x\n",
  1073. r, i, iwl_legacy_get_cmd_string(pkt->hdr.cmd),
  1074. pkt->hdr.cmd);
  1075. }
  1076. /*
  1077. * XXX: After here, we should always check rxb->page
  1078. * against NULL before touching it or its virtual
  1079. * memory (pkt). Because some rx_handler might have
  1080. * already taken or freed the pages.
  1081. */
  1082. if (reclaim) {
  1083. /* Invoke any callbacks, transfer the buffer to caller,
  1084. * and fire off the (possibly) blocking iwl_legacy_send_cmd()
  1085. * as we reclaim the driver command queue */
  1086. if (rxb->page)
  1087. iwl_legacy_tx_cmd_complete(priv, rxb);
  1088. else
  1089. IWL_WARN(priv, "Claim null rxb?\n");
  1090. }
  1091. /* Reuse the page if possible. For notification packets and
  1092. * SKBs that fail to Rx correctly, add them back into the
  1093. * rx_free list for reuse later. */
  1094. spin_lock_irqsave(&rxq->lock, flags);
  1095. if (rxb->page != NULL) {
  1096. rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
  1097. 0, PAGE_SIZE << priv->hw_params.rx_page_order,
  1098. PCI_DMA_FROMDEVICE);
  1099. list_add_tail(&rxb->list, &rxq->rx_free);
  1100. rxq->free_count++;
  1101. } else
  1102. list_add_tail(&rxb->list, &rxq->rx_used);
  1103. spin_unlock_irqrestore(&rxq->lock, flags);
  1104. i = (i + 1) & RX_QUEUE_MASK;
  1105. /* If there are a lot of unused frames,
  1106. * restock the Rx queue so ucode won't assert. */
  1107. if (fill_rx) {
  1108. count++;
  1109. if (count >= 8) {
  1110. rxq->read = i;
  1111. iwl3945_rx_replenish_now(priv);
  1112. count = 0;
  1113. }
  1114. }
  1115. }
  1116. /* Backtrack one entry */
  1117. rxq->read = i;
  1118. if (fill_rx)
  1119. iwl3945_rx_replenish_now(priv);
  1120. else
  1121. iwl3945_rx_queue_restock(priv);
  1122. }
  1123. /* call this function to flush any scheduled tasklet */
  1124. static inline void iwl3945_synchronize_irq(struct iwl_priv *priv)
  1125. {
  1126. /* wait to make sure we flush pending tasklet*/
  1127. synchronize_irq(priv->pci_dev->irq);
  1128. tasklet_kill(&priv->irq_tasklet);
  1129. }
  1130. static const char *iwl3945_desc_lookup(int i)
  1131. {
  1132. switch (i) {
  1133. case 1:
  1134. return "FAIL";
  1135. case 2:
  1136. return "BAD_PARAM";
  1137. case 3:
  1138. return "BAD_CHECKSUM";
  1139. case 4:
  1140. return "NMI_INTERRUPT";
  1141. case 5:
  1142. return "SYSASSERT";
  1143. case 6:
  1144. return "FATAL_ERROR";
  1145. }
  1146. return "UNKNOWN";
  1147. }
  1148. #define ERROR_START_OFFSET (1 * sizeof(u32))
  1149. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  1150. void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
  1151. {
  1152. u32 i;
  1153. u32 desc, time, count, base, data1;
  1154. u32 blink1, blink2, ilink1, ilink2;
  1155. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  1156. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  1157. IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
  1158. return;
  1159. }
  1160. count = iwl_legacy_read_targ_mem(priv, base);
  1161. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  1162. IWL_ERR(priv, "Start IWL Error Log Dump:\n");
  1163. IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
  1164. priv->status, count);
  1165. }
  1166. IWL_ERR(priv, "Desc Time asrtPC blink2 "
  1167. "ilink1 nmiPC Line\n");
  1168. for (i = ERROR_START_OFFSET;
  1169. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  1170. i += ERROR_ELEM_SIZE) {
  1171. desc = iwl_legacy_read_targ_mem(priv, base + i);
  1172. time =
  1173. iwl_legacy_read_targ_mem(priv, base + i + 1 * sizeof(u32));
  1174. blink1 =
  1175. iwl_legacy_read_targ_mem(priv, base + i + 2 * sizeof(u32));
  1176. blink2 =
  1177. iwl_legacy_read_targ_mem(priv, base + i + 3 * sizeof(u32));
  1178. ilink1 =
  1179. iwl_legacy_read_targ_mem(priv, base + i + 4 * sizeof(u32));
  1180. ilink2 =
  1181. iwl_legacy_read_targ_mem(priv, base + i + 5 * sizeof(u32));
  1182. data1 =
  1183. iwl_legacy_read_targ_mem(priv, base + i + 6 * sizeof(u32));
  1184. IWL_ERR(priv,
  1185. "%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  1186. iwl3945_desc_lookup(desc), desc, time, blink1, blink2,
  1187. ilink1, ilink2, data1);
  1188. trace_iwlwifi_legacy_dev_ucode_error(priv, desc, time, data1, 0,
  1189. 0, blink1, blink2, ilink1, ilink2);
  1190. }
  1191. }
  1192. static void iwl3945_irq_tasklet(struct iwl_priv *priv)
  1193. {
  1194. u32 inta, handled = 0;
  1195. u32 inta_fh;
  1196. unsigned long flags;
  1197. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1198. u32 inta_mask;
  1199. #endif
  1200. spin_lock_irqsave(&priv->lock, flags);
  1201. /* Ack/clear/reset pending uCode interrupts.
  1202. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  1203. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  1204. inta = iwl_read32(priv, CSR_INT);
  1205. iwl_write32(priv, CSR_INT, inta);
  1206. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  1207. * Any new interrupts that happen after this, either while we're
  1208. * in this tasklet, or later, will show up in next ISR/tasklet. */
  1209. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1210. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  1211. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1212. if (iwl_legacy_get_debug_level(priv) & IWL_DL_ISR) {
  1213. /* just for debug */
  1214. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1215. IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  1216. inta, inta_mask, inta_fh);
  1217. }
  1218. #endif
  1219. spin_unlock_irqrestore(&priv->lock, flags);
  1220. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  1221. * atomic, make sure that inta covers all the interrupts that
  1222. * we've discovered, even if FH interrupt came in just after
  1223. * reading CSR_INT. */
  1224. if (inta_fh & CSR39_FH_INT_RX_MASK)
  1225. inta |= CSR_INT_BIT_FH_RX;
  1226. if (inta_fh & CSR39_FH_INT_TX_MASK)
  1227. inta |= CSR_INT_BIT_FH_TX;
  1228. /* Now service all interrupt bits discovered above. */
  1229. if (inta & CSR_INT_BIT_HW_ERR) {
  1230. IWL_ERR(priv, "Hardware error detected. Restarting.\n");
  1231. /* Tell the device to stop sending interrupts */
  1232. iwl_legacy_disable_interrupts(priv);
  1233. priv->isr_stats.hw++;
  1234. iwl_legacy_irq_handle_error(priv);
  1235. handled |= CSR_INT_BIT_HW_ERR;
  1236. return;
  1237. }
  1238. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1239. if (iwl_legacy_get_debug_level(priv) & (IWL_DL_ISR)) {
  1240. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  1241. if (inta & CSR_INT_BIT_SCD) {
  1242. IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
  1243. "the frame/frames.\n");
  1244. priv->isr_stats.sch++;
  1245. }
  1246. /* Alive notification via Rx interrupt will do the real work */
  1247. if (inta & CSR_INT_BIT_ALIVE) {
  1248. IWL_DEBUG_ISR(priv, "Alive interrupt\n");
  1249. priv->isr_stats.alive++;
  1250. }
  1251. }
  1252. #endif
  1253. /* Safely ignore these bits for debug checks below */
  1254. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  1255. /* Error detected by uCode */
  1256. if (inta & CSR_INT_BIT_SW_ERR) {
  1257. IWL_ERR(priv, "Microcode SW error detected. "
  1258. "Restarting 0x%X.\n", inta);
  1259. priv->isr_stats.sw++;
  1260. iwl_legacy_irq_handle_error(priv);
  1261. handled |= CSR_INT_BIT_SW_ERR;
  1262. }
  1263. /* uCode wakes up after power-down sleep */
  1264. if (inta & CSR_INT_BIT_WAKEUP) {
  1265. IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
  1266. iwl_legacy_rx_queue_update_write_ptr(priv, &priv->rxq);
  1267. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[0]);
  1268. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[1]);
  1269. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[2]);
  1270. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[3]);
  1271. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[4]);
  1272. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[5]);
  1273. priv->isr_stats.wakeup++;
  1274. handled |= CSR_INT_BIT_WAKEUP;
  1275. }
  1276. /* All uCode command responses, including Tx command responses,
  1277. * Rx "responses" (frame-received notification), and other
  1278. * notifications from uCode come through here*/
  1279. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  1280. iwl3945_rx_handle(priv);
  1281. priv->isr_stats.rx++;
  1282. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  1283. }
  1284. if (inta & CSR_INT_BIT_FH_TX) {
  1285. IWL_DEBUG_ISR(priv, "Tx interrupt\n");
  1286. priv->isr_stats.tx++;
  1287. iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
  1288. iwl_legacy_write_direct32(priv, FH39_TCSR_CREDIT
  1289. (FH39_SRVC_CHNL), 0x0);
  1290. handled |= CSR_INT_BIT_FH_TX;
  1291. }
  1292. if (inta & ~handled) {
  1293. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  1294. priv->isr_stats.unhandled++;
  1295. }
  1296. if (inta & ~priv->inta_mask) {
  1297. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  1298. inta & ~priv->inta_mask);
  1299. IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
  1300. }
  1301. /* Re-enable all interrupts */
  1302. /* only Re-enable if disabled by irq */
  1303. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  1304. iwl_legacy_enable_interrupts(priv);
  1305. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1306. if (iwl_legacy_get_debug_level(priv) & (IWL_DL_ISR)) {
  1307. inta = iwl_read32(priv, CSR_INT);
  1308. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1309. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1310. IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  1311. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  1312. }
  1313. #endif
  1314. }
  1315. static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
  1316. enum ieee80211_band band,
  1317. u8 is_active, u8 n_probes,
  1318. struct iwl3945_scan_channel *scan_ch,
  1319. struct ieee80211_vif *vif)
  1320. {
  1321. struct ieee80211_channel *chan;
  1322. const struct ieee80211_supported_band *sband;
  1323. const struct iwl_channel_info *ch_info;
  1324. u16 passive_dwell = 0;
  1325. u16 active_dwell = 0;
  1326. int added, i;
  1327. sband = iwl_get_hw_mode(priv, band);
  1328. if (!sband)
  1329. return 0;
  1330. active_dwell = iwl_legacy_get_active_dwell_time(priv, band, n_probes);
  1331. passive_dwell = iwl_legacy_get_passive_dwell_time(priv, band, vif);
  1332. if (passive_dwell <= active_dwell)
  1333. passive_dwell = active_dwell + 1;
  1334. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  1335. chan = priv->scan_request->channels[i];
  1336. if (chan->band != band)
  1337. continue;
  1338. scan_ch->channel = chan->hw_value;
  1339. ch_info = iwl_legacy_get_channel_info(priv, band,
  1340. scan_ch->channel);
  1341. if (!iwl_legacy_is_channel_valid(ch_info)) {
  1342. IWL_DEBUG_SCAN(priv,
  1343. "Channel %d is INVALID for this band.\n",
  1344. scan_ch->channel);
  1345. continue;
  1346. }
  1347. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  1348. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1349. /* If passive , set up for auto-switch
  1350. * and use long active_dwell time.
  1351. */
  1352. if (!is_active || iwl_legacy_is_channel_passive(ch_info) ||
  1353. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
  1354. scan_ch->type = 0; /* passive */
  1355. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  1356. scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
  1357. } else {
  1358. scan_ch->type = 1; /* active */
  1359. }
  1360. /* Set direct probe bits. These may be used both for active
  1361. * scan channels (probes gets sent right away),
  1362. * or for passive channels (probes get se sent only after
  1363. * hearing clear Rx packet).*/
  1364. if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
  1365. if (n_probes)
  1366. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  1367. } else {
  1368. /* uCode v1 does not allow setting direct probe bits on
  1369. * passive channel. */
  1370. if ((scan_ch->type & 1) && n_probes)
  1371. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  1372. }
  1373. /* Set txpower levels to defaults */
  1374. scan_ch->tpc.dsp_atten = 110;
  1375. /* scan_pwr_info->tpc.dsp_atten; */
  1376. /*scan_pwr_info->tpc.tx_gain; */
  1377. if (band == IEEE80211_BAND_5GHZ)
  1378. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1379. else {
  1380. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  1381. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  1382. * power level:
  1383. * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
  1384. */
  1385. }
  1386. IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
  1387. scan_ch->channel,
  1388. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  1389. (scan_ch->type & 1) ?
  1390. active_dwell : passive_dwell);
  1391. scan_ch++;
  1392. added++;
  1393. }
  1394. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  1395. return added;
  1396. }
  1397. static void iwl3945_init_hw_rates(struct iwl_priv *priv,
  1398. struct ieee80211_rate *rates)
  1399. {
  1400. int i;
  1401. for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
  1402. rates[i].bitrate = iwl3945_rates[i].ieee * 5;
  1403. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  1404. rates[i].hw_value_short = i;
  1405. rates[i].flags = 0;
  1406. if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  1407. /*
  1408. * If CCK != 1M then set short preamble rate flag.
  1409. */
  1410. rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
  1411. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  1412. }
  1413. }
  1414. }
  1415. /******************************************************************************
  1416. *
  1417. * uCode download functions
  1418. *
  1419. ******************************************************************************/
  1420. static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
  1421. {
  1422. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_code);
  1423. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_data);
  1424. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  1425. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_init);
  1426. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  1427. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
  1428. }
  1429. /**
  1430. * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
  1431. * looking at all data.
  1432. */
  1433. static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
  1434. {
  1435. u32 val;
  1436. u32 save_len = len;
  1437. int rc = 0;
  1438. u32 errcnt;
  1439. IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
  1440. iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  1441. IWL39_RTC_INST_LOWER_BOUND);
  1442. errcnt = 0;
  1443. for (; len > 0; len -= sizeof(u32), image++) {
  1444. /* read data comes through single port, auto-incr addr */
  1445. /* NOTE: Use the debugless read so we don't flood kernel log
  1446. * if IWL_DL_IO is set */
  1447. val = _iwl_legacy_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  1448. if (val != le32_to_cpu(*image)) {
  1449. IWL_ERR(priv, "uCode INST section is invalid at "
  1450. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  1451. save_len - len, val, le32_to_cpu(*image));
  1452. rc = -EIO;
  1453. errcnt++;
  1454. if (errcnt >= 20)
  1455. break;
  1456. }
  1457. }
  1458. if (!errcnt)
  1459. IWL_DEBUG_INFO(priv,
  1460. "ucode image in INSTRUCTION memory is good\n");
  1461. return rc;
  1462. }
  1463. /**
  1464. * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
  1465. * using sample data 100 bytes apart. If these sample points are good,
  1466. * it's a pretty good bet that everything between them is good, too.
  1467. */
  1468. static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  1469. {
  1470. u32 val;
  1471. int rc = 0;
  1472. u32 errcnt = 0;
  1473. u32 i;
  1474. IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
  1475. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  1476. /* read data comes through single port, auto-incr addr */
  1477. /* NOTE: Use the debugless read so we don't flood kernel log
  1478. * if IWL_DL_IO is set */
  1479. iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  1480. i + IWL39_RTC_INST_LOWER_BOUND);
  1481. val = _iwl_legacy_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  1482. if (val != le32_to_cpu(*image)) {
  1483. #if 0 /* Enable this if you want to see details */
  1484. IWL_ERR(priv, "uCode INST section is invalid at "
  1485. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  1486. i, val, *image);
  1487. #endif
  1488. rc = -EIO;
  1489. errcnt++;
  1490. if (errcnt >= 3)
  1491. break;
  1492. }
  1493. }
  1494. return rc;
  1495. }
  1496. /**
  1497. * iwl3945_verify_ucode - determine which instruction image is in SRAM,
  1498. * and verify its contents
  1499. */
  1500. static int iwl3945_verify_ucode(struct iwl_priv *priv)
  1501. {
  1502. __le32 *image;
  1503. u32 len;
  1504. int rc = 0;
  1505. /* Try bootstrap */
  1506. image = (__le32 *)priv->ucode_boot.v_addr;
  1507. len = priv->ucode_boot.len;
  1508. rc = iwl3945_verify_inst_sparse(priv, image, len);
  1509. if (rc == 0) {
  1510. IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
  1511. return 0;
  1512. }
  1513. /* Try initialize */
  1514. image = (__le32 *)priv->ucode_init.v_addr;
  1515. len = priv->ucode_init.len;
  1516. rc = iwl3945_verify_inst_sparse(priv, image, len);
  1517. if (rc == 0) {
  1518. IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
  1519. return 0;
  1520. }
  1521. /* Try runtime/protocol */
  1522. image = (__le32 *)priv->ucode_code.v_addr;
  1523. len = priv->ucode_code.len;
  1524. rc = iwl3945_verify_inst_sparse(priv, image, len);
  1525. if (rc == 0) {
  1526. IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
  1527. return 0;
  1528. }
  1529. IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  1530. /* Since nothing seems to match, show first several data entries in
  1531. * instruction SRAM, so maybe visual inspection will give a clue.
  1532. * Selection of bootstrap image (vs. other images) is arbitrary. */
  1533. image = (__le32 *)priv->ucode_boot.v_addr;
  1534. len = priv->ucode_boot.len;
  1535. rc = iwl3945_verify_inst_full(priv, image, len);
  1536. return rc;
  1537. }
  1538. static void iwl3945_nic_start(struct iwl_priv *priv)
  1539. {
  1540. /* Remove all resets to allow NIC to operate */
  1541. iwl_write32(priv, CSR_RESET, 0);
  1542. }
  1543. #define IWL3945_UCODE_GET(item) \
  1544. static u32 iwl3945_ucode_get_##item(const struct iwl_ucode_header *ucode)\
  1545. { \
  1546. return le32_to_cpu(ucode->v1.item); \
  1547. }
  1548. static u32 iwl3945_ucode_get_header_size(u32 api_ver)
  1549. {
  1550. return 24;
  1551. }
  1552. static u8 *iwl3945_ucode_get_data(const struct iwl_ucode_header *ucode)
  1553. {
  1554. return (u8 *) ucode->v1.data;
  1555. }
  1556. IWL3945_UCODE_GET(inst_size);
  1557. IWL3945_UCODE_GET(data_size);
  1558. IWL3945_UCODE_GET(init_size);
  1559. IWL3945_UCODE_GET(init_data_size);
  1560. IWL3945_UCODE_GET(boot_size);
  1561. /**
  1562. * iwl3945_read_ucode - Read uCode images from disk file.
  1563. *
  1564. * Copy into buffers for card to fetch via bus-mastering
  1565. */
  1566. static int iwl3945_read_ucode(struct iwl_priv *priv)
  1567. {
  1568. const struct iwl_ucode_header *ucode;
  1569. int ret = -EINVAL, index;
  1570. const struct firmware *ucode_raw;
  1571. /* firmware file name contains uCode/driver compatibility version */
  1572. const char *name_pre = priv->cfg->fw_name_pre;
  1573. const unsigned int api_max = priv->cfg->ucode_api_max;
  1574. const unsigned int api_min = priv->cfg->ucode_api_min;
  1575. char buf[25];
  1576. u8 *src;
  1577. size_t len;
  1578. u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
  1579. /* Ask kernel firmware_class module to get the boot firmware off disk.
  1580. * request_firmware() is synchronous, file is in memory on return. */
  1581. for (index = api_max; index >= api_min; index--) {
  1582. sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
  1583. ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
  1584. if (ret < 0) {
  1585. IWL_ERR(priv, "%s firmware file req failed: %d\n",
  1586. buf, ret);
  1587. if (ret == -ENOENT)
  1588. continue;
  1589. else
  1590. goto error;
  1591. } else {
  1592. if (index < api_max)
  1593. IWL_ERR(priv, "Loaded firmware %s, "
  1594. "which is deprecated. "
  1595. " Please use API v%u instead.\n",
  1596. buf, api_max);
  1597. IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
  1598. "(%zd bytes) from disk\n",
  1599. buf, ucode_raw->size);
  1600. break;
  1601. }
  1602. }
  1603. if (ret < 0)
  1604. goto error;
  1605. /* Make sure that we got at least our header! */
  1606. if (ucode_raw->size < iwl3945_ucode_get_header_size(1)) {
  1607. IWL_ERR(priv, "File size way too small!\n");
  1608. ret = -EINVAL;
  1609. goto err_release;
  1610. }
  1611. /* Data from ucode file: header followed by uCode images */
  1612. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  1613. priv->ucode_ver = le32_to_cpu(ucode->ver);
  1614. api_ver = IWL_UCODE_API(priv->ucode_ver);
  1615. inst_size = iwl3945_ucode_get_inst_size(ucode);
  1616. data_size = iwl3945_ucode_get_data_size(ucode);
  1617. init_size = iwl3945_ucode_get_init_size(ucode);
  1618. init_data_size = iwl3945_ucode_get_init_data_size(ucode);
  1619. boot_size = iwl3945_ucode_get_boot_size(ucode);
  1620. src = iwl3945_ucode_get_data(ucode);
  1621. /* api_ver should match the api version forming part of the
  1622. * firmware filename ... but we don't check for that and only rely
  1623. * on the API version read from firmware header from here on forward */
  1624. if (api_ver < api_min || api_ver > api_max) {
  1625. IWL_ERR(priv, "Driver unable to support your firmware API. "
  1626. "Driver supports v%u, firmware is v%u.\n",
  1627. api_max, api_ver);
  1628. priv->ucode_ver = 0;
  1629. ret = -EINVAL;
  1630. goto err_release;
  1631. }
  1632. if (api_ver != api_max)
  1633. IWL_ERR(priv, "Firmware has old API version. Expected %u, "
  1634. "got %u. New firmware can be obtained "
  1635. "from http://www.intellinuxwireless.org.\n",
  1636. api_max, api_ver);
  1637. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
  1638. IWL_UCODE_MAJOR(priv->ucode_ver),
  1639. IWL_UCODE_MINOR(priv->ucode_ver),
  1640. IWL_UCODE_API(priv->ucode_ver),
  1641. IWL_UCODE_SERIAL(priv->ucode_ver));
  1642. snprintf(priv->hw->wiphy->fw_version,
  1643. sizeof(priv->hw->wiphy->fw_version),
  1644. "%u.%u.%u.%u",
  1645. IWL_UCODE_MAJOR(priv->ucode_ver),
  1646. IWL_UCODE_MINOR(priv->ucode_ver),
  1647. IWL_UCODE_API(priv->ucode_ver),
  1648. IWL_UCODE_SERIAL(priv->ucode_ver));
  1649. IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
  1650. priv->ucode_ver);
  1651. IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
  1652. inst_size);
  1653. IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
  1654. data_size);
  1655. IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
  1656. init_size);
  1657. IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
  1658. init_data_size);
  1659. IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
  1660. boot_size);
  1661. /* Verify size of file vs. image size info in file's header */
  1662. if (ucode_raw->size != iwl3945_ucode_get_header_size(api_ver) +
  1663. inst_size + data_size + init_size +
  1664. init_data_size + boot_size) {
  1665. IWL_DEBUG_INFO(priv,
  1666. "uCode file size %zd does not match expected size\n",
  1667. ucode_raw->size);
  1668. ret = -EINVAL;
  1669. goto err_release;
  1670. }
  1671. /* Verify that uCode images will fit in card's SRAM */
  1672. if (inst_size > IWL39_MAX_INST_SIZE) {
  1673. IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
  1674. inst_size);
  1675. ret = -EINVAL;
  1676. goto err_release;
  1677. }
  1678. if (data_size > IWL39_MAX_DATA_SIZE) {
  1679. IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
  1680. data_size);
  1681. ret = -EINVAL;
  1682. goto err_release;
  1683. }
  1684. if (init_size > IWL39_MAX_INST_SIZE) {
  1685. IWL_DEBUG_INFO(priv,
  1686. "uCode init instr len %d too large to fit in\n",
  1687. init_size);
  1688. ret = -EINVAL;
  1689. goto err_release;
  1690. }
  1691. if (init_data_size > IWL39_MAX_DATA_SIZE) {
  1692. IWL_DEBUG_INFO(priv,
  1693. "uCode init data len %d too large to fit in\n",
  1694. init_data_size);
  1695. ret = -EINVAL;
  1696. goto err_release;
  1697. }
  1698. if (boot_size > IWL39_MAX_BSM_SIZE) {
  1699. IWL_DEBUG_INFO(priv,
  1700. "uCode boot instr len %d too large to fit in\n",
  1701. boot_size);
  1702. ret = -EINVAL;
  1703. goto err_release;
  1704. }
  1705. /* Allocate ucode buffers for card's bus-master loading ... */
  1706. /* Runtime instructions and 2 copies of data:
  1707. * 1) unmodified from disk
  1708. * 2) backup cache for save/restore during power-downs */
  1709. priv->ucode_code.len = inst_size;
  1710. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
  1711. priv->ucode_data.len = data_size;
  1712. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
  1713. priv->ucode_data_backup.len = data_size;
  1714. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  1715. if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
  1716. !priv->ucode_data_backup.v_addr)
  1717. goto err_pci_alloc;
  1718. /* Initialization instructions and data */
  1719. if (init_size && init_data_size) {
  1720. priv->ucode_init.len = init_size;
  1721. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
  1722. priv->ucode_init_data.len = init_data_size;
  1723. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  1724. if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
  1725. goto err_pci_alloc;
  1726. }
  1727. /* Bootstrap (instructions only, no data) */
  1728. if (boot_size) {
  1729. priv->ucode_boot.len = boot_size;
  1730. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
  1731. if (!priv->ucode_boot.v_addr)
  1732. goto err_pci_alloc;
  1733. }
  1734. /* Copy images into buffers for card's bus-master reads ... */
  1735. /* Runtime instructions (first block of data in file) */
  1736. len = inst_size;
  1737. IWL_DEBUG_INFO(priv,
  1738. "Copying (but not loading) uCode instr len %zd\n", len);
  1739. memcpy(priv->ucode_code.v_addr, src, len);
  1740. src += len;
  1741. IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  1742. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  1743. /* Runtime data (2nd block)
  1744. * NOTE: Copy into backup buffer will be done in iwl3945_up() */
  1745. len = data_size;
  1746. IWL_DEBUG_INFO(priv,
  1747. "Copying (but not loading) uCode data len %zd\n", len);
  1748. memcpy(priv->ucode_data.v_addr, src, len);
  1749. memcpy(priv->ucode_data_backup.v_addr, src, len);
  1750. src += len;
  1751. /* Initialization instructions (3rd block) */
  1752. if (init_size) {
  1753. len = init_size;
  1754. IWL_DEBUG_INFO(priv,
  1755. "Copying (but not loading) init instr len %zd\n", len);
  1756. memcpy(priv->ucode_init.v_addr, src, len);
  1757. src += len;
  1758. }
  1759. /* Initialization data (4th block) */
  1760. if (init_data_size) {
  1761. len = init_data_size;
  1762. IWL_DEBUG_INFO(priv,
  1763. "Copying (but not loading) init data len %zd\n", len);
  1764. memcpy(priv->ucode_init_data.v_addr, src, len);
  1765. src += len;
  1766. }
  1767. /* Bootstrap instructions (5th block) */
  1768. len = boot_size;
  1769. IWL_DEBUG_INFO(priv,
  1770. "Copying (but not loading) boot instr len %zd\n", len);
  1771. memcpy(priv->ucode_boot.v_addr, src, len);
  1772. /* We have our copies now, allow OS release its copies */
  1773. release_firmware(ucode_raw);
  1774. return 0;
  1775. err_pci_alloc:
  1776. IWL_ERR(priv, "failed to allocate pci memory\n");
  1777. ret = -ENOMEM;
  1778. iwl3945_dealloc_ucode_pci(priv);
  1779. err_release:
  1780. release_firmware(ucode_raw);
  1781. error:
  1782. return ret;
  1783. }
  1784. /**
  1785. * iwl3945_set_ucode_ptrs - Set uCode address location
  1786. *
  1787. * Tell initialization uCode where to find runtime uCode.
  1788. *
  1789. * BSM registers initially contain pointers to initialization uCode.
  1790. * We need to replace them to load runtime uCode inst and data,
  1791. * and to save runtime data when powering down.
  1792. */
  1793. static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
  1794. {
  1795. dma_addr_t pinst;
  1796. dma_addr_t pdata;
  1797. /* bits 31:0 for 3945 */
  1798. pinst = priv->ucode_code.p_addr;
  1799. pdata = priv->ucode_data_backup.p_addr;
  1800. /* Tell bootstrap uCode where to find image to load */
  1801. iwl_legacy_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  1802. iwl_legacy_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  1803. iwl_legacy_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  1804. priv->ucode_data.len);
  1805. /* Inst byte count must be last to set up, bit 31 signals uCode
  1806. * that all new ptr/size info is in place */
  1807. iwl_legacy_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  1808. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  1809. IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
  1810. return 0;
  1811. }
  1812. /**
  1813. * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
  1814. *
  1815. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  1816. *
  1817. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  1818. */
  1819. static void iwl3945_init_alive_start(struct iwl_priv *priv)
  1820. {
  1821. /* Check alive response for "valid" sign from uCode */
  1822. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  1823. /* We had an error bringing up the hardware, so take it
  1824. * all the way back down so we can try again */
  1825. IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
  1826. goto restart;
  1827. }
  1828. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  1829. * This is a paranoid check, because we would not have gotten the
  1830. * "initialize" alive if code weren't properly loaded. */
  1831. if (iwl3945_verify_ucode(priv)) {
  1832. /* Runtime instruction load was bad;
  1833. * take it all the way back down so we can try again */
  1834. IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
  1835. goto restart;
  1836. }
  1837. /* Send pointers to protocol/runtime uCode image ... init code will
  1838. * load and launch runtime uCode, which will send us another "Alive"
  1839. * notification. */
  1840. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  1841. if (iwl3945_set_ucode_ptrs(priv)) {
  1842. /* Runtime instruction load won't happen;
  1843. * take it all the way back down so we can try again */
  1844. IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
  1845. goto restart;
  1846. }
  1847. return;
  1848. restart:
  1849. queue_work(priv->workqueue, &priv->restart);
  1850. }
  1851. /**
  1852. * iwl3945_alive_start - called after REPLY_ALIVE notification received
  1853. * from protocol/runtime uCode (initialization uCode's
  1854. * Alive gets handled by iwl3945_init_alive_start()).
  1855. */
  1856. static void iwl3945_alive_start(struct iwl_priv *priv)
  1857. {
  1858. int thermal_spin = 0;
  1859. u32 rfkill;
  1860. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1861. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  1862. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  1863. /* We had an error bringing up the hardware, so take it
  1864. * all the way back down so we can try again */
  1865. IWL_DEBUG_INFO(priv, "Alive failed.\n");
  1866. goto restart;
  1867. }
  1868. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  1869. * This is a paranoid check, because we would not have gotten the
  1870. * "runtime" alive if code weren't properly loaded. */
  1871. if (iwl3945_verify_ucode(priv)) {
  1872. /* Runtime instruction load was bad;
  1873. * take it all the way back down so we can try again */
  1874. IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
  1875. goto restart;
  1876. }
  1877. rfkill = iwl_legacy_read_prph(priv, APMG_RFKILL_REG);
  1878. IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
  1879. if (rfkill & 0x1) {
  1880. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  1881. /* if RFKILL is not on, then wait for thermal
  1882. * sensor in adapter to kick in */
  1883. while (iwl3945_hw_get_temperature(priv) == 0) {
  1884. thermal_spin++;
  1885. udelay(10);
  1886. }
  1887. if (thermal_spin)
  1888. IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
  1889. thermal_spin * 10);
  1890. } else
  1891. set_bit(STATUS_RF_KILL_HW, &priv->status);
  1892. /* After the ALIVE response, we can send commands to 3945 uCode */
  1893. set_bit(STATUS_ALIVE, &priv->status);
  1894. /* Enable watchdog to monitor the driver tx queues */
  1895. iwl_legacy_setup_watchdog(priv);
  1896. if (iwl_legacy_is_rfkill(priv))
  1897. return;
  1898. ieee80211_wake_queues(priv->hw);
  1899. priv->active_rate = IWL_RATES_MASK_3945;
  1900. iwl_legacy_power_update_mode(priv, true);
  1901. if (iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS)) {
  1902. struct iwl3945_rxon_cmd *active_rxon =
  1903. (struct iwl3945_rxon_cmd *)(&ctx->active);
  1904. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1905. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1906. } else {
  1907. /* Initialize our rx_config data */
  1908. iwl_legacy_connection_init_rx_config(priv, ctx);
  1909. }
  1910. /* Configure Bluetooth device coexistence support */
  1911. iwl_legacy_send_bt_config(priv);
  1912. set_bit(STATUS_READY, &priv->status);
  1913. /* Configure the adapter for unassociated operation */
  1914. iwl3945_commit_rxon(priv, ctx);
  1915. iwl3945_reg_txpower_periodic(priv);
  1916. IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
  1917. wake_up(&priv->wait_command_queue);
  1918. return;
  1919. restart:
  1920. queue_work(priv->workqueue, &priv->restart);
  1921. }
  1922. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
  1923. static void __iwl3945_down(struct iwl_priv *priv)
  1924. {
  1925. unsigned long flags;
  1926. int exit_pending;
  1927. IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
  1928. iwl_legacy_scan_cancel_timeout(priv, 200);
  1929. exit_pending = test_and_set_bit(STATUS_EXIT_PENDING, &priv->status);
  1930. /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
  1931. * to prevent rearm timer */
  1932. del_timer_sync(&priv->watchdog);
  1933. /* Station information will now be cleared in device */
  1934. iwl_legacy_clear_ucode_stations(priv, NULL);
  1935. iwl_legacy_dealloc_bcast_stations(priv);
  1936. iwl_legacy_clear_driver_stations(priv);
  1937. /* Unblock any waiting calls */
  1938. wake_up_all(&priv->wait_command_queue);
  1939. /* Wipe out the EXIT_PENDING status bit if we are not actually
  1940. * exiting the module */
  1941. if (!exit_pending)
  1942. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  1943. /* stop and reset the on-board processor */
  1944. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  1945. /* tell the device to stop sending interrupts */
  1946. spin_lock_irqsave(&priv->lock, flags);
  1947. iwl_legacy_disable_interrupts(priv);
  1948. spin_unlock_irqrestore(&priv->lock, flags);
  1949. iwl3945_synchronize_irq(priv);
  1950. if (priv->mac80211_registered)
  1951. ieee80211_stop_queues(priv->hw);
  1952. /* If we have not previously called iwl3945_init() then
  1953. * clear all bits but the RF Kill bits and return */
  1954. if (!iwl_legacy_is_init(priv)) {
  1955. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  1956. STATUS_RF_KILL_HW |
  1957. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  1958. STATUS_GEO_CONFIGURED |
  1959. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  1960. STATUS_EXIT_PENDING;
  1961. goto exit;
  1962. }
  1963. /* ...otherwise clear out all the status bits but the RF Kill
  1964. * bit and continue taking the NIC down. */
  1965. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  1966. STATUS_RF_KILL_HW |
  1967. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  1968. STATUS_GEO_CONFIGURED |
  1969. test_bit(STATUS_FW_ERROR, &priv->status) <<
  1970. STATUS_FW_ERROR |
  1971. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  1972. STATUS_EXIT_PENDING;
  1973. iwl3945_hw_txq_ctx_stop(priv);
  1974. iwl3945_hw_rxq_stop(priv);
  1975. /* Power-down device's busmaster DMA clocks */
  1976. iwl_legacy_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  1977. udelay(5);
  1978. /* Stop the device, and put it in low power state */
  1979. iwl_legacy_apm_stop(priv);
  1980. exit:
  1981. memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
  1982. if (priv->beacon_skb)
  1983. dev_kfree_skb(priv->beacon_skb);
  1984. priv->beacon_skb = NULL;
  1985. /* clear out any free frames */
  1986. iwl3945_clear_free_frames(priv);
  1987. }
  1988. static void iwl3945_down(struct iwl_priv *priv)
  1989. {
  1990. mutex_lock(&priv->mutex);
  1991. __iwl3945_down(priv);
  1992. mutex_unlock(&priv->mutex);
  1993. iwl3945_cancel_deferred_work(priv);
  1994. }
  1995. #define MAX_HW_RESTARTS 5
  1996. static int iwl3945_alloc_bcast_station(struct iwl_priv *priv)
  1997. {
  1998. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1999. unsigned long flags;
  2000. u8 sta_id;
  2001. spin_lock_irqsave(&priv->sta_lock, flags);
  2002. sta_id = iwl_legacy_prep_station(priv, ctx,
  2003. iwlegacy_bcast_addr, false, NULL);
  2004. if (sta_id == IWL_INVALID_STATION) {
  2005. IWL_ERR(priv, "Unable to prepare broadcast station\n");
  2006. spin_unlock_irqrestore(&priv->sta_lock, flags);
  2007. return -EINVAL;
  2008. }
  2009. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  2010. priv->stations[sta_id].used |= IWL_STA_BCAST;
  2011. spin_unlock_irqrestore(&priv->sta_lock, flags);
  2012. return 0;
  2013. }
  2014. static int __iwl3945_up(struct iwl_priv *priv)
  2015. {
  2016. int rc, i;
  2017. rc = iwl3945_alloc_bcast_station(priv);
  2018. if (rc)
  2019. return rc;
  2020. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  2021. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  2022. return -EIO;
  2023. }
  2024. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  2025. IWL_ERR(priv, "ucode not available for device bring up\n");
  2026. return -EIO;
  2027. }
  2028. /* If platform's RF_KILL switch is NOT set to KILL */
  2029. if (iwl_read32(priv, CSR_GP_CNTRL) &
  2030. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2031. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2032. else {
  2033. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2034. IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
  2035. return -ENODEV;
  2036. }
  2037. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2038. rc = iwl3945_hw_nic_init(priv);
  2039. if (rc) {
  2040. IWL_ERR(priv, "Unable to int nic\n");
  2041. return rc;
  2042. }
  2043. /* make sure rfkill handshake bits are cleared */
  2044. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2045. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  2046. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2047. /* clear (again), then enable host interrupts */
  2048. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2049. iwl_legacy_enable_interrupts(priv);
  2050. /* really make sure rfkill handshake bits are cleared */
  2051. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2052. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2053. /* Copy original ucode data image from disk into backup cache.
  2054. * This will be used to initialize the on-board processor's
  2055. * data SRAM for a clean start when the runtime program first loads. */
  2056. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  2057. priv->ucode_data.len);
  2058. /* We return success when we resume from suspend and rf_kill is on. */
  2059. if (test_bit(STATUS_RF_KILL_HW, &priv->status))
  2060. return 0;
  2061. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  2062. /* load bootstrap state machine,
  2063. * load bootstrap program into processor's memory,
  2064. * prepare to load the "initialize" uCode */
  2065. rc = priv->cfg->ops->lib->load_ucode(priv);
  2066. if (rc) {
  2067. IWL_ERR(priv,
  2068. "Unable to set up bootstrap uCode: %d\n", rc);
  2069. continue;
  2070. }
  2071. /* start card; "initialize" will load runtime ucode */
  2072. iwl3945_nic_start(priv);
  2073. IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
  2074. return 0;
  2075. }
  2076. set_bit(STATUS_EXIT_PENDING, &priv->status);
  2077. __iwl3945_down(priv);
  2078. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  2079. /* tried to restart and config the device for as long as our
  2080. * patience could withstand */
  2081. IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
  2082. return -EIO;
  2083. }
  2084. /*****************************************************************************
  2085. *
  2086. * Workqueue callbacks
  2087. *
  2088. *****************************************************************************/
  2089. static void iwl3945_bg_init_alive_start(struct work_struct *data)
  2090. {
  2091. struct iwl_priv *priv =
  2092. container_of(data, struct iwl_priv, init_alive_start.work);
  2093. mutex_lock(&priv->mutex);
  2094. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2095. goto out;
  2096. iwl3945_init_alive_start(priv);
  2097. out:
  2098. mutex_unlock(&priv->mutex);
  2099. }
  2100. static void iwl3945_bg_alive_start(struct work_struct *data)
  2101. {
  2102. struct iwl_priv *priv =
  2103. container_of(data, struct iwl_priv, alive_start.work);
  2104. mutex_lock(&priv->mutex);
  2105. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2106. goto out;
  2107. iwl3945_alive_start(priv);
  2108. out:
  2109. mutex_unlock(&priv->mutex);
  2110. }
  2111. /*
  2112. * 3945 cannot interrupt driver when hardware rf kill switch toggles;
  2113. * driver must poll CSR_GP_CNTRL_REG register for change. This register
  2114. * *is* readable even when device has been SW_RESET into low power mode
  2115. * (e.g. during RF KILL).
  2116. */
  2117. static void iwl3945_rfkill_poll(struct work_struct *data)
  2118. {
  2119. struct iwl_priv *priv =
  2120. container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
  2121. bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
  2122. bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
  2123. & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
  2124. if (new_rfkill != old_rfkill) {
  2125. if (new_rfkill)
  2126. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2127. else
  2128. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2129. wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
  2130. IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
  2131. new_rfkill ? "disable radio" : "enable radio");
  2132. }
  2133. /* Keep this running, even if radio now enabled. This will be
  2134. * cancelled in mac_start() if system decides to start again */
  2135. queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
  2136. round_jiffies_relative(2 * HZ));
  2137. }
  2138. int iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  2139. {
  2140. struct iwl_host_cmd cmd = {
  2141. .id = REPLY_SCAN_CMD,
  2142. .len = sizeof(struct iwl3945_scan_cmd),
  2143. .flags = CMD_SIZE_HUGE,
  2144. };
  2145. struct iwl3945_scan_cmd *scan;
  2146. u8 n_probes = 0;
  2147. enum ieee80211_band band;
  2148. bool is_active = false;
  2149. int ret;
  2150. u16 len;
  2151. lockdep_assert_held(&priv->mutex);
  2152. if (!priv->scan_cmd) {
  2153. priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
  2154. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  2155. if (!priv->scan_cmd) {
  2156. IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
  2157. return -ENOMEM;
  2158. }
  2159. }
  2160. scan = priv->scan_cmd;
  2161. memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
  2162. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  2163. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  2164. if (iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS)) {
  2165. u16 interval;
  2166. u32 extra;
  2167. u32 suspend_time = 100;
  2168. u32 scan_suspend_time = 100;
  2169. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  2170. interval = vif->bss_conf.beacon_int;
  2171. scan->suspend_time = 0;
  2172. scan->max_out_time = cpu_to_le32(200 * 1024);
  2173. if (!interval)
  2174. interval = suspend_time;
  2175. /*
  2176. * suspend time format:
  2177. * 0-19: beacon interval in usec (time before exec.)
  2178. * 20-23: 0
  2179. * 24-31: number of beacons (suspend between channels)
  2180. */
  2181. extra = (suspend_time / interval) << 24;
  2182. scan_suspend_time = 0xFF0FFFFF &
  2183. (extra | ((suspend_time % interval) * 1024));
  2184. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  2185. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  2186. scan_suspend_time, interval);
  2187. }
  2188. if (priv->scan_request->n_ssids) {
  2189. int i, p = 0;
  2190. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  2191. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  2192. /* always does wildcard anyway */
  2193. if (!priv->scan_request->ssids[i].ssid_len)
  2194. continue;
  2195. scan->direct_scan[p].id = WLAN_EID_SSID;
  2196. scan->direct_scan[p].len =
  2197. priv->scan_request->ssids[i].ssid_len;
  2198. memcpy(scan->direct_scan[p].ssid,
  2199. priv->scan_request->ssids[i].ssid,
  2200. priv->scan_request->ssids[i].ssid_len);
  2201. n_probes++;
  2202. p++;
  2203. }
  2204. is_active = true;
  2205. } else
  2206. IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
  2207. /* We don't build a direct scan probe request; the uCode will do
  2208. * that based on the direct_mask added to each channel entry */
  2209. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  2210. scan->tx_cmd.sta_id = priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id;
  2211. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2212. /* flags + rate selection */
  2213. switch (priv->scan_band) {
  2214. case IEEE80211_BAND_2GHZ:
  2215. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  2216. scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
  2217. band = IEEE80211_BAND_2GHZ;
  2218. break;
  2219. case IEEE80211_BAND_5GHZ:
  2220. scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
  2221. band = IEEE80211_BAND_5GHZ;
  2222. break;
  2223. default:
  2224. IWL_WARN(priv, "Invalid scan band\n");
  2225. return -EIO;
  2226. }
  2227. /*
  2228. * If active scaning is requested but a certain channel
  2229. * is marked passive, we can do active scanning if we
  2230. * detect transmissions.
  2231. */
  2232. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  2233. IWL_GOOD_CRC_TH_DISABLED;
  2234. len = iwl_legacy_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
  2235. vif->addr, priv->scan_request->ie,
  2236. priv->scan_request->ie_len,
  2237. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  2238. scan->tx_cmd.len = cpu_to_le16(len);
  2239. /* select Rx antennas */
  2240. scan->flags |= iwl3945_get_antenna_flags(priv);
  2241. scan->channel_count = iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
  2242. (void *)&scan->data[len], vif);
  2243. if (scan->channel_count == 0) {
  2244. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  2245. return -EIO;
  2246. }
  2247. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  2248. scan->channel_count * sizeof(struct iwl3945_scan_channel);
  2249. cmd.data = scan;
  2250. scan->len = cpu_to_le16(cmd.len);
  2251. set_bit(STATUS_SCAN_HW, &priv->status);
  2252. ret = iwl_legacy_send_cmd_sync(priv, &cmd);
  2253. if (ret)
  2254. clear_bit(STATUS_SCAN_HW, &priv->status);
  2255. return ret;
  2256. }
  2257. void iwl3945_post_scan(struct iwl_priv *priv)
  2258. {
  2259. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2260. /*
  2261. * Since setting the RXON may have been deferred while
  2262. * performing the scan, fire one off if needed
  2263. */
  2264. if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  2265. iwl3945_commit_rxon(priv, ctx);
  2266. }
  2267. static void iwl3945_bg_restart(struct work_struct *data)
  2268. {
  2269. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  2270. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2271. return;
  2272. if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
  2273. struct iwl_rxon_context *ctx;
  2274. mutex_lock(&priv->mutex);
  2275. for_each_context(priv, ctx)
  2276. ctx->vif = NULL;
  2277. priv->is_open = 0;
  2278. mutex_unlock(&priv->mutex);
  2279. iwl3945_down(priv);
  2280. ieee80211_restart_hw(priv->hw);
  2281. } else {
  2282. iwl3945_down(priv);
  2283. mutex_lock(&priv->mutex);
  2284. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  2285. mutex_unlock(&priv->mutex);
  2286. return;
  2287. }
  2288. __iwl3945_up(priv);
  2289. mutex_unlock(&priv->mutex);
  2290. }
  2291. }
  2292. static void iwl3945_bg_rx_replenish(struct work_struct *data)
  2293. {
  2294. struct iwl_priv *priv =
  2295. container_of(data, struct iwl_priv, rx_replenish);
  2296. mutex_lock(&priv->mutex);
  2297. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2298. goto out;
  2299. iwl3945_rx_replenish(priv);
  2300. out:
  2301. mutex_unlock(&priv->mutex);
  2302. }
  2303. void iwl3945_post_associate(struct iwl_priv *priv)
  2304. {
  2305. int rc = 0;
  2306. struct ieee80211_conf *conf = NULL;
  2307. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2308. if (!ctx->vif || !priv->is_open)
  2309. return;
  2310. IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
  2311. ctx->vif->bss_conf.aid, ctx->active.bssid_addr);
  2312. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2313. return;
  2314. iwl_legacy_scan_cancel_timeout(priv, 200);
  2315. conf = iwl_legacy_ieee80211_get_hw_conf(priv->hw);
  2316. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2317. iwl3945_commit_rxon(priv, ctx);
  2318. rc = iwl_legacy_send_rxon_timing(priv, ctx);
  2319. if (rc)
  2320. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  2321. "Attempting to continue.\n");
  2322. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  2323. ctx->staging.assoc_id = cpu_to_le16(ctx->vif->bss_conf.aid);
  2324. IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
  2325. ctx->vif->bss_conf.aid, ctx->vif->bss_conf.beacon_int);
  2326. if (ctx->vif->bss_conf.use_short_preamble)
  2327. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  2328. else
  2329. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  2330. if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
  2331. if (ctx->vif->bss_conf.use_short_slot)
  2332. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  2333. else
  2334. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2335. }
  2336. iwl3945_commit_rxon(priv, ctx);
  2337. switch (ctx->vif->type) {
  2338. case NL80211_IFTYPE_STATION:
  2339. iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
  2340. break;
  2341. case NL80211_IFTYPE_ADHOC:
  2342. iwl3945_send_beacon_cmd(priv);
  2343. break;
  2344. default:
  2345. IWL_ERR(priv, "%s Should not be called in %d mode\n",
  2346. __func__, ctx->vif->type);
  2347. break;
  2348. }
  2349. }
  2350. /*****************************************************************************
  2351. *
  2352. * mac80211 entry point functions
  2353. *
  2354. *****************************************************************************/
  2355. #define UCODE_READY_TIMEOUT (2 * HZ)
  2356. static int iwl3945_mac_start(struct ieee80211_hw *hw)
  2357. {
  2358. struct iwl_priv *priv = hw->priv;
  2359. int ret;
  2360. IWL_DEBUG_MAC80211(priv, "enter\n");
  2361. /* we should be verifying the device is ready to be opened */
  2362. mutex_lock(&priv->mutex);
  2363. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  2364. * ucode filename and max sizes are card-specific. */
  2365. if (!priv->ucode_code.len) {
  2366. ret = iwl3945_read_ucode(priv);
  2367. if (ret) {
  2368. IWL_ERR(priv, "Could not read microcode: %d\n", ret);
  2369. mutex_unlock(&priv->mutex);
  2370. goto out_release_irq;
  2371. }
  2372. }
  2373. ret = __iwl3945_up(priv);
  2374. mutex_unlock(&priv->mutex);
  2375. if (ret)
  2376. goto out_release_irq;
  2377. IWL_DEBUG_INFO(priv, "Start UP work.\n");
  2378. /* Wait for START_ALIVE from ucode. Otherwise callbacks from
  2379. * mac80211 will not be run successfully. */
  2380. ret = wait_event_timeout(priv->wait_command_queue,
  2381. test_bit(STATUS_READY, &priv->status),
  2382. UCODE_READY_TIMEOUT);
  2383. if (!ret) {
  2384. if (!test_bit(STATUS_READY, &priv->status)) {
  2385. IWL_ERR(priv,
  2386. "Wait for START_ALIVE timeout after %dms.\n",
  2387. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  2388. ret = -ETIMEDOUT;
  2389. goto out_release_irq;
  2390. }
  2391. }
  2392. /* ucode is running and will send rfkill notifications,
  2393. * no need to poll the killswitch state anymore */
  2394. cancel_delayed_work(&priv->_3945.rfkill_poll);
  2395. priv->is_open = 1;
  2396. IWL_DEBUG_MAC80211(priv, "leave\n");
  2397. return 0;
  2398. out_release_irq:
  2399. priv->is_open = 0;
  2400. IWL_DEBUG_MAC80211(priv, "leave - failed\n");
  2401. return ret;
  2402. }
  2403. static void iwl3945_mac_stop(struct ieee80211_hw *hw)
  2404. {
  2405. struct iwl_priv *priv = hw->priv;
  2406. IWL_DEBUG_MAC80211(priv, "enter\n");
  2407. if (!priv->is_open) {
  2408. IWL_DEBUG_MAC80211(priv, "leave - skip\n");
  2409. return;
  2410. }
  2411. priv->is_open = 0;
  2412. iwl3945_down(priv);
  2413. flush_workqueue(priv->workqueue);
  2414. /* start polling the killswitch state again */
  2415. queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
  2416. round_jiffies_relative(2 * HZ));
  2417. IWL_DEBUG_MAC80211(priv, "leave\n");
  2418. }
  2419. static void iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2420. {
  2421. struct iwl_priv *priv = hw->priv;
  2422. IWL_DEBUG_MAC80211(priv, "enter\n");
  2423. IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  2424. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  2425. if (iwl3945_tx_skb(priv, skb))
  2426. dev_kfree_skb_any(skb);
  2427. IWL_DEBUG_MAC80211(priv, "leave\n");
  2428. }
  2429. void iwl3945_config_ap(struct iwl_priv *priv)
  2430. {
  2431. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2432. struct ieee80211_vif *vif = ctx->vif;
  2433. int rc = 0;
  2434. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2435. return;
  2436. /* The following should be done only at AP bring up */
  2437. if (!(iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS))) {
  2438. /* RXON - unassoc (to set timing command) */
  2439. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2440. iwl3945_commit_rxon(priv, ctx);
  2441. /* RXON Timing */
  2442. rc = iwl_legacy_send_rxon_timing(priv, ctx);
  2443. if (rc)
  2444. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  2445. "Attempting to continue.\n");
  2446. ctx->staging.assoc_id = 0;
  2447. if (vif->bss_conf.use_short_preamble)
  2448. ctx->staging.flags |=
  2449. RXON_FLG_SHORT_PREAMBLE_MSK;
  2450. else
  2451. ctx->staging.flags &=
  2452. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  2453. if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
  2454. if (vif->bss_conf.use_short_slot)
  2455. ctx->staging.flags |=
  2456. RXON_FLG_SHORT_SLOT_MSK;
  2457. else
  2458. ctx->staging.flags &=
  2459. ~RXON_FLG_SHORT_SLOT_MSK;
  2460. }
  2461. /* restore RXON assoc */
  2462. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  2463. iwl3945_commit_rxon(priv, ctx);
  2464. }
  2465. iwl3945_send_beacon_cmd(priv);
  2466. }
  2467. static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2468. struct ieee80211_vif *vif,
  2469. struct ieee80211_sta *sta,
  2470. struct ieee80211_key_conf *key)
  2471. {
  2472. struct iwl_priv *priv = hw->priv;
  2473. int ret = 0;
  2474. u8 sta_id = IWL_INVALID_STATION;
  2475. u8 static_key;
  2476. IWL_DEBUG_MAC80211(priv, "enter\n");
  2477. if (iwl3945_mod_params.sw_crypto) {
  2478. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  2479. return -EOPNOTSUPP;
  2480. }
  2481. /*
  2482. * To support IBSS RSN, don't program group keys in IBSS, the
  2483. * hardware will then not attempt to decrypt the frames.
  2484. */
  2485. if (vif->type == NL80211_IFTYPE_ADHOC &&
  2486. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  2487. return -EOPNOTSUPP;
  2488. static_key = !iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS);
  2489. if (!static_key) {
  2490. sta_id = iwl_legacy_sta_id_or_broadcast(
  2491. priv, &priv->contexts[IWL_RXON_CTX_BSS], sta);
  2492. if (sta_id == IWL_INVALID_STATION)
  2493. return -EINVAL;
  2494. }
  2495. mutex_lock(&priv->mutex);
  2496. iwl_legacy_scan_cancel_timeout(priv, 100);
  2497. switch (cmd) {
  2498. case SET_KEY:
  2499. if (static_key)
  2500. ret = iwl3945_set_static_key(priv, key);
  2501. else
  2502. ret = iwl3945_set_dynamic_key(priv, key, sta_id);
  2503. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  2504. break;
  2505. case DISABLE_KEY:
  2506. if (static_key)
  2507. ret = iwl3945_remove_static_key(priv);
  2508. else
  2509. ret = iwl3945_clear_sta_key_info(priv, sta_id);
  2510. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  2511. break;
  2512. default:
  2513. ret = -EINVAL;
  2514. }
  2515. mutex_unlock(&priv->mutex);
  2516. IWL_DEBUG_MAC80211(priv, "leave\n");
  2517. return ret;
  2518. }
  2519. static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
  2520. struct ieee80211_vif *vif,
  2521. struct ieee80211_sta *sta)
  2522. {
  2523. struct iwl_priv *priv = hw->priv;
  2524. struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
  2525. int ret;
  2526. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  2527. u8 sta_id;
  2528. IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
  2529. sta->addr);
  2530. mutex_lock(&priv->mutex);
  2531. IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
  2532. sta->addr);
  2533. sta_priv->common.sta_id = IWL_INVALID_STATION;
  2534. ret = iwl_legacy_add_station_common(priv,
  2535. &priv->contexts[IWL_RXON_CTX_BSS],
  2536. sta->addr, is_ap, sta, &sta_id);
  2537. if (ret) {
  2538. IWL_ERR(priv, "Unable to add station %pM (%d)\n",
  2539. sta->addr, ret);
  2540. /* Should we return success if return code is EEXIST ? */
  2541. mutex_unlock(&priv->mutex);
  2542. return ret;
  2543. }
  2544. sta_priv->common.sta_id = sta_id;
  2545. /* Initialize rate scaling */
  2546. IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
  2547. sta->addr);
  2548. iwl3945_rs_rate_init(priv, sta, sta_id);
  2549. mutex_unlock(&priv->mutex);
  2550. return 0;
  2551. }
  2552. static void iwl3945_configure_filter(struct ieee80211_hw *hw,
  2553. unsigned int changed_flags,
  2554. unsigned int *total_flags,
  2555. u64 multicast)
  2556. {
  2557. struct iwl_priv *priv = hw->priv;
  2558. __le32 filter_or = 0, filter_nand = 0;
  2559. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2560. #define CHK(test, flag) do { \
  2561. if (*total_flags & (test)) \
  2562. filter_or |= (flag); \
  2563. else \
  2564. filter_nand |= (flag); \
  2565. } while (0)
  2566. IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
  2567. changed_flags, *total_flags);
  2568. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  2569. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
  2570. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  2571. #undef CHK
  2572. mutex_lock(&priv->mutex);
  2573. ctx->staging.filter_flags &= ~filter_nand;
  2574. ctx->staging.filter_flags |= filter_or;
  2575. /*
  2576. * Not committing directly because hardware can perform a scan,
  2577. * but even if hw is ready, committing here breaks for some reason,
  2578. * we'll eventually commit the filter flags change anyway.
  2579. */
  2580. mutex_unlock(&priv->mutex);
  2581. /*
  2582. * Receiving all multicast frames is always enabled by the
  2583. * default flags setup in iwl_legacy_connection_init_rx_config()
  2584. * since we currently do not support programming multicast
  2585. * filters into the device.
  2586. */
  2587. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  2588. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  2589. }
  2590. /*****************************************************************************
  2591. *
  2592. * sysfs attributes
  2593. *
  2594. *****************************************************************************/
  2595. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  2596. /*
  2597. * The following adds a new attribute to the sysfs representation
  2598. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  2599. * used for controlling the debug level.
  2600. *
  2601. * See the level definitions in iwl for details.
  2602. *
  2603. * The debug_level being managed using sysfs below is a per device debug
  2604. * level that is used instead of the global debug level if it (the per
  2605. * device debug level) is set.
  2606. */
  2607. static ssize_t iwl3945_show_debug_level(struct device *d,
  2608. struct device_attribute *attr, char *buf)
  2609. {
  2610. struct iwl_priv *priv = dev_get_drvdata(d);
  2611. return sprintf(buf, "0x%08X\n", iwl_legacy_get_debug_level(priv));
  2612. }
  2613. static ssize_t iwl3945_store_debug_level(struct device *d,
  2614. struct device_attribute *attr,
  2615. const char *buf, size_t count)
  2616. {
  2617. struct iwl_priv *priv = dev_get_drvdata(d);
  2618. unsigned long val;
  2619. int ret;
  2620. ret = strict_strtoul(buf, 0, &val);
  2621. if (ret)
  2622. IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
  2623. else {
  2624. priv->debug_level = val;
  2625. if (iwl_legacy_alloc_traffic_mem(priv))
  2626. IWL_ERR(priv,
  2627. "Not enough memory to generate traffic log\n");
  2628. }
  2629. return strnlen(buf, count);
  2630. }
  2631. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  2632. iwl3945_show_debug_level, iwl3945_store_debug_level);
  2633. #endif /* CONFIG_IWLWIFI_LEGACY_DEBUG */
  2634. static ssize_t iwl3945_show_temperature(struct device *d,
  2635. struct device_attribute *attr, char *buf)
  2636. {
  2637. struct iwl_priv *priv = dev_get_drvdata(d);
  2638. if (!iwl_legacy_is_alive(priv))
  2639. return -EAGAIN;
  2640. return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
  2641. }
  2642. static DEVICE_ATTR(temperature, S_IRUGO, iwl3945_show_temperature, NULL);
  2643. static ssize_t iwl3945_show_tx_power(struct device *d,
  2644. struct device_attribute *attr, char *buf)
  2645. {
  2646. struct iwl_priv *priv = dev_get_drvdata(d);
  2647. return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
  2648. }
  2649. static ssize_t iwl3945_store_tx_power(struct device *d,
  2650. struct device_attribute *attr,
  2651. const char *buf, size_t count)
  2652. {
  2653. struct iwl_priv *priv = dev_get_drvdata(d);
  2654. char *p = (char *)buf;
  2655. u32 val;
  2656. val = simple_strtoul(p, &p, 10);
  2657. if (p == buf)
  2658. IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
  2659. else
  2660. iwl3945_hw_reg_set_txpower(priv, val);
  2661. return count;
  2662. }
  2663. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, iwl3945_show_tx_power, iwl3945_store_tx_power);
  2664. static ssize_t iwl3945_show_flags(struct device *d,
  2665. struct device_attribute *attr, char *buf)
  2666. {
  2667. struct iwl_priv *priv = dev_get_drvdata(d);
  2668. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2669. return sprintf(buf, "0x%04X\n", ctx->active.flags);
  2670. }
  2671. static ssize_t iwl3945_store_flags(struct device *d,
  2672. struct device_attribute *attr,
  2673. const char *buf, size_t count)
  2674. {
  2675. struct iwl_priv *priv = dev_get_drvdata(d);
  2676. u32 flags = simple_strtoul(buf, NULL, 0);
  2677. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2678. mutex_lock(&priv->mutex);
  2679. if (le32_to_cpu(ctx->staging.flags) != flags) {
  2680. /* Cancel any currently running scans... */
  2681. if (iwl_legacy_scan_cancel_timeout(priv, 100))
  2682. IWL_WARN(priv, "Could not cancel scan.\n");
  2683. else {
  2684. IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
  2685. flags);
  2686. ctx->staging.flags = cpu_to_le32(flags);
  2687. iwl3945_commit_rxon(priv, ctx);
  2688. }
  2689. }
  2690. mutex_unlock(&priv->mutex);
  2691. return count;
  2692. }
  2693. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, iwl3945_show_flags, iwl3945_store_flags);
  2694. static ssize_t iwl3945_show_filter_flags(struct device *d,
  2695. struct device_attribute *attr, char *buf)
  2696. {
  2697. struct iwl_priv *priv = dev_get_drvdata(d);
  2698. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2699. return sprintf(buf, "0x%04X\n",
  2700. le32_to_cpu(ctx->active.filter_flags));
  2701. }
  2702. static ssize_t iwl3945_store_filter_flags(struct device *d,
  2703. struct device_attribute *attr,
  2704. const char *buf, size_t count)
  2705. {
  2706. struct iwl_priv *priv = dev_get_drvdata(d);
  2707. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2708. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  2709. mutex_lock(&priv->mutex);
  2710. if (le32_to_cpu(ctx->staging.filter_flags) != filter_flags) {
  2711. /* Cancel any currently running scans... */
  2712. if (iwl_legacy_scan_cancel_timeout(priv, 100))
  2713. IWL_WARN(priv, "Could not cancel scan.\n");
  2714. else {
  2715. IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
  2716. "0x%04X\n", filter_flags);
  2717. ctx->staging.filter_flags =
  2718. cpu_to_le32(filter_flags);
  2719. iwl3945_commit_rxon(priv, ctx);
  2720. }
  2721. }
  2722. mutex_unlock(&priv->mutex);
  2723. return count;
  2724. }
  2725. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, iwl3945_show_filter_flags,
  2726. iwl3945_store_filter_flags);
  2727. static ssize_t iwl3945_show_measurement(struct device *d,
  2728. struct device_attribute *attr, char *buf)
  2729. {
  2730. struct iwl_priv *priv = dev_get_drvdata(d);
  2731. struct iwl_spectrum_notification measure_report;
  2732. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  2733. u8 *data = (u8 *)&measure_report;
  2734. unsigned long flags;
  2735. spin_lock_irqsave(&priv->lock, flags);
  2736. if (!(priv->measurement_status & MEASUREMENT_READY)) {
  2737. spin_unlock_irqrestore(&priv->lock, flags);
  2738. return 0;
  2739. }
  2740. memcpy(&measure_report, &priv->measure_report, size);
  2741. priv->measurement_status = 0;
  2742. spin_unlock_irqrestore(&priv->lock, flags);
  2743. while (size && (PAGE_SIZE - len)) {
  2744. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  2745. PAGE_SIZE - len, 1);
  2746. len = strlen(buf);
  2747. if (PAGE_SIZE - len)
  2748. buf[len++] = '\n';
  2749. ofs += 16;
  2750. size -= min(size, 16U);
  2751. }
  2752. return len;
  2753. }
  2754. static ssize_t iwl3945_store_measurement(struct device *d,
  2755. struct device_attribute *attr,
  2756. const char *buf, size_t count)
  2757. {
  2758. struct iwl_priv *priv = dev_get_drvdata(d);
  2759. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2760. struct ieee80211_measurement_params params = {
  2761. .channel = le16_to_cpu(ctx->active.channel),
  2762. .start_time = cpu_to_le64(priv->_3945.last_tsf),
  2763. .duration = cpu_to_le16(1),
  2764. };
  2765. u8 type = IWL_MEASURE_BASIC;
  2766. u8 buffer[32];
  2767. u8 channel;
  2768. if (count) {
  2769. char *p = buffer;
  2770. strncpy(buffer, buf, min(sizeof(buffer), count));
  2771. channel = simple_strtoul(p, NULL, 0);
  2772. if (channel)
  2773. params.channel = channel;
  2774. p = buffer;
  2775. while (*p && *p != ' ')
  2776. p++;
  2777. if (*p)
  2778. type = simple_strtoul(p + 1, NULL, 0);
  2779. }
  2780. IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
  2781. "channel %d (for '%s')\n", type, params.channel, buf);
  2782. iwl3945_get_measurement(priv, &params, type);
  2783. return count;
  2784. }
  2785. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
  2786. iwl3945_show_measurement, iwl3945_store_measurement);
  2787. static ssize_t iwl3945_store_retry_rate(struct device *d,
  2788. struct device_attribute *attr,
  2789. const char *buf, size_t count)
  2790. {
  2791. struct iwl_priv *priv = dev_get_drvdata(d);
  2792. priv->retry_rate = simple_strtoul(buf, NULL, 0);
  2793. if (priv->retry_rate <= 0)
  2794. priv->retry_rate = 1;
  2795. return count;
  2796. }
  2797. static ssize_t iwl3945_show_retry_rate(struct device *d,
  2798. struct device_attribute *attr, char *buf)
  2799. {
  2800. struct iwl_priv *priv = dev_get_drvdata(d);
  2801. return sprintf(buf, "%d", priv->retry_rate);
  2802. }
  2803. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, iwl3945_show_retry_rate,
  2804. iwl3945_store_retry_rate);
  2805. static ssize_t iwl3945_show_channels(struct device *d,
  2806. struct device_attribute *attr, char *buf)
  2807. {
  2808. /* all this shit doesn't belong into sysfs anyway */
  2809. return 0;
  2810. }
  2811. static DEVICE_ATTR(channels, S_IRUSR, iwl3945_show_channels, NULL);
  2812. static ssize_t iwl3945_show_antenna(struct device *d,
  2813. struct device_attribute *attr, char *buf)
  2814. {
  2815. struct iwl_priv *priv = dev_get_drvdata(d);
  2816. if (!iwl_legacy_is_alive(priv))
  2817. return -EAGAIN;
  2818. return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
  2819. }
  2820. static ssize_t iwl3945_store_antenna(struct device *d,
  2821. struct device_attribute *attr,
  2822. const char *buf, size_t count)
  2823. {
  2824. struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
  2825. int ant;
  2826. if (count == 0)
  2827. return 0;
  2828. if (sscanf(buf, "%1i", &ant) != 1) {
  2829. IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
  2830. return count;
  2831. }
  2832. if ((ant >= 0) && (ant <= 2)) {
  2833. IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
  2834. iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
  2835. } else
  2836. IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
  2837. return count;
  2838. }
  2839. static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, iwl3945_show_antenna, iwl3945_store_antenna);
  2840. static ssize_t iwl3945_show_status(struct device *d,
  2841. struct device_attribute *attr, char *buf)
  2842. {
  2843. struct iwl_priv *priv = dev_get_drvdata(d);
  2844. if (!iwl_legacy_is_alive(priv))
  2845. return -EAGAIN;
  2846. return sprintf(buf, "0x%08x\n", (int)priv->status);
  2847. }
  2848. static DEVICE_ATTR(status, S_IRUGO, iwl3945_show_status, NULL);
  2849. static ssize_t iwl3945_dump_error_log(struct device *d,
  2850. struct device_attribute *attr,
  2851. const char *buf, size_t count)
  2852. {
  2853. struct iwl_priv *priv = dev_get_drvdata(d);
  2854. char *p = (char *)buf;
  2855. if (p[0] == '1')
  2856. iwl3945_dump_nic_error_log(priv);
  2857. return strnlen(buf, count);
  2858. }
  2859. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, iwl3945_dump_error_log);
  2860. /*****************************************************************************
  2861. *
  2862. * driver setup and tear down
  2863. *
  2864. *****************************************************************************/
  2865. static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
  2866. {
  2867. priv->workqueue = create_singlethread_workqueue(DRV_NAME);
  2868. init_waitqueue_head(&priv->wait_command_queue);
  2869. INIT_WORK(&priv->restart, iwl3945_bg_restart);
  2870. INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
  2871. INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
  2872. INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
  2873. INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
  2874. iwl_legacy_setup_scan_deferred_work(priv);
  2875. iwl3945_hw_setup_deferred_work(priv);
  2876. init_timer(&priv->watchdog);
  2877. priv->watchdog.data = (unsigned long)priv;
  2878. priv->watchdog.function = iwl_legacy_bg_watchdog;
  2879. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  2880. iwl3945_irq_tasklet, (unsigned long)priv);
  2881. }
  2882. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
  2883. {
  2884. iwl3945_hw_cancel_deferred_work(priv);
  2885. cancel_delayed_work_sync(&priv->init_alive_start);
  2886. cancel_delayed_work(&priv->alive_start);
  2887. iwl_legacy_cancel_scan_deferred_work(priv);
  2888. }
  2889. static struct attribute *iwl3945_sysfs_entries[] = {
  2890. &dev_attr_antenna.attr,
  2891. &dev_attr_channels.attr,
  2892. &dev_attr_dump_errors.attr,
  2893. &dev_attr_flags.attr,
  2894. &dev_attr_filter_flags.attr,
  2895. &dev_attr_measurement.attr,
  2896. &dev_attr_retry_rate.attr,
  2897. &dev_attr_status.attr,
  2898. &dev_attr_temperature.attr,
  2899. &dev_attr_tx_power.attr,
  2900. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  2901. &dev_attr_debug_level.attr,
  2902. #endif
  2903. NULL
  2904. };
  2905. static struct attribute_group iwl3945_attribute_group = {
  2906. .name = NULL, /* put in device directory */
  2907. .attrs = iwl3945_sysfs_entries,
  2908. };
  2909. struct ieee80211_ops iwl3945_hw_ops = {
  2910. .tx = iwl3945_mac_tx,
  2911. .start = iwl3945_mac_start,
  2912. .stop = iwl3945_mac_stop,
  2913. .add_interface = iwl_legacy_mac_add_interface,
  2914. .remove_interface = iwl_legacy_mac_remove_interface,
  2915. .change_interface = iwl_legacy_mac_change_interface,
  2916. .config = iwl_legacy_mac_config,
  2917. .configure_filter = iwl3945_configure_filter,
  2918. .set_key = iwl3945_mac_set_key,
  2919. .conf_tx = iwl_legacy_mac_conf_tx,
  2920. .reset_tsf = iwl_legacy_mac_reset_tsf,
  2921. .bss_info_changed = iwl_legacy_mac_bss_info_changed,
  2922. .hw_scan = iwl_legacy_mac_hw_scan,
  2923. .sta_add = iwl3945_mac_sta_add,
  2924. .sta_remove = iwl_legacy_mac_sta_remove,
  2925. .tx_last_beacon = iwl_legacy_mac_tx_last_beacon,
  2926. };
  2927. static int iwl3945_init_drv(struct iwl_priv *priv)
  2928. {
  2929. int ret;
  2930. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  2931. priv->retry_rate = 1;
  2932. priv->beacon_skb = NULL;
  2933. spin_lock_init(&priv->sta_lock);
  2934. spin_lock_init(&priv->hcmd_lock);
  2935. INIT_LIST_HEAD(&priv->free_frames);
  2936. mutex_init(&priv->mutex);
  2937. priv->ieee_channels = NULL;
  2938. priv->ieee_rates = NULL;
  2939. priv->band = IEEE80211_BAND_2GHZ;
  2940. priv->iw_mode = NL80211_IFTYPE_STATION;
  2941. priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
  2942. /* initialize force reset */
  2943. priv->force_reset.reset_duration = IWL_DELAY_NEXT_FORCE_FW_RELOAD;
  2944. if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
  2945. IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
  2946. eeprom->version);
  2947. ret = -EINVAL;
  2948. goto err;
  2949. }
  2950. ret = iwl_legacy_init_channel_map(priv);
  2951. if (ret) {
  2952. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  2953. goto err;
  2954. }
  2955. /* Set up txpower settings in driver for all channels */
  2956. if (iwl3945_txpower_set_from_eeprom(priv)) {
  2957. ret = -EIO;
  2958. goto err_free_channel_map;
  2959. }
  2960. ret = iwl_legacy_init_geos(priv);
  2961. if (ret) {
  2962. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  2963. goto err_free_channel_map;
  2964. }
  2965. iwl3945_init_hw_rates(priv, priv->ieee_rates);
  2966. return 0;
  2967. err_free_channel_map:
  2968. iwl_legacy_free_channel_map(priv);
  2969. err:
  2970. return ret;
  2971. }
  2972. #define IWL3945_MAX_PROBE_REQUEST 200
  2973. static int iwl3945_setup_mac(struct iwl_priv *priv)
  2974. {
  2975. int ret;
  2976. struct ieee80211_hw *hw = priv->hw;
  2977. hw->rate_control_algorithm = "iwl-3945-rs";
  2978. hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
  2979. hw->vif_data_size = sizeof(struct iwl_vif_priv);
  2980. /* Tell mac80211 our characteristics */
  2981. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  2982. IEEE80211_HW_SPECTRUM_MGMT;
  2983. hw->wiphy->interface_modes =
  2984. priv->contexts[IWL_RXON_CTX_BSS].interface_modes;
  2985. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  2986. WIPHY_FLAG_DISABLE_BEACON_HINTS |
  2987. WIPHY_FLAG_IBSS_RSN;
  2988. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
  2989. /* we create the 802.11 header and a zero-length SSID element */
  2990. hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
  2991. /* Default value; 4 EDCA QOS priorities */
  2992. hw->queues = 4;
  2993. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  2994. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  2995. &priv->bands[IEEE80211_BAND_2GHZ];
  2996. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  2997. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  2998. &priv->bands[IEEE80211_BAND_5GHZ];
  2999. iwl_legacy_leds_init(priv);
  3000. ret = ieee80211_register_hw(priv->hw);
  3001. if (ret) {
  3002. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  3003. return ret;
  3004. }
  3005. priv->mac80211_registered = 1;
  3006. return 0;
  3007. }
  3008. static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3009. {
  3010. int err = 0, i;
  3011. struct iwl_priv *priv;
  3012. struct ieee80211_hw *hw;
  3013. struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
  3014. struct iwl3945_eeprom *eeprom;
  3015. unsigned long flags;
  3016. /***********************
  3017. * 1. Allocating HW data
  3018. * ********************/
  3019. /* mac80211 allocates memory for this device instance, including
  3020. * space for this driver's private structure */
  3021. hw = iwl_legacy_alloc_all(cfg);
  3022. if (hw == NULL) {
  3023. pr_err("Can not allocate network device\n");
  3024. err = -ENOMEM;
  3025. goto out;
  3026. }
  3027. priv = hw->priv;
  3028. SET_IEEE80211_DEV(hw, &pdev->dev);
  3029. priv->cmd_queue = IWL39_CMD_QUEUE_NUM;
  3030. /* 3945 has only one valid context */
  3031. priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
  3032. for (i = 0; i < NUM_IWL_RXON_CTX; i++)
  3033. priv->contexts[i].ctxid = i;
  3034. priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
  3035. priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
  3036. priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
  3037. priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
  3038. priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
  3039. priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
  3040. priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
  3041. BIT(NL80211_IFTYPE_STATION) |
  3042. BIT(NL80211_IFTYPE_ADHOC);
  3043. priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
  3044. priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
  3045. priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
  3046. /*
  3047. * Disabling hardware scan means that mac80211 will perform scans
  3048. * "the hard way", rather than using device's scan.
  3049. */
  3050. if (iwl3945_mod_params.disable_hw_scan) {
  3051. IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
  3052. iwl3945_hw_ops.hw_scan = NULL;
  3053. }
  3054. IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
  3055. priv->cfg = cfg;
  3056. priv->pci_dev = pdev;
  3057. priv->inta_mask = CSR_INI_SET_MASK;
  3058. if (iwl_legacy_alloc_traffic_mem(priv))
  3059. IWL_ERR(priv, "Not enough memory to generate traffic log\n");
  3060. /***************************
  3061. * 2. Initializing PCI bus
  3062. * *************************/
  3063. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  3064. PCIE_LINK_STATE_CLKPM);
  3065. if (pci_enable_device(pdev)) {
  3066. err = -ENODEV;
  3067. goto out_ieee80211_free_hw;
  3068. }
  3069. pci_set_master(pdev);
  3070. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3071. if (!err)
  3072. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3073. if (err) {
  3074. IWL_WARN(priv, "No suitable DMA available.\n");
  3075. goto out_pci_disable_device;
  3076. }
  3077. pci_set_drvdata(pdev, priv);
  3078. err = pci_request_regions(pdev, DRV_NAME);
  3079. if (err)
  3080. goto out_pci_disable_device;
  3081. /***********************
  3082. * 3. Read REV Register
  3083. * ********************/
  3084. priv->hw_base = pci_iomap(pdev, 0, 0);
  3085. if (!priv->hw_base) {
  3086. err = -ENODEV;
  3087. goto out_pci_release_regions;
  3088. }
  3089. IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
  3090. (unsigned long long) pci_resource_len(pdev, 0));
  3091. IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
  3092. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  3093. * PCI Tx retries from interfering with C3 CPU state */
  3094. pci_write_config_byte(pdev, 0x41, 0x00);
  3095. /* these spin locks will be used in apm_ops.init and EEPROM access
  3096. * we should init now
  3097. */
  3098. spin_lock_init(&priv->reg_lock);
  3099. spin_lock_init(&priv->lock);
  3100. /*
  3101. * stop and reset the on-board processor just in case it is in a
  3102. * strange state ... like being left stranded by a primary kernel
  3103. * and this is now the kdump kernel trying to start up
  3104. */
  3105. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  3106. /***********************
  3107. * 4. Read EEPROM
  3108. * ********************/
  3109. /* Read the EEPROM */
  3110. err = iwl_legacy_eeprom_init(priv);
  3111. if (err) {
  3112. IWL_ERR(priv, "Unable to init EEPROM\n");
  3113. goto out_iounmap;
  3114. }
  3115. /* MAC Address location in EEPROM same for 3945/4965 */
  3116. eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  3117. IWL_DEBUG_INFO(priv, "MAC address: %pM\n", eeprom->mac_address);
  3118. SET_IEEE80211_PERM_ADDR(priv->hw, eeprom->mac_address);
  3119. /***********************
  3120. * 5. Setup HW Constants
  3121. * ********************/
  3122. /* Device-specific setup */
  3123. if (iwl3945_hw_set_hw_params(priv)) {
  3124. IWL_ERR(priv, "failed to set hw settings\n");
  3125. goto out_eeprom_free;
  3126. }
  3127. /***********************
  3128. * 6. Setup priv
  3129. * ********************/
  3130. err = iwl3945_init_drv(priv);
  3131. if (err) {
  3132. IWL_ERR(priv, "initializing driver failed\n");
  3133. goto out_unset_hw_params;
  3134. }
  3135. IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
  3136. priv->cfg->name);
  3137. /***********************
  3138. * 7. Setup Services
  3139. * ********************/
  3140. spin_lock_irqsave(&priv->lock, flags);
  3141. iwl_legacy_disable_interrupts(priv);
  3142. spin_unlock_irqrestore(&priv->lock, flags);
  3143. pci_enable_msi(priv->pci_dev);
  3144. err = request_irq(priv->pci_dev->irq, iwl_legacy_isr,
  3145. IRQF_SHARED, DRV_NAME, priv);
  3146. if (err) {
  3147. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
  3148. goto out_disable_msi;
  3149. }
  3150. err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  3151. if (err) {
  3152. IWL_ERR(priv, "failed to create sysfs device attributes\n");
  3153. goto out_release_irq;
  3154. }
  3155. iwl_legacy_set_rxon_channel(priv,
  3156. &priv->bands[IEEE80211_BAND_2GHZ].channels[5],
  3157. &priv->contexts[IWL_RXON_CTX_BSS]);
  3158. iwl3945_setup_deferred_work(priv);
  3159. iwl3945_setup_rx_handlers(priv);
  3160. iwl_legacy_power_initialize(priv);
  3161. /*********************************
  3162. * 8. Setup and Register mac80211
  3163. * *******************************/
  3164. iwl_legacy_enable_interrupts(priv);
  3165. err = iwl3945_setup_mac(priv);
  3166. if (err)
  3167. goto out_remove_sysfs;
  3168. err = iwl_legacy_dbgfs_register(priv, DRV_NAME);
  3169. if (err)
  3170. IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
  3171. /* Start monitoring the killswitch */
  3172. queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
  3173. 2 * HZ);
  3174. return 0;
  3175. out_remove_sysfs:
  3176. destroy_workqueue(priv->workqueue);
  3177. priv->workqueue = NULL;
  3178. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  3179. out_release_irq:
  3180. free_irq(priv->pci_dev->irq, priv);
  3181. out_disable_msi:
  3182. pci_disable_msi(priv->pci_dev);
  3183. iwl_legacy_free_geos(priv);
  3184. iwl_legacy_free_channel_map(priv);
  3185. out_unset_hw_params:
  3186. iwl3945_unset_hw_params(priv);
  3187. out_eeprom_free:
  3188. iwl_legacy_eeprom_free(priv);
  3189. out_iounmap:
  3190. pci_iounmap(pdev, priv->hw_base);
  3191. out_pci_release_regions:
  3192. pci_release_regions(pdev);
  3193. out_pci_disable_device:
  3194. pci_set_drvdata(pdev, NULL);
  3195. pci_disable_device(pdev);
  3196. out_ieee80211_free_hw:
  3197. iwl_legacy_free_traffic_mem(priv);
  3198. ieee80211_free_hw(priv->hw);
  3199. out:
  3200. return err;
  3201. }
  3202. static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
  3203. {
  3204. struct iwl_priv *priv = pci_get_drvdata(pdev);
  3205. unsigned long flags;
  3206. if (!priv)
  3207. return;
  3208. IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
  3209. iwl_legacy_dbgfs_unregister(priv);
  3210. set_bit(STATUS_EXIT_PENDING, &priv->status);
  3211. iwl_legacy_leds_exit(priv);
  3212. if (priv->mac80211_registered) {
  3213. ieee80211_unregister_hw(priv->hw);
  3214. priv->mac80211_registered = 0;
  3215. } else {
  3216. iwl3945_down(priv);
  3217. }
  3218. /*
  3219. * Make sure device is reset to low power before unloading driver.
  3220. * This may be redundant with iwl_down(), but there are paths to
  3221. * run iwl_down() without calling apm_ops.stop(), and there are
  3222. * paths to avoid running iwl_down() at all before leaving driver.
  3223. * This (inexpensive) call *makes sure* device is reset.
  3224. */
  3225. iwl_legacy_apm_stop(priv);
  3226. /* make sure we flush any pending irq or
  3227. * tasklet for the driver
  3228. */
  3229. spin_lock_irqsave(&priv->lock, flags);
  3230. iwl_legacy_disable_interrupts(priv);
  3231. spin_unlock_irqrestore(&priv->lock, flags);
  3232. iwl3945_synchronize_irq(priv);
  3233. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  3234. cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
  3235. iwl3945_dealloc_ucode_pci(priv);
  3236. if (priv->rxq.bd)
  3237. iwl3945_rx_queue_free(priv, &priv->rxq);
  3238. iwl3945_hw_txq_ctx_free(priv);
  3239. iwl3945_unset_hw_params(priv);
  3240. /*netif_stop_queue(dev); */
  3241. flush_workqueue(priv->workqueue);
  3242. /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
  3243. * priv->workqueue... so we can't take down the workqueue
  3244. * until now... */
  3245. destroy_workqueue(priv->workqueue);
  3246. priv->workqueue = NULL;
  3247. iwl_legacy_free_traffic_mem(priv);
  3248. free_irq(pdev->irq, priv);
  3249. pci_disable_msi(pdev);
  3250. pci_iounmap(pdev, priv->hw_base);
  3251. pci_release_regions(pdev);
  3252. pci_disable_device(pdev);
  3253. pci_set_drvdata(pdev, NULL);
  3254. iwl_legacy_free_channel_map(priv);
  3255. iwl_legacy_free_geos(priv);
  3256. kfree(priv->scan_cmd);
  3257. if (priv->beacon_skb)
  3258. dev_kfree_skb(priv->beacon_skb);
  3259. ieee80211_free_hw(priv->hw);
  3260. }
  3261. /*****************************************************************************
  3262. *
  3263. * driver and module entry point
  3264. *
  3265. *****************************************************************************/
  3266. static struct pci_driver iwl3945_driver = {
  3267. .name = DRV_NAME,
  3268. .id_table = iwl3945_hw_card_ids,
  3269. .probe = iwl3945_pci_probe,
  3270. .remove = __devexit_p(iwl3945_pci_remove),
  3271. .driver.pm = IWL_LEGACY_PM_OPS,
  3272. };
  3273. static int __init iwl3945_init(void)
  3274. {
  3275. int ret;
  3276. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  3277. pr_info(DRV_COPYRIGHT "\n");
  3278. ret = iwl3945_rate_control_register();
  3279. if (ret) {
  3280. pr_err("Unable to register rate control algorithm: %d\n", ret);
  3281. return ret;
  3282. }
  3283. ret = pci_register_driver(&iwl3945_driver);
  3284. if (ret) {
  3285. pr_err("Unable to initialize PCI module\n");
  3286. goto error_register;
  3287. }
  3288. return ret;
  3289. error_register:
  3290. iwl3945_rate_control_unregister();
  3291. return ret;
  3292. }
  3293. static void __exit iwl3945_exit(void)
  3294. {
  3295. pci_unregister_driver(&iwl3945_driver);
  3296. iwl3945_rate_control_unregister();
  3297. }
  3298. MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
  3299. module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
  3300. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  3301. module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
  3302. MODULE_PARM_DESC(swcrypto,
  3303. "using software crypto (default 1 [software])");
  3304. module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
  3305. int, S_IRUGO);
  3306. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 1)");
  3307. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  3308. module_param_named(debug, iwlegacy_debug_level, uint, S_IRUGO | S_IWUSR);
  3309. MODULE_PARM_DESC(debug, "debug output mask");
  3310. #endif
  3311. module_param_named(fw_restart, iwl3945_mod_params.restart_fw, int, S_IRUGO);
  3312. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
  3313. module_exit(iwl3945_exit);
  3314. module_init(iwl3945_init);