iwl-agn-lib.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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. #include "iwl-shared.h"
  43. static void iwlagn_count_tx_err_status(struct iwl_priv *priv, u16 status)
  44. {
  45. status &= TX_STATUS_MSK;
  46. switch (status) {
  47. case TX_STATUS_POSTPONE_DELAY:
  48. priv->reply_tx_stats.pp_delay++;
  49. break;
  50. case TX_STATUS_POSTPONE_FEW_BYTES:
  51. priv->reply_tx_stats.pp_few_bytes++;
  52. break;
  53. case TX_STATUS_POSTPONE_BT_PRIO:
  54. priv->reply_tx_stats.pp_bt_prio++;
  55. break;
  56. case TX_STATUS_POSTPONE_QUIET_PERIOD:
  57. priv->reply_tx_stats.pp_quiet_period++;
  58. break;
  59. case TX_STATUS_POSTPONE_CALC_TTAK:
  60. priv->reply_tx_stats.pp_calc_ttak++;
  61. break;
  62. case TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY:
  63. priv->reply_tx_stats.int_crossed_retry++;
  64. break;
  65. case TX_STATUS_FAIL_SHORT_LIMIT:
  66. priv->reply_tx_stats.short_limit++;
  67. break;
  68. case TX_STATUS_FAIL_LONG_LIMIT:
  69. priv->reply_tx_stats.long_limit++;
  70. break;
  71. case TX_STATUS_FAIL_FIFO_UNDERRUN:
  72. priv->reply_tx_stats.fifo_underrun++;
  73. break;
  74. case TX_STATUS_FAIL_DRAIN_FLOW:
  75. priv->reply_tx_stats.drain_flow++;
  76. break;
  77. case TX_STATUS_FAIL_RFKILL_FLUSH:
  78. priv->reply_tx_stats.rfkill_flush++;
  79. break;
  80. case TX_STATUS_FAIL_LIFE_EXPIRE:
  81. priv->reply_tx_stats.life_expire++;
  82. break;
  83. case TX_STATUS_FAIL_DEST_PS:
  84. priv->reply_tx_stats.dest_ps++;
  85. break;
  86. case TX_STATUS_FAIL_HOST_ABORTED:
  87. priv->reply_tx_stats.host_abort++;
  88. break;
  89. case TX_STATUS_FAIL_BT_RETRY:
  90. priv->reply_tx_stats.bt_retry++;
  91. break;
  92. case TX_STATUS_FAIL_STA_INVALID:
  93. priv->reply_tx_stats.sta_invalid++;
  94. break;
  95. case TX_STATUS_FAIL_FRAG_DROPPED:
  96. priv->reply_tx_stats.frag_drop++;
  97. break;
  98. case TX_STATUS_FAIL_TID_DISABLE:
  99. priv->reply_tx_stats.tid_disable++;
  100. break;
  101. case TX_STATUS_FAIL_FIFO_FLUSHED:
  102. priv->reply_tx_stats.fifo_flush++;
  103. break;
  104. case TX_STATUS_FAIL_INSUFFICIENT_CF_POLL:
  105. priv->reply_tx_stats.insuff_cf_poll++;
  106. break;
  107. case TX_STATUS_FAIL_PASSIVE_NO_RX:
  108. priv->reply_tx_stats.fail_hw_drop++;
  109. break;
  110. case TX_STATUS_FAIL_NO_BEACON_ON_RADAR:
  111. priv->reply_tx_stats.sta_color_mismatch++;
  112. break;
  113. default:
  114. priv->reply_tx_stats.unknown++;
  115. break;
  116. }
  117. }
  118. void iwlagn_count_agg_tx_err_status(struct iwl_priv *priv, u16 status)
  119. {
  120. status &= AGG_TX_STATUS_MSK;
  121. switch (status) {
  122. case AGG_TX_STATE_UNDERRUN_MSK:
  123. priv->reply_agg_tx_stats.underrun++;
  124. break;
  125. case AGG_TX_STATE_BT_PRIO_MSK:
  126. priv->reply_agg_tx_stats.bt_prio++;
  127. break;
  128. case AGG_TX_STATE_FEW_BYTES_MSK:
  129. priv->reply_agg_tx_stats.few_bytes++;
  130. break;
  131. case AGG_TX_STATE_ABORT_MSK:
  132. priv->reply_agg_tx_stats.abort++;
  133. break;
  134. case AGG_TX_STATE_LAST_SENT_TTL_MSK:
  135. priv->reply_agg_tx_stats.last_sent_ttl++;
  136. break;
  137. case AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK:
  138. priv->reply_agg_tx_stats.last_sent_try++;
  139. break;
  140. case AGG_TX_STATE_LAST_SENT_BT_KILL_MSK:
  141. priv->reply_agg_tx_stats.last_sent_bt_kill++;
  142. break;
  143. case AGG_TX_STATE_SCD_QUERY_MSK:
  144. priv->reply_agg_tx_stats.scd_query++;
  145. break;
  146. case AGG_TX_STATE_TEST_BAD_CRC32_MSK:
  147. priv->reply_agg_tx_stats.bad_crc32++;
  148. break;
  149. case AGG_TX_STATE_RESPONSE_MSK:
  150. priv->reply_agg_tx_stats.response++;
  151. break;
  152. case AGG_TX_STATE_DUMP_TX_MSK:
  153. priv->reply_agg_tx_stats.dump_tx++;
  154. break;
  155. case AGG_TX_STATE_DELAY_TX_MSK:
  156. priv->reply_agg_tx_stats.delay_tx++;
  157. break;
  158. default:
  159. priv->reply_agg_tx_stats.unknown++;
  160. break;
  161. }
  162. }
  163. void iwlagn_set_tx_status(struct iwl_priv *priv,
  164. struct ieee80211_tx_info *info,
  165. struct iwlagn_tx_resp *tx_resp,
  166. bool is_agg)
  167. {
  168. u16 status = le16_to_cpu(tx_resp->status.status);
  169. info->status.rates[0].count = tx_resp->failure_frame + 1;
  170. if (is_agg)
  171. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  172. info->flags |= iwl_tx_status_to_mac80211(status);
  173. iwlagn_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
  174. info);
  175. if (!iwl_is_tx_success(status))
  176. iwlagn_count_tx_err_status(priv, status);
  177. }
  178. #ifdef CONFIG_IWLWIFI_DEBUG
  179. #define AGG_TX_STATE_FAIL(x) case AGG_TX_STATE_ ## x: return #x
  180. const char *iwl_get_agg_tx_fail_reason(u16 status)
  181. {
  182. status &= AGG_TX_STATUS_MSK;
  183. switch (status) {
  184. case AGG_TX_STATE_TRANSMITTED:
  185. return "SUCCESS";
  186. AGG_TX_STATE_FAIL(UNDERRUN_MSK);
  187. AGG_TX_STATE_FAIL(BT_PRIO_MSK);
  188. AGG_TX_STATE_FAIL(FEW_BYTES_MSK);
  189. AGG_TX_STATE_FAIL(ABORT_MSK);
  190. AGG_TX_STATE_FAIL(LAST_SENT_TTL_MSK);
  191. AGG_TX_STATE_FAIL(LAST_SENT_TRY_CNT_MSK);
  192. AGG_TX_STATE_FAIL(LAST_SENT_BT_KILL_MSK);
  193. AGG_TX_STATE_FAIL(SCD_QUERY_MSK);
  194. AGG_TX_STATE_FAIL(TEST_BAD_CRC32_MSK);
  195. AGG_TX_STATE_FAIL(RESPONSE_MSK);
  196. AGG_TX_STATE_FAIL(DUMP_TX_MSK);
  197. AGG_TX_STATE_FAIL(DELAY_TX_MSK);
  198. }
  199. return "UNKNOWN";
  200. }
  201. #endif /* CONFIG_IWLWIFI_DEBUG */
  202. void iwl_check_abort_status(struct iwl_priv *priv,
  203. u8 frame_count, u32 status)
  204. {
  205. if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
  206. IWL_ERR(priv, "Tx flush command to flush out all frames\n");
  207. if (!test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  208. queue_work(priv->shrd->workqueue, &priv->tx_flush);
  209. }
  210. }
  211. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  212. {
  213. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  214. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  215. }
  216. int iwlagn_send_tx_power(struct iwl_priv *priv)
  217. {
  218. struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
  219. u8 tx_ant_cfg_cmd;
  220. if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->shrd->status),
  221. "TX Power requested while scanning!\n"))
  222. return -EAGAIN;
  223. /* half dBm need to multiply */
  224. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  225. if (priv->tx_power_lmt_in_half_dbm &&
  226. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  227. /*
  228. * For the newer devices which using enhanced/extend tx power
  229. * table in EEPROM, the format is in half dBm. driver need to
  230. * convert to dBm format before report to mac80211.
  231. * By doing so, there is a possibility of 1/2 dBm resolution
  232. * lost. driver will perform "round-up" operation before
  233. * reporting, but it will cause 1/2 dBm tx power over the
  234. * regulatory limit. Perform the checking here, if the
  235. * "tx_power_user_lmt" is higher than EEPROM value (in
  236. * half-dBm format), lower the tx power based on EEPROM
  237. */
  238. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  239. }
  240. tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
  241. tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
  242. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  243. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  244. else
  245. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  246. return iwl_trans_send_cmd_pdu(trans(priv), tx_ant_cfg_cmd, CMD_SYNC,
  247. sizeof(tx_power_cmd), &tx_power_cmd);
  248. }
  249. void iwlagn_temperature(struct iwl_priv *priv)
  250. {
  251. /* store temperature from correct statistics (in Celsius) */
  252. priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
  253. iwl_tt_handler(priv);
  254. }
  255. u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
  256. {
  257. struct iwl_eeprom_calib_hdr {
  258. u8 version;
  259. u8 pa_type;
  260. u16 voltage;
  261. } *hdr;
  262. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  263. EEPROM_CALIB_ALL);
  264. return hdr->version;
  265. }
  266. /*
  267. * EEPROM
  268. */
  269. static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
  270. {
  271. u16 offset = 0;
  272. if ((address & INDIRECT_ADDRESS) == 0)
  273. return address;
  274. switch (address & INDIRECT_TYPE_MSK) {
  275. case INDIRECT_HOST:
  276. offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
  277. break;
  278. case INDIRECT_GENERAL:
  279. offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
  280. break;
  281. case INDIRECT_REGULATORY:
  282. offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
  283. break;
  284. case INDIRECT_TXP_LIMIT:
  285. offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT);
  286. break;
  287. case INDIRECT_TXP_LIMIT_SIZE:
  288. offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT_SIZE);
  289. break;
  290. case INDIRECT_CALIBRATION:
  291. offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
  292. break;
  293. case INDIRECT_PROCESS_ADJST:
  294. offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
  295. break;
  296. case INDIRECT_OTHERS:
  297. offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
  298. break;
  299. default:
  300. IWL_ERR(priv, "illegal indirect type: 0x%X\n",
  301. address & INDIRECT_TYPE_MSK);
  302. break;
  303. }
  304. /* translate the offset from words to byte */
  305. return (address & ADDRESS_MSK) + (offset << 1);
  306. }
  307. const u8 *iwl_eeprom_query_addr(const struct iwl_priv *priv, size_t offset)
  308. {
  309. u32 address = eeprom_indirect_address(priv, offset);
  310. BUG_ON(address >= priv->cfg->base_params->eeprom_size);
  311. return &priv->eeprom[address];
  312. }
  313. struct iwl_mod_params iwlagn_mod_params = {
  314. .amsdu_size_8K = 1,
  315. .restart_fw = 1,
  316. .plcp_check = true,
  317. .bt_coex_active = true,
  318. .no_sleep_autoadjust = true,
  319. .power_level = IWL_POWER_INDEX_1,
  320. .bt_ch_announce = true,
  321. .wanted_ucode_alternative = 1,
  322. .auto_agg = true,
  323. /* the rest are 0 by default */
  324. };
  325. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  326. {
  327. int idx = 0;
  328. int band_offset = 0;
  329. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  330. if (rate_n_flags & RATE_MCS_HT_MSK) {
  331. idx = (rate_n_flags & 0xff);
  332. return idx;
  333. /* Legacy rate format, search for match in table */
  334. } else {
  335. if (band == IEEE80211_BAND_5GHZ)
  336. band_offset = IWL_FIRST_OFDM_RATE;
  337. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  338. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  339. return idx - band_offset;
  340. }
  341. return -1;
  342. }
  343. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  344. struct ieee80211_vif *vif,
  345. enum ieee80211_band band,
  346. struct iwl_scan_channel *scan_ch)
  347. {
  348. const struct ieee80211_supported_band *sband;
  349. u16 passive_dwell = 0;
  350. u16 active_dwell = 0;
  351. int added = 0;
  352. u16 channel = 0;
  353. sband = iwl_get_hw_mode(priv, band);
  354. if (!sband) {
  355. IWL_ERR(priv, "invalid band\n");
  356. return added;
  357. }
  358. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  359. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  360. if (passive_dwell <= active_dwell)
  361. passive_dwell = active_dwell + 1;
  362. channel = iwl_get_single_channel_number(priv, band);
  363. if (channel) {
  364. scan_ch->channel = cpu_to_le16(channel);
  365. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  366. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  367. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  368. /* Set txpower levels to defaults */
  369. scan_ch->dsp_atten = 110;
  370. if (band == IEEE80211_BAND_5GHZ)
  371. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  372. else
  373. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  374. added++;
  375. } else
  376. IWL_ERR(priv, "no valid channel found\n");
  377. return added;
  378. }
  379. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  380. struct ieee80211_vif *vif,
  381. enum ieee80211_band band,
  382. u8 is_active, u8 n_probes,
  383. struct iwl_scan_channel *scan_ch)
  384. {
  385. struct ieee80211_channel *chan;
  386. const struct ieee80211_supported_band *sband;
  387. const struct iwl_channel_info *ch_info;
  388. u16 passive_dwell = 0;
  389. u16 active_dwell = 0;
  390. int added, i;
  391. u16 channel;
  392. sband = iwl_get_hw_mode(priv, band);
  393. if (!sband)
  394. return 0;
  395. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  396. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  397. if (passive_dwell <= active_dwell)
  398. passive_dwell = active_dwell + 1;
  399. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  400. chan = priv->scan_request->channels[i];
  401. if (chan->band != band)
  402. continue;
  403. channel = chan->hw_value;
  404. scan_ch->channel = cpu_to_le16(channel);
  405. ch_info = iwl_get_channel_info(priv, band, channel);
  406. if (!is_channel_valid(ch_info)) {
  407. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  408. channel);
  409. continue;
  410. }
  411. if (!is_active || is_channel_passive(ch_info) ||
  412. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  413. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  414. else
  415. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  416. if (n_probes)
  417. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  418. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  419. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  420. /* Set txpower levels to defaults */
  421. scan_ch->dsp_atten = 110;
  422. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  423. * power level:
  424. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  425. */
  426. if (band == IEEE80211_BAND_5GHZ)
  427. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  428. else
  429. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  430. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  431. channel, le32_to_cpu(scan_ch->type),
  432. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  433. "ACTIVE" : "PASSIVE",
  434. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  435. active_dwell : passive_dwell);
  436. scan_ch++;
  437. added++;
  438. }
  439. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  440. return added;
  441. }
  442. int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  443. {
  444. struct iwl_host_cmd cmd = {
  445. .id = REPLY_SCAN_CMD,
  446. .len = { sizeof(struct iwl_scan_cmd), },
  447. .flags = CMD_SYNC,
  448. };
  449. struct iwl_scan_cmd *scan;
  450. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  451. u32 rate_flags = 0;
  452. u16 cmd_len;
  453. u16 rx_chain = 0;
  454. enum ieee80211_band band;
  455. u8 n_probes = 0;
  456. u8 rx_ant = hw_params(priv).valid_rx_ant;
  457. u8 rate;
  458. bool is_active = false;
  459. int chan_mod;
  460. u8 active_chains;
  461. u8 scan_tx_antennas = hw_params(priv).valid_tx_ant;
  462. int ret;
  463. lockdep_assert_held(&priv->shrd->mutex);
  464. if (vif)
  465. ctx = iwl_rxon_ctx_from_vif(vif);
  466. if (!priv->scan_cmd) {
  467. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  468. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  469. if (!priv->scan_cmd) {
  470. IWL_DEBUG_SCAN(priv,
  471. "fail to allocate memory for scan\n");
  472. return -ENOMEM;
  473. }
  474. }
  475. scan = priv->scan_cmd;
  476. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  477. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  478. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  479. if (priv->scan_type != IWL_SCAN_ROC &&
  480. iwl_is_any_associated(priv)) {
  481. u16 interval = 0;
  482. u32 extra;
  483. u32 suspend_time = 100;
  484. u32 scan_suspend_time = 100;
  485. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  486. switch (priv->scan_type) {
  487. case IWL_SCAN_ROC:
  488. WARN_ON(1);
  489. break;
  490. case IWL_SCAN_RADIO_RESET:
  491. interval = 0;
  492. break;
  493. case IWL_SCAN_NORMAL:
  494. interval = vif->bss_conf.beacon_int;
  495. break;
  496. }
  497. scan->suspend_time = 0;
  498. scan->max_out_time = cpu_to_le32(200 * 1024);
  499. if (!interval)
  500. interval = suspend_time;
  501. extra = (suspend_time / interval) << 22;
  502. scan_suspend_time = (extra |
  503. ((suspend_time % interval) * 1024));
  504. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  505. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  506. scan_suspend_time, interval);
  507. } else if (priv->scan_type == IWL_SCAN_ROC) {
  508. scan->suspend_time = 0;
  509. scan->max_out_time = 0;
  510. scan->quiet_time = 0;
  511. scan->quiet_plcp_th = 0;
  512. }
  513. switch (priv->scan_type) {
  514. case IWL_SCAN_RADIO_RESET:
  515. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  516. break;
  517. case IWL_SCAN_NORMAL:
  518. if (priv->scan_request->n_ssids) {
  519. int i, p = 0;
  520. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  521. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  522. /* always does wildcard anyway */
  523. if (!priv->scan_request->ssids[i].ssid_len)
  524. continue;
  525. scan->direct_scan[p].id = WLAN_EID_SSID;
  526. scan->direct_scan[p].len =
  527. priv->scan_request->ssids[i].ssid_len;
  528. memcpy(scan->direct_scan[p].ssid,
  529. priv->scan_request->ssids[i].ssid,
  530. priv->scan_request->ssids[i].ssid_len);
  531. n_probes++;
  532. p++;
  533. }
  534. is_active = true;
  535. } else
  536. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  537. break;
  538. case IWL_SCAN_ROC:
  539. IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
  540. break;
  541. }
  542. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  543. scan->tx_cmd.sta_id = ctx->bcast_sta_id;
  544. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  545. switch (priv->scan_band) {
  546. case IEEE80211_BAND_2GHZ:
  547. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  548. chan_mod = le32_to_cpu(
  549. priv->contexts[IWL_RXON_CTX_BSS].active.flags &
  550. RXON_FLG_CHANNEL_MODE_MSK)
  551. >> RXON_FLG_CHANNEL_MODE_POS;
  552. if (chan_mod == CHANNEL_MODE_PURE_40) {
  553. rate = IWL_RATE_6M_PLCP;
  554. } else {
  555. rate = IWL_RATE_1M_PLCP;
  556. rate_flags = RATE_MCS_CCK_MSK;
  557. }
  558. /*
  559. * Internal scans are passive, so we can indiscriminately set
  560. * the BT ignore flag on 2.4 GHz since it applies to TX only.
  561. */
  562. if (priv->cfg->bt_params &&
  563. priv->cfg->bt_params->advanced_bt_coexist)
  564. scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
  565. break;
  566. case IEEE80211_BAND_5GHZ:
  567. rate = IWL_RATE_6M_PLCP;
  568. break;
  569. default:
  570. IWL_WARN(priv, "Invalid scan band\n");
  571. return -EIO;
  572. }
  573. /*
  574. * If active scanning is requested but a certain channel is
  575. * marked passive, we can do active scanning if we detect
  576. * transmissions.
  577. *
  578. * There is an issue with some firmware versions that triggers
  579. * a sysassert on a "good CRC threshold" of zero (== disabled),
  580. * on a radar channel even though this means that we should NOT
  581. * send probes.
  582. *
  583. * The "good CRC threshold" is the number of frames that we
  584. * need to receive during our dwell time on a channel before
  585. * sending out probes -- setting this to a huge value will
  586. * mean we never reach it, but at the same time work around
  587. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  588. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  589. *
  590. * This was fixed in later versions along with some other
  591. * scan changes, and the threshold behaves as a flag in those
  592. * versions.
  593. */
  594. if (priv->new_scan_threshold_behaviour)
  595. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  596. IWL_GOOD_CRC_TH_DISABLED;
  597. else
  598. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  599. IWL_GOOD_CRC_TH_NEVER;
  600. band = priv->scan_band;
  601. if (priv->cfg->scan_rx_antennas[band])
  602. rx_ant = priv->cfg->scan_rx_antennas[band];
  603. if (band == IEEE80211_BAND_2GHZ &&
  604. priv->cfg->bt_params &&
  605. priv->cfg->bt_params->advanced_bt_coexist) {
  606. /* transmit 2.4 GHz probes only on first antenna */
  607. scan_tx_antennas = first_antenna(scan_tx_antennas);
  608. }
  609. priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
  610. scan_tx_antennas);
  611. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  612. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  613. /* In power save mode use one chain, otherwise use all chains */
  614. if (test_bit(STATUS_POWER_PMI, &priv->shrd->status)) {
  615. /* rx_ant has been set to all valid chains previously */
  616. active_chains = rx_ant &
  617. ((u8)(priv->chain_noise_data.active_chains));
  618. if (!active_chains)
  619. active_chains = rx_ant;
  620. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  621. priv->chain_noise_data.active_chains);
  622. rx_ant = first_antenna(active_chains);
  623. }
  624. if (priv->cfg->bt_params &&
  625. priv->cfg->bt_params->advanced_bt_coexist &&
  626. priv->bt_full_concurrent) {
  627. /* operated as 1x1 in full concurrency mode */
  628. rx_ant = first_antenna(rx_ant);
  629. }
  630. /* MIMO is not used here, but value is required */
  631. rx_chain |=
  632. hw_params(priv).valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  633. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  634. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  635. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  636. scan->rx_chain = cpu_to_le16(rx_chain);
  637. switch (priv->scan_type) {
  638. case IWL_SCAN_NORMAL:
  639. cmd_len = iwl_fill_probe_req(priv,
  640. (struct ieee80211_mgmt *)scan->data,
  641. vif->addr,
  642. priv->scan_request->ie,
  643. priv->scan_request->ie_len,
  644. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  645. break;
  646. case IWL_SCAN_RADIO_RESET:
  647. case IWL_SCAN_ROC:
  648. /* use bcast addr, will not be transmitted but must be valid */
  649. cmd_len = iwl_fill_probe_req(priv,
  650. (struct ieee80211_mgmt *)scan->data,
  651. iwl_bcast_addr, NULL, 0,
  652. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  653. break;
  654. default:
  655. BUG();
  656. }
  657. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  658. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  659. RXON_FILTER_BCON_AWARE_MSK);
  660. switch (priv->scan_type) {
  661. case IWL_SCAN_RADIO_RESET:
  662. scan->channel_count =
  663. iwl_get_single_channel_for_scan(priv, vif, band,
  664. (void *)&scan->data[cmd_len]);
  665. break;
  666. case IWL_SCAN_NORMAL:
  667. scan->channel_count =
  668. iwl_get_channels_for_scan(priv, vif, band,
  669. is_active, n_probes,
  670. (void *)&scan->data[cmd_len]);
  671. break;
  672. case IWL_SCAN_ROC: {
  673. struct iwl_scan_channel *scan_ch;
  674. scan->channel_count = 1;
  675. scan_ch = (void *)&scan->data[cmd_len];
  676. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  677. scan_ch->channel =
  678. cpu_to_le16(priv->hw_roc_channel->hw_value);
  679. scan_ch->active_dwell =
  680. scan_ch->passive_dwell =
  681. cpu_to_le16(priv->hw_roc_duration);
  682. /* Set txpower levels to defaults */
  683. scan_ch->dsp_atten = 110;
  684. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  685. * power level:
  686. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  687. */
  688. if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
  689. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  690. else
  691. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  692. }
  693. break;
  694. }
  695. if (scan->channel_count == 0) {
  696. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  697. return -EIO;
  698. }
  699. cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
  700. scan->channel_count * sizeof(struct iwl_scan_channel);
  701. cmd.data[0] = scan;
  702. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  703. scan->len = cpu_to_le16(cmd.len[0]);
  704. /* set scan bit here for PAN params */
  705. set_bit(STATUS_SCAN_HW, &priv->shrd->status);
  706. ret = iwlagn_set_pan_params(priv);
  707. if (ret)
  708. return ret;
  709. ret = iwl_trans_send_cmd(trans(priv), &cmd);
  710. if (ret) {
  711. clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
  712. iwlagn_set_pan_params(priv);
  713. }
  714. return ret;
  715. }
  716. int iwlagn_manage_ibss_station(struct iwl_priv *priv,
  717. struct ieee80211_vif *vif, bool add)
  718. {
  719. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  720. if (add)
  721. return iwlagn_add_bssid_station(priv, vif_priv->ctx,
  722. vif->bss_conf.bssid,
  723. &vif_priv->ibss_bssid_sta_id);
  724. return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
  725. vif->bss_conf.bssid);
  726. }
  727. void iwl_free_tfds_in_queue(struct iwl_priv *priv,
  728. int sta_id, int tid, int freed)
  729. {
  730. lockdep_assert_held(&priv->shrd->sta_lock);
  731. if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  732. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  733. else {
  734. IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
  735. priv->stations[sta_id].tid[tid].tfds_in_queue,
  736. freed);
  737. priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
  738. }
  739. }
  740. #define IWL_FLUSH_WAIT_MS 2000
  741. int iwlagn_wait_tx_queue_empty(struct iwl_priv *priv)
  742. {
  743. struct iwl_tx_queue *txq;
  744. struct iwl_queue *q;
  745. int cnt;
  746. unsigned long now = jiffies;
  747. int ret = 0;
  748. /* waiting for all the tx frames complete might take a while */
  749. for (cnt = 0; cnt < hw_params(priv).max_txq_num; cnt++) {
  750. if (cnt == priv->shrd->cmd_queue)
  751. continue;
  752. txq = &priv->txq[cnt];
  753. q = &txq->q;
  754. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  755. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  756. msleep(1);
  757. if (q->read_ptr != q->write_ptr) {
  758. IWL_ERR(priv, "fail to flush all tx fifo queues\n");
  759. ret = -ETIMEDOUT;
  760. break;
  761. }
  762. }
  763. return ret;
  764. }
  765. #define IWL_TX_QUEUE_MSK 0xfffff
  766. /**
  767. * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
  768. *
  769. * pre-requirements:
  770. * 1. acquire mutex before calling
  771. * 2. make sure rf is on and not in exit state
  772. */
  773. int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  774. {
  775. struct iwl_txfifo_flush_cmd flush_cmd;
  776. struct iwl_host_cmd cmd = {
  777. .id = REPLY_TXFIFO_FLUSH,
  778. .len = { sizeof(struct iwl_txfifo_flush_cmd), },
  779. .flags = CMD_SYNC,
  780. .data = { &flush_cmd, },
  781. };
  782. might_sleep();
  783. memset(&flush_cmd, 0, sizeof(flush_cmd));
  784. if (flush_control & BIT(IWL_RXON_CTX_BSS))
  785. flush_cmd.fifo_control = IWL_SCD_VO_MSK | IWL_SCD_VI_MSK |
  786. IWL_SCD_BE_MSK | IWL_SCD_BK_MSK |
  787. IWL_SCD_MGMT_MSK;
  788. if ((flush_control & BIT(IWL_RXON_CTX_PAN)) &&
  789. (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS)))
  790. flush_cmd.fifo_control |= IWL_PAN_SCD_VO_MSK |
  791. IWL_PAN_SCD_VI_MSK | IWL_PAN_SCD_BE_MSK |
  792. IWL_PAN_SCD_BK_MSK | IWL_PAN_SCD_MGMT_MSK |
  793. IWL_PAN_SCD_MULTICAST_MSK;
  794. if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
  795. flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
  796. IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
  797. flush_cmd.fifo_control);
  798. flush_cmd.flush_control = cpu_to_le16(flush_control);
  799. return iwl_trans_send_cmd(trans(priv), &cmd);
  800. }
  801. void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  802. {
  803. mutex_lock(&priv->shrd->mutex);
  804. ieee80211_stop_queues(priv->hw);
  805. if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
  806. IWL_ERR(priv, "flush request fail\n");
  807. goto done;
  808. }
  809. IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
  810. iwlagn_wait_tx_queue_empty(priv);
  811. done:
  812. ieee80211_wake_queues(priv->hw);
  813. mutex_unlock(&priv->shrd->mutex);
  814. }
  815. /*
  816. * BT coex
  817. */
  818. /*
  819. * Macros to access the lookup table.
  820. *
  821. * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
  822. * wifi_prio, wifi_txrx and wifi_sh_ant_req.
  823. *
  824. * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
  825. *
  826. * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
  827. * one after another in 32-bit registers, and "registers" 0 through 7 contain
  828. * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
  829. *
  830. * These macros encode that format.
  831. */
  832. #define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
  833. wifi_txrx, wifi_sh_ant_req) \
  834. (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
  835. (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
  836. #define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
  837. lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
  838. #define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  839. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  840. (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
  841. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  842. wifi_sh_ant_req))))
  843. #define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  844. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  845. LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
  846. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  847. wifi_sh_ant_req))
  848. #define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
  849. wifi_req, wifi_prio, wifi_txrx, \
  850. wifi_sh_ant_req) \
  851. LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
  852. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  853. wifi_sh_ant_req))
  854. #define LUT_WLAN_KILL_OP(lut, op, val) \
  855. lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
  856. #define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  857. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  858. (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  859. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
  860. #define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  861. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  862. LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  863. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  864. #define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  865. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  866. LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  867. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  868. #define LUT_ANT_SWITCH_OP(lut, op, val) \
  869. lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
  870. #define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  871. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  872. (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  873. wifi_req, wifi_prio, wifi_txrx, \
  874. wifi_sh_ant_req))))
  875. #define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  876. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  877. LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  878. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  879. #define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  880. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  881. LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  882. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  883. static const __le32 iwlagn_def_3w_lookup[12] = {
  884. cpu_to_le32(0xaaaaaaaa),
  885. cpu_to_le32(0xaaaaaaaa),
  886. cpu_to_le32(0xaeaaaaaa),
  887. cpu_to_le32(0xaaaaaaaa),
  888. cpu_to_le32(0xcc00ff28),
  889. cpu_to_le32(0x0000aaaa),
  890. cpu_to_le32(0xcc00aaaa),
  891. cpu_to_le32(0x0000aaaa),
  892. cpu_to_le32(0xc0004000),
  893. cpu_to_le32(0x00004000),
  894. cpu_to_le32(0xf0005000),
  895. cpu_to_le32(0xf0005000),
  896. };
  897. static const __le32 iwlagn_concurrent_lookup[12] = {
  898. cpu_to_le32(0xaaaaaaaa),
  899. cpu_to_le32(0xaaaaaaaa),
  900. cpu_to_le32(0xaaaaaaaa),
  901. cpu_to_le32(0xaaaaaaaa),
  902. cpu_to_le32(0xaaaaaaaa),
  903. cpu_to_le32(0xaaaaaaaa),
  904. cpu_to_le32(0xaaaaaaaa),
  905. cpu_to_le32(0xaaaaaaaa),
  906. cpu_to_le32(0x00000000),
  907. cpu_to_le32(0x00000000),
  908. cpu_to_le32(0x00000000),
  909. cpu_to_le32(0x00000000),
  910. };
  911. void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
  912. {
  913. struct iwl_basic_bt_cmd basic = {
  914. .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
  915. .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
  916. .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
  917. .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
  918. };
  919. struct iwl6000_bt_cmd bt_cmd_6000;
  920. struct iwl2000_bt_cmd bt_cmd_2000;
  921. int ret;
  922. BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
  923. sizeof(basic.bt3_lookup_table));
  924. if (priv->cfg->bt_params) {
  925. if (priv->cfg->bt_params->bt_session_2) {
  926. bt_cmd_2000.prio_boost = cpu_to_le32(
  927. priv->cfg->bt_params->bt_prio_boost);
  928. bt_cmd_2000.tx_prio_boost = 0;
  929. bt_cmd_2000.rx_prio_boost = 0;
  930. } else {
  931. bt_cmd_6000.prio_boost =
  932. priv->cfg->bt_params->bt_prio_boost;
  933. bt_cmd_6000.tx_prio_boost = 0;
  934. bt_cmd_6000.rx_prio_boost = 0;
  935. }
  936. } else {
  937. IWL_ERR(priv, "failed to construct BT Coex Config\n");
  938. return;
  939. }
  940. basic.kill_ack_mask = priv->kill_ack_mask;
  941. basic.kill_cts_mask = priv->kill_cts_mask;
  942. basic.valid = priv->bt_valid;
  943. /*
  944. * Configure BT coex mode to "no coexistence" when the
  945. * user disabled BT coexistence, we have no interface
  946. * (might be in monitor mode), or the interface is in
  947. * IBSS mode (no proper uCode support for coex then).
  948. */
  949. if (!iwlagn_mod_params.bt_coex_active ||
  950. priv->iw_mode == NL80211_IFTYPE_ADHOC) {
  951. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
  952. } else {
  953. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
  954. IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
  955. if (!priv->bt_enable_pspoll)
  956. basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
  957. else
  958. basic.flags &= ~IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
  959. if (priv->bt_ch_announce)
  960. basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
  961. IWL_DEBUG_COEX(priv, "BT coex flag: 0X%x\n", basic.flags);
  962. }
  963. priv->bt_enable_flag = basic.flags;
  964. if (priv->bt_full_concurrent)
  965. memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
  966. sizeof(iwlagn_concurrent_lookup));
  967. else
  968. memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
  969. sizeof(iwlagn_def_3w_lookup));
  970. IWL_DEBUG_COEX(priv, "BT coex %s in %s mode\n",
  971. basic.flags ? "active" : "disabled",
  972. priv->bt_full_concurrent ?
  973. "full concurrency" : "3-wire");
  974. if (priv->cfg->bt_params->bt_session_2) {
  975. memcpy(&bt_cmd_2000.basic, &basic,
  976. sizeof(basic));
  977. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
  978. CMD_SYNC, sizeof(bt_cmd_2000), &bt_cmd_2000);
  979. } else {
  980. memcpy(&bt_cmd_6000.basic, &basic,
  981. sizeof(basic));
  982. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
  983. CMD_SYNC, sizeof(bt_cmd_6000), &bt_cmd_6000);
  984. }
  985. if (ret)
  986. IWL_ERR(priv, "failed to send BT Coex Config\n");
  987. }
  988. void iwlagn_bt_adjust_rssi_monitor(struct iwl_priv *priv, bool rssi_ena)
  989. {
  990. struct iwl_rxon_context *ctx, *found_ctx = NULL;
  991. bool found_ap = false;
  992. lockdep_assert_held(&priv->shrd->mutex);
  993. /* Check whether AP or GO mode is active. */
  994. if (rssi_ena) {
  995. for_each_context(priv, ctx) {
  996. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_AP &&
  997. iwl_is_associated_ctx(ctx)) {
  998. found_ap = true;
  999. break;
  1000. }
  1001. }
  1002. }
  1003. /*
  1004. * If disable was received or If GO/AP mode, disable RSSI
  1005. * measurements.
  1006. */
  1007. if (!rssi_ena || found_ap) {
  1008. if (priv->cur_rssi_ctx) {
  1009. ctx = priv->cur_rssi_ctx;
  1010. ieee80211_disable_rssi_reports(ctx->vif);
  1011. priv->cur_rssi_ctx = NULL;
  1012. }
  1013. return;
  1014. }
  1015. /*
  1016. * If rssi measurements need to be enabled, consider all cases now.
  1017. * Figure out how many contexts are active.
  1018. */
  1019. for_each_context(priv, ctx) {
  1020. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
  1021. iwl_is_associated_ctx(ctx)) {
  1022. found_ctx = ctx;
  1023. break;
  1024. }
  1025. }
  1026. /*
  1027. * rssi monitor already enabled for the correct interface...nothing
  1028. * to do.
  1029. */
  1030. if (found_ctx == priv->cur_rssi_ctx)
  1031. return;
  1032. /*
  1033. * Figure out if rssi monitor is currently enabled, and needs
  1034. * to be changed. If rssi monitor is already enabled, disable
  1035. * it first else just enable rssi measurements on the
  1036. * interface found above.
  1037. */
  1038. if (priv->cur_rssi_ctx) {
  1039. ctx = priv->cur_rssi_ctx;
  1040. if (ctx->vif)
  1041. ieee80211_disable_rssi_reports(ctx->vif);
  1042. }
  1043. priv->cur_rssi_ctx = found_ctx;
  1044. if (!found_ctx)
  1045. return;
  1046. ieee80211_enable_rssi_reports(found_ctx->vif,
  1047. IWLAGN_BT_PSP_MIN_RSSI_THRESHOLD,
  1048. IWLAGN_BT_PSP_MAX_RSSI_THRESHOLD);
  1049. }
  1050. static bool iwlagn_bt_traffic_is_sco(struct iwl_bt_uart_msg *uart_msg)
  1051. {
  1052. return BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3 >>
  1053. BT_UART_MSG_FRAME3SCOESCO_POS;
  1054. }
  1055. static void iwlagn_bt_traffic_change_work(struct work_struct *work)
  1056. {
  1057. struct iwl_priv *priv =
  1058. container_of(work, struct iwl_priv, bt_traffic_change_work);
  1059. struct iwl_rxon_context *ctx;
  1060. int smps_request = -1;
  1061. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  1062. /* bt coex disabled */
  1063. return;
  1064. }
  1065. /*
  1066. * Note: bt_traffic_load can be overridden by scan complete and
  1067. * coex profile notifications. Ignore that since only bad consequence
  1068. * can be not matching debug print with actual state.
  1069. */
  1070. IWL_DEBUG_COEX(priv, "BT traffic load changes: %d\n",
  1071. priv->bt_traffic_load);
  1072. switch (priv->bt_traffic_load) {
  1073. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1074. if (priv->bt_status)
  1075. smps_request = IEEE80211_SMPS_DYNAMIC;
  1076. else
  1077. smps_request = IEEE80211_SMPS_AUTOMATIC;
  1078. break;
  1079. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1080. smps_request = IEEE80211_SMPS_DYNAMIC;
  1081. break;
  1082. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1083. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1084. smps_request = IEEE80211_SMPS_STATIC;
  1085. break;
  1086. default:
  1087. IWL_ERR(priv, "Invalid BT traffic load: %d\n",
  1088. priv->bt_traffic_load);
  1089. break;
  1090. }
  1091. mutex_lock(&priv->shrd->mutex);
  1092. /*
  1093. * We can not send command to firmware while scanning. When the scan
  1094. * complete we will schedule this work again. We do check with mutex
  1095. * locked to prevent new scan request to arrive. We do not check
  1096. * STATUS_SCANNING to avoid race when queue_work two times from
  1097. * different notifications, but quit and not perform any work at all.
  1098. */
  1099. if (test_bit(STATUS_SCAN_HW, &priv->shrd->status))
  1100. goto out;
  1101. iwl_update_chain_flags(priv);
  1102. if (smps_request != -1) {
  1103. priv->current_ht_config.smps = smps_request;
  1104. for_each_context(priv, ctx) {
  1105. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
  1106. ieee80211_request_smps(ctx->vif, smps_request);
  1107. }
  1108. }
  1109. /*
  1110. * Dynamic PS poll related functionality. Adjust RSSI measurements if
  1111. * necessary.
  1112. */
  1113. iwlagn_bt_coex_rssi_monitor(priv);
  1114. out:
  1115. mutex_unlock(&priv->shrd->mutex);
  1116. }
  1117. /*
  1118. * If BT sco traffic, and RSSI monitor is enabled, move measurements to the
  1119. * correct interface or disable it if this is the last interface to be
  1120. * removed.
  1121. */
  1122. void iwlagn_bt_coex_rssi_monitor(struct iwl_priv *priv)
  1123. {
  1124. if (priv->bt_is_sco &&
  1125. priv->bt_traffic_load == IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS)
  1126. iwlagn_bt_adjust_rssi_monitor(priv, true);
  1127. else
  1128. iwlagn_bt_adjust_rssi_monitor(priv, false);
  1129. }
  1130. static void iwlagn_print_uartmsg(struct iwl_priv *priv,
  1131. struct iwl_bt_uart_msg *uart_msg)
  1132. {
  1133. IWL_DEBUG_COEX(priv, "Message Type = 0x%X, SSN = 0x%X, "
  1134. "Update Req = 0x%X",
  1135. (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
  1136. BT_UART_MSG_FRAME1MSGTYPE_POS,
  1137. (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
  1138. BT_UART_MSG_FRAME1SSN_POS,
  1139. (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
  1140. BT_UART_MSG_FRAME1UPDATEREQ_POS);
  1141. IWL_DEBUG_COEX(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
  1142. "Chl_SeqN = 0x%X, In band = 0x%X",
  1143. (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
  1144. BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
  1145. (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
  1146. BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
  1147. (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
  1148. BT_UART_MSG_FRAME2CHLSEQN_POS,
  1149. (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
  1150. BT_UART_MSG_FRAME2INBAND_POS);
  1151. IWL_DEBUG_COEX(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
  1152. "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
  1153. (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
  1154. BT_UART_MSG_FRAME3SCOESCO_POS,
  1155. (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
  1156. BT_UART_MSG_FRAME3SNIFF_POS,
  1157. (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
  1158. BT_UART_MSG_FRAME3A2DP_POS,
  1159. (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
  1160. BT_UART_MSG_FRAME3ACL_POS,
  1161. (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
  1162. BT_UART_MSG_FRAME3MASTER_POS,
  1163. (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
  1164. BT_UART_MSG_FRAME3OBEX_POS);
  1165. IWL_DEBUG_COEX(priv, "Idle duration = 0x%X",
  1166. (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
  1167. BT_UART_MSG_FRAME4IDLEDURATION_POS);
  1168. IWL_DEBUG_COEX(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
  1169. "eSCO Retransmissions = 0x%X",
  1170. (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
  1171. BT_UART_MSG_FRAME5TXACTIVITY_POS,
  1172. (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
  1173. BT_UART_MSG_FRAME5RXACTIVITY_POS,
  1174. (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
  1175. BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
  1176. IWL_DEBUG_COEX(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
  1177. (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
  1178. BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
  1179. (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
  1180. BT_UART_MSG_FRAME6DISCOVERABLE_POS);
  1181. IWL_DEBUG_COEX(priv, "Sniff Activity = 0x%X, Page = "
  1182. "0x%X, Inquiry = 0x%X, Connectable = 0x%X",
  1183. (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
  1184. BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
  1185. (BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
  1186. BT_UART_MSG_FRAME7PAGE_POS,
  1187. (BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
  1188. BT_UART_MSG_FRAME7INQUIRY_POS,
  1189. (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
  1190. BT_UART_MSG_FRAME7CONNECTABLE_POS);
  1191. }
  1192. static void iwlagn_set_kill_msk(struct iwl_priv *priv,
  1193. struct iwl_bt_uart_msg *uart_msg)
  1194. {
  1195. u8 kill_msk;
  1196. static const __le32 bt_kill_ack_msg[2] = {
  1197. IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
  1198. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  1199. static const __le32 bt_kill_cts_msg[2] = {
  1200. IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
  1201. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  1202. kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
  1203. ? 1 : 0;
  1204. if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
  1205. priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
  1206. priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
  1207. priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
  1208. priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
  1209. priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
  1210. /* schedule to send runtime bt_config */
  1211. queue_work(priv->shrd->workqueue, &priv->bt_runtime_config);
  1212. }
  1213. }
  1214. void iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
  1215. struct iwl_rx_mem_buffer *rxb)
  1216. {
  1217. unsigned long flags;
  1218. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1219. struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
  1220. struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
  1221. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  1222. /* bt coex disabled */
  1223. return;
  1224. }
  1225. IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
  1226. IWL_DEBUG_COEX(priv, " status: %d\n", coex->bt_status);
  1227. IWL_DEBUG_COEX(priv, " traffic load: %d\n", coex->bt_traffic_load);
  1228. IWL_DEBUG_COEX(priv, " CI compliance: %d\n",
  1229. coex->bt_ci_compliance);
  1230. iwlagn_print_uartmsg(priv, uart_msg);
  1231. priv->last_bt_traffic_load = priv->bt_traffic_load;
  1232. priv->bt_is_sco = iwlagn_bt_traffic_is_sco(uart_msg);
  1233. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  1234. if (priv->bt_status != coex->bt_status ||
  1235. priv->last_bt_traffic_load != coex->bt_traffic_load) {
  1236. if (coex->bt_status) {
  1237. /* BT on */
  1238. if (!priv->bt_ch_announce)
  1239. priv->bt_traffic_load =
  1240. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1241. else
  1242. priv->bt_traffic_load =
  1243. coex->bt_traffic_load;
  1244. } else {
  1245. /* BT off */
  1246. priv->bt_traffic_load =
  1247. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  1248. }
  1249. priv->bt_status = coex->bt_status;
  1250. queue_work(priv->shrd->workqueue,
  1251. &priv->bt_traffic_change_work);
  1252. }
  1253. }
  1254. iwlagn_set_kill_msk(priv, uart_msg);
  1255. /* FIXME: based on notification, adjust the prio_boost */
  1256. spin_lock_irqsave(&priv->shrd->lock, flags);
  1257. priv->bt_ci_compliance = coex->bt_ci_compliance;
  1258. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  1259. }
  1260. void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
  1261. {
  1262. priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
  1263. iwlagn_bt_coex_profile_notif;
  1264. }
  1265. void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
  1266. {
  1267. INIT_WORK(&priv->bt_traffic_change_work,
  1268. iwlagn_bt_traffic_change_work);
  1269. }
  1270. void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
  1271. {
  1272. cancel_work_sync(&priv->bt_traffic_change_work);
  1273. }
  1274. static bool is_single_rx_stream(struct iwl_priv *priv)
  1275. {
  1276. return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
  1277. priv->current_ht_config.single_chain_sufficient;
  1278. }
  1279. #define IWL_NUM_RX_CHAINS_MULTIPLE 3
  1280. #define IWL_NUM_RX_CHAINS_SINGLE 2
  1281. #define IWL_NUM_IDLE_CHAINS_DUAL 2
  1282. #define IWL_NUM_IDLE_CHAINS_SINGLE 1
  1283. /*
  1284. * Determine how many receiver/antenna chains to use.
  1285. *
  1286. * More provides better reception via diversity. Fewer saves power
  1287. * at the expense of throughput, but only when not in powersave to
  1288. * start with.
  1289. *
  1290. * MIMO (dual stream) requires at least 2, but works better with 3.
  1291. * This does not determine *which* chains to use, just how many.
  1292. */
  1293. static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
  1294. {
  1295. if (priv->cfg->bt_params &&
  1296. priv->cfg->bt_params->advanced_bt_coexist &&
  1297. (priv->bt_full_concurrent ||
  1298. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  1299. /*
  1300. * only use chain 'A' in bt high traffic load or
  1301. * full concurrency mode
  1302. */
  1303. return IWL_NUM_RX_CHAINS_SINGLE;
  1304. }
  1305. /* # of Rx chains to use when expecting MIMO. */
  1306. if (is_single_rx_stream(priv))
  1307. return IWL_NUM_RX_CHAINS_SINGLE;
  1308. else
  1309. return IWL_NUM_RX_CHAINS_MULTIPLE;
  1310. }
  1311. /*
  1312. * When we are in power saving mode, unless device support spatial
  1313. * multiplexing power save, use the active count for rx chain count.
  1314. */
  1315. static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
  1316. {
  1317. /* # Rx chains when idling, depending on SMPS mode */
  1318. switch (priv->current_ht_config.smps) {
  1319. case IEEE80211_SMPS_STATIC:
  1320. case IEEE80211_SMPS_DYNAMIC:
  1321. return IWL_NUM_IDLE_CHAINS_SINGLE;
  1322. case IEEE80211_SMPS_OFF:
  1323. return active_cnt;
  1324. default:
  1325. WARN(1, "invalid SMPS mode %d",
  1326. priv->current_ht_config.smps);
  1327. return active_cnt;
  1328. }
  1329. }
  1330. /* up to 4 chains */
  1331. static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
  1332. {
  1333. u8 res;
  1334. res = (chain_bitmap & BIT(0)) >> 0;
  1335. res += (chain_bitmap & BIT(1)) >> 1;
  1336. res += (chain_bitmap & BIT(2)) >> 2;
  1337. res += (chain_bitmap & BIT(3)) >> 3;
  1338. return res;
  1339. }
  1340. /**
  1341. * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  1342. *
  1343. * Selects how many and which Rx receivers/antennas/chains to use.
  1344. * This should not be used for scan command ... it puts data in wrong place.
  1345. */
  1346. void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  1347. {
  1348. bool is_single = is_single_rx_stream(priv);
  1349. bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->shrd->status);
  1350. u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
  1351. u32 active_chains;
  1352. u16 rx_chain;
  1353. /* Tell uCode which antennas are actually connected.
  1354. * Before first association, we assume all antennas are connected.
  1355. * Just after first association, iwl_chain_noise_calibration()
  1356. * checks which antennas actually *are* connected. */
  1357. if (priv->chain_noise_data.active_chains)
  1358. active_chains = priv->chain_noise_data.active_chains;
  1359. else
  1360. active_chains = hw_params(priv).valid_rx_ant;
  1361. if (priv->cfg->bt_params &&
  1362. priv->cfg->bt_params->advanced_bt_coexist &&
  1363. (priv->bt_full_concurrent ||
  1364. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  1365. /*
  1366. * only use chain 'A' in bt high traffic load or
  1367. * full concurrency mode
  1368. */
  1369. active_chains = first_antenna(active_chains);
  1370. }
  1371. rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
  1372. /* How many receivers should we use? */
  1373. active_rx_cnt = iwl_get_active_rx_chain_count(priv);
  1374. idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
  1375. /* correct rx chain count according hw settings
  1376. * and chain noise calibration
  1377. */
  1378. valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
  1379. if (valid_rx_cnt < active_rx_cnt)
  1380. active_rx_cnt = valid_rx_cnt;
  1381. if (valid_rx_cnt < idle_rx_cnt)
  1382. idle_rx_cnt = valid_rx_cnt;
  1383. rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
  1384. rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
  1385. ctx->staging.rx_chain = cpu_to_le16(rx_chain);
  1386. if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
  1387. ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  1388. else
  1389. ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  1390. IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
  1391. ctx->staging.rx_chain,
  1392. active_rx_cnt, idle_rx_cnt);
  1393. WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
  1394. active_rx_cnt < idle_rx_cnt);
  1395. }
  1396. u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
  1397. {
  1398. int i;
  1399. u8 ind = ant;
  1400. if (priv->band == IEEE80211_BAND_2GHZ &&
  1401. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
  1402. return 0;
  1403. for (i = 0; i < RATE_ANT_NUM - 1; i++) {
  1404. ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
  1405. if (valid & BIT(ind))
  1406. return ind;
  1407. }
  1408. return ant;
  1409. }
  1410. static const char *get_csr_string(int cmd)
  1411. {
  1412. switch (cmd) {
  1413. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1414. IWL_CMD(CSR_INT_COALESCING);
  1415. IWL_CMD(CSR_INT);
  1416. IWL_CMD(CSR_INT_MASK);
  1417. IWL_CMD(CSR_FH_INT_STATUS);
  1418. IWL_CMD(CSR_GPIO_IN);
  1419. IWL_CMD(CSR_RESET);
  1420. IWL_CMD(CSR_GP_CNTRL);
  1421. IWL_CMD(CSR_HW_REV);
  1422. IWL_CMD(CSR_EEPROM_REG);
  1423. IWL_CMD(CSR_EEPROM_GP);
  1424. IWL_CMD(CSR_OTP_GP_REG);
  1425. IWL_CMD(CSR_GIO_REG);
  1426. IWL_CMD(CSR_GP_UCODE_REG);
  1427. IWL_CMD(CSR_GP_DRIVER_REG);
  1428. IWL_CMD(CSR_UCODE_DRV_GP1);
  1429. IWL_CMD(CSR_UCODE_DRV_GP2);
  1430. IWL_CMD(CSR_LED_REG);
  1431. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1432. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1433. IWL_CMD(CSR_ANA_PLL_CFG);
  1434. IWL_CMD(CSR_HW_REV_WA_REG);
  1435. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1436. default:
  1437. return "UNKNOWN";
  1438. }
  1439. }
  1440. void iwl_dump_csr(struct iwl_priv *priv)
  1441. {
  1442. int i;
  1443. static const u32 csr_tbl[] = {
  1444. CSR_HW_IF_CONFIG_REG,
  1445. CSR_INT_COALESCING,
  1446. CSR_INT,
  1447. CSR_INT_MASK,
  1448. CSR_FH_INT_STATUS,
  1449. CSR_GPIO_IN,
  1450. CSR_RESET,
  1451. CSR_GP_CNTRL,
  1452. CSR_HW_REV,
  1453. CSR_EEPROM_REG,
  1454. CSR_EEPROM_GP,
  1455. CSR_OTP_GP_REG,
  1456. CSR_GIO_REG,
  1457. CSR_GP_UCODE_REG,
  1458. CSR_GP_DRIVER_REG,
  1459. CSR_UCODE_DRV_GP1,
  1460. CSR_UCODE_DRV_GP2,
  1461. CSR_LED_REG,
  1462. CSR_DRAM_INT_TBL_REG,
  1463. CSR_GIO_CHICKEN_BITS,
  1464. CSR_ANA_PLL_CFG,
  1465. CSR_HW_REV_WA_REG,
  1466. CSR_DBG_HPET_MEM_REG
  1467. };
  1468. IWL_ERR(priv, "CSR values:\n");
  1469. IWL_ERR(priv, "(2nd byte of CSR_INT_COALESCING is "
  1470. "CSR_INT_PERIODIC_REG)\n");
  1471. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1472. IWL_ERR(priv, " %25s: 0X%08x\n",
  1473. get_csr_string(csr_tbl[i]),
  1474. iwl_read32(priv, csr_tbl[i]));
  1475. }
  1476. }
  1477. static const char *get_fh_string(int cmd)
  1478. {
  1479. switch (cmd) {
  1480. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1481. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1482. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1483. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1484. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1485. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1486. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1487. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1488. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1489. default:
  1490. return "UNKNOWN";
  1491. }
  1492. }
  1493. int iwl_dump_fh(struct iwl_priv *priv, char **buf, bool display)
  1494. {
  1495. int i;
  1496. #ifdef CONFIG_IWLWIFI_DEBUG
  1497. int pos = 0;
  1498. size_t bufsz = 0;
  1499. #endif
  1500. static const u32 fh_tbl[] = {
  1501. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1502. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1503. FH_RSCSR_CHNL0_WPTR,
  1504. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1505. FH_MEM_RSSR_SHARED_CTRL_REG,
  1506. FH_MEM_RSSR_RX_STATUS_REG,
  1507. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1508. FH_TSSR_TX_STATUS_REG,
  1509. FH_TSSR_TX_ERROR_REG
  1510. };
  1511. #ifdef CONFIG_IWLWIFI_DEBUG
  1512. if (display) {
  1513. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1514. *buf = kmalloc(bufsz, GFP_KERNEL);
  1515. if (!*buf)
  1516. return -ENOMEM;
  1517. pos += scnprintf(*buf + pos, bufsz - pos,
  1518. "FH register values:\n");
  1519. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1520. pos += scnprintf(*buf + pos, bufsz - pos,
  1521. " %34s: 0X%08x\n",
  1522. get_fh_string(fh_tbl[i]),
  1523. iwl_read_direct32(priv, fh_tbl[i]));
  1524. }
  1525. return pos;
  1526. }
  1527. #endif
  1528. IWL_ERR(priv, "FH register values:\n");
  1529. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1530. IWL_ERR(priv, " %34s: 0X%08x\n",
  1531. get_fh_string(fh_tbl[i]),
  1532. iwl_read_direct32(priv, fh_tbl[i]));
  1533. }
  1534. return 0;
  1535. }
  1536. /* notification wait support */
  1537. void iwlagn_init_notification_wait(struct iwl_priv *priv,
  1538. struct iwl_notification_wait *wait_entry,
  1539. u8 cmd,
  1540. void (*fn)(struct iwl_priv *priv,
  1541. struct iwl_rx_packet *pkt,
  1542. void *data),
  1543. void *fn_data)
  1544. {
  1545. wait_entry->fn = fn;
  1546. wait_entry->fn_data = fn_data;
  1547. wait_entry->cmd = cmd;
  1548. wait_entry->triggered = false;
  1549. wait_entry->aborted = false;
  1550. spin_lock_bh(&priv->notif_wait_lock);
  1551. list_add(&wait_entry->list, &priv->notif_waits);
  1552. spin_unlock_bh(&priv->notif_wait_lock);
  1553. }
  1554. int iwlagn_wait_notification(struct iwl_priv *priv,
  1555. struct iwl_notification_wait *wait_entry,
  1556. unsigned long timeout)
  1557. {
  1558. int ret;
  1559. ret = wait_event_timeout(priv->notif_waitq,
  1560. wait_entry->triggered || wait_entry->aborted,
  1561. timeout);
  1562. spin_lock_bh(&priv->notif_wait_lock);
  1563. list_del(&wait_entry->list);
  1564. spin_unlock_bh(&priv->notif_wait_lock);
  1565. if (wait_entry->aborted)
  1566. return -EIO;
  1567. /* return value is always >= 0 */
  1568. if (ret <= 0)
  1569. return -ETIMEDOUT;
  1570. return 0;
  1571. }
  1572. void iwlagn_remove_notification(struct iwl_priv *priv,
  1573. struct iwl_notification_wait *wait_entry)
  1574. {
  1575. spin_lock_bh(&priv->notif_wait_lock);
  1576. list_del(&wait_entry->list);
  1577. spin_unlock_bh(&priv->notif_wait_lock);
  1578. }