iwl-3945.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * James P. Ketrenos <ipw2100-admin@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/version.h>
  29. #include <linux/init.h>
  30. #include <linux/pci.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/delay.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/wireless.h>
  36. #include <linux/firmware.h>
  37. #include <linux/etherdevice.h>
  38. #include <asm/unaligned.h>
  39. #include <net/mac80211.h>
  40. #include "iwl-3945.h"
  41. #include "iwl-helpers.h"
  42. #include "iwl-3945-rs.h"
  43. #define IWL_DECLARE_RATE_INFO(r, ip, in, rp, rn, pp, np) \
  44. [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
  45. IWL_RATE_##r##M_IEEE, \
  46. IWL_RATE_##ip##M_INDEX, \
  47. IWL_RATE_##in##M_INDEX, \
  48. IWL_RATE_##rp##M_INDEX, \
  49. IWL_RATE_##rn##M_INDEX, \
  50. IWL_RATE_##pp##M_INDEX, \
  51. IWL_RATE_##np##M_INDEX, \
  52. IWL_RATE_##r##M_INDEX_TABLE, \
  53. IWL_RATE_##ip##M_INDEX_TABLE }
  54. /*
  55. * Parameter order:
  56. * rate, prev rate, next rate, prev tgg rate, next tgg rate
  57. *
  58. * If there isn't a valid next or previous rate then INV is used which
  59. * maps to IWL_RATE_INVALID
  60. *
  61. */
  62. const struct iwl3945_rate_info iwl3945_rates[IWL_RATE_COUNT] = {
  63. IWL_DECLARE_RATE_INFO(1, INV, 2, INV, 2, INV, 2), /* 1mbps */
  64. IWL_DECLARE_RATE_INFO(2, 1, 5, 1, 5, 1, 5), /* 2mbps */
  65. IWL_DECLARE_RATE_INFO(5, 2, 6, 2, 11, 2, 11), /*5.5mbps */
  66. IWL_DECLARE_RATE_INFO(11, 9, 12, 5, 12, 5, 18), /* 11mbps */
  67. IWL_DECLARE_RATE_INFO(6, 5, 9, 5, 11, 5, 11), /* 6mbps */
  68. IWL_DECLARE_RATE_INFO(9, 6, 11, 5, 11, 5, 11), /* 9mbps */
  69. IWL_DECLARE_RATE_INFO(12, 11, 18, 11, 18, 11, 18), /* 12mbps */
  70. IWL_DECLARE_RATE_INFO(18, 12, 24, 12, 24, 11, 24), /* 18mbps */
  71. IWL_DECLARE_RATE_INFO(24, 18, 36, 18, 36, 18, 36), /* 24mbps */
  72. IWL_DECLARE_RATE_INFO(36, 24, 48, 24, 48, 24, 48), /* 36mbps */
  73. IWL_DECLARE_RATE_INFO(48, 36, 54, 36, 54, 36, 54), /* 48mbps */
  74. IWL_DECLARE_RATE_INFO(54, 48, INV, 48, INV, 48, INV),/* 54mbps */
  75. };
  76. /* 1 = enable the iwl3945_disable_events() function */
  77. #define IWL_EVT_DISABLE (0)
  78. #define IWL_EVT_DISABLE_SIZE (1532/32)
  79. /**
  80. * iwl3945_disable_events - Disable selected events in uCode event log
  81. *
  82. * Disable an event by writing "1"s into "disable"
  83. * bitmap in SRAM. Bit position corresponds to Event # (id/type).
  84. * Default values of 0 enable uCode events to be logged.
  85. * Use for only special debugging. This function is just a placeholder as-is,
  86. * you'll need to provide the special bits! ...
  87. * ... and set IWL_EVT_DISABLE to 1. */
  88. void iwl3945_disable_events(struct iwl3945_priv *priv)
  89. {
  90. int ret;
  91. int i;
  92. u32 base; /* SRAM address of event log header */
  93. u32 disable_ptr; /* SRAM address of event-disable bitmap array */
  94. u32 array_size; /* # of u32 entries in array */
  95. u32 evt_disable[IWL_EVT_DISABLE_SIZE] = {
  96. 0x00000000, /* 31 - 0 Event id numbers */
  97. 0x00000000, /* 63 - 32 */
  98. 0x00000000, /* 95 - 64 */
  99. 0x00000000, /* 127 - 96 */
  100. 0x00000000, /* 159 - 128 */
  101. 0x00000000, /* 191 - 160 */
  102. 0x00000000, /* 223 - 192 */
  103. 0x00000000, /* 255 - 224 */
  104. 0x00000000, /* 287 - 256 */
  105. 0x00000000, /* 319 - 288 */
  106. 0x00000000, /* 351 - 320 */
  107. 0x00000000, /* 383 - 352 */
  108. 0x00000000, /* 415 - 384 */
  109. 0x00000000, /* 447 - 416 */
  110. 0x00000000, /* 479 - 448 */
  111. 0x00000000, /* 511 - 480 */
  112. 0x00000000, /* 543 - 512 */
  113. 0x00000000, /* 575 - 544 */
  114. 0x00000000, /* 607 - 576 */
  115. 0x00000000, /* 639 - 608 */
  116. 0x00000000, /* 671 - 640 */
  117. 0x00000000, /* 703 - 672 */
  118. 0x00000000, /* 735 - 704 */
  119. 0x00000000, /* 767 - 736 */
  120. 0x00000000, /* 799 - 768 */
  121. 0x00000000, /* 831 - 800 */
  122. 0x00000000, /* 863 - 832 */
  123. 0x00000000, /* 895 - 864 */
  124. 0x00000000, /* 927 - 896 */
  125. 0x00000000, /* 959 - 928 */
  126. 0x00000000, /* 991 - 960 */
  127. 0x00000000, /* 1023 - 992 */
  128. 0x00000000, /* 1055 - 1024 */
  129. 0x00000000, /* 1087 - 1056 */
  130. 0x00000000, /* 1119 - 1088 */
  131. 0x00000000, /* 1151 - 1120 */
  132. 0x00000000, /* 1183 - 1152 */
  133. 0x00000000, /* 1215 - 1184 */
  134. 0x00000000, /* 1247 - 1216 */
  135. 0x00000000, /* 1279 - 1248 */
  136. 0x00000000, /* 1311 - 1280 */
  137. 0x00000000, /* 1343 - 1312 */
  138. 0x00000000, /* 1375 - 1344 */
  139. 0x00000000, /* 1407 - 1376 */
  140. 0x00000000, /* 1439 - 1408 */
  141. 0x00000000, /* 1471 - 1440 */
  142. 0x00000000, /* 1503 - 1472 */
  143. };
  144. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  145. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  146. IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
  147. return;
  148. }
  149. ret = iwl3945_grab_nic_access(priv);
  150. if (ret) {
  151. IWL_WARNING("Can not read from adapter at this time.\n");
  152. return;
  153. }
  154. disable_ptr = iwl3945_read_targ_mem(priv, base + (4 * sizeof(u32)));
  155. array_size = iwl3945_read_targ_mem(priv, base + (5 * sizeof(u32)));
  156. iwl3945_release_nic_access(priv);
  157. if (IWL_EVT_DISABLE && (array_size == IWL_EVT_DISABLE_SIZE)) {
  158. IWL_DEBUG_INFO("Disabling selected uCode log events at 0x%x\n",
  159. disable_ptr);
  160. ret = iwl3945_grab_nic_access(priv);
  161. for (i = 0; i < IWL_EVT_DISABLE_SIZE; i++)
  162. iwl3945_write_targ_mem(priv,
  163. disable_ptr + (i * sizeof(u32)),
  164. evt_disable[i]);
  165. iwl3945_release_nic_access(priv);
  166. } else {
  167. IWL_DEBUG_INFO("Selected uCode log events may be disabled\n");
  168. IWL_DEBUG_INFO(" by writing \"1\"s into disable bitmap\n");
  169. IWL_DEBUG_INFO(" in SRAM at 0x%x, size %d u32s\n",
  170. disable_ptr, array_size);
  171. }
  172. }
  173. static int iwl3945_hwrate_to_plcp_idx(u8 plcp)
  174. {
  175. int idx;
  176. for (idx = 0; idx < IWL_RATE_COUNT; idx++)
  177. if (iwl3945_rates[idx].plcp == plcp)
  178. return idx;
  179. return -1;
  180. }
  181. /**
  182. * iwl3945_get_antenna_flags - Get antenna flags for RXON command
  183. * @priv: eeprom and antenna fields are used to determine antenna flags
  184. *
  185. * priv->eeprom is used to determine if antenna AUX/MAIN are reversed
  186. * priv->antenna specifies the antenna diversity mode:
  187. *
  188. * IWL_ANTENNA_DIVERISTY - NIC selects best antenna by itself
  189. * IWL_ANTENNA_MAIN - Force MAIN antenna
  190. * IWL_ANTENNA_AUX - Force AUX antenna
  191. */
  192. __le32 iwl3945_get_antenna_flags(const struct iwl3945_priv *priv)
  193. {
  194. switch (priv->antenna) {
  195. case IWL_ANTENNA_DIVERSITY:
  196. return 0;
  197. case IWL_ANTENNA_MAIN:
  198. if (priv->eeprom.antenna_switch_type)
  199. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  200. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  201. case IWL_ANTENNA_AUX:
  202. if (priv->eeprom.antenna_switch_type)
  203. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  204. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  205. }
  206. /* bad antenna selector value */
  207. IWL_ERROR("Bad antenna selector value (0x%x)\n", priv->antenna);
  208. return 0; /* "diversity" is default if error */
  209. }
  210. /*****************************************************************************
  211. *
  212. * Intel PRO/Wireless 3945ABG/BG Network Connection
  213. *
  214. * RX handler implementations
  215. *
  216. * Used by iwl-base.c
  217. *
  218. *****************************************************************************/
  219. void iwl3945_hw_rx_statistics(struct iwl3945_priv *priv, struct iwl3945_rx_mem_buffer *rxb)
  220. {
  221. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  222. IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
  223. (int)sizeof(struct iwl3945_notif_statistics),
  224. le32_to_cpu(pkt->len));
  225. memcpy(&priv->statistics, pkt->u.raw, sizeof(priv->statistics));
  226. priv->last_statistics_time = jiffies;
  227. }
  228. /******************************************************************************
  229. *
  230. * Misc. internal state and helper functions
  231. *
  232. ******************************************************************************/
  233. #ifdef CONFIG_IWL3945_DEBUG
  234. /**
  235. * iwl3945_report_frame - dump frame to syslog during debug sessions
  236. *
  237. * You may hack this function to show different aspects of received frames,
  238. * including selective frame dumps.
  239. * group100 parameter selects whether to show 1 out of 100 good frames.
  240. */
  241. static void iwl3945_dbg_report_frame(struct iwl3945_priv *priv,
  242. struct iwl3945_rx_packet *pkt,
  243. struct ieee80211_hdr *header, int group100)
  244. {
  245. u32 to_us;
  246. u32 print_summary = 0;
  247. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  248. u32 hundred = 0;
  249. u32 dataframe = 0;
  250. u16 fc;
  251. u16 seq_ctl;
  252. u16 channel;
  253. u16 phy_flags;
  254. u16 length;
  255. u16 status;
  256. u16 bcn_tmr;
  257. u32 tsf_low;
  258. u64 tsf;
  259. u8 rssi;
  260. u8 agc;
  261. u16 sig_avg;
  262. u16 noise_diff;
  263. struct iwl3945_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
  264. struct iwl3945_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
  265. struct iwl3945_rx_frame_end *rx_end = IWL_RX_END(pkt);
  266. u8 *data = IWL_RX_DATA(pkt);
  267. /* MAC header */
  268. fc = le16_to_cpu(header->frame_control);
  269. seq_ctl = le16_to_cpu(header->seq_ctrl);
  270. /* metadata */
  271. channel = le16_to_cpu(rx_hdr->channel);
  272. phy_flags = le16_to_cpu(rx_hdr->phy_flags);
  273. length = le16_to_cpu(rx_hdr->len);
  274. /* end-of-frame status and timestamp */
  275. status = le32_to_cpu(rx_end->status);
  276. bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
  277. tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
  278. tsf = le64_to_cpu(rx_end->timestamp);
  279. /* signal statistics */
  280. rssi = rx_stats->rssi;
  281. agc = rx_stats->agc;
  282. sig_avg = le16_to_cpu(rx_stats->sig_avg);
  283. noise_diff = le16_to_cpu(rx_stats->noise_diff);
  284. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  285. /* if data frame is to us and all is good,
  286. * (optionally) print summary for only 1 out of every 100 */
  287. if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
  288. (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  289. dataframe = 1;
  290. if (!group100)
  291. print_summary = 1; /* print each frame */
  292. else if (priv->framecnt_to_us < 100) {
  293. priv->framecnt_to_us++;
  294. print_summary = 0;
  295. } else {
  296. priv->framecnt_to_us = 0;
  297. print_summary = 1;
  298. hundred = 1;
  299. }
  300. } else {
  301. /* print summary for all other frames */
  302. print_summary = 1;
  303. }
  304. if (print_summary) {
  305. char *title;
  306. u32 rate;
  307. if (hundred)
  308. title = "100Frames";
  309. else if (fc & IEEE80211_FCTL_RETRY)
  310. title = "Retry";
  311. else if (ieee80211_is_assoc_response(fc))
  312. title = "AscRsp";
  313. else if (ieee80211_is_reassoc_response(fc))
  314. title = "RasRsp";
  315. else if (ieee80211_is_probe_response(fc)) {
  316. title = "PrbRsp";
  317. print_dump = 1; /* dump frame contents */
  318. } else if (ieee80211_is_beacon(fc)) {
  319. title = "Beacon";
  320. print_dump = 1; /* dump frame contents */
  321. } else if (ieee80211_is_atim(fc))
  322. title = "ATIM";
  323. else if (ieee80211_is_auth(fc))
  324. title = "Auth";
  325. else if (ieee80211_is_deauth(fc))
  326. title = "DeAuth";
  327. else if (ieee80211_is_disassoc(fc))
  328. title = "DisAssoc";
  329. else
  330. title = "Frame";
  331. rate = iwl3945_hwrate_to_plcp_idx(rx_hdr->rate);
  332. if (rate == -1)
  333. rate = 0;
  334. else
  335. rate = iwl3945_rates[rate].ieee / 2;
  336. /* print frame summary.
  337. * MAC addresses show just the last byte (for brevity),
  338. * but you can hack it to show more, if you'd like to. */
  339. if (dataframe)
  340. IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
  341. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  342. title, fc, header->addr1[5],
  343. length, rssi, channel, rate);
  344. else {
  345. /* src/dst addresses assume managed mode */
  346. IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
  347. "src=0x%02x, rssi=%u, tim=%lu usec, "
  348. "phy=0x%02x, chnl=%d\n",
  349. title, fc, header->addr1[5],
  350. header->addr3[5], rssi,
  351. tsf_low - priv->scan_start_tsf,
  352. phy_flags, channel);
  353. }
  354. }
  355. if (print_dump)
  356. iwl3945_print_hex_dump(IWL_DL_RX, data, length);
  357. }
  358. #else
  359. static inline void iwl3945_dbg_report_frame(struct iwl3945_priv *priv,
  360. struct iwl3945_rx_packet *pkt,
  361. struct ieee80211_hdr *header, int group100)
  362. {
  363. }
  364. #endif
  365. static void iwl3945_add_radiotap(struct iwl3945_priv *priv,
  366. struct sk_buff *skb,
  367. struct iwl3945_rx_frame_hdr *rx_hdr,
  368. struct ieee80211_rx_status *stats)
  369. {
  370. /* First cache any information we need before we overwrite
  371. * the information provided in the skb from the hardware */
  372. s8 signal = stats->ssi;
  373. s8 noise = 0;
  374. int rate = stats->rate_idx;
  375. u64 tsf = stats->mactime;
  376. __le16 phy_flags_hw = rx_hdr->phy_flags;
  377. struct iwl3945_rt_rx_hdr {
  378. struct ieee80211_radiotap_header rt_hdr;
  379. __le64 rt_tsf; /* TSF */
  380. u8 rt_flags; /* radiotap packet flags */
  381. u8 rt_rate; /* rate in 500kb/s */
  382. __le16 rt_channelMHz; /* channel in MHz */
  383. __le16 rt_chbitmask; /* channel bitfield */
  384. s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
  385. s8 rt_dbmnoise;
  386. u8 rt_antenna; /* antenna number */
  387. } __attribute__ ((packed)) *iwl3945_rt;
  388. if (skb_headroom(skb) < sizeof(*iwl3945_rt)) {
  389. if (net_ratelimit())
  390. printk(KERN_ERR "not enough headroom [%d] for "
  391. "radiotap head [%zd]\n",
  392. skb_headroom(skb), sizeof(*iwl3945_rt));
  393. return;
  394. }
  395. /* put radiotap header in front of 802.11 header and data */
  396. iwl3945_rt = (void *)skb_push(skb, sizeof(*iwl3945_rt));
  397. /* initialise radiotap header */
  398. iwl3945_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  399. iwl3945_rt->rt_hdr.it_pad = 0;
  400. /* total header + data */
  401. put_unaligned(cpu_to_le16(sizeof(*iwl3945_rt)),
  402. &iwl3945_rt->rt_hdr.it_len);
  403. /* Indicate all the fields we add to the radiotap header */
  404. put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
  405. (1 << IEEE80211_RADIOTAP_FLAGS) |
  406. (1 << IEEE80211_RADIOTAP_RATE) |
  407. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  408. (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
  409. (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
  410. (1 << IEEE80211_RADIOTAP_ANTENNA)),
  411. &iwl3945_rt->rt_hdr.it_present);
  412. /* Zero the flags, we'll add to them as we go */
  413. iwl3945_rt->rt_flags = 0;
  414. put_unaligned(cpu_to_le64(tsf), &iwl3945_rt->rt_tsf);
  415. iwl3945_rt->rt_dbmsignal = signal;
  416. iwl3945_rt->rt_dbmnoise = noise;
  417. /* Convert the channel frequency and set the flags */
  418. put_unaligned(cpu_to_le16(stats->freq), &iwl3945_rt->rt_channelMHz);
  419. if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
  420. put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
  421. IEEE80211_CHAN_5GHZ),
  422. &iwl3945_rt->rt_chbitmask);
  423. else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
  424. put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
  425. IEEE80211_CHAN_2GHZ),
  426. &iwl3945_rt->rt_chbitmask);
  427. else /* 802.11g */
  428. put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
  429. IEEE80211_CHAN_2GHZ),
  430. &iwl3945_rt->rt_chbitmask);
  431. if (rate == -1)
  432. iwl3945_rt->rt_rate = 0;
  433. else
  434. iwl3945_rt->rt_rate = iwl3945_rates[rate].ieee;
  435. /* antenna number */
  436. iwl3945_rt->rt_antenna =
  437. le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
  438. /* set the preamble flag if we have it */
  439. if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  440. iwl3945_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
  441. stats->flag |= RX_FLAG_RADIOTAP;
  442. }
  443. static void iwl3945_handle_data_packet(struct iwl3945_priv *priv, int is_data,
  444. struct iwl3945_rx_mem_buffer *rxb,
  445. struct ieee80211_rx_status *stats)
  446. {
  447. struct ieee80211_hdr *hdr;
  448. struct iwl3945_rx_packet *pkt = (struct iwl3945_rx_packet *)rxb->skb->data;
  449. struct iwl3945_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
  450. struct iwl3945_rx_frame_end *rx_end = IWL_RX_END(pkt);
  451. short len = le16_to_cpu(rx_hdr->len);
  452. /* We received data from the HW, so stop the watchdog */
  453. if (unlikely((len + IWL_RX_FRAME_SIZE) > skb_tailroom(rxb->skb))) {
  454. IWL_DEBUG_DROP("Corruption detected!\n");
  455. return;
  456. }
  457. /* We only process data packets if the interface is open */
  458. if (unlikely(!priv->is_open)) {
  459. IWL_DEBUG_DROP_LIMIT
  460. ("Dropping packet while interface is not open.\n");
  461. return;
  462. }
  463. skb_reserve(rxb->skb, (void *)rx_hdr->payload - (void *)pkt);
  464. /* Set the size of the skb to the size of the frame */
  465. skb_put(rxb->skb, le16_to_cpu(rx_hdr->len));
  466. hdr = (void *)rxb->skb->data;
  467. if (iwl3945_param_hwcrypto)
  468. iwl3945_set_decrypted_flag(priv, rxb->skb,
  469. le32_to_cpu(rx_end->status), stats);
  470. if (priv->add_radiotap)
  471. iwl3945_add_radiotap(priv, rxb->skb, rx_hdr, stats);
  472. ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
  473. rxb->skb = NULL;
  474. }
  475. #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
  476. static void iwl3945_rx_reply_rx(struct iwl3945_priv *priv,
  477. struct iwl3945_rx_mem_buffer *rxb)
  478. {
  479. struct ieee80211_hdr *header;
  480. struct ieee80211_rx_status rx_status;
  481. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  482. struct iwl3945_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
  483. struct iwl3945_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
  484. struct iwl3945_rx_frame_end *rx_end = IWL_RX_END(pkt);
  485. int snr;
  486. u16 rx_stats_sig_avg = le16_to_cpu(rx_stats->sig_avg);
  487. u16 rx_stats_noise_diff = le16_to_cpu(rx_stats->noise_diff);
  488. u8 network_packet;
  489. rx_status.antenna = 0;
  490. rx_status.flag = 0;
  491. rx_status.mactime = le64_to_cpu(rx_end->timestamp);
  492. rx_status.freq = ieee80211chan2mhz(le16_to_cpu(rx_hdr->channel));
  493. rx_status.band = (rx_hdr->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  494. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  495. rx_status.rate_idx = iwl3945_hwrate_to_plcp_idx(rx_hdr->rate);
  496. if (rx_status.band == IEEE80211_BAND_5GHZ)
  497. rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
  498. if ((unlikely(rx_stats->phy_count > 20))) {
  499. IWL_DEBUG_DROP
  500. ("dsp size out of range [0,20]: "
  501. "%d/n", rx_stats->phy_count);
  502. return;
  503. }
  504. if (!(rx_end->status & RX_RES_STATUS_NO_CRC32_ERROR)
  505. || !(rx_end->status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  506. IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n", rx_end->status);
  507. return;
  508. }
  509. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
  510. iwl3945_handle_data_packet(priv, 1, rxb, &rx_status);
  511. return;
  512. }
  513. /* Convert 3945's rssi indicator to dBm */
  514. rx_status.ssi = rx_stats->rssi - IWL_RSSI_OFFSET;
  515. /* Set default noise value to -127 */
  516. if (priv->last_rx_noise == 0)
  517. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  518. /* 3945 provides noise info for OFDM frames only.
  519. * sig_avg and noise_diff are measured by the 3945's digital signal
  520. * processor (DSP), and indicate linear levels of signal level and
  521. * distortion/noise within the packet preamble after
  522. * automatic gain control (AGC). sig_avg should stay fairly
  523. * constant if the radio's AGC is working well.
  524. * Since these values are linear (not dB or dBm), linear
  525. * signal-to-noise ratio (SNR) is (sig_avg / noise_diff).
  526. * Convert linear SNR to dB SNR, then subtract that from rssi dBm
  527. * to obtain noise level in dBm.
  528. * Calculate rx_status.signal (quality indicator in %) based on SNR. */
  529. if (rx_stats_noise_diff) {
  530. snr = rx_stats_sig_avg / rx_stats_noise_diff;
  531. rx_status.noise = rx_status.ssi -
  532. iwl3945_calc_db_from_ratio(snr);
  533. rx_status.signal = iwl3945_calc_sig_qual(rx_status.ssi,
  534. rx_status.noise);
  535. /* If noise info not available, calculate signal quality indicator (%)
  536. * using just the dBm signal level. */
  537. } else {
  538. rx_status.noise = priv->last_rx_noise;
  539. rx_status.signal = iwl3945_calc_sig_qual(rx_status.ssi, 0);
  540. }
  541. IWL_DEBUG_STATS("Rssi %d noise %d qual %d sig_avg %d noise_diff %d\n",
  542. rx_status.ssi, rx_status.noise, rx_status.signal,
  543. rx_stats_sig_avg, rx_stats_noise_diff);
  544. header = (struct ieee80211_hdr *)IWL_RX_DATA(pkt);
  545. network_packet = iwl3945_is_network_packet(priv, header);
  546. IWL_DEBUG_STATS_LIMIT("[%c] %d RSSI:%d Signal:%u, Noise:%u, Rate:%u\n",
  547. network_packet ? '*' : ' ',
  548. le16_to_cpu(rx_hdr->channel),
  549. rx_status.ssi, rx_status.ssi,
  550. rx_status.ssi, rx_status.rate_idx);
  551. #ifdef CONFIG_IWL3945_DEBUG
  552. if (iwl3945_debug_level & (IWL_DL_RX))
  553. /* Set "1" to report good data frames in groups of 100 */
  554. iwl3945_dbg_report_frame(priv, pkt, header, 1);
  555. #endif
  556. if (network_packet) {
  557. priv->last_beacon_time = le32_to_cpu(rx_end->beacon_timestamp);
  558. priv->last_tsf = le64_to_cpu(rx_end->timestamp);
  559. priv->last_rx_rssi = rx_status.ssi;
  560. priv->last_rx_noise = rx_status.noise;
  561. }
  562. switch (le16_to_cpu(header->frame_control) & IEEE80211_FCTL_FTYPE) {
  563. case IEEE80211_FTYPE_MGMT:
  564. switch (le16_to_cpu(header->frame_control) &
  565. IEEE80211_FCTL_STYPE) {
  566. case IEEE80211_STYPE_PROBE_RESP:
  567. case IEEE80211_STYPE_BEACON:{
  568. /* If this is a beacon or probe response for
  569. * our network then cache the beacon
  570. * timestamp */
  571. if ((((priv->iw_mode == IEEE80211_IF_TYPE_STA)
  572. && !compare_ether_addr(header->addr2,
  573. priv->bssid)) ||
  574. ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  575. && !compare_ether_addr(header->addr3,
  576. priv->bssid)))) {
  577. struct ieee80211_mgmt *mgmt =
  578. (struct ieee80211_mgmt *)header;
  579. __le32 *pos;
  580. pos =
  581. (__le32 *) & mgmt->u.beacon.
  582. timestamp;
  583. priv->timestamp0 = le32_to_cpu(pos[0]);
  584. priv->timestamp1 = le32_to_cpu(pos[1]);
  585. priv->beacon_int = le16_to_cpu(
  586. mgmt->u.beacon.beacon_int);
  587. if (priv->call_post_assoc_from_beacon &&
  588. (priv->iw_mode ==
  589. IEEE80211_IF_TYPE_STA))
  590. queue_work(priv->workqueue,
  591. &priv->post_associate.work);
  592. priv->call_post_assoc_from_beacon = 0;
  593. }
  594. break;
  595. }
  596. case IEEE80211_STYPE_ACTION:
  597. /* TODO: Parse 802.11h frames for CSA... */
  598. break;
  599. /*
  600. * TODO: Use the new callback function from
  601. * mac80211 instead of sniffing these packets.
  602. */
  603. case IEEE80211_STYPE_ASSOC_RESP:
  604. case IEEE80211_STYPE_REASSOC_RESP:{
  605. struct ieee80211_mgmt *mgnt =
  606. (struct ieee80211_mgmt *)header;
  607. /* We have just associated, give some
  608. * time for the 4-way handshake if
  609. * any. Don't start scan too early. */
  610. priv->next_scan_jiffies = jiffies +
  611. IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
  612. priv->assoc_id = (~((1 << 15) | (1 << 14)) &
  613. le16_to_cpu(mgnt->u.
  614. assoc_resp.aid));
  615. priv->assoc_capability =
  616. le16_to_cpu(mgnt->u.assoc_resp.capab_info);
  617. if (priv->beacon_int)
  618. queue_work(priv->workqueue,
  619. &priv->post_associate.work);
  620. else
  621. priv->call_post_assoc_from_beacon = 1;
  622. break;
  623. }
  624. case IEEE80211_STYPE_PROBE_REQ:{
  625. DECLARE_MAC_BUF(mac1);
  626. DECLARE_MAC_BUF(mac2);
  627. DECLARE_MAC_BUF(mac3);
  628. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  629. IWL_DEBUG_DROP
  630. ("Dropping (non network): %s"
  631. ", %s, %s\n",
  632. print_mac(mac1, header->addr1),
  633. print_mac(mac2, header->addr2),
  634. print_mac(mac3, header->addr3));
  635. return;
  636. }
  637. }
  638. iwl3945_handle_data_packet(priv, 0, rxb, &rx_status);
  639. break;
  640. case IEEE80211_FTYPE_CTL:
  641. break;
  642. case IEEE80211_FTYPE_DATA: {
  643. DECLARE_MAC_BUF(mac1);
  644. DECLARE_MAC_BUF(mac2);
  645. DECLARE_MAC_BUF(mac3);
  646. if (unlikely(iwl3945_is_duplicate_packet(priv, header)))
  647. IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
  648. print_mac(mac1, header->addr1),
  649. print_mac(mac2, header->addr2),
  650. print_mac(mac3, header->addr3));
  651. else
  652. iwl3945_handle_data_packet(priv, 1, rxb, &rx_status);
  653. break;
  654. }
  655. }
  656. }
  657. int iwl3945_hw_txq_attach_buf_to_tfd(struct iwl3945_priv *priv, void *ptr,
  658. dma_addr_t addr, u16 len)
  659. {
  660. int count;
  661. u32 pad;
  662. struct iwl3945_tfd_frame *tfd = (struct iwl3945_tfd_frame *)ptr;
  663. count = TFD_CTL_COUNT_GET(le32_to_cpu(tfd->control_flags));
  664. pad = TFD_CTL_PAD_GET(le32_to_cpu(tfd->control_flags));
  665. if ((count >= NUM_TFD_CHUNKS) || (count < 0)) {
  666. IWL_ERROR("Error can not send more than %d chunks\n",
  667. NUM_TFD_CHUNKS);
  668. return -EINVAL;
  669. }
  670. tfd->pa[count].addr = cpu_to_le32(addr);
  671. tfd->pa[count].len = cpu_to_le32(len);
  672. count++;
  673. tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(count) |
  674. TFD_CTL_PAD_SET(pad));
  675. return 0;
  676. }
  677. /**
  678. * iwl3945_hw_txq_free_tfd - Free one TFD, those at index [txq->q.read_ptr]
  679. *
  680. * Does NOT advance any indexes
  681. */
  682. int iwl3945_hw_txq_free_tfd(struct iwl3945_priv *priv, struct iwl3945_tx_queue *txq)
  683. {
  684. struct iwl3945_tfd_frame *bd_tmp = (struct iwl3945_tfd_frame *)&txq->bd[0];
  685. struct iwl3945_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
  686. struct pci_dev *dev = priv->pci_dev;
  687. int i;
  688. int counter;
  689. /* classify bd */
  690. if (txq->q.id == IWL_CMD_QUEUE_NUM)
  691. /* nothing to cleanup after for host commands */
  692. return 0;
  693. /* sanity check */
  694. counter = TFD_CTL_COUNT_GET(le32_to_cpu(bd->control_flags));
  695. if (counter > NUM_TFD_CHUNKS) {
  696. IWL_ERROR("Too many chunks: %i\n", counter);
  697. /* @todo issue fatal error, it is quite serious situation */
  698. return 0;
  699. }
  700. /* unmap chunks if any */
  701. for (i = 1; i < counter; i++) {
  702. pci_unmap_single(dev, le32_to_cpu(bd->pa[i].addr),
  703. le32_to_cpu(bd->pa[i].len), PCI_DMA_TODEVICE);
  704. if (txq->txb[txq->q.read_ptr].skb[0]) {
  705. struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[0];
  706. if (txq->txb[txq->q.read_ptr].skb[0]) {
  707. /* Can be called from interrupt context */
  708. dev_kfree_skb_any(skb);
  709. txq->txb[txq->q.read_ptr].skb[0] = NULL;
  710. }
  711. }
  712. }
  713. return 0;
  714. }
  715. u8 iwl3945_hw_find_station(struct iwl3945_priv *priv, const u8 *addr)
  716. {
  717. int i;
  718. int ret = IWL_INVALID_STATION;
  719. unsigned long flags;
  720. DECLARE_MAC_BUF(mac);
  721. spin_lock_irqsave(&priv->sta_lock, flags);
  722. for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
  723. if ((priv->stations[i].used) &&
  724. (!compare_ether_addr
  725. (priv->stations[i].sta.sta.addr, addr))) {
  726. ret = i;
  727. goto out;
  728. }
  729. IWL_DEBUG_INFO("can not find STA %s (total %d)\n",
  730. print_mac(mac, addr), priv->num_stations);
  731. out:
  732. spin_unlock_irqrestore(&priv->sta_lock, flags);
  733. return ret;
  734. }
  735. /**
  736. * iwl3945_hw_build_tx_cmd_rate - Add rate portion to TX_CMD:
  737. *
  738. */
  739. void iwl3945_hw_build_tx_cmd_rate(struct iwl3945_priv *priv,
  740. struct iwl3945_cmd *cmd,
  741. struct ieee80211_tx_control *ctrl,
  742. struct ieee80211_hdr *hdr, int sta_id, int tx_id)
  743. {
  744. unsigned long flags;
  745. u16 rate_index = min(ctrl->tx_rate->hw_value & 0xffff, IWL_RATE_COUNT - 1);
  746. u16 rate_mask;
  747. int rate;
  748. u8 rts_retry_limit;
  749. u8 data_retry_limit;
  750. __le32 tx_flags;
  751. u16 fc = le16_to_cpu(hdr->frame_control);
  752. rate = iwl3945_rates[rate_index].plcp;
  753. tx_flags = cmd->cmd.tx.tx_flags;
  754. /* We need to figure out how to get the sta->supp_rates while
  755. * in this running context; perhaps encoding into ctrl->tx_rate? */
  756. rate_mask = IWL_RATES_MASK;
  757. spin_lock_irqsave(&priv->sta_lock, flags);
  758. priv->stations[sta_id].current_rate.rate_n_flags = rate;
  759. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  760. (sta_id != IWL3945_BROADCAST_ID) &&
  761. (sta_id != IWL_MULTICAST_ID))
  762. priv->stations[IWL_STA_ID].current_rate.rate_n_flags = rate;
  763. spin_unlock_irqrestore(&priv->sta_lock, flags);
  764. if (tx_id >= IWL_CMD_QUEUE_NUM)
  765. rts_retry_limit = 3;
  766. else
  767. rts_retry_limit = 7;
  768. if (ieee80211_is_probe_response(fc)) {
  769. data_retry_limit = 3;
  770. if (data_retry_limit < rts_retry_limit)
  771. rts_retry_limit = data_retry_limit;
  772. } else
  773. data_retry_limit = IWL_DEFAULT_TX_RETRY;
  774. if (priv->data_retry_limit != -1)
  775. data_retry_limit = priv->data_retry_limit;
  776. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
  777. switch (fc & IEEE80211_FCTL_STYPE) {
  778. case IEEE80211_STYPE_AUTH:
  779. case IEEE80211_STYPE_DEAUTH:
  780. case IEEE80211_STYPE_ASSOC_REQ:
  781. case IEEE80211_STYPE_REASSOC_REQ:
  782. if (tx_flags & TX_CMD_FLG_RTS_MSK) {
  783. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  784. tx_flags |= TX_CMD_FLG_CTS_MSK;
  785. }
  786. break;
  787. default:
  788. break;
  789. }
  790. }
  791. cmd->cmd.tx.rts_retry_limit = rts_retry_limit;
  792. cmd->cmd.tx.data_retry_limit = data_retry_limit;
  793. cmd->cmd.tx.rate = rate;
  794. cmd->cmd.tx.tx_flags = tx_flags;
  795. /* OFDM */
  796. cmd->cmd.tx.supp_rates[0] =
  797. ((rate_mask & IWL_OFDM_RATES_MASK) >> IWL_FIRST_OFDM_RATE) & 0xFF;
  798. /* CCK */
  799. cmd->cmd.tx.supp_rates[1] = (rate_mask & 0xF);
  800. IWL_DEBUG_RATE("Tx sta id: %d, rate: %d (plcp), flags: 0x%4X "
  801. "cck/ofdm mask: 0x%x/0x%x\n", sta_id,
  802. cmd->cmd.tx.rate, le32_to_cpu(cmd->cmd.tx.tx_flags),
  803. cmd->cmd.tx.supp_rates[1], cmd->cmd.tx.supp_rates[0]);
  804. }
  805. u8 iwl3945_sync_sta(struct iwl3945_priv *priv, int sta_id, u16 tx_rate, u8 flags)
  806. {
  807. unsigned long flags_spin;
  808. struct iwl3945_station_entry *station;
  809. if (sta_id == IWL_INVALID_STATION)
  810. return IWL_INVALID_STATION;
  811. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  812. station = &priv->stations[sta_id];
  813. station->sta.sta.modify_mask = STA_MODIFY_TX_RATE_MSK;
  814. station->sta.rate_n_flags = cpu_to_le16(tx_rate);
  815. station->current_rate.rate_n_flags = tx_rate;
  816. station->sta.mode = STA_CONTROL_MODIFY_MSK;
  817. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  818. iwl3945_send_add_station(priv, &station->sta, flags);
  819. IWL_DEBUG_RATE("SCALE sync station %d to rate %d\n",
  820. sta_id, tx_rate);
  821. return sta_id;
  822. }
  823. static int iwl3945_nic_set_pwr_src(struct iwl3945_priv *priv, int pwr_max)
  824. {
  825. int rc;
  826. unsigned long flags;
  827. spin_lock_irqsave(&priv->lock, flags);
  828. rc = iwl3945_grab_nic_access(priv);
  829. if (rc) {
  830. spin_unlock_irqrestore(&priv->lock, flags);
  831. return rc;
  832. }
  833. if (!pwr_max) {
  834. u32 val;
  835. rc = pci_read_config_dword(priv->pci_dev,
  836. PCI_POWER_SOURCE, &val);
  837. if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT) {
  838. iwl3945_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  839. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  840. ~APMG_PS_CTRL_MSK_PWR_SRC);
  841. iwl3945_release_nic_access(priv);
  842. iwl3945_poll_bit(priv, CSR_GPIO_IN,
  843. CSR_GPIO_IN_VAL_VAUX_PWR_SRC,
  844. CSR_GPIO_IN_BIT_AUX_POWER, 5000);
  845. } else
  846. iwl3945_release_nic_access(priv);
  847. } else {
  848. iwl3945_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  849. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  850. ~APMG_PS_CTRL_MSK_PWR_SRC);
  851. iwl3945_release_nic_access(priv);
  852. iwl3945_poll_bit(priv, CSR_GPIO_IN, CSR_GPIO_IN_VAL_VMAIN_PWR_SRC,
  853. CSR_GPIO_IN_BIT_AUX_POWER, 5000); /* uS */
  854. }
  855. spin_unlock_irqrestore(&priv->lock, flags);
  856. return rc;
  857. }
  858. static int iwl3945_rx_init(struct iwl3945_priv *priv, struct iwl3945_rx_queue *rxq)
  859. {
  860. int rc;
  861. unsigned long flags;
  862. spin_lock_irqsave(&priv->lock, flags);
  863. rc = iwl3945_grab_nic_access(priv);
  864. if (rc) {
  865. spin_unlock_irqrestore(&priv->lock, flags);
  866. return rc;
  867. }
  868. iwl3945_write_direct32(priv, FH_RCSR_RBD_BASE(0), rxq->dma_addr);
  869. iwl3945_write_direct32(priv, FH_RCSR_RPTR_ADDR(0),
  870. priv->hw_setting.shared_phys +
  871. offsetof(struct iwl3945_shared, rx_read_ptr[0]));
  872. iwl3945_write_direct32(priv, FH_RCSR_WPTR(0), 0);
  873. iwl3945_write_direct32(priv, FH_RCSR_CONFIG(0),
  874. ALM_FH_RCSR_RX_CONFIG_REG_VAL_DMA_CHNL_EN_ENABLE |
  875. ALM_FH_RCSR_RX_CONFIG_REG_VAL_RDRBD_EN_ENABLE |
  876. ALM_FH_RCSR_RX_CONFIG_REG_BIT_WR_STTS_EN |
  877. ALM_FH_RCSR_RX_CONFIG_REG_VAL_MAX_FRAG_SIZE_128 |
  878. (RX_QUEUE_SIZE_LOG << ALM_FH_RCSR_RX_CONFIG_REG_POS_RBDC_SIZE) |
  879. ALM_FH_RCSR_RX_CONFIG_REG_VAL_IRQ_DEST_INT_HOST |
  880. (1 << ALM_FH_RCSR_RX_CONFIG_REG_POS_IRQ_RBTH) |
  881. ALM_FH_RCSR_RX_CONFIG_REG_VAL_MSG_MODE_FH);
  882. /* fake read to flush all prev I/O */
  883. iwl3945_read_direct32(priv, FH_RSSR_CTRL);
  884. iwl3945_release_nic_access(priv);
  885. spin_unlock_irqrestore(&priv->lock, flags);
  886. return 0;
  887. }
  888. static int iwl3945_tx_reset(struct iwl3945_priv *priv)
  889. {
  890. int rc;
  891. unsigned long flags;
  892. spin_lock_irqsave(&priv->lock, flags);
  893. rc = iwl3945_grab_nic_access(priv);
  894. if (rc) {
  895. spin_unlock_irqrestore(&priv->lock, flags);
  896. return rc;
  897. }
  898. /* bypass mode */
  899. iwl3945_write_prph(priv, ALM_SCD_MODE_REG, 0x2);
  900. /* RA 0 is active */
  901. iwl3945_write_prph(priv, ALM_SCD_ARASTAT_REG, 0x01);
  902. /* all 6 fifo are active */
  903. iwl3945_write_prph(priv, ALM_SCD_TXFACT_REG, 0x3f);
  904. iwl3945_write_prph(priv, ALM_SCD_SBYP_MODE_1_REG, 0x010000);
  905. iwl3945_write_prph(priv, ALM_SCD_SBYP_MODE_2_REG, 0x030002);
  906. iwl3945_write_prph(priv, ALM_SCD_TXF4MF_REG, 0x000004);
  907. iwl3945_write_prph(priv, ALM_SCD_TXF5MF_REG, 0x000005);
  908. iwl3945_write_direct32(priv, FH_TSSR_CBB_BASE,
  909. priv->hw_setting.shared_phys);
  910. iwl3945_write_direct32(priv, FH_TSSR_MSG_CONFIG,
  911. ALM_FH_TSSR_TX_MSG_CONFIG_REG_VAL_SNOOP_RD_TXPD_ON |
  912. ALM_FH_TSSR_TX_MSG_CONFIG_REG_VAL_ORDER_RD_TXPD_ON |
  913. ALM_FH_TSSR_TX_MSG_CONFIG_REG_VAL_MAX_FRAG_SIZE_128B |
  914. ALM_FH_TSSR_TX_MSG_CONFIG_REG_VAL_SNOOP_RD_TFD_ON |
  915. ALM_FH_TSSR_TX_MSG_CONFIG_REG_VAL_ORDER_RD_CBB_ON |
  916. ALM_FH_TSSR_TX_MSG_CONFIG_REG_VAL_ORDER_RSP_WAIT_TH |
  917. ALM_FH_TSSR_TX_MSG_CONFIG_REG_VAL_RSP_WAIT_TH);
  918. iwl3945_release_nic_access(priv);
  919. spin_unlock_irqrestore(&priv->lock, flags);
  920. return 0;
  921. }
  922. /**
  923. * iwl3945_txq_ctx_reset - Reset TX queue context
  924. *
  925. * Destroys all DMA structures and initialize them again
  926. */
  927. static int iwl3945_txq_ctx_reset(struct iwl3945_priv *priv)
  928. {
  929. int rc;
  930. int txq_id, slots_num;
  931. iwl3945_hw_txq_ctx_free(priv);
  932. /* Tx CMD queue */
  933. rc = iwl3945_tx_reset(priv);
  934. if (rc)
  935. goto error;
  936. /* Tx queue(s) */
  937. for (txq_id = 0; txq_id < TFD_QUEUE_MAX; txq_id++) {
  938. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  939. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  940. rc = iwl3945_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  941. txq_id);
  942. if (rc) {
  943. IWL_ERROR("Tx %d queue init failed\n", txq_id);
  944. goto error;
  945. }
  946. }
  947. return rc;
  948. error:
  949. iwl3945_hw_txq_ctx_free(priv);
  950. return rc;
  951. }
  952. int iwl3945_hw_nic_init(struct iwl3945_priv *priv)
  953. {
  954. u8 rev_id;
  955. int rc;
  956. unsigned long flags;
  957. struct iwl3945_rx_queue *rxq = &priv->rxq;
  958. iwl3945_power_init_handle(priv);
  959. spin_lock_irqsave(&priv->lock, flags);
  960. iwl3945_set_bit(priv, CSR_ANA_PLL_CFG, (1 << 24));
  961. iwl3945_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  962. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  963. iwl3945_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  964. rc = iwl3945_poll_bit(priv, CSR_GP_CNTRL,
  965. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  966. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  967. if (rc < 0) {
  968. spin_unlock_irqrestore(&priv->lock, flags);
  969. IWL_DEBUG_INFO("Failed to init the card\n");
  970. return rc;
  971. }
  972. rc = iwl3945_grab_nic_access(priv);
  973. if (rc) {
  974. spin_unlock_irqrestore(&priv->lock, flags);
  975. return rc;
  976. }
  977. iwl3945_write_prph(priv, APMG_CLK_EN_REG,
  978. APMG_CLK_VAL_DMA_CLK_RQT |
  979. APMG_CLK_VAL_BSM_CLK_RQT);
  980. udelay(20);
  981. iwl3945_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
  982. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  983. iwl3945_release_nic_access(priv);
  984. spin_unlock_irqrestore(&priv->lock, flags);
  985. /* Determine HW type */
  986. rc = pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &rev_id);
  987. if (rc)
  988. return rc;
  989. IWL_DEBUG_INFO("HW Revision ID = 0x%X\n", rev_id);
  990. iwl3945_nic_set_pwr_src(priv, 1);
  991. spin_lock_irqsave(&priv->lock, flags);
  992. if (rev_id & PCI_CFG_REV_ID_BIT_RTP)
  993. IWL_DEBUG_INFO("RTP type \n");
  994. else if (rev_id & PCI_CFG_REV_ID_BIT_BASIC_SKU) {
  995. IWL_DEBUG_INFO("ALM-MB type\n");
  996. iwl3945_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  997. CSR_HW_IF_CONFIG_REG_BIT_ALMAGOR_MB);
  998. } else {
  999. IWL_DEBUG_INFO("ALM-MM type\n");
  1000. iwl3945_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  1001. CSR_HW_IF_CONFIG_REG_BIT_ALMAGOR_MM);
  1002. }
  1003. if (EEPROM_SKU_CAP_OP_MODE_MRC == priv->eeprom.sku_cap) {
  1004. IWL_DEBUG_INFO("SKU OP mode is mrc\n");
  1005. iwl3945_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  1006. CSR_HW_IF_CONFIG_REG_BIT_SKU_MRC);
  1007. } else
  1008. IWL_DEBUG_INFO("SKU OP mode is basic\n");
  1009. if ((priv->eeprom.board_revision & 0xF0) == 0xD0) {
  1010. IWL_DEBUG_INFO("3945ABG revision is 0x%X\n",
  1011. priv->eeprom.board_revision);
  1012. iwl3945_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  1013. CSR_HW_IF_CONFIG_REG_BIT_BOARD_TYPE);
  1014. } else {
  1015. IWL_DEBUG_INFO("3945ABG revision is 0x%X\n",
  1016. priv->eeprom.board_revision);
  1017. iwl3945_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
  1018. CSR_HW_IF_CONFIG_REG_BIT_BOARD_TYPE);
  1019. }
  1020. if (priv->eeprom.almgor_m_version <= 1) {
  1021. iwl3945_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  1022. CSR_HW_IF_CONFIG_REG_BITS_SILICON_TYPE_A);
  1023. IWL_DEBUG_INFO("Card M type A version is 0x%X\n",
  1024. priv->eeprom.almgor_m_version);
  1025. } else {
  1026. IWL_DEBUG_INFO("Card M type B version is 0x%X\n",
  1027. priv->eeprom.almgor_m_version);
  1028. iwl3945_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  1029. CSR_HW_IF_CONFIG_REG_BITS_SILICON_TYPE_B);
  1030. }
  1031. spin_unlock_irqrestore(&priv->lock, flags);
  1032. if (priv->eeprom.sku_cap & EEPROM_SKU_CAP_SW_RF_KILL_ENABLE)
  1033. IWL_DEBUG_RF_KILL("SW RF KILL supported in EEPROM.\n");
  1034. if (priv->eeprom.sku_cap & EEPROM_SKU_CAP_HW_RF_KILL_ENABLE)
  1035. IWL_DEBUG_RF_KILL("HW RF KILL supported in EEPROM.\n");
  1036. /* Allocate the RX queue, or reset if it is already allocated */
  1037. if (!rxq->bd) {
  1038. rc = iwl3945_rx_queue_alloc(priv);
  1039. if (rc) {
  1040. IWL_ERROR("Unable to initialize Rx queue\n");
  1041. return -ENOMEM;
  1042. }
  1043. } else
  1044. iwl3945_rx_queue_reset(priv, rxq);
  1045. iwl3945_rx_replenish(priv);
  1046. iwl3945_rx_init(priv, rxq);
  1047. spin_lock_irqsave(&priv->lock, flags);
  1048. /* Look at using this instead:
  1049. rxq->need_update = 1;
  1050. iwl3945_rx_queue_update_write_ptr(priv, rxq);
  1051. */
  1052. rc = iwl3945_grab_nic_access(priv);
  1053. if (rc) {
  1054. spin_unlock_irqrestore(&priv->lock, flags);
  1055. return rc;
  1056. }
  1057. iwl3945_write_direct32(priv, FH_RCSR_WPTR(0), rxq->write & ~7);
  1058. iwl3945_release_nic_access(priv);
  1059. spin_unlock_irqrestore(&priv->lock, flags);
  1060. rc = iwl3945_txq_ctx_reset(priv);
  1061. if (rc)
  1062. return rc;
  1063. set_bit(STATUS_INIT, &priv->status);
  1064. return 0;
  1065. }
  1066. /**
  1067. * iwl3945_hw_txq_ctx_free - Free TXQ Context
  1068. *
  1069. * Destroy all TX DMA queues and structures
  1070. */
  1071. void iwl3945_hw_txq_ctx_free(struct iwl3945_priv *priv)
  1072. {
  1073. int txq_id;
  1074. /* Tx queues */
  1075. for (txq_id = 0; txq_id < TFD_QUEUE_MAX; txq_id++)
  1076. iwl3945_tx_queue_free(priv, &priv->txq[txq_id]);
  1077. }
  1078. void iwl3945_hw_txq_ctx_stop(struct iwl3945_priv *priv)
  1079. {
  1080. int queue;
  1081. unsigned long flags;
  1082. spin_lock_irqsave(&priv->lock, flags);
  1083. if (iwl3945_grab_nic_access(priv)) {
  1084. spin_unlock_irqrestore(&priv->lock, flags);
  1085. iwl3945_hw_txq_ctx_free(priv);
  1086. return;
  1087. }
  1088. /* stop SCD */
  1089. iwl3945_write_prph(priv, ALM_SCD_MODE_REG, 0);
  1090. /* reset TFD queues */
  1091. for (queue = TFD_QUEUE_MIN; queue < TFD_QUEUE_MAX; queue++) {
  1092. iwl3945_write_direct32(priv, FH_TCSR_CONFIG(queue), 0x0);
  1093. iwl3945_poll_direct_bit(priv, FH_TSSR_TX_STATUS,
  1094. ALM_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(queue),
  1095. 1000);
  1096. }
  1097. iwl3945_release_nic_access(priv);
  1098. spin_unlock_irqrestore(&priv->lock, flags);
  1099. iwl3945_hw_txq_ctx_free(priv);
  1100. }
  1101. int iwl3945_hw_nic_stop_master(struct iwl3945_priv *priv)
  1102. {
  1103. int rc = 0;
  1104. u32 reg_val;
  1105. unsigned long flags;
  1106. spin_lock_irqsave(&priv->lock, flags);
  1107. /* set stop master bit */
  1108. iwl3945_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  1109. reg_val = iwl3945_read32(priv, CSR_GP_CNTRL);
  1110. if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
  1111. (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
  1112. IWL_DEBUG_INFO("Card in power save, master is already "
  1113. "stopped\n");
  1114. else {
  1115. rc = iwl3945_poll_bit(priv, CSR_RESET,
  1116. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  1117. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  1118. if (rc < 0) {
  1119. spin_unlock_irqrestore(&priv->lock, flags);
  1120. return rc;
  1121. }
  1122. }
  1123. spin_unlock_irqrestore(&priv->lock, flags);
  1124. IWL_DEBUG_INFO("stop master\n");
  1125. return rc;
  1126. }
  1127. int iwl3945_hw_nic_reset(struct iwl3945_priv *priv)
  1128. {
  1129. int rc;
  1130. unsigned long flags;
  1131. iwl3945_hw_nic_stop_master(priv);
  1132. spin_lock_irqsave(&priv->lock, flags);
  1133. iwl3945_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  1134. rc = iwl3945_poll_bit(priv, CSR_GP_CNTRL,
  1135. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  1136. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  1137. rc = iwl3945_grab_nic_access(priv);
  1138. if (!rc) {
  1139. iwl3945_write_prph(priv, APMG_CLK_CTRL_REG,
  1140. APMG_CLK_VAL_BSM_CLK_RQT);
  1141. udelay(10);
  1142. iwl3945_set_bit(priv, CSR_GP_CNTRL,
  1143. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  1144. iwl3945_write_prph(priv, APMG_RTC_INT_MSK_REG, 0x0);
  1145. iwl3945_write_prph(priv, APMG_RTC_INT_STT_REG,
  1146. 0xFFFFFFFF);
  1147. /* enable DMA */
  1148. iwl3945_write_prph(priv, APMG_CLK_EN_REG,
  1149. APMG_CLK_VAL_DMA_CLK_RQT |
  1150. APMG_CLK_VAL_BSM_CLK_RQT);
  1151. udelay(10);
  1152. iwl3945_set_bits_prph(priv, APMG_PS_CTRL_REG,
  1153. APMG_PS_CTRL_VAL_RESET_REQ);
  1154. udelay(5);
  1155. iwl3945_clear_bits_prph(priv, APMG_PS_CTRL_REG,
  1156. APMG_PS_CTRL_VAL_RESET_REQ);
  1157. iwl3945_release_nic_access(priv);
  1158. }
  1159. /* Clear the 'host command active' bit... */
  1160. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  1161. wake_up_interruptible(&priv->wait_command_queue);
  1162. spin_unlock_irqrestore(&priv->lock, flags);
  1163. return rc;
  1164. }
  1165. /**
  1166. * iwl3945_hw_reg_adjust_power_by_temp
  1167. * return index delta into power gain settings table
  1168. */
  1169. static int iwl3945_hw_reg_adjust_power_by_temp(int new_reading, int old_reading)
  1170. {
  1171. return (new_reading - old_reading) * (-11) / 100;
  1172. }
  1173. /**
  1174. * iwl3945_hw_reg_temp_out_of_range - Keep temperature in sane range
  1175. */
  1176. static inline int iwl3945_hw_reg_temp_out_of_range(int temperature)
  1177. {
  1178. return (((temperature < -260) || (temperature > 25)) ? 1 : 0);
  1179. }
  1180. int iwl3945_hw_get_temperature(struct iwl3945_priv *priv)
  1181. {
  1182. return iwl3945_read32(priv, CSR_UCODE_DRV_GP2);
  1183. }
  1184. /**
  1185. * iwl3945_hw_reg_txpower_get_temperature
  1186. * get the current temperature by reading from NIC
  1187. */
  1188. static int iwl3945_hw_reg_txpower_get_temperature(struct iwl3945_priv *priv)
  1189. {
  1190. int temperature;
  1191. temperature = iwl3945_hw_get_temperature(priv);
  1192. /* driver's okay range is -260 to +25.
  1193. * human readable okay range is 0 to +285 */
  1194. IWL_DEBUG_INFO("Temperature: %d\n", temperature + IWL_TEMP_CONVERT);
  1195. /* handle insane temp reading */
  1196. if (iwl3945_hw_reg_temp_out_of_range(temperature)) {
  1197. IWL_ERROR("Error bad temperature value %d\n", temperature);
  1198. /* if really really hot(?),
  1199. * substitute the 3rd band/group's temp measured at factory */
  1200. if (priv->last_temperature > 100)
  1201. temperature = priv->eeprom.groups[2].temperature;
  1202. else /* else use most recent "sane" value from driver */
  1203. temperature = priv->last_temperature;
  1204. }
  1205. return temperature; /* raw, not "human readable" */
  1206. }
  1207. /* Adjust Txpower only if temperature variance is greater than threshold.
  1208. *
  1209. * Both are lower than older versions' 9 degrees */
  1210. #define IWL_TEMPERATURE_LIMIT_TIMER 6
  1211. /**
  1212. * is_temp_calib_needed - determines if new calibration is needed
  1213. *
  1214. * records new temperature in tx_mgr->temperature.
  1215. * replaces tx_mgr->last_temperature *only* if calib needed
  1216. * (assumes caller will actually do the calibration!). */
  1217. static int is_temp_calib_needed(struct iwl3945_priv *priv)
  1218. {
  1219. int temp_diff;
  1220. priv->temperature = iwl3945_hw_reg_txpower_get_temperature(priv);
  1221. temp_diff = priv->temperature - priv->last_temperature;
  1222. /* get absolute value */
  1223. if (temp_diff < 0) {
  1224. IWL_DEBUG_POWER("Getting cooler, delta %d,\n", temp_diff);
  1225. temp_diff = -temp_diff;
  1226. } else if (temp_diff == 0)
  1227. IWL_DEBUG_POWER("Same temp,\n");
  1228. else
  1229. IWL_DEBUG_POWER("Getting warmer, delta %d,\n", temp_diff);
  1230. /* if we don't need calibration, *don't* update last_temperature */
  1231. if (temp_diff < IWL_TEMPERATURE_LIMIT_TIMER) {
  1232. IWL_DEBUG_POWER("Timed thermal calib not needed\n");
  1233. return 0;
  1234. }
  1235. IWL_DEBUG_POWER("Timed thermal calib needed\n");
  1236. /* assume that caller will actually do calib ...
  1237. * update the "last temperature" value */
  1238. priv->last_temperature = priv->temperature;
  1239. return 1;
  1240. }
  1241. #define IWL_MAX_GAIN_ENTRIES 78
  1242. #define IWL_CCK_FROM_OFDM_POWER_DIFF -5
  1243. #define IWL_CCK_FROM_OFDM_INDEX_DIFF (10)
  1244. /* radio and DSP power table, each step is 1/2 dB.
  1245. * 1st number is for RF analog gain, 2nd number is for DSP pre-DAC gain. */
  1246. static struct iwl3945_tx_power power_gain_table[2][IWL_MAX_GAIN_ENTRIES] = {
  1247. {
  1248. {251, 127}, /* 2.4 GHz, highest power */
  1249. {251, 127},
  1250. {251, 127},
  1251. {251, 127},
  1252. {251, 125},
  1253. {251, 110},
  1254. {251, 105},
  1255. {251, 98},
  1256. {187, 125},
  1257. {187, 115},
  1258. {187, 108},
  1259. {187, 99},
  1260. {243, 119},
  1261. {243, 111},
  1262. {243, 105},
  1263. {243, 97},
  1264. {243, 92},
  1265. {211, 106},
  1266. {211, 100},
  1267. {179, 120},
  1268. {179, 113},
  1269. {179, 107},
  1270. {147, 125},
  1271. {147, 119},
  1272. {147, 112},
  1273. {147, 106},
  1274. {147, 101},
  1275. {147, 97},
  1276. {147, 91},
  1277. {115, 107},
  1278. {235, 121},
  1279. {235, 115},
  1280. {235, 109},
  1281. {203, 127},
  1282. {203, 121},
  1283. {203, 115},
  1284. {203, 108},
  1285. {203, 102},
  1286. {203, 96},
  1287. {203, 92},
  1288. {171, 110},
  1289. {171, 104},
  1290. {171, 98},
  1291. {139, 116},
  1292. {227, 125},
  1293. {227, 119},
  1294. {227, 113},
  1295. {227, 107},
  1296. {227, 101},
  1297. {227, 96},
  1298. {195, 113},
  1299. {195, 106},
  1300. {195, 102},
  1301. {195, 95},
  1302. {163, 113},
  1303. {163, 106},
  1304. {163, 102},
  1305. {163, 95},
  1306. {131, 113},
  1307. {131, 106},
  1308. {131, 102},
  1309. {131, 95},
  1310. {99, 113},
  1311. {99, 106},
  1312. {99, 102},
  1313. {99, 95},
  1314. {67, 113},
  1315. {67, 106},
  1316. {67, 102},
  1317. {67, 95},
  1318. {35, 113},
  1319. {35, 106},
  1320. {35, 102},
  1321. {35, 95},
  1322. {3, 113},
  1323. {3, 106},
  1324. {3, 102},
  1325. {3, 95} }, /* 2.4 GHz, lowest power */
  1326. {
  1327. {251, 127}, /* 5.x GHz, highest power */
  1328. {251, 120},
  1329. {251, 114},
  1330. {219, 119},
  1331. {219, 101},
  1332. {187, 113},
  1333. {187, 102},
  1334. {155, 114},
  1335. {155, 103},
  1336. {123, 117},
  1337. {123, 107},
  1338. {123, 99},
  1339. {123, 92},
  1340. {91, 108},
  1341. {59, 125},
  1342. {59, 118},
  1343. {59, 109},
  1344. {59, 102},
  1345. {59, 96},
  1346. {59, 90},
  1347. {27, 104},
  1348. {27, 98},
  1349. {27, 92},
  1350. {115, 118},
  1351. {115, 111},
  1352. {115, 104},
  1353. {83, 126},
  1354. {83, 121},
  1355. {83, 113},
  1356. {83, 105},
  1357. {83, 99},
  1358. {51, 118},
  1359. {51, 111},
  1360. {51, 104},
  1361. {51, 98},
  1362. {19, 116},
  1363. {19, 109},
  1364. {19, 102},
  1365. {19, 98},
  1366. {19, 93},
  1367. {171, 113},
  1368. {171, 107},
  1369. {171, 99},
  1370. {139, 120},
  1371. {139, 113},
  1372. {139, 107},
  1373. {139, 99},
  1374. {107, 120},
  1375. {107, 113},
  1376. {107, 107},
  1377. {107, 99},
  1378. {75, 120},
  1379. {75, 113},
  1380. {75, 107},
  1381. {75, 99},
  1382. {43, 120},
  1383. {43, 113},
  1384. {43, 107},
  1385. {43, 99},
  1386. {11, 120},
  1387. {11, 113},
  1388. {11, 107},
  1389. {11, 99},
  1390. {131, 107},
  1391. {131, 99},
  1392. {99, 120},
  1393. {99, 113},
  1394. {99, 107},
  1395. {99, 99},
  1396. {67, 120},
  1397. {67, 113},
  1398. {67, 107},
  1399. {67, 99},
  1400. {35, 120},
  1401. {35, 113},
  1402. {35, 107},
  1403. {35, 99},
  1404. {3, 120} } /* 5.x GHz, lowest power */
  1405. };
  1406. static inline u8 iwl3945_hw_reg_fix_power_index(int index)
  1407. {
  1408. if (index < 0)
  1409. return 0;
  1410. if (index >= IWL_MAX_GAIN_ENTRIES)
  1411. return IWL_MAX_GAIN_ENTRIES - 1;
  1412. return (u8) index;
  1413. }
  1414. /* Kick off thermal recalibration check every 60 seconds */
  1415. #define REG_RECALIB_PERIOD (60)
  1416. /**
  1417. * iwl3945_hw_reg_set_scan_power - Set Tx power for scan probe requests
  1418. *
  1419. * Set (in our channel info database) the direct scan Tx power for 1 Mbit (CCK)
  1420. * or 6 Mbit (OFDM) rates.
  1421. */
  1422. static void iwl3945_hw_reg_set_scan_power(struct iwl3945_priv *priv, u32 scan_tbl_index,
  1423. s32 rate_index, const s8 *clip_pwrs,
  1424. struct iwl3945_channel_info *ch_info,
  1425. int band_index)
  1426. {
  1427. struct iwl3945_scan_power_info *scan_power_info;
  1428. s8 power;
  1429. u8 power_index;
  1430. scan_power_info = &ch_info->scan_pwr_info[scan_tbl_index];
  1431. /* use this channel group's 6Mbit clipping/saturation pwr,
  1432. * but cap at regulatory scan power restriction (set during init
  1433. * based on eeprom channel data) for this channel. */
  1434. power = min(ch_info->scan_power, clip_pwrs[IWL_RATE_6M_INDEX_TABLE]);
  1435. /* further limit to user's max power preference.
  1436. * FIXME: Other spectrum management power limitations do not
  1437. * seem to apply?? */
  1438. power = min(power, priv->user_txpower_limit);
  1439. scan_power_info->requested_power = power;
  1440. /* find difference between new scan *power* and current "normal"
  1441. * Tx *power* for 6Mb. Use this difference (x2) to adjust the
  1442. * current "normal" temperature-compensated Tx power *index* for
  1443. * this rate (1Mb or 6Mb) to yield new temp-compensated scan power
  1444. * *index*. */
  1445. power_index = ch_info->power_info[rate_index].power_table_index
  1446. - (power - ch_info->power_info
  1447. [IWL_RATE_6M_INDEX_TABLE].requested_power) * 2;
  1448. /* store reference index that we use when adjusting *all* scan
  1449. * powers. So we can accommodate user (all channel) or spectrum
  1450. * management (single channel) power changes "between" temperature
  1451. * feedback compensation procedures.
  1452. * don't force fit this reference index into gain table; it may be a
  1453. * negative number. This will help avoid errors when we're at
  1454. * the lower bounds (highest gains, for warmest temperatures)
  1455. * of the table. */
  1456. /* don't exceed table bounds for "real" setting */
  1457. power_index = iwl3945_hw_reg_fix_power_index(power_index);
  1458. scan_power_info->power_table_index = power_index;
  1459. scan_power_info->tpc.tx_gain =
  1460. power_gain_table[band_index][power_index].tx_gain;
  1461. scan_power_info->tpc.dsp_atten =
  1462. power_gain_table[band_index][power_index].dsp_atten;
  1463. }
  1464. /**
  1465. * iwl3945_hw_reg_send_txpower - fill in Tx Power command with gain settings
  1466. *
  1467. * Configures power settings for all rates for the current channel,
  1468. * using values from channel info struct, and send to NIC
  1469. */
  1470. int iwl3945_hw_reg_send_txpower(struct iwl3945_priv *priv)
  1471. {
  1472. int rate_idx, i;
  1473. const struct iwl3945_channel_info *ch_info = NULL;
  1474. struct iwl3945_txpowertable_cmd txpower = {
  1475. .channel = priv->active_rxon.channel,
  1476. };
  1477. txpower.band = (priv->band == IEEE80211_BAND_5GHZ) ? 0 : 1;
  1478. ch_info = iwl3945_get_channel_info(priv,
  1479. priv->band,
  1480. le16_to_cpu(priv->active_rxon.channel));
  1481. if (!ch_info) {
  1482. IWL_ERROR
  1483. ("Failed to get channel info for channel %d [%d]\n",
  1484. le16_to_cpu(priv->active_rxon.channel), priv->band);
  1485. return -EINVAL;
  1486. }
  1487. if (!is_channel_valid(ch_info)) {
  1488. IWL_DEBUG_POWER("Not calling TX_PWR_TABLE_CMD on "
  1489. "non-Tx channel.\n");
  1490. return 0;
  1491. }
  1492. /* fill cmd with power settings for all rates for current channel */
  1493. /* Fill OFDM rate */
  1494. for (rate_idx = IWL_FIRST_OFDM_RATE, i = 0;
  1495. rate_idx <= IWL_LAST_OFDM_RATE; rate_idx++, i++) {
  1496. txpower.power[i].tpc = ch_info->power_info[i].tpc;
  1497. txpower.power[i].rate = iwl3945_rates[rate_idx].plcp;
  1498. IWL_DEBUG_POWER("ch %d:%d rf %d dsp %3d rate code 0x%02x\n",
  1499. le16_to_cpu(txpower.channel),
  1500. txpower.band,
  1501. txpower.power[i].tpc.tx_gain,
  1502. txpower.power[i].tpc.dsp_atten,
  1503. txpower.power[i].rate);
  1504. }
  1505. /* Fill CCK rates */
  1506. for (rate_idx = IWL_FIRST_CCK_RATE;
  1507. rate_idx <= IWL_LAST_CCK_RATE; rate_idx++, i++) {
  1508. txpower.power[i].tpc = ch_info->power_info[i].tpc;
  1509. txpower.power[i].rate = iwl3945_rates[rate_idx].plcp;
  1510. IWL_DEBUG_POWER("ch %d:%d rf %d dsp %3d rate code 0x%02x\n",
  1511. le16_to_cpu(txpower.channel),
  1512. txpower.band,
  1513. txpower.power[i].tpc.tx_gain,
  1514. txpower.power[i].tpc.dsp_atten,
  1515. txpower.power[i].rate);
  1516. }
  1517. return iwl3945_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD,
  1518. sizeof(struct iwl3945_txpowertable_cmd), &txpower);
  1519. }
  1520. /**
  1521. * iwl3945_hw_reg_set_new_power - Configures power tables at new levels
  1522. * @ch_info: Channel to update. Uses power_info.requested_power.
  1523. *
  1524. * Replace requested_power and base_power_index ch_info fields for
  1525. * one channel.
  1526. *
  1527. * Called if user or spectrum management changes power preferences.
  1528. * Takes into account h/w and modulation limitations (clip power).
  1529. *
  1530. * This does *not* send anything to NIC, just sets up ch_info for one channel.
  1531. *
  1532. * NOTE: reg_compensate_for_temperature_dif() *must* be run after this to
  1533. * properly fill out the scan powers, and actual h/w gain settings,
  1534. * and send changes to NIC
  1535. */
  1536. static int iwl3945_hw_reg_set_new_power(struct iwl3945_priv *priv,
  1537. struct iwl3945_channel_info *ch_info)
  1538. {
  1539. struct iwl3945_channel_power_info *power_info;
  1540. int power_changed = 0;
  1541. int i;
  1542. const s8 *clip_pwrs;
  1543. int power;
  1544. /* Get this chnlgrp's rate-to-max/clip-powers table */
  1545. clip_pwrs = priv->clip_groups[ch_info->group_index].clip_powers;
  1546. /* Get this channel's rate-to-current-power settings table */
  1547. power_info = ch_info->power_info;
  1548. /* update OFDM Txpower settings */
  1549. for (i = IWL_RATE_6M_INDEX_TABLE; i <= IWL_RATE_54M_INDEX_TABLE;
  1550. i++, ++power_info) {
  1551. int delta_idx;
  1552. /* limit new power to be no more than h/w capability */
  1553. power = min(ch_info->curr_txpow, clip_pwrs[i]);
  1554. if (power == power_info->requested_power)
  1555. continue;
  1556. /* find difference between old and new requested powers,
  1557. * update base (non-temp-compensated) power index */
  1558. delta_idx = (power - power_info->requested_power) * 2;
  1559. power_info->base_power_index -= delta_idx;
  1560. /* save new requested power value */
  1561. power_info->requested_power = power;
  1562. power_changed = 1;
  1563. }
  1564. /* update CCK Txpower settings, based on OFDM 12M setting ...
  1565. * ... all CCK power settings for a given channel are the *same*. */
  1566. if (power_changed) {
  1567. power =
  1568. ch_info->power_info[IWL_RATE_12M_INDEX_TABLE].
  1569. requested_power + IWL_CCK_FROM_OFDM_POWER_DIFF;
  1570. /* do all CCK rates' iwl3945_channel_power_info structures */
  1571. for (i = IWL_RATE_1M_INDEX_TABLE; i <= IWL_RATE_11M_INDEX_TABLE; i++) {
  1572. power_info->requested_power = power;
  1573. power_info->base_power_index =
  1574. ch_info->power_info[IWL_RATE_12M_INDEX_TABLE].
  1575. base_power_index + IWL_CCK_FROM_OFDM_INDEX_DIFF;
  1576. ++power_info;
  1577. }
  1578. }
  1579. return 0;
  1580. }
  1581. /**
  1582. * iwl3945_hw_reg_get_ch_txpower_limit - returns new power limit for channel
  1583. *
  1584. * NOTE: Returned power limit may be less (but not more) than requested,
  1585. * based strictly on regulatory (eeprom and spectrum mgt) limitations
  1586. * (no consideration for h/w clipping limitations).
  1587. */
  1588. static int iwl3945_hw_reg_get_ch_txpower_limit(struct iwl3945_channel_info *ch_info)
  1589. {
  1590. s8 max_power;
  1591. #if 0
  1592. /* if we're using TGd limits, use lower of TGd or EEPROM */
  1593. if (ch_info->tgd_data.max_power != 0)
  1594. max_power = min(ch_info->tgd_data.max_power,
  1595. ch_info->eeprom.max_power_avg);
  1596. /* else just use EEPROM limits */
  1597. else
  1598. #endif
  1599. max_power = ch_info->eeprom.max_power_avg;
  1600. return min(max_power, ch_info->max_power_avg);
  1601. }
  1602. /**
  1603. * iwl3945_hw_reg_comp_txpower_temp - Compensate for temperature
  1604. *
  1605. * Compensate txpower settings of *all* channels for temperature.
  1606. * This only accounts for the difference between current temperature
  1607. * and the factory calibration temperatures, and bases the new settings
  1608. * on the channel's base_power_index.
  1609. *
  1610. * If RxOn is "associated", this sends the new Txpower to NIC!
  1611. */
  1612. static int iwl3945_hw_reg_comp_txpower_temp(struct iwl3945_priv *priv)
  1613. {
  1614. struct iwl3945_channel_info *ch_info = NULL;
  1615. int delta_index;
  1616. const s8 *clip_pwrs; /* array of h/w max power levels for each rate */
  1617. u8 a_band;
  1618. u8 rate_index;
  1619. u8 scan_tbl_index;
  1620. u8 i;
  1621. int ref_temp;
  1622. int temperature = priv->temperature;
  1623. /* set up new Tx power info for each and every channel, 2.4 and 5.x */
  1624. for (i = 0; i < priv->channel_count; i++) {
  1625. ch_info = &priv->channel_info[i];
  1626. a_band = is_channel_a_band(ch_info);
  1627. /* Get this chnlgrp's factory calibration temperature */
  1628. ref_temp = (s16)priv->eeprom.groups[ch_info->group_index].
  1629. temperature;
  1630. /* get power index adjustment based on curr and factory
  1631. * temps */
  1632. delta_index = iwl3945_hw_reg_adjust_power_by_temp(temperature,
  1633. ref_temp);
  1634. /* set tx power value for all rates, OFDM and CCK */
  1635. for (rate_index = 0; rate_index < IWL_RATE_COUNT;
  1636. rate_index++) {
  1637. int power_idx =
  1638. ch_info->power_info[rate_index].base_power_index;
  1639. /* temperature compensate */
  1640. power_idx += delta_index;
  1641. /* stay within table range */
  1642. power_idx = iwl3945_hw_reg_fix_power_index(power_idx);
  1643. ch_info->power_info[rate_index].
  1644. power_table_index = (u8) power_idx;
  1645. ch_info->power_info[rate_index].tpc =
  1646. power_gain_table[a_band][power_idx];
  1647. }
  1648. /* Get this chnlgrp's rate-to-max/clip-powers table */
  1649. clip_pwrs = priv->clip_groups[ch_info->group_index].clip_powers;
  1650. /* set scan tx power, 1Mbit for CCK, 6Mbit for OFDM */
  1651. for (scan_tbl_index = 0;
  1652. scan_tbl_index < IWL_NUM_SCAN_RATES; scan_tbl_index++) {
  1653. s32 actual_index = (scan_tbl_index == 0) ?
  1654. IWL_RATE_1M_INDEX_TABLE : IWL_RATE_6M_INDEX_TABLE;
  1655. iwl3945_hw_reg_set_scan_power(priv, scan_tbl_index,
  1656. actual_index, clip_pwrs,
  1657. ch_info, a_band);
  1658. }
  1659. }
  1660. /* send Txpower command for current channel to ucode */
  1661. return iwl3945_hw_reg_send_txpower(priv);
  1662. }
  1663. int iwl3945_hw_reg_set_txpower(struct iwl3945_priv *priv, s8 power)
  1664. {
  1665. struct iwl3945_channel_info *ch_info;
  1666. s8 max_power;
  1667. u8 a_band;
  1668. u8 i;
  1669. if (priv->user_txpower_limit == power) {
  1670. IWL_DEBUG_POWER("Requested Tx power same as current "
  1671. "limit: %ddBm.\n", power);
  1672. return 0;
  1673. }
  1674. IWL_DEBUG_POWER("Setting upper limit clamp to %ddBm.\n", power);
  1675. priv->user_txpower_limit = power;
  1676. /* set up new Tx powers for each and every channel, 2.4 and 5.x */
  1677. for (i = 0; i < priv->channel_count; i++) {
  1678. ch_info = &priv->channel_info[i];
  1679. a_band = is_channel_a_band(ch_info);
  1680. /* find minimum power of all user and regulatory constraints
  1681. * (does not consider h/w clipping limitations) */
  1682. max_power = iwl3945_hw_reg_get_ch_txpower_limit(ch_info);
  1683. max_power = min(power, max_power);
  1684. if (max_power != ch_info->curr_txpow) {
  1685. ch_info->curr_txpow = max_power;
  1686. /* this considers the h/w clipping limitations */
  1687. iwl3945_hw_reg_set_new_power(priv, ch_info);
  1688. }
  1689. }
  1690. /* update txpower settings for all channels,
  1691. * send to NIC if associated. */
  1692. is_temp_calib_needed(priv);
  1693. iwl3945_hw_reg_comp_txpower_temp(priv);
  1694. return 0;
  1695. }
  1696. /* will add 3945 channel switch cmd handling later */
  1697. int iwl3945_hw_channel_switch(struct iwl3945_priv *priv, u16 channel)
  1698. {
  1699. return 0;
  1700. }
  1701. /**
  1702. * iwl3945_reg_txpower_periodic - called when time to check our temperature.
  1703. *
  1704. * -- reset periodic timer
  1705. * -- see if temp has changed enough to warrant re-calibration ... if so:
  1706. * -- correct coeffs for temp (can reset temp timer)
  1707. * -- save this temp as "last",
  1708. * -- send new set of gain settings to NIC
  1709. * NOTE: This should continue working, even when we're not associated,
  1710. * so we can keep our internal table of scan powers current. */
  1711. void iwl3945_reg_txpower_periodic(struct iwl3945_priv *priv)
  1712. {
  1713. /* This will kick in the "brute force"
  1714. * iwl3945_hw_reg_comp_txpower_temp() below */
  1715. if (!is_temp_calib_needed(priv))
  1716. goto reschedule;
  1717. /* Set up a new set of temp-adjusted TxPowers, send to NIC.
  1718. * This is based *only* on current temperature,
  1719. * ignoring any previous power measurements */
  1720. iwl3945_hw_reg_comp_txpower_temp(priv);
  1721. reschedule:
  1722. queue_delayed_work(priv->workqueue,
  1723. &priv->thermal_periodic, REG_RECALIB_PERIOD * HZ);
  1724. }
  1725. static void iwl3945_bg_reg_txpower_periodic(struct work_struct *work)
  1726. {
  1727. struct iwl3945_priv *priv = container_of(work, struct iwl3945_priv,
  1728. thermal_periodic.work);
  1729. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1730. return;
  1731. mutex_lock(&priv->mutex);
  1732. iwl3945_reg_txpower_periodic(priv);
  1733. mutex_unlock(&priv->mutex);
  1734. }
  1735. /**
  1736. * iwl3945_hw_reg_get_ch_grp_index - find the channel-group index (0-4)
  1737. * for the channel.
  1738. *
  1739. * This function is used when initializing channel-info structs.
  1740. *
  1741. * NOTE: These channel groups do *NOT* match the bands above!
  1742. * These channel groups are based on factory-tested channels;
  1743. * on A-band, EEPROM's "group frequency" entries represent the top
  1744. * channel in each group 1-4. Group 5 All B/G channels are in group 0.
  1745. */
  1746. static u16 iwl3945_hw_reg_get_ch_grp_index(struct iwl3945_priv *priv,
  1747. const struct iwl3945_channel_info *ch_info)
  1748. {
  1749. struct iwl3945_eeprom_txpower_group *ch_grp = &priv->eeprom.groups[0];
  1750. u8 group;
  1751. u16 group_index = 0; /* based on factory calib frequencies */
  1752. u8 grp_channel;
  1753. /* Find the group index for the channel ... don't use index 1(?) */
  1754. if (is_channel_a_band(ch_info)) {
  1755. for (group = 1; group < 5; group++) {
  1756. grp_channel = ch_grp[group].group_channel;
  1757. if (ch_info->channel <= grp_channel) {
  1758. group_index = group;
  1759. break;
  1760. }
  1761. }
  1762. /* group 4 has a few channels *above* its factory cal freq */
  1763. if (group == 5)
  1764. group_index = 4;
  1765. } else
  1766. group_index = 0; /* 2.4 GHz, group 0 */
  1767. IWL_DEBUG_POWER("Chnl %d mapped to grp %d\n", ch_info->channel,
  1768. group_index);
  1769. return group_index;
  1770. }
  1771. /**
  1772. * iwl3945_hw_reg_get_matched_power_index - Interpolate to get nominal index
  1773. *
  1774. * Interpolate to get nominal (i.e. at factory calibration temperature) index
  1775. * into radio/DSP gain settings table for requested power.
  1776. */
  1777. static int iwl3945_hw_reg_get_matched_power_index(struct iwl3945_priv *priv,
  1778. s8 requested_power,
  1779. s32 setting_index, s32 *new_index)
  1780. {
  1781. const struct iwl3945_eeprom_txpower_group *chnl_grp = NULL;
  1782. s32 index0, index1;
  1783. s32 power = 2 * requested_power;
  1784. s32 i;
  1785. const struct iwl3945_eeprom_txpower_sample *samples;
  1786. s32 gains0, gains1;
  1787. s32 res;
  1788. s32 denominator;
  1789. chnl_grp = &priv->eeprom.groups[setting_index];
  1790. samples = chnl_grp->samples;
  1791. for (i = 0; i < 5; i++) {
  1792. if (power == samples[i].power) {
  1793. *new_index = samples[i].gain_index;
  1794. return 0;
  1795. }
  1796. }
  1797. if (power > samples[1].power) {
  1798. index0 = 0;
  1799. index1 = 1;
  1800. } else if (power > samples[2].power) {
  1801. index0 = 1;
  1802. index1 = 2;
  1803. } else if (power > samples[3].power) {
  1804. index0 = 2;
  1805. index1 = 3;
  1806. } else {
  1807. index0 = 3;
  1808. index1 = 4;
  1809. }
  1810. denominator = (s32) samples[index1].power - (s32) samples[index0].power;
  1811. if (denominator == 0)
  1812. return -EINVAL;
  1813. gains0 = (s32) samples[index0].gain_index * (1 << 19);
  1814. gains1 = (s32) samples[index1].gain_index * (1 << 19);
  1815. res = gains0 + (gains1 - gains0) *
  1816. ((s32) power - (s32) samples[index0].power) / denominator +
  1817. (1 << 18);
  1818. *new_index = res >> 19;
  1819. return 0;
  1820. }
  1821. static void iwl3945_hw_reg_init_channel_groups(struct iwl3945_priv *priv)
  1822. {
  1823. u32 i;
  1824. s32 rate_index;
  1825. const struct iwl3945_eeprom_txpower_group *group;
  1826. IWL_DEBUG_POWER("Initializing factory calib info from EEPROM\n");
  1827. for (i = 0; i < IWL_NUM_TX_CALIB_GROUPS; i++) {
  1828. s8 *clip_pwrs; /* table of power levels for each rate */
  1829. s8 satur_pwr; /* saturation power for each chnl group */
  1830. group = &priv->eeprom.groups[i];
  1831. /* sanity check on factory saturation power value */
  1832. if (group->saturation_power < 40) {
  1833. IWL_WARNING("Error: saturation power is %d, "
  1834. "less than minimum expected 40\n",
  1835. group->saturation_power);
  1836. return;
  1837. }
  1838. /*
  1839. * Derive requested power levels for each rate, based on
  1840. * hardware capabilities (saturation power for band).
  1841. * Basic value is 3dB down from saturation, with further
  1842. * power reductions for highest 3 data rates. These
  1843. * backoffs provide headroom for high rate modulation
  1844. * power peaks, without too much distortion (clipping).
  1845. */
  1846. /* we'll fill in this array with h/w max power levels */
  1847. clip_pwrs = (s8 *) priv->clip_groups[i].clip_powers;
  1848. /* divide factory saturation power by 2 to find -3dB level */
  1849. satur_pwr = (s8) (group->saturation_power >> 1);
  1850. /* fill in channel group's nominal powers for each rate */
  1851. for (rate_index = 0;
  1852. rate_index < IWL_RATE_COUNT; rate_index++, clip_pwrs++) {
  1853. switch (rate_index) {
  1854. case IWL_RATE_36M_INDEX_TABLE:
  1855. if (i == 0) /* B/G */
  1856. *clip_pwrs = satur_pwr;
  1857. else /* A */
  1858. *clip_pwrs = satur_pwr - 5;
  1859. break;
  1860. case IWL_RATE_48M_INDEX_TABLE:
  1861. if (i == 0)
  1862. *clip_pwrs = satur_pwr - 7;
  1863. else
  1864. *clip_pwrs = satur_pwr - 10;
  1865. break;
  1866. case IWL_RATE_54M_INDEX_TABLE:
  1867. if (i == 0)
  1868. *clip_pwrs = satur_pwr - 9;
  1869. else
  1870. *clip_pwrs = satur_pwr - 12;
  1871. break;
  1872. default:
  1873. *clip_pwrs = satur_pwr;
  1874. break;
  1875. }
  1876. }
  1877. }
  1878. }
  1879. /**
  1880. * iwl3945_txpower_set_from_eeprom - Set channel power info based on EEPROM
  1881. *
  1882. * Second pass (during init) to set up priv->channel_info
  1883. *
  1884. * Set up Tx-power settings in our channel info database for each VALID
  1885. * (for this geo/SKU) channel, at all Tx data rates, based on eeprom values
  1886. * and current temperature.
  1887. *
  1888. * Since this is based on current temperature (at init time), these values may
  1889. * not be valid for very long, but it gives us a starting/default point,
  1890. * and allows us to active (i.e. using Tx) scan.
  1891. *
  1892. * This does *not* write values to NIC, just sets up our internal table.
  1893. */
  1894. int iwl3945_txpower_set_from_eeprom(struct iwl3945_priv *priv)
  1895. {
  1896. struct iwl3945_channel_info *ch_info = NULL;
  1897. struct iwl3945_channel_power_info *pwr_info;
  1898. int delta_index;
  1899. u8 rate_index;
  1900. u8 scan_tbl_index;
  1901. const s8 *clip_pwrs; /* array of power levels for each rate */
  1902. u8 gain, dsp_atten;
  1903. s8 power;
  1904. u8 pwr_index, base_pwr_index, a_band;
  1905. u8 i;
  1906. int temperature;
  1907. /* save temperature reference,
  1908. * so we can determine next time to calibrate */
  1909. temperature = iwl3945_hw_reg_txpower_get_temperature(priv);
  1910. priv->last_temperature = temperature;
  1911. iwl3945_hw_reg_init_channel_groups(priv);
  1912. /* initialize Tx power info for each and every channel, 2.4 and 5.x */
  1913. for (i = 0, ch_info = priv->channel_info; i < priv->channel_count;
  1914. i++, ch_info++) {
  1915. a_band = is_channel_a_band(ch_info);
  1916. if (!is_channel_valid(ch_info))
  1917. continue;
  1918. /* find this channel's channel group (*not* "band") index */
  1919. ch_info->group_index =
  1920. iwl3945_hw_reg_get_ch_grp_index(priv, ch_info);
  1921. /* Get this chnlgrp's rate->max/clip-powers table */
  1922. clip_pwrs = priv->clip_groups[ch_info->group_index].clip_powers;
  1923. /* calculate power index *adjustment* value according to
  1924. * diff between current temperature and factory temperature */
  1925. delta_index = iwl3945_hw_reg_adjust_power_by_temp(temperature,
  1926. priv->eeprom.groups[ch_info->group_index].
  1927. temperature);
  1928. IWL_DEBUG_POWER("Delta index for channel %d: %d [%d]\n",
  1929. ch_info->channel, delta_index, temperature +
  1930. IWL_TEMP_CONVERT);
  1931. /* set tx power value for all OFDM rates */
  1932. for (rate_index = 0; rate_index < IWL_OFDM_RATES;
  1933. rate_index++) {
  1934. s32 power_idx;
  1935. int rc;
  1936. /* use channel group's clip-power table,
  1937. * but don't exceed channel's max power */
  1938. s8 pwr = min(ch_info->max_power_avg,
  1939. clip_pwrs[rate_index]);
  1940. pwr_info = &ch_info->power_info[rate_index];
  1941. /* get base (i.e. at factory-measured temperature)
  1942. * power table index for this rate's power */
  1943. rc = iwl3945_hw_reg_get_matched_power_index(priv, pwr,
  1944. ch_info->group_index,
  1945. &power_idx);
  1946. if (rc) {
  1947. IWL_ERROR("Invalid power index\n");
  1948. return rc;
  1949. }
  1950. pwr_info->base_power_index = (u8) power_idx;
  1951. /* temperature compensate */
  1952. power_idx += delta_index;
  1953. /* stay within range of gain table */
  1954. power_idx = iwl3945_hw_reg_fix_power_index(power_idx);
  1955. /* fill 1 OFDM rate's iwl3945_channel_power_info struct */
  1956. pwr_info->requested_power = pwr;
  1957. pwr_info->power_table_index = (u8) power_idx;
  1958. pwr_info->tpc.tx_gain =
  1959. power_gain_table[a_band][power_idx].tx_gain;
  1960. pwr_info->tpc.dsp_atten =
  1961. power_gain_table[a_band][power_idx].dsp_atten;
  1962. }
  1963. /* set tx power for CCK rates, based on OFDM 12 Mbit settings*/
  1964. pwr_info = &ch_info->power_info[IWL_RATE_12M_INDEX_TABLE];
  1965. power = pwr_info->requested_power +
  1966. IWL_CCK_FROM_OFDM_POWER_DIFF;
  1967. pwr_index = pwr_info->power_table_index +
  1968. IWL_CCK_FROM_OFDM_INDEX_DIFF;
  1969. base_pwr_index = pwr_info->base_power_index +
  1970. IWL_CCK_FROM_OFDM_INDEX_DIFF;
  1971. /* stay within table range */
  1972. pwr_index = iwl3945_hw_reg_fix_power_index(pwr_index);
  1973. gain = power_gain_table[a_band][pwr_index].tx_gain;
  1974. dsp_atten = power_gain_table[a_band][pwr_index].dsp_atten;
  1975. /* fill each CCK rate's iwl3945_channel_power_info structure
  1976. * NOTE: All CCK-rate Txpwrs are the same for a given chnl!
  1977. * NOTE: CCK rates start at end of OFDM rates! */
  1978. for (rate_index = 0;
  1979. rate_index < IWL_CCK_RATES; rate_index++) {
  1980. pwr_info = &ch_info->power_info[rate_index+IWL_OFDM_RATES];
  1981. pwr_info->requested_power = power;
  1982. pwr_info->power_table_index = pwr_index;
  1983. pwr_info->base_power_index = base_pwr_index;
  1984. pwr_info->tpc.tx_gain = gain;
  1985. pwr_info->tpc.dsp_atten = dsp_atten;
  1986. }
  1987. /* set scan tx power, 1Mbit for CCK, 6Mbit for OFDM */
  1988. for (scan_tbl_index = 0;
  1989. scan_tbl_index < IWL_NUM_SCAN_RATES; scan_tbl_index++) {
  1990. s32 actual_index = (scan_tbl_index == 0) ?
  1991. IWL_RATE_1M_INDEX_TABLE : IWL_RATE_6M_INDEX_TABLE;
  1992. iwl3945_hw_reg_set_scan_power(priv, scan_tbl_index,
  1993. actual_index, clip_pwrs, ch_info, a_band);
  1994. }
  1995. }
  1996. return 0;
  1997. }
  1998. int iwl3945_hw_rxq_stop(struct iwl3945_priv *priv)
  1999. {
  2000. int rc;
  2001. unsigned long flags;
  2002. spin_lock_irqsave(&priv->lock, flags);
  2003. rc = iwl3945_grab_nic_access(priv);
  2004. if (rc) {
  2005. spin_unlock_irqrestore(&priv->lock, flags);
  2006. return rc;
  2007. }
  2008. iwl3945_write_direct32(priv, FH_RCSR_CONFIG(0), 0);
  2009. rc = iwl3945_poll_direct_bit(priv, FH_RSSR_STATUS, (1 << 24), 1000);
  2010. if (rc < 0)
  2011. IWL_ERROR("Can't stop Rx DMA.\n");
  2012. iwl3945_release_nic_access(priv);
  2013. spin_unlock_irqrestore(&priv->lock, flags);
  2014. return 0;
  2015. }
  2016. int iwl3945_hw_tx_queue_init(struct iwl3945_priv *priv, struct iwl3945_tx_queue *txq)
  2017. {
  2018. int rc;
  2019. unsigned long flags;
  2020. int txq_id = txq->q.id;
  2021. struct iwl3945_shared *shared_data = priv->hw_setting.shared_virt;
  2022. shared_data->tx_base_ptr[txq_id] = cpu_to_le32((u32)txq->q.dma_addr);
  2023. spin_lock_irqsave(&priv->lock, flags);
  2024. rc = iwl3945_grab_nic_access(priv);
  2025. if (rc) {
  2026. spin_unlock_irqrestore(&priv->lock, flags);
  2027. return rc;
  2028. }
  2029. iwl3945_write_direct32(priv, FH_CBCC_CTRL(txq_id), 0);
  2030. iwl3945_write_direct32(priv, FH_CBCC_BASE(txq_id), 0);
  2031. iwl3945_write_direct32(priv, FH_TCSR_CONFIG(txq_id),
  2032. ALM_FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_RTC_NOINT |
  2033. ALM_FH_TCSR_TX_CONFIG_REG_VAL_MSG_MODE_TXF |
  2034. ALM_FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_IFTFD |
  2035. ALM_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL |
  2036. ALM_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE);
  2037. iwl3945_release_nic_access(priv);
  2038. /* fake read to flush all prev. writes */
  2039. iwl3945_read32(priv, FH_TSSR_CBB_BASE);
  2040. spin_unlock_irqrestore(&priv->lock, flags);
  2041. return 0;
  2042. }
  2043. int iwl3945_hw_get_rx_read(struct iwl3945_priv *priv)
  2044. {
  2045. struct iwl3945_shared *shared_data = priv->hw_setting.shared_virt;
  2046. return le32_to_cpu(shared_data->rx_read_ptr[0]);
  2047. }
  2048. /**
  2049. * iwl3945_init_hw_rate_table - Initialize the hardware rate fallback table
  2050. */
  2051. int iwl3945_init_hw_rate_table(struct iwl3945_priv *priv)
  2052. {
  2053. int rc, i, index, prev_index;
  2054. struct iwl3945_rate_scaling_cmd rate_cmd = {
  2055. .reserved = {0, 0, 0},
  2056. };
  2057. struct iwl3945_rate_scaling_info *table = rate_cmd.table;
  2058. for (i = 0; i < ARRAY_SIZE(iwl3945_rates); i++) {
  2059. index = iwl3945_rates[i].table_rs_index;
  2060. table[index].rate_n_flags =
  2061. iwl3945_hw_set_rate_n_flags(iwl3945_rates[i].plcp, 0);
  2062. table[index].try_cnt = priv->retry_rate;
  2063. prev_index = iwl3945_get_prev_ieee_rate(i);
  2064. table[index].next_rate_index = iwl3945_rates[prev_index].table_rs_index;
  2065. }
  2066. switch (priv->band) {
  2067. case IEEE80211_BAND_5GHZ:
  2068. IWL_DEBUG_RATE("Select A mode rate scale\n");
  2069. /* If one of the following CCK rates is used,
  2070. * have it fall back to the 6M OFDM rate */
  2071. for (i = IWL_RATE_1M_INDEX_TABLE; i <= IWL_RATE_11M_INDEX_TABLE; i++)
  2072. table[i].next_rate_index = iwl3945_rates[IWL_FIRST_OFDM_RATE].table_rs_index;
  2073. /* Don't fall back to CCK rates */
  2074. table[IWL_RATE_12M_INDEX_TABLE].next_rate_index = IWL_RATE_9M_INDEX_TABLE;
  2075. /* Don't drop out of OFDM rates */
  2076. table[IWL_RATE_6M_INDEX_TABLE].next_rate_index =
  2077. iwl3945_rates[IWL_FIRST_OFDM_RATE].table_rs_index;
  2078. break;
  2079. case IEEE80211_BAND_2GHZ:
  2080. IWL_DEBUG_RATE("Select B/G mode rate scale\n");
  2081. /* If an OFDM rate is used, have it fall back to the
  2082. * 1M CCK rates */
  2083. for (i = IWL_RATE_6M_INDEX_TABLE; i <= IWL_RATE_54M_INDEX_TABLE; i++)
  2084. table[i].next_rate_index = iwl3945_rates[IWL_FIRST_CCK_RATE].table_rs_index;
  2085. /* CCK shouldn't fall back to OFDM... */
  2086. table[IWL_RATE_11M_INDEX_TABLE].next_rate_index = IWL_RATE_5M_INDEX_TABLE;
  2087. break;
  2088. default:
  2089. WARN_ON(1);
  2090. break;
  2091. }
  2092. /* Update the rate scaling for control frame Tx */
  2093. rate_cmd.table_id = 0;
  2094. rc = iwl3945_send_cmd_pdu(priv, REPLY_RATE_SCALE, sizeof(rate_cmd),
  2095. &rate_cmd);
  2096. if (rc)
  2097. return rc;
  2098. /* Update the rate scaling for data frame Tx */
  2099. rate_cmd.table_id = 1;
  2100. return iwl3945_send_cmd_pdu(priv, REPLY_RATE_SCALE, sizeof(rate_cmd),
  2101. &rate_cmd);
  2102. }
  2103. /* Called when initializing driver */
  2104. int iwl3945_hw_set_hw_setting(struct iwl3945_priv *priv)
  2105. {
  2106. memset((void *)&priv->hw_setting, 0,
  2107. sizeof(struct iwl3945_driver_hw_info));
  2108. priv->hw_setting.shared_virt =
  2109. pci_alloc_consistent(priv->pci_dev,
  2110. sizeof(struct iwl3945_shared),
  2111. &priv->hw_setting.shared_phys);
  2112. if (!priv->hw_setting.shared_virt) {
  2113. IWL_ERROR("failed to allocate pci memory\n");
  2114. mutex_unlock(&priv->mutex);
  2115. return -ENOMEM;
  2116. }
  2117. priv->hw_setting.rx_buf_size = IWL_RX_BUF_SIZE;
  2118. priv->hw_setting.max_pkt_size = 2342;
  2119. priv->hw_setting.tx_cmd_len = sizeof(struct iwl3945_tx_cmd);
  2120. priv->hw_setting.max_rxq_size = RX_QUEUE_SIZE;
  2121. priv->hw_setting.max_rxq_log = RX_QUEUE_SIZE_LOG;
  2122. priv->hw_setting.max_stations = IWL3945_STATION_COUNT;
  2123. priv->hw_setting.bcast_sta_id = IWL3945_BROADCAST_ID;
  2124. priv->hw_setting.tx_ant_num = 2;
  2125. return 0;
  2126. }
  2127. unsigned int iwl3945_hw_get_beacon_cmd(struct iwl3945_priv *priv,
  2128. struct iwl3945_frame *frame, u8 rate)
  2129. {
  2130. struct iwl3945_tx_beacon_cmd *tx_beacon_cmd;
  2131. unsigned int frame_size;
  2132. tx_beacon_cmd = (struct iwl3945_tx_beacon_cmd *)&frame->u;
  2133. memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
  2134. tx_beacon_cmd->tx.sta_id = IWL3945_BROADCAST_ID;
  2135. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2136. frame_size = iwl3945_fill_beacon_frame(priv,
  2137. tx_beacon_cmd->frame,
  2138. iwl3945_broadcast_addr,
  2139. sizeof(frame->u) - sizeof(*tx_beacon_cmd));
  2140. BUG_ON(frame_size > MAX_MPDU_SIZE);
  2141. tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
  2142. tx_beacon_cmd->tx.rate = rate;
  2143. tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
  2144. TX_CMD_FLG_TSF_MSK);
  2145. /* supp_rates[0] == OFDM start at IWL_FIRST_OFDM_RATE*/
  2146. tx_beacon_cmd->tx.supp_rates[0] =
  2147. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  2148. tx_beacon_cmd->tx.supp_rates[1] =
  2149. (IWL_CCK_BASIC_RATES_MASK & 0xF);
  2150. return (sizeof(struct iwl3945_tx_beacon_cmd) + frame_size);
  2151. }
  2152. void iwl3945_hw_rx_handler_setup(struct iwl3945_priv *priv)
  2153. {
  2154. priv->rx_handlers[REPLY_3945_RX] = iwl3945_rx_reply_rx;
  2155. }
  2156. void iwl3945_hw_setup_deferred_work(struct iwl3945_priv *priv)
  2157. {
  2158. INIT_DELAYED_WORK(&priv->thermal_periodic,
  2159. iwl3945_bg_reg_txpower_periodic);
  2160. }
  2161. void iwl3945_hw_cancel_deferred_work(struct iwl3945_priv *priv)
  2162. {
  2163. cancel_delayed_work(&priv->thermal_periodic);
  2164. }
  2165. struct pci_device_id iwl3945_hw_card_ids[] = {
  2166. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4222)},
  2167. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4227)},
  2168. {0}
  2169. };
  2170. MODULE_DEVICE_TABLE(pci, iwl3945_hw_card_ids);