iwl-agn-lib.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/etherdevice.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/sched.h>
  34. #include "iwl-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-helpers.h"
  38. #include "iwl-agn-hw.h"
  39. #include "iwl-agn.h"
  40. #include "iwl-sta.h"
  41. static inline u32 iwlagn_get_scd_ssn(struct iwlagn_tx_resp *tx_resp)
  42. {
  43. return le32_to_cpup((__le32 *)&tx_resp->status +
  44. tx_resp->frame_count) & MAX_SN;
  45. }
  46. static void iwlagn_count_tx_err_status(struct iwl_priv *priv, u16 status)
  47. {
  48. status &= TX_STATUS_MSK;
  49. switch (status) {
  50. case TX_STATUS_POSTPONE_DELAY:
  51. priv->_agn.reply_tx_stats.pp_delay++;
  52. break;
  53. case TX_STATUS_POSTPONE_FEW_BYTES:
  54. priv->_agn.reply_tx_stats.pp_few_bytes++;
  55. break;
  56. case TX_STATUS_POSTPONE_BT_PRIO:
  57. priv->_agn.reply_tx_stats.pp_bt_prio++;
  58. break;
  59. case TX_STATUS_POSTPONE_QUIET_PERIOD:
  60. priv->_agn.reply_tx_stats.pp_quiet_period++;
  61. break;
  62. case TX_STATUS_POSTPONE_CALC_TTAK:
  63. priv->_agn.reply_tx_stats.pp_calc_ttak++;
  64. break;
  65. case TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY:
  66. priv->_agn.reply_tx_stats.int_crossed_retry++;
  67. break;
  68. case TX_STATUS_FAIL_SHORT_LIMIT:
  69. priv->_agn.reply_tx_stats.short_limit++;
  70. break;
  71. case TX_STATUS_FAIL_LONG_LIMIT:
  72. priv->_agn.reply_tx_stats.long_limit++;
  73. break;
  74. case TX_STATUS_FAIL_FIFO_UNDERRUN:
  75. priv->_agn.reply_tx_stats.fifo_underrun++;
  76. break;
  77. case TX_STATUS_FAIL_DRAIN_FLOW:
  78. priv->_agn.reply_tx_stats.drain_flow++;
  79. break;
  80. case TX_STATUS_FAIL_RFKILL_FLUSH:
  81. priv->_agn.reply_tx_stats.rfkill_flush++;
  82. break;
  83. case TX_STATUS_FAIL_LIFE_EXPIRE:
  84. priv->_agn.reply_tx_stats.life_expire++;
  85. break;
  86. case TX_STATUS_FAIL_DEST_PS:
  87. priv->_agn.reply_tx_stats.dest_ps++;
  88. break;
  89. case TX_STATUS_FAIL_HOST_ABORTED:
  90. priv->_agn.reply_tx_stats.host_abort++;
  91. break;
  92. case TX_STATUS_FAIL_BT_RETRY:
  93. priv->_agn.reply_tx_stats.bt_retry++;
  94. break;
  95. case TX_STATUS_FAIL_STA_INVALID:
  96. priv->_agn.reply_tx_stats.sta_invalid++;
  97. break;
  98. case TX_STATUS_FAIL_FRAG_DROPPED:
  99. priv->_agn.reply_tx_stats.frag_drop++;
  100. break;
  101. case TX_STATUS_FAIL_TID_DISABLE:
  102. priv->_agn.reply_tx_stats.tid_disable++;
  103. break;
  104. case TX_STATUS_FAIL_FIFO_FLUSHED:
  105. priv->_agn.reply_tx_stats.fifo_flush++;
  106. break;
  107. case TX_STATUS_FAIL_INSUFFICIENT_CF_POLL:
  108. priv->_agn.reply_tx_stats.insuff_cf_poll++;
  109. break;
  110. case TX_STATUS_FAIL_PASSIVE_NO_RX:
  111. priv->_agn.reply_tx_stats.fail_hw_drop++;
  112. break;
  113. case TX_STATUS_FAIL_NO_BEACON_ON_RADAR:
  114. priv->_agn.reply_tx_stats.sta_color_mismatch++;
  115. break;
  116. default:
  117. priv->_agn.reply_tx_stats.unknown++;
  118. break;
  119. }
  120. }
  121. static void iwlagn_count_agg_tx_err_status(struct iwl_priv *priv, u16 status)
  122. {
  123. status &= AGG_TX_STATUS_MSK;
  124. switch (status) {
  125. case AGG_TX_STATE_UNDERRUN_MSK:
  126. priv->_agn.reply_agg_tx_stats.underrun++;
  127. break;
  128. case AGG_TX_STATE_BT_PRIO_MSK:
  129. priv->_agn.reply_agg_tx_stats.bt_prio++;
  130. break;
  131. case AGG_TX_STATE_FEW_BYTES_MSK:
  132. priv->_agn.reply_agg_tx_stats.few_bytes++;
  133. break;
  134. case AGG_TX_STATE_ABORT_MSK:
  135. priv->_agn.reply_agg_tx_stats.abort++;
  136. break;
  137. case AGG_TX_STATE_LAST_SENT_TTL_MSK:
  138. priv->_agn.reply_agg_tx_stats.last_sent_ttl++;
  139. break;
  140. case AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK:
  141. priv->_agn.reply_agg_tx_stats.last_sent_try++;
  142. break;
  143. case AGG_TX_STATE_LAST_SENT_BT_KILL_MSK:
  144. priv->_agn.reply_agg_tx_stats.last_sent_bt_kill++;
  145. break;
  146. case AGG_TX_STATE_SCD_QUERY_MSK:
  147. priv->_agn.reply_agg_tx_stats.scd_query++;
  148. break;
  149. case AGG_TX_STATE_TEST_BAD_CRC32_MSK:
  150. priv->_agn.reply_agg_tx_stats.bad_crc32++;
  151. break;
  152. case AGG_TX_STATE_RESPONSE_MSK:
  153. priv->_agn.reply_agg_tx_stats.response++;
  154. break;
  155. case AGG_TX_STATE_DUMP_TX_MSK:
  156. priv->_agn.reply_agg_tx_stats.dump_tx++;
  157. break;
  158. case AGG_TX_STATE_DELAY_TX_MSK:
  159. priv->_agn.reply_agg_tx_stats.delay_tx++;
  160. break;
  161. default:
  162. priv->_agn.reply_agg_tx_stats.unknown++;
  163. break;
  164. }
  165. }
  166. static void iwlagn_set_tx_status(struct iwl_priv *priv,
  167. struct ieee80211_tx_info *info,
  168. struct iwl_rxon_context *ctx,
  169. struct iwlagn_tx_resp *tx_resp,
  170. int txq_id, bool is_agg)
  171. {
  172. u16 status = le16_to_cpu(tx_resp->status.status);
  173. info->status.rates[0].count = tx_resp->failure_frame + 1;
  174. if (is_agg)
  175. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  176. info->flags |= iwl_tx_status_to_mac80211(status);
  177. iwlagn_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
  178. info);
  179. if (!iwl_is_tx_success(status))
  180. iwlagn_count_tx_err_status(priv, status);
  181. if (status == TX_STATUS_FAIL_PASSIVE_NO_RX &&
  182. iwl_is_associated_ctx(ctx) && ctx->vif &&
  183. ctx->vif->type == NL80211_IFTYPE_STATION) {
  184. ctx->last_tx_rejected = true;
  185. iwl_stop_queue(priv, &priv->txq[txq_id]);
  186. }
  187. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
  188. "0x%x retries %d\n",
  189. txq_id,
  190. iwl_get_tx_fail_reason(status), status,
  191. le32_to_cpu(tx_resp->rate_n_flags),
  192. tx_resp->failure_frame);
  193. }
  194. #ifdef CONFIG_IWLWIFI_DEBUG
  195. #define AGG_TX_STATE_FAIL(x) case AGG_TX_STATE_ ## x: return #x
  196. const char *iwl_get_agg_tx_fail_reason(u16 status)
  197. {
  198. status &= AGG_TX_STATUS_MSK;
  199. switch (status) {
  200. case AGG_TX_STATE_TRANSMITTED:
  201. return "SUCCESS";
  202. AGG_TX_STATE_FAIL(UNDERRUN_MSK);
  203. AGG_TX_STATE_FAIL(BT_PRIO_MSK);
  204. AGG_TX_STATE_FAIL(FEW_BYTES_MSK);
  205. AGG_TX_STATE_FAIL(ABORT_MSK);
  206. AGG_TX_STATE_FAIL(LAST_SENT_TTL_MSK);
  207. AGG_TX_STATE_FAIL(LAST_SENT_TRY_CNT_MSK);
  208. AGG_TX_STATE_FAIL(LAST_SENT_BT_KILL_MSK);
  209. AGG_TX_STATE_FAIL(SCD_QUERY_MSK);
  210. AGG_TX_STATE_FAIL(TEST_BAD_CRC32_MSK);
  211. AGG_TX_STATE_FAIL(RESPONSE_MSK);
  212. AGG_TX_STATE_FAIL(DUMP_TX_MSK);
  213. AGG_TX_STATE_FAIL(DELAY_TX_MSK);
  214. }
  215. return "UNKNOWN";
  216. }
  217. #endif /* CONFIG_IWLWIFI_DEBUG */
  218. static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
  219. struct iwl_ht_agg *agg,
  220. struct iwlagn_tx_resp *tx_resp,
  221. int txq_id, u16 start_idx)
  222. {
  223. u16 status;
  224. struct agg_tx_status *frame_status = &tx_resp->status;
  225. struct ieee80211_hdr *hdr = NULL;
  226. int i, sh, idx;
  227. u16 seq;
  228. if (agg->wait_for_ba)
  229. IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
  230. agg->frame_count = tx_resp->frame_count;
  231. agg->start_idx = start_idx;
  232. agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  233. agg->bitmap = 0;
  234. /* # frames attempted by Tx command */
  235. if (agg->frame_count == 1) {
  236. struct iwl_tx_info *txb;
  237. /* Only one frame was attempted; no block-ack will arrive */
  238. idx = start_idx;
  239. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
  240. agg->frame_count, agg->start_idx, idx);
  241. txb = &priv->txq[txq_id].txb[idx];
  242. iwlagn_set_tx_status(priv, IEEE80211_SKB_CB(txb->skb),
  243. txb->ctx, tx_resp, txq_id, true);
  244. agg->wait_for_ba = 0;
  245. } else {
  246. /* Two or more frames were attempted; expect block-ack */
  247. u64 bitmap = 0;
  248. /*
  249. * Start is the lowest frame sent. It may not be the first
  250. * frame in the batch; we figure this out dynamically during
  251. * the following loop.
  252. */
  253. int start = agg->start_idx;
  254. /* Construct bit-map of pending frames within Tx window */
  255. for (i = 0; i < agg->frame_count; i++) {
  256. u16 sc;
  257. status = le16_to_cpu(frame_status[i].status);
  258. seq = le16_to_cpu(frame_status[i].sequence);
  259. idx = SEQ_TO_INDEX(seq);
  260. txq_id = SEQ_TO_QUEUE(seq);
  261. if (status & AGG_TX_STATUS_MSK)
  262. iwlagn_count_agg_tx_err_status(priv, status);
  263. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  264. AGG_TX_STATE_ABORT_MSK))
  265. continue;
  266. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
  267. agg->frame_count, txq_id, idx);
  268. IWL_DEBUG_TX_REPLY(priv, "status %s (0x%08x), "
  269. "try-count (0x%08x)\n",
  270. iwl_get_agg_tx_fail_reason(status),
  271. status & AGG_TX_STATUS_MSK,
  272. status & AGG_TX_TRY_MSK);
  273. hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
  274. if (!hdr) {
  275. IWL_ERR(priv,
  276. "BUG_ON idx doesn't point to valid skb"
  277. " idx=%d, txq_id=%d\n", idx, txq_id);
  278. return -1;
  279. }
  280. sc = le16_to_cpu(hdr->seq_ctrl);
  281. if (idx != (SEQ_TO_SN(sc) & 0xff)) {
  282. IWL_ERR(priv,
  283. "BUG_ON idx doesn't match seq control"
  284. " idx=%d, seq_idx=%d, seq=%d\n",
  285. idx, SEQ_TO_SN(sc),
  286. hdr->seq_ctrl);
  287. return -1;
  288. }
  289. IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
  290. i, idx, SEQ_TO_SN(sc));
  291. /*
  292. * sh -> how many frames ahead of the starting frame is
  293. * the current one?
  294. *
  295. * Note that all frames sent in the batch must be in a
  296. * 64-frame window, so this number should be in [0,63].
  297. * If outside of this window, then we've found a new
  298. * "first" frame in the batch and need to change start.
  299. */
  300. sh = idx - start;
  301. /*
  302. * If >= 64, out of window. start must be at the front
  303. * of the circular buffer, idx must be near the end of
  304. * the buffer, and idx is the new "first" frame. Shift
  305. * the indices around.
  306. */
  307. if (sh >= 64) {
  308. /* Shift bitmap by start - idx, wrapped */
  309. sh = 0x100 - idx + start;
  310. bitmap = bitmap << sh;
  311. /* Now idx is the new start so sh = 0 */
  312. sh = 0;
  313. start = idx;
  314. /*
  315. * If <= -64 then wraps the 256-pkt circular buffer
  316. * (e.g., start = 255 and idx = 0, sh should be 1)
  317. */
  318. } else if (sh <= -64) {
  319. sh = 0x100 - start + idx;
  320. /*
  321. * If < 0 but > -64, out of window. idx is before start
  322. * but not wrapped. Shift the indices around.
  323. */
  324. } else if (sh < 0) {
  325. /* Shift by how far start is ahead of idx */
  326. sh = start - idx;
  327. bitmap = bitmap << sh;
  328. /* Now idx is the new start so sh = 0 */
  329. start = idx;
  330. sh = 0;
  331. }
  332. /* Sequence number start + sh was sent in this batch */
  333. bitmap |= 1ULL << sh;
  334. IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
  335. start, (unsigned long long)bitmap);
  336. }
  337. /*
  338. * Store the bitmap and possibly the new start, if we wrapped
  339. * the buffer above
  340. */
  341. agg->bitmap = bitmap;
  342. agg->start_idx = start;
  343. IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
  344. agg->frame_count, agg->start_idx,
  345. (unsigned long long)agg->bitmap);
  346. if (bitmap)
  347. agg->wait_for_ba = 1;
  348. }
  349. return 0;
  350. }
  351. void iwl_check_abort_status(struct iwl_priv *priv,
  352. u8 frame_count, u32 status)
  353. {
  354. if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
  355. IWL_ERR(priv, "Tx flush command to flush out all frames\n");
  356. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  357. queue_work(priv->workqueue, &priv->tx_flush);
  358. }
  359. }
  360. static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
  361. struct iwl_rx_mem_buffer *rxb)
  362. {
  363. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  364. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  365. int txq_id = SEQ_TO_QUEUE(sequence);
  366. int index = SEQ_TO_INDEX(sequence);
  367. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  368. struct ieee80211_tx_info *info;
  369. struct iwlagn_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  370. struct iwl_tx_info *txb;
  371. u32 status = le16_to_cpu(tx_resp->status.status);
  372. int tid;
  373. int sta_id;
  374. int freed;
  375. unsigned long flags;
  376. if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
  377. IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
  378. "is out of range [0-%d] %d %d\n", txq_id,
  379. index, txq->q.n_bd, txq->q.write_ptr,
  380. txq->q.read_ptr);
  381. return;
  382. }
  383. txq->time_stamp = jiffies;
  384. txb = &txq->txb[txq->q.read_ptr];
  385. info = IEEE80211_SKB_CB(txb->skb);
  386. memset(&info->status, 0, sizeof(info->status));
  387. tid = (tx_resp->ra_tid & IWLAGN_TX_RES_TID_MSK) >>
  388. IWLAGN_TX_RES_TID_POS;
  389. sta_id = (tx_resp->ra_tid & IWLAGN_TX_RES_RA_MSK) >>
  390. IWLAGN_TX_RES_RA_POS;
  391. spin_lock_irqsave(&priv->sta_lock, flags);
  392. if (txq->sched_retry) {
  393. const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
  394. struct iwl_ht_agg *agg;
  395. agg = &priv->stations[sta_id].tid[tid].agg;
  396. /*
  397. * If the BT kill count is non-zero, we'll get this
  398. * notification again.
  399. */
  400. if (tx_resp->bt_kill_count && tx_resp->frame_count == 1 &&
  401. priv->cfg->bt_params &&
  402. priv->cfg->bt_params->advanced_bt_coexist) {
  403. IWL_WARN(priv, "receive reply tx with bt_kill\n");
  404. }
  405. iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  406. /* check if BAR is needed */
  407. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
  408. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  409. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  410. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  411. IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
  412. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  413. scd_ssn , index, txq_id, txq->swq_id);
  414. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  415. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  416. if (priv->mac80211_registered &&
  417. (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  418. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
  419. iwl_wake_queue(priv, txq);
  420. }
  421. } else {
  422. iwlagn_set_tx_status(priv, info, txb->ctx, tx_resp,
  423. txq_id, false);
  424. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  425. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  426. if (priv->mac80211_registered &&
  427. iwl_queue_space(&txq->q) > txq->q.low_mark &&
  428. status != TX_STATUS_FAIL_PASSIVE_NO_RX)
  429. iwl_wake_queue(priv, txq);
  430. }
  431. iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
  432. iwl_check_abort_status(priv, tx_resp->frame_count, status);
  433. spin_unlock_irqrestore(&priv->sta_lock, flags);
  434. }
  435. void iwlagn_rx_handler_setup(struct iwl_priv *priv)
  436. {
  437. /* init calibration handlers */
  438. priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
  439. iwlagn_rx_calib_result;
  440. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  441. /* set up notification wait support */
  442. spin_lock_init(&priv->_agn.notif_wait_lock);
  443. INIT_LIST_HEAD(&priv->_agn.notif_waits);
  444. init_waitqueue_head(&priv->_agn.notif_waitq);
  445. }
  446. void iwlagn_setup_deferred_work(struct iwl_priv *priv)
  447. {
  448. /*
  449. * nothing need to be done here anymore
  450. * still keep for future use if needed
  451. */
  452. }
  453. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  454. {
  455. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  456. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  457. }
  458. int iwlagn_send_tx_power(struct iwl_priv *priv)
  459. {
  460. struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
  461. u8 tx_ant_cfg_cmd;
  462. if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->status),
  463. "TX Power requested while scanning!\n"))
  464. return -EAGAIN;
  465. /* half dBm need to multiply */
  466. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  467. if (priv->tx_power_lmt_in_half_dbm &&
  468. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  469. /*
  470. * For the newer devices which using enhanced/extend tx power
  471. * table in EEPROM, the format is in half dBm. driver need to
  472. * convert to dBm format before report to mac80211.
  473. * By doing so, there is a possibility of 1/2 dBm resolution
  474. * lost. driver will perform "round-up" operation before
  475. * reporting, but it will cause 1/2 dBm tx power over the
  476. * regulatory limit. Perform the checking here, if the
  477. * "tx_power_user_lmt" is higher than EEPROM value (in
  478. * half-dBm format), lower the tx power based on EEPROM
  479. */
  480. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  481. }
  482. tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
  483. tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
  484. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  485. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  486. else
  487. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  488. return iwl_send_cmd_pdu(priv, tx_ant_cfg_cmd, sizeof(tx_power_cmd),
  489. &tx_power_cmd);
  490. }
  491. void iwlagn_temperature(struct iwl_priv *priv)
  492. {
  493. /* store temperature from correct statistics (in Celsius) */
  494. priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
  495. iwl_tt_handler(priv);
  496. }
  497. u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
  498. {
  499. struct iwl_eeprom_calib_hdr {
  500. u8 version;
  501. u8 pa_type;
  502. u16 voltage;
  503. } *hdr;
  504. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  505. EEPROM_CALIB_ALL);
  506. return hdr->version;
  507. }
  508. /*
  509. * EEPROM
  510. */
  511. static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
  512. {
  513. u16 offset = 0;
  514. if ((address & INDIRECT_ADDRESS) == 0)
  515. return address;
  516. switch (address & INDIRECT_TYPE_MSK) {
  517. case INDIRECT_HOST:
  518. offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
  519. break;
  520. case INDIRECT_GENERAL:
  521. offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
  522. break;
  523. case INDIRECT_REGULATORY:
  524. offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
  525. break;
  526. case INDIRECT_TXP_LIMIT:
  527. offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT);
  528. break;
  529. case INDIRECT_TXP_LIMIT_SIZE:
  530. offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT_SIZE);
  531. break;
  532. case INDIRECT_CALIBRATION:
  533. offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
  534. break;
  535. case INDIRECT_PROCESS_ADJST:
  536. offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
  537. break;
  538. case INDIRECT_OTHERS:
  539. offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
  540. break;
  541. default:
  542. IWL_ERR(priv, "illegal indirect type: 0x%X\n",
  543. address & INDIRECT_TYPE_MSK);
  544. break;
  545. }
  546. /* translate the offset from words to byte */
  547. return (address & ADDRESS_MSK) + (offset << 1);
  548. }
  549. const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
  550. size_t offset)
  551. {
  552. u32 address = eeprom_indirect_address(priv, offset);
  553. BUG_ON(address >= priv->cfg->base_params->eeprom_size);
  554. return &priv->eeprom[address];
  555. }
  556. struct iwl_mod_params iwlagn_mod_params = {
  557. .amsdu_size_8K = 1,
  558. .restart_fw = 1,
  559. .plcp_check = true,
  560. /* the rest are 0 by default */
  561. };
  562. void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  563. {
  564. unsigned long flags;
  565. int i;
  566. spin_lock_irqsave(&rxq->lock, flags);
  567. INIT_LIST_HEAD(&rxq->rx_free);
  568. INIT_LIST_HEAD(&rxq->rx_used);
  569. /* Fill the rx_used queue with _all_ of the Rx buffers */
  570. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  571. /* In the reset function, these buffers may have been allocated
  572. * to an SKB, so we need to unmap and free potential storage */
  573. if (rxq->pool[i].page != NULL) {
  574. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  575. PAGE_SIZE << priv->hw_params.rx_page_order,
  576. PCI_DMA_FROMDEVICE);
  577. __iwl_free_pages(priv, rxq->pool[i].page);
  578. rxq->pool[i].page = NULL;
  579. }
  580. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  581. }
  582. for (i = 0; i < RX_QUEUE_SIZE; i++)
  583. rxq->queue[i] = NULL;
  584. /* Set us so that we have processed and used all buffers, but have
  585. * not restocked the Rx queue with fresh buffers */
  586. rxq->read = rxq->write = 0;
  587. rxq->write_actual = 0;
  588. rxq->free_count = 0;
  589. spin_unlock_irqrestore(&rxq->lock, flags);
  590. }
  591. int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  592. {
  593. u32 rb_size;
  594. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  595. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  596. rb_timeout = RX_RB_TIMEOUT;
  597. if (iwlagn_mod_params.amsdu_size_8K)
  598. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  599. else
  600. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  601. /* Stop Rx DMA */
  602. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  603. /* Reset driver's Rx queue write index */
  604. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  605. /* Tell device where to find RBD circular buffer in DRAM */
  606. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  607. (u32)(rxq->bd_dma >> 8));
  608. /* Tell device where in DRAM to update its Rx status */
  609. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  610. rxq->rb_stts_dma >> 4);
  611. /* Enable Rx DMA
  612. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  613. * the credit mechanism in 5000 HW RX FIFO
  614. * Direct rx interrupts to hosts
  615. * Rx buffer size 4 or 8k
  616. * RB timeout 0x10
  617. * 256 RBDs
  618. */
  619. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  620. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  621. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  622. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  623. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  624. rb_size|
  625. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  626. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  627. /* Set interrupt coalescing timer to default (2048 usecs) */
  628. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  629. return 0;
  630. }
  631. static void iwlagn_set_pwr_vmain(struct iwl_priv *priv)
  632. {
  633. /*
  634. * (for documentation purposes)
  635. * to set power to V_AUX, do:
  636. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  637. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  638. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  639. ~APMG_PS_CTRL_MSK_PWR_SRC);
  640. */
  641. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  642. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  643. ~APMG_PS_CTRL_MSK_PWR_SRC);
  644. }
  645. int iwlagn_hw_nic_init(struct iwl_priv *priv)
  646. {
  647. unsigned long flags;
  648. struct iwl_rx_queue *rxq = &priv->rxq;
  649. int ret;
  650. /* nic_init */
  651. spin_lock_irqsave(&priv->lock, flags);
  652. priv->cfg->ops->lib->apm_ops.init(priv);
  653. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  654. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  655. spin_unlock_irqrestore(&priv->lock, flags);
  656. iwlagn_set_pwr_vmain(priv);
  657. priv->cfg->ops->lib->apm_ops.config(priv);
  658. /* Allocate the RX queue, or reset if it is already allocated */
  659. if (!rxq->bd) {
  660. ret = iwl_rx_queue_alloc(priv);
  661. if (ret) {
  662. IWL_ERR(priv, "Unable to initialize Rx queue\n");
  663. return -ENOMEM;
  664. }
  665. } else
  666. iwlagn_rx_queue_reset(priv, rxq);
  667. iwlagn_rx_replenish(priv);
  668. iwlagn_rx_init(priv, rxq);
  669. spin_lock_irqsave(&priv->lock, flags);
  670. rxq->need_update = 1;
  671. iwl_rx_queue_update_write_ptr(priv, rxq);
  672. spin_unlock_irqrestore(&priv->lock, flags);
  673. /* Allocate or reset and init all Tx and Command queues */
  674. if (!priv->txq) {
  675. ret = iwlagn_txq_ctx_alloc(priv);
  676. if (ret)
  677. return ret;
  678. } else
  679. iwlagn_txq_ctx_reset(priv);
  680. if (priv->cfg->base_params->shadow_reg_enable) {
  681. /* enable shadow regs in HW */
  682. iwl_set_bit(priv, CSR_MAC_SHADOW_REG_CTRL,
  683. 0x800FFFFF);
  684. }
  685. set_bit(STATUS_INIT, &priv->status);
  686. return 0;
  687. }
  688. /**
  689. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  690. */
  691. static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
  692. dma_addr_t dma_addr)
  693. {
  694. return cpu_to_le32((u32)(dma_addr >> 8));
  695. }
  696. /**
  697. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  698. *
  699. * If there are slots in the RX queue that need to be restocked,
  700. * and we have free pre-allocated buffers, fill the ranks as much
  701. * as we can, pulling from rx_free.
  702. *
  703. * This moves the 'write' index forward to catch up with 'processed', and
  704. * also updates the memory address in the firmware to reference the new
  705. * target buffer.
  706. */
  707. void iwlagn_rx_queue_restock(struct iwl_priv *priv)
  708. {
  709. struct iwl_rx_queue *rxq = &priv->rxq;
  710. struct list_head *element;
  711. struct iwl_rx_mem_buffer *rxb;
  712. unsigned long flags;
  713. spin_lock_irqsave(&rxq->lock, flags);
  714. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  715. /* The overwritten rxb must be a used one */
  716. rxb = rxq->queue[rxq->write];
  717. BUG_ON(rxb && rxb->page);
  718. /* Get next free Rx buffer, remove from free list */
  719. element = rxq->rx_free.next;
  720. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  721. list_del(element);
  722. /* Point to Rx buffer via next RBD in circular buffer */
  723. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
  724. rxb->page_dma);
  725. rxq->queue[rxq->write] = rxb;
  726. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  727. rxq->free_count--;
  728. }
  729. spin_unlock_irqrestore(&rxq->lock, flags);
  730. /* If the pre-allocated buffer pool is dropping low, schedule to
  731. * refill it */
  732. if (rxq->free_count <= RX_LOW_WATERMARK)
  733. queue_work(priv->workqueue, &priv->rx_replenish);
  734. /* If we've added more space for the firmware to place data, tell it.
  735. * Increment device's write pointer in multiples of 8. */
  736. if (rxq->write_actual != (rxq->write & ~0x7)) {
  737. spin_lock_irqsave(&rxq->lock, flags);
  738. rxq->need_update = 1;
  739. spin_unlock_irqrestore(&rxq->lock, flags);
  740. iwl_rx_queue_update_write_ptr(priv, rxq);
  741. }
  742. }
  743. /**
  744. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  745. *
  746. * When moving to rx_free an SKB is allocated for the slot.
  747. *
  748. * Also restock the Rx queue via iwl_rx_queue_restock.
  749. * This is called as a scheduled work item (except for during initialization)
  750. */
  751. void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  752. {
  753. struct iwl_rx_queue *rxq = &priv->rxq;
  754. struct list_head *element;
  755. struct iwl_rx_mem_buffer *rxb;
  756. struct page *page;
  757. unsigned long flags;
  758. gfp_t gfp_mask = priority;
  759. while (1) {
  760. spin_lock_irqsave(&rxq->lock, flags);
  761. if (list_empty(&rxq->rx_used)) {
  762. spin_unlock_irqrestore(&rxq->lock, flags);
  763. return;
  764. }
  765. spin_unlock_irqrestore(&rxq->lock, flags);
  766. if (rxq->free_count > RX_LOW_WATERMARK)
  767. gfp_mask |= __GFP_NOWARN;
  768. if (priv->hw_params.rx_page_order > 0)
  769. gfp_mask |= __GFP_COMP;
  770. /* Alloc a new receive buffer */
  771. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  772. if (!page) {
  773. if (net_ratelimit())
  774. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  775. "order: %d\n",
  776. priv->hw_params.rx_page_order);
  777. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  778. net_ratelimit())
  779. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  780. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  781. rxq->free_count);
  782. /* We don't reschedule replenish work here -- we will
  783. * call the restock method and if it still needs
  784. * more buffers it will schedule replenish */
  785. return;
  786. }
  787. spin_lock_irqsave(&rxq->lock, flags);
  788. if (list_empty(&rxq->rx_used)) {
  789. spin_unlock_irqrestore(&rxq->lock, flags);
  790. __free_pages(page, priv->hw_params.rx_page_order);
  791. return;
  792. }
  793. element = rxq->rx_used.next;
  794. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  795. list_del(element);
  796. spin_unlock_irqrestore(&rxq->lock, flags);
  797. BUG_ON(rxb->page);
  798. rxb->page = page;
  799. /* Get physical address of the RB */
  800. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  801. PAGE_SIZE << priv->hw_params.rx_page_order,
  802. PCI_DMA_FROMDEVICE);
  803. /* dma address must be no more than 36 bits */
  804. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  805. /* and also 256 byte aligned! */
  806. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  807. spin_lock_irqsave(&rxq->lock, flags);
  808. list_add_tail(&rxb->list, &rxq->rx_free);
  809. rxq->free_count++;
  810. spin_unlock_irqrestore(&rxq->lock, flags);
  811. }
  812. }
  813. void iwlagn_rx_replenish(struct iwl_priv *priv)
  814. {
  815. unsigned long flags;
  816. iwlagn_rx_allocate(priv, GFP_KERNEL);
  817. spin_lock_irqsave(&priv->lock, flags);
  818. iwlagn_rx_queue_restock(priv);
  819. spin_unlock_irqrestore(&priv->lock, flags);
  820. }
  821. void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  822. {
  823. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  824. iwlagn_rx_queue_restock(priv);
  825. }
  826. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  827. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  828. * This free routine walks the list of POOL entries and if SKB is set to
  829. * non NULL it is unmapped and freed
  830. */
  831. void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  832. {
  833. int i;
  834. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  835. if (rxq->pool[i].page != NULL) {
  836. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  837. PAGE_SIZE << priv->hw_params.rx_page_order,
  838. PCI_DMA_FROMDEVICE);
  839. __iwl_free_pages(priv, rxq->pool[i].page);
  840. rxq->pool[i].page = NULL;
  841. }
  842. }
  843. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  844. rxq->bd_dma);
  845. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  846. rxq->rb_stts, rxq->rb_stts_dma);
  847. rxq->bd = NULL;
  848. rxq->rb_stts = NULL;
  849. }
  850. int iwlagn_rxq_stop(struct iwl_priv *priv)
  851. {
  852. /* stop Rx DMA */
  853. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  854. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  855. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  856. return 0;
  857. }
  858. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  859. {
  860. int idx = 0;
  861. int band_offset = 0;
  862. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  863. if (rate_n_flags & RATE_MCS_HT_MSK) {
  864. idx = (rate_n_flags & 0xff);
  865. return idx;
  866. /* Legacy rate format, search for match in table */
  867. } else {
  868. if (band == IEEE80211_BAND_5GHZ)
  869. band_offset = IWL_FIRST_OFDM_RATE;
  870. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  871. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  872. return idx - band_offset;
  873. }
  874. return -1;
  875. }
  876. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  877. struct ieee80211_vif *vif,
  878. enum ieee80211_band band,
  879. struct iwl_scan_channel *scan_ch)
  880. {
  881. const struct ieee80211_supported_band *sband;
  882. u16 passive_dwell = 0;
  883. u16 active_dwell = 0;
  884. int added = 0;
  885. u16 channel = 0;
  886. sband = iwl_get_hw_mode(priv, band);
  887. if (!sband) {
  888. IWL_ERR(priv, "invalid band\n");
  889. return added;
  890. }
  891. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  892. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  893. if (passive_dwell <= active_dwell)
  894. passive_dwell = active_dwell + 1;
  895. channel = iwl_get_single_channel_number(priv, band);
  896. if (channel) {
  897. scan_ch->channel = cpu_to_le16(channel);
  898. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  899. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  900. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  901. /* Set txpower levels to defaults */
  902. scan_ch->dsp_atten = 110;
  903. if (band == IEEE80211_BAND_5GHZ)
  904. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  905. else
  906. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  907. added++;
  908. } else
  909. IWL_ERR(priv, "no valid channel found\n");
  910. return added;
  911. }
  912. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  913. struct ieee80211_vif *vif,
  914. enum ieee80211_band band,
  915. u8 is_active, u8 n_probes,
  916. struct iwl_scan_channel *scan_ch)
  917. {
  918. struct ieee80211_channel *chan;
  919. const struct ieee80211_supported_band *sband;
  920. const struct iwl_channel_info *ch_info;
  921. u16 passive_dwell = 0;
  922. u16 active_dwell = 0;
  923. int added, i;
  924. u16 channel;
  925. sband = iwl_get_hw_mode(priv, band);
  926. if (!sband)
  927. return 0;
  928. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  929. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  930. if (passive_dwell <= active_dwell)
  931. passive_dwell = active_dwell + 1;
  932. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  933. chan = priv->scan_request->channels[i];
  934. if (chan->band != band)
  935. continue;
  936. channel = chan->hw_value;
  937. scan_ch->channel = cpu_to_le16(channel);
  938. ch_info = iwl_get_channel_info(priv, band, channel);
  939. if (!is_channel_valid(ch_info)) {
  940. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  941. channel);
  942. continue;
  943. }
  944. if (!is_active || is_channel_passive(ch_info) ||
  945. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  946. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  947. else
  948. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  949. if (n_probes)
  950. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  951. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  952. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  953. /* Set txpower levels to defaults */
  954. scan_ch->dsp_atten = 110;
  955. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  956. * power level:
  957. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  958. */
  959. if (band == IEEE80211_BAND_5GHZ)
  960. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  961. else
  962. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  963. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  964. channel, le32_to_cpu(scan_ch->type),
  965. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  966. "ACTIVE" : "PASSIVE",
  967. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  968. active_dwell : passive_dwell);
  969. scan_ch++;
  970. added++;
  971. }
  972. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  973. return added;
  974. }
  975. static int iwl_fill_offch_tx(struct iwl_priv *priv, void *data, size_t maxlen)
  976. {
  977. struct sk_buff *skb = priv->_agn.offchan_tx_skb;
  978. if (skb->len < maxlen)
  979. maxlen = skb->len;
  980. memcpy(data, skb->data, maxlen);
  981. return maxlen;
  982. }
  983. int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  984. {
  985. struct iwl_host_cmd cmd = {
  986. .id = REPLY_SCAN_CMD,
  987. .len = { sizeof(struct iwl_scan_cmd), },
  988. };
  989. struct iwl_scan_cmd *scan;
  990. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  991. u32 rate_flags = 0;
  992. u16 cmd_len;
  993. u16 rx_chain = 0;
  994. enum ieee80211_band band;
  995. u8 n_probes = 0;
  996. u8 rx_ant = priv->hw_params.valid_rx_ant;
  997. u8 rate;
  998. bool is_active = false;
  999. int chan_mod;
  1000. u8 active_chains;
  1001. u8 scan_tx_antennas = priv->hw_params.valid_tx_ant;
  1002. int ret;
  1003. lockdep_assert_held(&priv->mutex);
  1004. if (vif)
  1005. ctx = iwl_rxon_ctx_from_vif(vif);
  1006. if (!priv->scan_cmd) {
  1007. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  1008. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  1009. if (!priv->scan_cmd) {
  1010. IWL_DEBUG_SCAN(priv,
  1011. "fail to allocate memory for scan\n");
  1012. return -ENOMEM;
  1013. }
  1014. }
  1015. scan = priv->scan_cmd;
  1016. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  1017. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  1018. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  1019. if (priv->scan_type != IWL_SCAN_OFFCH_TX &&
  1020. iwl_is_any_associated(priv)) {
  1021. u16 interval = 0;
  1022. u32 extra;
  1023. u32 suspend_time = 100;
  1024. u32 scan_suspend_time = 100;
  1025. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  1026. switch (priv->scan_type) {
  1027. case IWL_SCAN_OFFCH_TX:
  1028. WARN_ON(1);
  1029. break;
  1030. case IWL_SCAN_RADIO_RESET:
  1031. interval = 0;
  1032. break;
  1033. case IWL_SCAN_NORMAL:
  1034. interval = vif->bss_conf.beacon_int;
  1035. break;
  1036. }
  1037. scan->suspend_time = 0;
  1038. scan->max_out_time = cpu_to_le32(200 * 1024);
  1039. if (!interval)
  1040. interval = suspend_time;
  1041. extra = (suspend_time / interval) << 22;
  1042. scan_suspend_time = (extra |
  1043. ((suspend_time % interval) * 1024));
  1044. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  1045. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  1046. scan_suspend_time, interval);
  1047. } else if (priv->scan_type == IWL_SCAN_OFFCH_TX) {
  1048. scan->suspend_time = 0;
  1049. scan->max_out_time =
  1050. cpu_to_le32(1024 * priv->_agn.offchan_tx_timeout);
  1051. }
  1052. switch (priv->scan_type) {
  1053. case IWL_SCAN_RADIO_RESET:
  1054. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  1055. break;
  1056. case IWL_SCAN_NORMAL:
  1057. if (priv->scan_request->n_ssids) {
  1058. int i, p = 0;
  1059. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  1060. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  1061. /* always does wildcard anyway */
  1062. if (!priv->scan_request->ssids[i].ssid_len)
  1063. continue;
  1064. scan->direct_scan[p].id = WLAN_EID_SSID;
  1065. scan->direct_scan[p].len =
  1066. priv->scan_request->ssids[i].ssid_len;
  1067. memcpy(scan->direct_scan[p].ssid,
  1068. priv->scan_request->ssids[i].ssid,
  1069. priv->scan_request->ssids[i].ssid_len);
  1070. n_probes++;
  1071. p++;
  1072. }
  1073. is_active = true;
  1074. } else
  1075. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  1076. break;
  1077. case IWL_SCAN_OFFCH_TX:
  1078. IWL_DEBUG_SCAN(priv, "Start offchannel TX scan.\n");
  1079. break;
  1080. }
  1081. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  1082. scan->tx_cmd.sta_id = ctx->bcast_sta_id;
  1083. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1084. switch (priv->scan_band) {
  1085. case IEEE80211_BAND_2GHZ:
  1086. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  1087. chan_mod = le32_to_cpu(
  1088. priv->contexts[IWL_RXON_CTX_BSS].active.flags &
  1089. RXON_FLG_CHANNEL_MODE_MSK)
  1090. >> RXON_FLG_CHANNEL_MODE_POS;
  1091. if (chan_mod == CHANNEL_MODE_PURE_40) {
  1092. rate = IWL_RATE_6M_PLCP;
  1093. } else {
  1094. rate = IWL_RATE_1M_PLCP;
  1095. rate_flags = RATE_MCS_CCK_MSK;
  1096. }
  1097. /*
  1098. * Internal scans are passive, so we can indiscriminately set
  1099. * the BT ignore flag on 2.4 GHz since it applies to TX only.
  1100. */
  1101. if (priv->cfg->bt_params &&
  1102. priv->cfg->bt_params->advanced_bt_coexist)
  1103. scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
  1104. break;
  1105. case IEEE80211_BAND_5GHZ:
  1106. rate = IWL_RATE_6M_PLCP;
  1107. break;
  1108. default:
  1109. IWL_WARN(priv, "Invalid scan band\n");
  1110. return -EIO;
  1111. }
  1112. /*
  1113. * If active scanning is requested but a certain channel is
  1114. * marked passive, we can do active scanning if we detect
  1115. * transmissions.
  1116. *
  1117. * There is an issue with some firmware versions that triggers
  1118. * a sysassert on a "good CRC threshold" of zero (== disabled),
  1119. * on a radar channel even though this means that we should NOT
  1120. * send probes.
  1121. *
  1122. * The "good CRC threshold" is the number of frames that we
  1123. * need to receive during our dwell time on a channel before
  1124. * sending out probes -- setting this to a huge value will
  1125. * mean we never reach it, but at the same time work around
  1126. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  1127. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  1128. *
  1129. * This was fixed in later versions along with some other
  1130. * scan changes, and the threshold behaves as a flag in those
  1131. * versions.
  1132. */
  1133. if (priv->new_scan_threshold_behaviour)
  1134. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  1135. IWL_GOOD_CRC_TH_DISABLED;
  1136. else
  1137. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  1138. IWL_GOOD_CRC_TH_NEVER;
  1139. band = priv->scan_band;
  1140. if (priv->cfg->scan_rx_antennas[band])
  1141. rx_ant = priv->cfg->scan_rx_antennas[band];
  1142. if (band == IEEE80211_BAND_2GHZ &&
  1143. priv->cfg->bt_params &&
  1144. priv->cfg->bt_params->advanced_bt_coexist) {
  1145. /* transmit 2.4 GHz probes only on first antenna */
  1146. scan_tx_antennas = first_antenna(scan_tx_antennas);
  1147. }
  1148. priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
  1149. scan_tx_antennas);
  1150. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  1151. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  1152. /* In power save mode use one chain, otherwise use all chains */
  1153. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  1154. /* rx_ant has been set to all valid chains previously */
  1155. active_chains = rx_ant &
  1156. ((u8)(priv->chain_noise_data.active_chains));
  1157. if (!active_chains)
  1158. active_chains = rx_ant;
  1159. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  1160. priv->chain_noise_data.active_chains);
  1161. rx_ant = first_antenna(active_chains);
  1162. }
  1163. if (priv->cfg->bt_params &&
  1164. priv->cfg->bt_params->advanced_bt_coexist &&
  1165. priv->bt_full_concurrent) {
  1166. /* operated as 1x1 in full concurrency mode */
  1167. rx_ant = first_antenna(rx_ant);
  1168. }
  1169. /* MIMO is not used here, but value is required */
  1170. rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  1171. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  1172. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  1173. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  1174. scan->rx_chain = cpu_to_le16(rx_chain);
  1175. switch (priv->scan_type) {
  1176. case IWL_SCAN_NORMAL:
  1177. cmd_len = iwl_fill_probe_req(priv,
  1178. (struct ieee80211_mgmt *)scan->data,
  1179. vif->addr,
  1180. priv->scan_request->ie,
  1181. priv->scan_request->ie_len,
  1182. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1183. break;
  1184. case IWL_SCAN_RADIO_RESET:
  1185. /* use bcast addr, will not be transmitted but must be valid */
  1186. cmd_len = iwl_fill_probe_req(priv,
  1187. (struct ieee80211_mgmt *)scan->data,
  1188. iwl_bcast_addr, NULL, 0,
  1189. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1190. break;
  1191. case IWL_SCAN_OFFCH_TX:
  1192. cmd_len = iwl_fill_offch_tx(priv, scan->data,
  1193. IWL_MAX_SCAN_SIZE
  1194. - sizeof(*scan)
  1195. - sizeof(struct iwl_scan_channel));
  1196. scan->scan_flags |= IWL_SCAN_FLAGS_ACTION_FRAME_TX;
  1197. break;
  1198. default:
  1199. BUG();
  1200. }
  1201. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  1202. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  1203. RXON_FILTER_BCON_AWARE_MSK);
  1204. switch (priv->scan_type) {
  1205. case IWL_SCAN_RADIO_RESET:
  1206. scan->channel_count =
  1207. iwl_get_single_channel_for_scan(priv, vif, band,
  1208. (void *)&scan->data[cmd_len]);
  1209. break;
  1210. case IWL_SCAN_NORMAL:
  1211. scan->channel_count =
  1212. iwl_get_channels_for_scan(priv, vif, band,
  1213. is_active, n_probes,
  1214. (void *)&scan->data[cmd_len]);
  1215. break;
  1216. case IWL_SCAN_OFFCH_TX: {
  1217. struct iwl_scan_channel *scan_ch;
  1218. scan->channel_count = 1;
  1219. scan_ch = (void *)&scan->data[cmd_len];
  1220. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  1221. scan_ch->channel =
  1222. cpu_to_le16(priv->_agn.offchan_tx_chan->hw_value);
  1223. scan_ch->active_dwell =
  1224. cpu_to_le16(priv->_agn.offchan_tx_timeout);
  1225. scan_ch->passive_dwell = 0;
  1226. /* Set txpower levels to defaults */
  1227. scan_ch->dsp_atten = 110;
  1228. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  1229. * power level:
  1230. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  1231. */
  1232. if (priv->_agn.offchan_tx_chan->band == IEEE80211_BAND_5GHZ)
  1233. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1234. else
  1235. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  1236. }
  1237. break;
  1238. }
  1239. if (scan->channel_count == 0) {
  1240. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  1241. return -EIO;
  1242. }
  1243. cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
  1244. scan->channel_count * sizeof(struct iwl_scan_channel);
  1245. cmd.data[0] = scan;
  1246. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  1247. scan->len = cpu_to_le16(cmd.len[0]);
  1248. /* set scan bit here for PAN params */
  1249. set_bit(STATUS_SCAN_HW, &priv->status);
  1250. if (priv->cfg->ops->hcmd->set_pan_params) {
  1251. ret = priv->cfg->ops->hcmd->set_pan_params(priv);
  1252. if (ret)
  1253. return ret;
  1254. }
  1255. ret = iwl_send_cmd_sync(priv, &cmd);
  1256. if (ret) {
  1257. clear_bit(STATUS_SCAN_HW, &priv->status);
  1258. if (priv->cfg->ops->hcmd->set_pan_params)
  1259. priv->cfg->ops->hcmd->set_pan_params(priv);
  1260. }
  1261. return ret;
  1262. }
  1263. int iwlagn_manage_ibss_station(struct iwl_priv *priv,
  1264. struct ieee80211_vif *vif, bool add)
  1265. {
  1266. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1267. if (add)
  1268. return iwlagn_add_bssid_station(priv, vif_priv->ctx,
  1269. vif->bss_conf.bssid,
  1270. &vif_priv->ibss_bssid_sta_id);
  1271. return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
  1272. vif->bss_conf.bssid);
  1273. }
  1274. void iwl_free_tfds_in_queue(struct iwl_priv *priv,
  1275. int sta_id, int tid, int freed)
  1276. {
  1277. lockdep_assert_held(&priv->sta_lock);
  1278. if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  1279. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  1280. else {
  1281. IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
  1282. priv->stations[sta_id].tid[tid].tfds_in_queue,
  1283. freed);
  1284. priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
  1285. }
  1286. }
  1287. #define IWL_FLUSH_WAIT_MS 2000
  1288. int iwlagn_wait_tx_queue_empty(struct iwl_priv *priv)
  1289. {
  1290. struct iwl_tx_queue *txq;
  1291. struct iwl_queue *q;
  1292. int cnt;
  1293. unsigned long now = jiffies;
  1294. int ret = 0;
  1295. /* waiting for all the tx frames complete might take a while */
  1296. for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
  1297. if (cnt == priv->cmd_queue)
  1298. continue;
  1299. txq = &priv->txq[cnt];
  1300. q = &txq->q;
  1301. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1302. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1303. msleep(1);
  1304. if (q->read_ptr != q->write_ptr) {
  1305. IWL_ERR(priv, "fail to flush all tx fifo queues\n");
  1306. ret = -ETIMEDOUT;
  1307. break;
  1308. }
  1309. }
  1310. return ret;
  1311. }
  1312. #define IWL_TX_QUEUE_MSK 0xfffff
  1313. /**
  1314. * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
  1315. *
  1316. * pre-requirements:
  1317. * 1. acquire mutex before calling
  1318. * 2. make sure rf is on and not in exit state
  1319. */
  1320. int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  1321. {
  1322. struct iwl_txfifo_flush_cmd flush_cmd;
  1323. struct iwl_host_cmd cmd = {
  1324. .id = REPLY_TXFIFO_FLUSH,
  1325. .len = { sizeof(struct iwl_txfifo_flush_cmd), },
  1326. .flags = CMD_SYNC,
  1327. .data = { &flush_cmd, },
  1328. };
  1329. might_sleep();
  1330. memset(&flush_cmd, 0, sizeof(flush_cmd));
  1331. flush_cmd.fifo_control = IWL_TX_FIFO_VO_MSK | IWL_TX_FIFO_VI_MSK |
  1332. IWL_TX_FIFO_BE_MSK | IWL_TX_FIFO_BK_MSK;
  1333. if (priv->cfg->sku & IWL_SKU_N)
  1334. flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
  1335. IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
  1336. flush_cmd.fifo_control);
  1337. flush_cmd.flush_control = cpu_to_le16(flush_control);
  1338. return iwl_send_cmd(priv, &cmd);
  1339. }
  1340. void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  1341. {
  1342. mutex_lock(&priv->mutex);
  1343. ieee80211_stop_queues(priv->hw);
  1344. if (priv->cfg->ops->lib->txfifo_flush(priv, IWL_DROP_ALL)) {
  1345. IWL_ERR(priv, "flush request fail\n");
  1346. goto done;
  1347. }
  1348. IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
  1349. iwlagn_wait_tx_queue_empty(priv);
  1350. done:
  1351. ieee80211_wake_queues(priv->hw);
  1352. mutex_unlock(&priv->mutex);
  1353. }
  1354. /*
  1355. * BT coex
  1356. */
  1357. /*
  1358. * Macros to access the lookup table.
  1359. *
  1360. * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
  1361. * wifi_prio, wifi_txrx and wifi_sh_ant_req.
  1362. *
  1363. * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
  1364. *
  1365. * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
  1366. * one after another in 32-bit registers, and "registers" 0 through 7 contain
  1367. * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
  1368. *
  1369. * These macros encode that format.
  1370. */
  1371. #define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
  1372. wifi_txrx, wifi_sh_ant_req) \
  1373. (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
  1374. (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
  1375. #define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
  1376. lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
  1377. #define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1378. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1379. (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
  1380. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1381. wifi_sh_ant_req))))
  1382. #define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1383. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1384. LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
  1385. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1386. wifi_sh_ant_req))
  1387. #define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
  1388. wifi_req, wifi_prio, wifi_txrx, \
  1389. wifi_sh_ant_req) \
  1390. LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
  1391. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1392. wifi_sh_ant_req))
  1393. #define LUT_WLAN_KILL_OP(lut, op, val) \
  1394. lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
  1395. #define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1396. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1397. (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1398. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
  1399. #define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1400. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1401. LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1402. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1403. #define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1404. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1405. LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1406. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1407. #define LUT_ANT_SWITCH_OP(lut, op, val) \
  1408. lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
  1409. #define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1410. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1411. (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1412. wifi_req, wifi_prio, wifi_txrx, \
  1413. wifi_sh_ant_req))))
  1414. #define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1415. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1416. LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1417. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1418. #define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1419. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1420. LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1421. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1422. static const __le32 iwlagn_def_3w_lookup[12] = {
  1423. cpu_to_le32(0xaaaaaaaa),
  1424. cpu_to_le32(0xaaaaaaaa),
  1425. cpu_to_le32(0xaeaaaaaa),
  1426. cpu_to_le32(0xaaaaaaaa),
  1427. cpu_to_le32(0xcc00ff28),
  1428. cpu_to_le32(0x0000aaaa),
  1429. cpu_to_le32(0xcc00aaaa),
  1430. cpu_to_le32(0x0000aaaa),
  1431. cpu_to_le32(0xc0004000),
  1432. cpu_to_le32(0x00004000),
  1433. cpu_to_le32(0xf0005000),
  1434. cpu_to_le32(0xf0005000),
  1435. };
  1436. static const __le32 iwlagn_concurrent_lookup[12] = {
  1437. cpu_to_le32(0xaaaaaaaa),
  1438. cpu_to_le32(0xaaaaaaaa),
  1439. cpu_to_le32(0xaaaaaaaa),
  1440. cpu_to_le32(0xaaaaaaaa),
  1441. cpu_to_le32(0xaaaaaaaa),
  1442. cpu_to_le32(0xaaaaaaaa),
  1443. cpu_to_le32(0xaaaaaaaa),
  1444. cpu_to_le32(0xaaaaaaaa),
  1445. cpu_to_le32(0x00000000),
  1446. cpu_to_le32(0x00000000),
  1447. cpu_to_le32(0x00000000),
  1448. cpu_to_le32(0x00000000),
  1449. };
  1450. void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
  1451. {
  1452. struct iwl_basic_bt_cmd basic = {
  1453. .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
  1454. .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
  1455. .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
  1456. .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
  1457. };
  1458. struct iwl6000_bt_cmd bt_cmd_6000;
  1459. struct iwl2000_bt_cmd bt_cmd_2000;
  1460. int ret;
  1461. BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
  1462. sizeof(basic.bt3_lookup_table));
  1463. if (priv->cfg->bt_params) {
  1464. if (priv->cfg->bt_params->bt_session_2) {
  1465. bt_cmd_2000.prio_boost = cpu_to_le32(
  1466. priv->cfg->bt_params->bt_prio_boost);
  1467. bt_cmd_2000.tx_prio_boost = 0;
  1468. bt_cmd_2000.rx_prio_boost = 0;
  1469. } else {
  1470. bt_cmd_6000.prio_boost =
  1471. priv->cfg->bt_params->bt_prio_boost;
  1472. bt_cmd_6000.tx_prio_boost = 0;
  1473. bt_cmd_6000.rx_prio_boost = 0;
  1474. }
  1475. } else {
  1476. IWL_ERR(priv, "failed to construct BT Coex Config\n");
  1477. return;
  1478. }
  1479. basic.kill_ack_mask = priv->kill_ack_mask;
  1480. basic.kill_cts_mask = priv->kill_cts_mask;
  1481. basic.valid = priv->bt_valid;
  1482. /*
  1483. * Configure BT coex mode to "no coexistence" when the
  1484. * user disabled BT coexistence, we have no interface
  1485. * (might be in monitor mode), or the interface is in
  1486. * IBSS mode (no proper uCode support for coex then).
  1487. */
  1488. if (!bt_coex_active || priv->iw_mode == NL80211_IFTYPE_ADHOC) {
  1489. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
  1490. } else {
  1491. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
  1492. IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
  1493. if (priv->cfg->bt_params &&
  1494. priv->cfg->bt_params->bt_sco_disable)
  1495. basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
  1496. if (priv->bt_ch_announce)
  1497. basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
  1498. IWL_DEBUG_INFO(priv, "BT coex flag: 0X%x\n", basic.flags);
  1499. }
  1500. priv->bt_enable_flag = basic.flags;
  1501. if (priv->bt_full_concurrent)
  1502. memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
  1503. sizeof(iwlagn_concurrent_lookup));
  1504. else
  1505. memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
  1506. sizeof(iwlagn_def_3w_lookup));
  1507. IWL_DEBUG_INFO(priv, "BT coex %s in %s mode\n",
  1508. basic.flags ? "active" : "disabled",
  1509. priv->bt_full_concurrent ?
  1510. "full concurrency" : "3-wire");
  1511. if (priv->cfg->bt_params->bt_session_2) {
  1512. memcpy(&bt_cmd_2000.basic, &basic,
  1513. sizeof(basic));
  1514. ret = iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  1515. sizeof(bt_cmd_2000), &bt_cmd_2000);
  1516. } else {
  1517. memcpy(&bt_cmd_6000.basic, &basic,
  1518. sizeof(basic));
  1519. ret = iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  1520. sizeof(bt_cmd_6000), &bt_cmd_6000);
  1521. }
  1522. if (ret)
  1523. IWL_ERR(priv, "failed to send BT Coex Config\n");
  1524. }
  1525. static void iwlagn_bt_traffic_change_work(struct work_struct *work)
  1526. {
  1527. struct iwl_priv *priv =
  1528. container_of(work, struct iwl_priv, bt_traffic_change_work);
  1529. struct iwl_rxon_context *ctx;
  1530. int smps_request = -1;
  1531. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  1532. /* bt coex disabled */
  1533. return;
  1534. }
  1535. /*
  1536. * Note: bt_traffic_load can be overridden by scan complete and
  1537. * coex profile notifications. Ignore that since only bad consequence
  1538. * can be not matching debug print with actual state.
  1539. */
  1540. IWL_DEBUG_INFO(priv, "BT traffic load changes: %d\n",
  1541. priv->bt_traffic_load);
  1542. switch (priv->bt_traffic_load) {
  1543. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1544. if (priv->bt_status)
  1545. smps_request = IEEE80211_SMPS_DYNAMIC;
  1546. else
  1547. smps_request = IEEE80211_SMPS_AUTOMATIC;
  1548. break;
  1549. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1550. smps_request = IEEE80211_SMPS_DYNAMIC;
  1551. break;
  1552. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1553. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1554. smps_request = IEEE80211_SMPS_STATIC;
  1555. break;
  1556. default:
  1557. IWL_ERR(priv, "Invalid BT traffic load: %d\n",
  1558. priv->bt_traffic_load);
  1559. break;
  1560. }
  1561. mutex_lock(&priv->mutex);
  1562. /*
  1563. * We can not send command to firmware while scanning. When the scan
  1564. * complete we will schedule this work again. We do check with mutex
  1565. * locked to prevent new scan request to arrive. We do not check
  1566. * STATUS_SCANNING to avoid race when queue_work two times from
  1567. * different notifications, but quit and not perform any work at all.
  1568. */
  1569. if (test_bit(STATUS_SCAN_HW, &priv->status))
  1570. goto out;
  1571. if (priv->cfg->ops->lib->update_chain_flags)
  1572. priv->cfg->ops->lib->update_chain_flags(priv);
  1573. if (smps_request != -1) {
  1574. for_each_context(priv, ctx) {
  1575. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
  1576. ieee80211_request_smps(ctx->vif, smps_request);
  1577. }
  1578. }
  1579. out:
  1580. mutex_unlock(&priv->mutex);
  1581. }
  1582. static void iwlagn_print_uartmsg(struct iwl_priv *priv,
  1583. struct iwl_bt_uart_msg *uart_msg)
  1584. {
  1585. IWL_DEBUG_NOTIF(priv, "Message Type = 0x%X, SSN = 0x%X, "
  1586. "Update Req = 0x%X",
  1587. (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
  1588. BT_UART_MSG_FRAME1MSGTYPE_POS,
  1589. (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
  1590. BT_UART_MSG_FRAME1SSN_POS,
  1591. (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
  1592. BT_UART_MSG_FRAME1UPDATEREQ_POS);
  1593. IWL_DEBUG_NOTIF(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
  1594. "Chl_SeqN = 0x%X, In band = 0x%X",
  1595. (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
  1596. BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
  1597. (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
  1598. BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
  1599. (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
  1600. BT_UART_MSG_FRAME2CHLSEQN_POS,
  1601. (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
  1602. BT_UART_MSG_FRAME2INBAND_POS);
  1603. IWL_DEBUG_NOTIF(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
  1604. "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
  1605. (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
  1606. BT_UART_MSG_FRAME3SCOESCO_POS,
  1607. (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
  1608. BT_UART_MSG_FRAME3SNIFF_POS,
  1609. (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
  1610. BT_UART_MSG_FRAME3A2DP_POS,
  1611. (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
  1612. BT_UART_MSG_FRAME3ACL_POS,
  1613. (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
  1614. BT_UART_MSG_FRAME3MASTER_POS,
  1615. (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
  1616. BT_UART_MSG_FRAME3OBEX_POS);
  1617. IWL_DEBUG_NOTIF(priv, "Idle duration = 0x%X",
  1618. (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
  1619. BT_UART_MSG_FRAME4IDLEDURATION_POS);
  1620. IWL_DEBUG_NOTIF(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
  1621. "eSCO Retransmissions = 0x%X",
  1622. (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
  1623. BT_UART_MSG_FRAME5TXACTIVITY_POS,
  1624. (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
  1625. BT_UART_MSG_FRAME5RXACTIVITY_POS,
  1626. (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
  1627. BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
  1628. IWL_DEBUG_NOTIF(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
  1629. (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
  1630. BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
  1631. (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
  1632. BT_UART_MSG_FRAME6DISCOVERABLE_POS);
  1633. IWL_DEBUG_NOTIF(priv, "Sniff Activity = 0x%X, Page = "
  1634. "0x%X, Inquiry = 0x%X, Connectable = 0x%X",
  1635. (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
  1636. BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
  1637. (BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
  1638. BT_UART_MSG_FRAME7PAGE_POS,
  1639. (BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
  1640. BT_UART_MSG_FRAME7INQUIRY_POS,
  1641. (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
  1642. BT_UART_MSG_FRAME7CONNECTABLE_POS);
  1643. }
  1644. static void iwlagn_set_kill_msk(struct iwl_priv *priv,
  1645. struct iwl_bt_uart_msg *uart_msg)
  1646. {
  1647. u8 kill_msk;
  1648. static const __le32 bt_kill_ack_msg[2] = {
  1649. IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
  1650. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  1651. static const __le32 bt_kill_cts_msg[2] = {
  1652. IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
  1653. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  1654. kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
  1655. ? 1 : 0;
  1656. if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
  1657. priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
  1658. priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
  1659. priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
  1660. priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
  1661. priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
  1662. /* schedule to send runtime bt_config */
  1663. queue_work(priv->workqueue, &priv->bt_runtime_config);
  1664. }
  1665. }
  1666. void iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
  1667. struct iwl_rx_mem_buffer *rxb)
  1668. {
  1669. unsigned long flags;
  1670. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1671. struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
  1672. struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
  1673. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  1674. /* bt coex disabled */
  1675. return;
  1676. }
  1677. IWL_DEBUG_NOTIF(priv, "BT Coex notification:\n");
  1678. IWL_DEBUG_NOTIF(priv, " status: %d\n", coex->bt_status);
  1679. IWL_DEBUG_NOTIF(priv, " traffic load: %d\n", coex->bt_traffic_load);
  1680. IWL_DEBUG_NOTIF(priv, " CI compliance: %d\n",
  1681. coex->bt_ci_compliance);
  1682. iwlagn_print_uartmsg(priv, uart_msg);
  1683. priv->last_bt_traffic_load = priv->bt_traffic_load;
  1684. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  1685. if (priv->bt_status != coex->bt_status ||
  1686. priv->last_bt_traffic_load != coex->bt_traffic_load) {
  1687. if (coex->bt_status) {
  1688. /* BT on */
  1689. if (!priv->bt_ch_announce)
  1690. priv->bt_traffic_load =
  1691. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1692. else
  1693. priv->bt_traffic_load =
  1694. coex->bt_traffic_load;
  1695. } else {
  1696. /* BT off */
  1697. priv->bt_traffic_load =
  1698. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  1699. }
  1700. priv->bt_status = coex->bt_status;
  1701. queue_work(priv->workqueue,
  1702. &priv->bt_traffic_change_work);
  1703. }
  1704. }
  1705. iwlagn_set_kill_msk(priv, uart_msg);
  1706. /* FIXME: based on notification, adjust the prio_boost */
  1707. spin_lock_irqsave(&priv->lock, flags);
  1708. priv->bt_ci_compliance = coex->bt_ci_compliance;
  1709. spin_unlock_irqrestore(&priv->lock, flags);
  1710. }
  1711. void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
  1712. {
  1713. iwlagn_rx_handler_setup(priv);
  1714. priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
  1715. iwlagn_bt_coex_profile_notif;
  1716. }
  1717. void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
  1718. {
  1719. iwlagn_setup_deferred_work(priv);
  1720. INIT_WORK(&priv->bt_traffic_change_work,
  1721. iwlagn_bt_traffic_change_work);
  1722. }
  1723. void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
  1724. {
  1725. cancel_work_sync(&priv->bt_traffic_change_work);
  1726. }
  1727. static bool is_single_rx_stream(struct iwl_priv *priv)
  1728. {
  1729. return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
  1730. priv->current_ht_config.single_chain_sufficient;
  1731. }
  1732. #define IWL_NUM_RX_CHAINS_MULTIPLE 3
  1733. #define IWL_NUM_RX_CHAINS_SINGLE 2
  1734. #define IWL_NUM_IDLE_CHAINS_DUAL 2
  1735. #define IWL_NUM_IDLE_CHAINS_SINGLE 1
  1736. /*
  1737. * Determine how many receiver/antenna chains to use.
  1738. *
  1739. * More provides better reception via diversity. Fewer saves power
  1740. * at the expense of throughput, but only when not in powersave to
  1741. * start with.
  1742. *
  1743. * MIMO (dual stream) requires at least 2, but works better with 3.
  1744. * This does not determine *which* chains to use, just how many.
  1745. */
  1746. static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
  1747. {
  1748. if (priv->cfg->bt_params &&
  1749. priv->cfg->bt_params->advanced_bt_coexist &&
  1750. (priv->bt_full_concurrent ||
  1751. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  1752. /*
  1753. * only use chain 'A' in bt high traffic load or
  1754. * full concurrency mode
  1755. */
  1756. return IWL_NUM_RX_CHAINS_SINGLE;
  1757. }
  1758. /* # of Rx chains to use when expecting MIMO. */
  1759. if (is_single_rx_stream(priv))
  1760. return IWL_NUM_RX_CHAINS_SINGLE;
  1761. else
  1762. return IWL_NUM_RX_CHAINS_MULTIPLE;
  1763. }
  1764. /*
  1765. * When we are in power saving mode, unless device support spatial
  1766. * multiplexing power save, use the active count for rx chain count.
  1767. */
  1768. static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
  1769. {
  1770. /* # Rx chains when idling, depending on SMPS mode */
  1771. switch (priv->current_ht_config.smps) {
  1772. case IEEE80211_SMPS_STATIC:
  1773. case IEEE80211_SMPS_DYNAMIC:
  1774. return IWL_NUM_IDLE_CHAINS_SINGLE;
  1775. case IEEE80211_SMPS_AUTOMATIC:
  1776. case IEEE80211_SMPS_OFF:
  1777. return active_cnt;
  1778. default:
  1779. WARN(1, "invalid SMPS mode %d",
  1780. priv->current_ht_config.smps);
  1781. return active_cnt;
  1782. }
  1783. }
  1784. /* up to 4 chains */
  1785. static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
  1786. {
  1787. u8 res;
  1788. res = (chain_bitmap & BIT(0)) >> 0;
  1789. res += (chain_bitmap & BIT(1)) >> 1;
  1790. res += (chain_bitmap & BIT(2)) >> 2;
  1791. res += (chain_bitmap & BIT(3)) >> 3;
  1792. return res;
  1793. }
  1794. /**
  1795. * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  1796. *
  1797. * Selects how many and which Rx receivers/antennas/chains to use.
  1798. * This should not be used for scan command ... it puts data in wrong place.
  1799. */
  1800. void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  1801. {
  1802. bool is_single = is_single_rx_stream(priv);
  1803. bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
  1804. u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
  1805. u32 active_chains;
  1806. u16 rx_chain;
  1807. /* Tell uCode which antennas are actually connected.
  1808. * Before first association, we assume all antennas are connected.
  1809. * Just after first association, iwl_chain_noise_calibration()
  1810. * checks which antennas actually *are* connected. */
  1811. if (priv->chain_noise_data.active_chains)
  1812. active_chains = priv->chain_noise_data.active_chains;
  1813. else
  1814. active_chains = priv->hw_params.valid_rx_ant;
  1815. if (priv->cfg->bt_params &&
  1816. priv->cfg->bt_params->advanced_bt_coexist &&
  1817. (priv->bt_full_concurrent ||
  1818. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  1819. /*
  1820. * only use chain 'A' in bt high traffic load or
  1821. * full concurrency mode
  1822. */
  1823. active_chains = first_antenna(active_chains);
  1824. }
  1825. rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
  1826. /* How many receivers should we use? */
  1827. active_rx_cnt = iwl_get_active_rx_chain_count(priv);
  1828. idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
  1829. /* correct rx chain count according hw settings
  1830. * and chain noise calibration
  1831. */
  1832. valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
  1833. if (valid_rx_cnt < active_rx_cnt)
  1834. active_rx_cnt = valid_rx_cnt;
  1835. if (valid_rx_cnt < idle_rx_cnt)
  1836. idle_rx_cnt = valid_rx_cnt;
  1837. rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
  1838. rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
  1839. ctx->staging.rx_chain = cpu_to_le16(rx_chain);
  1840. if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
  1841. ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  1842. else
  1843. ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  1844. IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
  1845. ctx->staging.rx_chain,
  1846. active_rx_cnt, idle_rx_cnt);
  1847. WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
  1848. active_rx_cnt < idle_rx_cnt);
  1849. }
  1850. u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
  1851. {
  1852. int i;
  1853. u8 ind = ant;
  1854. if (priv->band == IEEE80211_BAND_2GHZ &&
  1855. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
  1856. return 0;
  1857. for (i = 0; i < RATE_ANT_NUM - 1; i++) {
  1858. ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
  1859. if (valid & BIT(ind))
  1860. return ind;
  1861. }
  1862. return ant;
  1863. }
  1864. static const char *get_csr_string(int cmd)
  1865. {
  1866. switch (cmd) {
  1867. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1868. IWL_CMD(CSR_INT_COALESCING);
  1869. IWL_CMD(CSR_INT);
  1870. IWL_CMD(CSR_INT_MASK);
  1871. IWL_CMD(CSR_FH_INT_STATUS);
  1872. IWL_CMD(CSR_GPIO_IN);
  1873. IWL_CMD(CSR_RESET);
  1874. IWL_CMD(CSR_GP_CNTRL);
  1875. IWL_CMD(CSR_HW_REV);
  1876. IWL_CMD(CSR_EEPROM_REG);
  1877. IWL_CMD(CSR_EEPROM_GP);
  1878. IWL_CMD(CSR_OTP_GP_REG);
  1879. IWL_CMD(CSR_GIO_REG);
  1880. IWL_CMD(CSR_GP_UCODE_REG);
  1881. IWL_CMD(CSR_GP_DRIVER_REG);
  1882. IWL_CMD(CSR_UCODE_DRV_GP1);
  1883. IWL_CMD(CSR_UCODE_DRV_GP2);
  1884. IWL_CMD(CSR_LED_REG);
  1885. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1886. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1887. IWL_CMD(CSR_ANA_PLL_CFG);
  1888. IWL_CMD(CSR_HW_REV_WA_REG);
  1889. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1890. default:
  1891. return "UNKNOWN";
  1892. }
  1893. }
  1894. void iwl_dump_csr(struct iwl_priv *priv)
  1895. {
  1896. int i;
  1897. static const u32 csr_tbl[] = {
  1898. CSR_HW_IF_CONFIG_REG,
  1899. CSR_INT_COALESCING,
  1900. CSR_INT,
  1901. CSR_INT_MASK,
  1902. CSR_FH_INT_STATUS,
  1903. CSR_GPIO_IN,
  1904. CSR_RESET,
  1905. CSR_GP_CNTRL,
  1906. CSR_HW_REV,
  1907. CSR_EEPROM_REG,
  1908. CSR_EEPROM_GP,
  1909. CSR_OTP_GP_REG,
  1910. CSR_GIO_REG,
  1911. CSR_GP_UCODE_REG,
  1912. CSR_GP_DRIVER_REG,
  1913. CSR_UCODE_DRV_GP1,
  1914. CSR_UCODE_DRV_GP2,
  1915. CSR_LED_REG,
  1916. CSR_DRAM_INT_TBL_REG,
  1917. CSR_GIO_CHICKEN_BITS,
  1918. CSR_ANA_PLL_CFG,
  1919. CSR_HW_REV_WA_REG,
  1920. CSR_DBG_HPET_MEM_REG
  1921. };
  1922. IWL_ERR(priv, "CSR values:\n");
  1923. IWL_ERR(priv, "(2nd byte of CSR_INT_COALESCING is "
  1924. "CSR_INT_PERIODIC_REG)\n");
  1925. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1926. IWL_ERR(priv, " %25s: 0X%08x\n",
  1927. get_csr_string(csr_tbl[i]),
  1928. iwl_read32(priv, csr_tbl[i]));
  1929. }
  1930. }
  1931. static const char *get_fh_string(int cmd)
  1932. {
  1933. switch (cmd) {
  1934. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1935. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1936. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1937. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1938. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1939. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1940. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1941. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1942. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1943. default:
  1944. return "UNKNOWN";
  1945. }
  1946. }
  1947. int iwl_dump_fh(struct iwl_priv *priv, char **buf, bool display)
  1948. {
  1949. int i;
  1950. #ifdef CONFIG_IWLWIFI_DEBUG
  1951. int pos = 0;
  1952. size_t bufsz = 0;
  1953. #endif
  1954. static const u32 fh_tbl[] = {
  1955. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1956. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1957. FH_RSCSR_CHNL0_WPTR,
  1958. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1959. FH_MEM_RSSR_SHARED_CTRL_REG,
  1960. FH_MEM_RSSR_RX_STATUS_REG,
  1961. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1962. FH_TSSR_TX_STATUS_REG,
  1963. FH_TSSR_TX_ERROR_REG
  1964. };
  1965. #ifdef CONFIG_IWLWIFI_DEBUG
  1966. if (display) {
  1967. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1968. *buf = kmalloc(bufsz, GFP_KERNEL);
  1969. if (!*buf)
  1970. return -ENOMEM;
  1971. pos += scnprintf(*buf + pos, bufsz - pos,
  1972. "FH register values:\n");
  1973. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1974. pos += scnprintf(*buf + pos, bufsz - pos,
  1975. " %34s: 0X%08x\n",
  1976. get_fh_string(fh_tbl[i]),
  1977. iwl_read_direct32(priv, fh_tbl[i]));
  1978. }
  1979. return pos;
  1980. }
  1981. #endif
  1982. IWL_ERR(priv, "FH register values:\n");
  1983. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1984. IWL_ERR(priv, " %34s: 0X%08x\n",
  1985. get_fh_string(fh_tbl[i]),
  1986. iwl_read_direct32(priv, fh_tbl[i]));
  1987. }
  1988. return 0;
  1989. }
  1990. /* notification wait support */
  1991. void iwlagn_init_notification_wait(struct iwl_priv *priv,
  1992. struct iwl_notification_wait *wait_entry,
  1993. u8 cmd,
  1994. void (*fn)(struct iwl_priv *priv,
  1995. struct iwl_rx_packet *pkt,
  1996. void *data),
  1997. void *fn_data)
  1998. {
  1999. wait_entry->fn = fn;
  2000. wait_entry->fn_data = fn_data;
  2001. wait_entry->cmd = cmd;
  2002. wait_entry->triggered = false;
  2003. wait_entry->aborted = false;
  2004. spin_lock_bh(&priv->_agn.notif_wait_lock);
  2005. list_add(&wait_entry->list, &priv->_agn.notif_waits);
  2006. spin_unlock_bh(&priv->_agn.notif_wait_lock);
  2007. }
  2008. int iwlagn_wait_notification(struct iwl_priv *priv,
  2009. struct iwl_notification_wait *wait_entry,
  2010. unsigned long timeout)
  2011. {
  2012. int ret;
  2013. ret = wait_event_timeout(priv->_agn.notif_waitq,
  2014. wait_entry->triggered || wait_entry->aborted,
  2015. timeout);
  2016. spin_lock_bh(&priv->_agn.notif_wait_lock);
  2017. list_del(&wait_entry->list);
  2018. spin_unlock_bh(&priv->_agn.notif_wait_lock);
  2019. if (wait_entry->aborted)
  2020. return -EIO;
  2021. /* return value is always >= 0 */
  2022. if (ret <= 0)
  2023. return -ETIMEDOUT;
  2024. return 0;
  2025. }
  2026. void iwlagn_remove_notification(struct iwl_priv *priv,
  2027. struct iwl_notification_wait *wait_entry)
  2028. {
  2029. spin_lock_bh(&priv->_agn.notif_wait_lock);
  2030. list_del(&wait_entry->list);
  2031. spin_unlock_bh(&priv->_agn.notif_wait_lock);
  2032. }
  2033. int iwlagn_start_device(struct iwl_priv *priv)
  2034. {
  2035. int ret;
  2036. if (iwl_prepare_card_hw(priv)) {
  2037. IWL_WARN(priv, "Exit HW not ready\n");
  2038. return -EIO;
  2039. }
  2040. /* If platform's RF_KILL switch is NOT set to KILL */
  2041. if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2042. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2043. else
  2044. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2045. if (iwl_is_rfkill(priv)) {
  2046. wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
  2047. iwl_enable_interrupts(priv);
  2048. return -ERFKILL;
  2049. }
  2050. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2051. ret = iwlagn_hw_nic_init(priv);
  2052. if (ret) {
  2053. IWL_ERR(priv, "Unable to init nic\n");
  2054. return ret;
  2055. }
  2056. /* make sure rfkill handshake bits are cleared */
  2057. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2058. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  2059. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2060. /* clear (again), then enable host interrupts */
  2061. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2062. iwl_enable_interrupts(priv);
  2063. /* really make sure rfkill handshake bits are cleared */
  2064. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2065. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2066. return 0;
  2067. }
  2068. void iwlagn_stop_device(struct iwl_priv *priv)
  2069. {
  2070. unsigned long flags;
  2071. /* stop and reset the on-board processor */
  2072. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  2073. /* tell the device to stop sending interrupts */
  2074. spin_lock_irqsave(&priv->lock, flags);
  2075. iwl_disable_interrupts(priv);
  2076. spin_unlock_irqrestore(&priv->lock, flags);
  2077. iwl_synchronize_irq(priv);
  2078. /* device going down, Stop using ICT table */
  2079. iwl_disable_ict(priv);
  2080. /*
  2081. * If a HW restart happens during firmware loading,
  2082. * then the firmware loading might call this function
  2083. * and later it might be called again due to the
  2084. * restart. So don't process again if the device is
  2085. * already dead.
  2086. */
  2087. if (test_bit(STATUS_DEVICE_ENABLED, &priv->status)) {
  2088. iwlagn_txq_ctx_stop(priv);
  2089. iwlagn_rxq_stop(priv);
  2090. /* Power-down device's busmaster DMA clocks */
  2091. iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  2092. udelay(5);
  2093. }
  2094. /* Make sure (redundant) we've released our request to stay awake */
  2095. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2096. /* Stop the device, and put it in low power state */
  2097. iwl_apm_stop(priv);
  2098. }