iwl-3945.c 72 KB

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