iwl-agn-lib.c 67 KB

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