iwl-3945.c 68 KB

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