iwl-agn-lib.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/etherdevice.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/sched.h>
  34. #include "iwl-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-helpers.h"
  38. #include "iwl-agn-hw.h"
  39. #include "iwl-agn.h"
  40. #include "iwl-sta.h"
  41. static inline u32 iwlagn_get_scd_ssn(struct iwl5000_tx_resp *tx_resp)
  42. {
  43. return le32_to_cpup((__le32 *)&tx_resp->status +
  44. tx_resp->frame_count) & MAX_SN;
  45. }
  46. static void iwlagn_set_tx_status(struct iwl_priv *priv,
  47. struct ieee80211_tx_info *info,
  48. struct iwl5000_tx_resp *tx_resp,
  49. int txq_id, bool is_agg)
  50. {
  51. u16 status = le16_to_cpu(tx_resp->status.status);
  52. info->status.rates[0].count = tx_resp->failure_frame + 1;
  53. if (is_agg)
  54. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  55. info->flags |= iwl_tx_status_to_mac80211(status);
  56. iwlagn_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
  57. info);
  58. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
  59. "0x%x retries %d\n",
  60. txq_id,
  61. iwl_get_tx_fail_reason(status), status,
  62. le32_to_cpu(tx_resp->rate_n_flags),
  63. tx_resp->failure_frame);
  64. }
  65. static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
  66. struct iwl_ht_agg *agg,
  67. struct iwl5000_tx_resp *tx_resp,
  68. int txq_id, u16 start_idx)
  69. {
  70. u16 status;
  71. struct agg_tx_status *frame_status = &tx_resp->status;
  72. struct ieee80211_hdr *hdr = NULL;
  73. int i, sh, idx;
  74. u16 seq;
  75. if (agg->wait_for_ba)
  76. IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
  77. agg->frame_count = tx_resp->frame_count;
  78. agg->start_idx = start_idx;
  79. agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  80. agg->bitmap = 0;
  81. /* # frames attempted by Tx command */
  82. if (agg->frame_count == 1) {
  83. /* Only one frame was attempted; no block-ack will arrive */
  84. idx = start_idx;
  85. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
  86. agg->frame_count, agg->start_idx, idx);
  87. iwlagn_set_tx_status(priv,
  88. IEEE80211_SKB_CB(
  89. priv->txq[txq_id].txb[idx].skb),
  90. tx_resp, txq_id, true);
  91. agg->wait_for_ba = 0;
  92. } else {
  93. /* Two or more frames were attempted; expect block-ack */
  94. u64 bitmap = 0;
  95. /*
  96. * Start is the lowest frame sent. It may not be the first
  97. * frame in the batch; we figure this out dynamically during
  98. * the following loop.
  99. */
  100. int start = agg->start_idx;
  101. /* Construct bit-map of pending frames within Tx window */
  102. for (i = 0; i < agg->frame_count; i++) {
  103. u16 sc;
  104. status = le16_to_cpu(frame_status[i].status);
  105. seq = le16_to_cpu(frame_status[i].sequence);
  106. idx = SEQ_TO_INDEX(seq);
  107. txq_id = SEQ_TO_QUEUE(seq);
  108. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  109. AGG_TX_STATE_ABORT_MSK))
  110. continue;
  111. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
  112. agg->frame_count, txq_id, idx);
  113. hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
  114. if (!hdr) {
  115. IWL_ERR(priv,
  116. "BUG_ON idx doesn't point to valid skb"
  117. " idx=%d, txq_id=%d\n", idx, txq_id);
  118. return -1;
  119. }
  120. sc = le16_to_cpu(hdr->seq_ctrl);
  121. if (idx != (SEQ_TO_SN(sc) & 0xff)) {
  122. IWL_ERR(priv,
  123. "BUG_ON idx doesn't match seq control"
  124. " idx=%d, seq_idx=%d, seq=%d\n",
  125. idx, SEQ_TO_SN(sc),
  126. hdr->seq_ctrl);
  127. return -1;
  128. }
  129. IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
  130. i, idx, SEQ_TO_SN(sc));
  131. /*
  132. * sh -> how many frames ahead of the starting frame is
  133. * the current one?
  134. *
  135. * Note that all frames sent in the batch must be in a
  136. * 64-frame window, so this number should be in [0,63].
  137. * If outside of this window, then we've found a new
  138. * "first" frame in the batch and need to change start.
  139. */
  140. sh = idx - start;
  141. /*
  142. * If >= 64, out of window. start must be at the front
  143. * of the circular buffer, idx must be near the end of
  144. * the buffer, and idx is the new "first" frame. Shift
  145. * the indices around.
  146. */
  147. if (sh >= 64) {
  148. /* Shift bitmap by start - idx, wrapped */
  149. sh = 0x100 - idx + start;
  150. bitmap = bitmap << sh;
  151. /* Now idx is the new start so sh = 0 */
  152. sh = 0;
  153. start = idx;
  154. /*
  155. * If <= -64 then wraps the 256-pkt circular buffer
  156. * (e.g., start = 255 and idx = 0, sh should be 1)
  157. */
  158. } else if (sh <= -64) {
  159. sh = 0x100 - start + idx;
  160. /*
  161. * If < 0 but > -64, out of window. idx is before start
  162. * but not wrapped. Shift the indices around.
  163. */
  164. } else if (sh < 0) {
  165. /* Shift by how far start is ahead of idx */
  166. sh = start - idx;
  167. bitmap = bitmap << sh;
  168. /* Now idx is the new start so sh = 0 */
  169. start = idx;
  170. sh = 0;
  171. }
  172. /* Sequence number start + sh was sent in this batch */
  173. bitmap |= 1ULL << sh;
  174. IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
  175. start, (unsigned long long)bitmap);
  176. }
  177. /*
  178. * Store the bitmap and possibly the new start, if we wrapped
  179. * the buffer above
  180. */
  181. agg->bitmap = bitmap;
  182. agg->start_idx = start;
  183. IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
  184. agg->frame_count, agg->start_idx,
  185. (unsigned long long)agg->bitmap);
  186. if (bitmap)
  187. agg->wait_for_ba = 1;
  188. }
  189. return 0;
  190. }
  191. void iwl_check_abort_status(struct iwl_priv *priv,
  192. u8 frame_count, u32 status)
  193. {
  194. if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
  195. IWL_ERR(priv, "Tx flush command to flush out all frames\n");
  196. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  197. queue_work(priv->workqueue, &priv->tx_flush);
  198. }
  199. }
  200. static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
  201. struct iwl_rx_mem_buffer *rxb)
  202. {
  203. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  204. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  205. int txq_id = SEQ_TO_QUEUE(sequence);
  206. int index = SEQ_TO_INDEX(sequence);
  207. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  208. struct ieee80211_tx_info *info;
  209. struct iwl5000_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  210. u32 status = le16_to_cpu(tx_resp->status.status);
  211. int tid;
  212. int sta_id;
  213. int freed;
  214. unsigned long flags;
  215. if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
  216. IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
  217. "is out of range [0-%d] %d %d\n", txq_id,
  218. index, txq->q.n_bd, txq->q.write_ptr,
  219. txq->q.read_ptr);
  220. return;
  221. }
  222. info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb);
  223. memset(&info->status, 0, sizeof(info->status));
  224. tid = (tx_resp->ra_tid & IWL50_TX_RES_TID_MSK) >> IWL50_TX_RES_TID_POS;
  225. sta_id = (tx_resp->ra_tid & IWL50_TX_RES_RA_MSK) >> IWL50_TX_RES_RA_POS;
  226. spin_lock_irqsave(&priv->sta_lock, flags);
  227. if (txq->sched_retry) {
  228. const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
  229. struct iwl_ht_agg *agg;
  230. agg = &priv->stations[sta_id].tid[tid].agg;
  231. /*
  232. * If the BT kill count is non-zero, we'll get this
  233. * notification again.
  234. */
  235. if (tx_resp->bt_kill_count && tx_resp->frame_count == 1 &&
  236. priv->cfg->advanced_bt_coexist) {
  237. IWL_WARN(priv, "receive reply tx with bt_kill\n");
  238. }
  239. iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  240. /* check if BAR is needed */
  241. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
  242. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  243. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  244. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  245. IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
  246. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  247. scd_ssn , index, txq_id, txq->swq_id);
  248. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  249. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  250. if (priv->mac80211_registered &&
  251. (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  252. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
  253. if (agg->state == IWL_AGG_OFF)
  254. iwl_wake_queue(priv, txq_id);
  255. else
  256. iwl_wake_queue(priv, txq->swq_id);
  257. }
  258. }
  259. } else {
  260. BUG_ON(txq_id != txq->swq_id);
  261. iwlagn_set_tx_status(priv, info, tx_resp, txq_id, false);
  262. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  263. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  264. if (priv->mac80211_registered &&
  265. (iwl_queue_space(&txq->q) > txq->q.low_mark))
  266. iwl_wake_queue(priv, txq_id);
  267. }
  268. iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
  269. iwl_check_abort_status(priv, tx_resp->frame_count, status);
  270. spin_unlock_irqrestore(&priv->sta_lock, flags);
  271. }
  272. void iwlagn_rx_handler_setup(struct iwl_priv *priv)
  273. {
  274. /* init calibration handlers */
  275. priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
  276. iwlagn_rx_calib_result;
  277. priv->rx_handlers[CALIBRATION_COMPLETE_NOTIFICATION] =
  278. iwlagn_rx_calib_complete;
  279. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  280. }
  281. void iwlagn_setup_deferred_work(struct iwl_priv *priv)
  282. {
  283. /* in agn, the tx power calibration is done in uCode */
  284. priv->disable_tx_power_cal = 1;
  285. }
  286. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  287. {
  288. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  289. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  290. }
  291. int iwlagn_send_tx_power(struct iwl_priv *priv)
  292. {
  293. struct iwl5000_tx_power_dbm_cmd tx_power_cmd;
  294. u8 tx_ant_cfg_cmd;
  295. /* half dBm need to multiply */
  296. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  297. if (priv->tx_power_lmt_in_half_dbm &&
  298. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  299. /*
  300. * For the newer devices which using enhanced/extend tx power
  301. * table in EEPROM, the format is in half dBm. driver need to
  302. * convert to dBm format before report to mac80211.
  303. * By doing so, there is a possibility of 1/2 dBm resolution
  304. * lost. driver will perform "round-up" operation before
  305. * reporting, but it will cause 1/2 dBm tx power over the
  306. * regulatory limit. Perform the checking here, if the
  307. * "tx_power_user_lmt" is higher than EEPROM value (in
  308. * half-dBm format), lower the tx power based on EEPROM
  309. */
  310. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  311. }
  312. tx_power_cmd.flags = IWL50_TX_POWER_NO_CLOSED;
  313. tx_power_cmd.srv_chan_lmt = IWL50_TX_POWER_AUTO;
  314. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  315. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  316. else
  317. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  318. return iwl_send_cmd_pdu_async(priv, tx_ant_cfg_cmd,
  319. sizeof(tx_power_cmd), &tx_power_cmd,
  320. NULL);
  321. }
  322. void iwlagn_temperature(struct iwl_priv *priv)
  323. {
  324. /* store temperature from statistics (in Celsius) */
  325. priv->temperature =
  326. le32_to_cpu(priv->_agn.statistics.general.common.temperature);
  327. iwl_tt_handler(priv);
  328. }
  329. u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
  330. {
  331. struct iwl_eeprom_calib_hdr {
  332. u8 version;
  333. u8 pa_type;
  334. u16 voltage;
  335. } *hdr;
  336. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  337. EEPROM_CALIB_ALL);
  338. return hdr->version;
  339. }
  340. /*
  341. * EEPROM
  342. */
  343. static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
  344. {
  345. u16 offset = 0;
  346. if ((address & INDIRECT_ADDRESS) == 0)
  347. return address;
  348. switch (address & INDIRECT_TYPE_MSK) {
  349. case INDIRECT_HOST:
  350. offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
  351. break;
  352. case INDIRECT_GENERAL:
  353. offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
  354. break;
  355. case INDIRECT_REGULATORY:
  356. offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
  357. break;
  358. case INDIRECT_CALIBRATION:
  359. offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
  360. break;
  361. case INDIRECT_PROCESS_ADJST:
  362. offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
  363. break;
  364. case INDIRECT_OTHERS:
  365. offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
  366. break;
  367. default:
  368. IWL_ERR(priv, "illegal indirect type: 0x%X\n",
  369. address & INDIRECT_TYPE_MSK);
  370. break;
  371. }
  372. /* translate the offset from words to byte */
  373. return (address & ADDRESS_MSK) + (offset << 1);
  374. }
  375. const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
  376. size_t offset)
  377. {
  378. u32 address = eeprom_indirect_address(priv, offset);
  379. BUG_ON(address >= priv->cfg->eeprom_size);
  380. return &priv->eeprom[address];
  381. }
  382. struct iwl_mod_params iwlagn_mod_params = {
  383. .amsdu_size_8K = 1,
  384. .restart_fw = 1,
  385. /* the rest are 0 by default */
  386. };
  387. void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  388. {
  389. unsigned long flags;
  390. int i;
  391. spin_lock_irqsave(&rxq->lock, flags);
  392. INIT_LIST_HEAD(&rxq->rx_free);
  393. INIT_LIST_HEAD(&rxq->rx_used);
  394. /* Fill the rx_used queue with _all_ of the Rx buffers */
  395. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  396. /* In the reset function, these buffers may have been allocated
  397. * to an SKB, so we need to unmap and free potential storage */
  398. if (rxq->pool[i].page != NULL) {
  399. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  400. PAGE_SIZE << priv->hw_params.rx_page_order,
  401. PCI_DMA_FROMDEVICE);
  402. __iwl_free_pages(priv, rxq->pool[i].page);
  403. rxq->pool[i].page = NULL;
  404. }
  405. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  406. }
  407. for (i = 0; i < RX_QUEUE_SIZE; i++)
  408. rxq->queue[i] = NULL;
  409. /* Set us so that we have processed and used all buffers, but have
  410. * not restocked the Rx queue with fresh buffers */
  411. rxq->read = rxq->write = 0;
  412. rxq->write_actual = 0;
  413. rxq->free_count = 0;
  414. spin_unlock_irqrestore(&rxq->lock, flags);
  415. }
  416. int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  417. {
  418. u32 rb_size;
  419. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  420. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  421. if (!priv->cfg->use_isr_legacy)
  422. rb_timeout = RX_RB_TIMEOUT;
  423. if (priv->cfg->mod_params->amsdu_size_8K)
  424. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  425. else
  426. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  427. /* Stop Rx DMA */
  428. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  429. /* Reset driver's Rx queue write index */
  430. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  431. /* Tell device where to find RBD circular buffer in DRAM */
  432. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  433. (u32)(rxq->bd_dma >> 8));
  434. /* Tell device where in DRAM to update its Rx status */
  435. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  436. rxq->rb_stts_dma >> 4);
  437. /* Enable Rx DMA
  438. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  439. * the credit mechanism in 5000 HW RX FIFO
  440. * Direct rx interrupts to hosts
  441. * Rx buffer size 4 or 8k
  442. * RB timeout 0x10
  443. * 256 RBDs
  444. */
  445. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  446. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  447. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  448. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  449. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  450. rb_size|
  451. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  452. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  453. /* Set interrupt coalescing timer to default (2048 usecs) */
  454. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  455. return 0;
  456. }
  457. int iwlagn_hw_nic_init(struct iwl_priv *priv)
  458. {
  459. unsigned long flags;
  460. struct iwl_rx_queue *rxq = &priv->rxq;
  461. int ret;
  462. /* nic_init */
  463. spin_lock_irqsave(&priv->lock, flags);
  464. priv->cfg->ops->lib->apm_ops.init(priv);
  465. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  466. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  467. spin_unlock_irqrestore(&priv->lock, flags);
  468. ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
  469. priv->cfg->ops->lib->apm_ops.config(priv);
  470. /* Allocate the RX queue, or reset if it is already allocated */
  471. if (!rxq->bd) {
  472. ret = iwl_rx_queue_alloc(priv);
  473. if (ret) {
  474. IWL_ERR(priv, "Unable to initialize Rx queue\n");
  475. return -ENOMEM;
  476. }
  477. } else
  478. iwlagn_rx_queue_reset(priv, rxq);
  479. iwlagn_rx_replenish(priv);
  480. iwlagn_rx_init(priv, rxq);
  481. spin_lock_irqsave(&priv->lock, flags);
  482. rxq->need_update = 1;
  483. iwl_rx_queue_update_write_ptr(priv, rxq);
  484. spin_unlock_irqrestore(&priv->lock, flags);
  485. /* Allocate or reset and init all Tx and Command queues */
  486. if (!priv->txq) {
  487. ret = iwlagn_txq_ctx_alloc(priv);
  488. if (ret)
  489. return ret;
  490. } else
  491. iwlagn_txq_ctx_reset(priv);
  492. set_bit(STATUS_INIT, &priv->status);
  493. return 0;
  494. }
  495. /**
  496. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  497. */
  498. static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
  499. dma_addr_t dma_addr)
  500. {
  501. return cpu_to_le32((u32)(dma_addr >> 8));
  502. }
  503. /**
  504. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  505. *
  506. * If there are slots in the RX queue that need to be restocked,
  507. * and we have free pre-allocated buffers, fill the ranks as much
  508. * as we can, pulling from rx_free.
  509. *
  510. * This moves the 'write' index forward to catch up with 'processed', and
  511. * also updates the memory address in the firmware to reference the new
  512. * target buffer.
  513. */
  514. void iwlagn_rx_queue_restock(struct iwl_priv *priv)
  515. {
  516. struct iwl_rx_queue *rxq = &priv->rxq;
  517. struct list_head *element;
  518. struct iwl_rx_mem_buffer *rxb;
  519. unsigned long flags;
  520. spin_lock_irqsave(&rxq->lock, flags);
  521. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  522. /* The overwritten rxb must be a used one */
  523. rxb = rxq->queue[rxq->write];
  524. BUG_ON(rxb && rxb->page);
  525. /* Get next free Rx buffer, remove from free list */
  526. element = rxq->rx_free.next;
  527. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  528. list_del(element);
  529. /* Point to Rx buffer via next RBD in circular buffer */
  530. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
  531. rxb->page_dma);
  532. rxq->queue[rxq->write] = rxb;
  533. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  534. rxq->free_count--;
  535. }
  536. spin_unlock_irqrestore(&rxq->lock, flags);
  537. /* If the pre-allocated buffer pool is dropping low, schedule to
  538. * refill it */
  539. if (rxq->free_count <= RX_LOW_WATERMARK)
  540. queue_work(priv->workqueue, &priv->rx_replenish);
  541. /* If we've added more space for the firmware to place data, tell it.
  542. * Increment device's write pointer in multiples of 8. */
  543. if (rxq->write_actual != (rxq->write & ~0x7)) {
  544. spin_lock_irqsave(&rxq->lock, flags);
  545. rxq->need_update = 1;
  546. spin_unlock_irqrestore(&rxq->lock, flags);
  547. iwl_rx_queue_update_write_ptr(priv, rxq);
  548. }
  549. }
  550. /**
  551. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  552. *
  553. * When moving to rx_free an SKB is allocated for the slot.
  554. *
  555. * Also restock the Rx queue via iwl_rx_queue_restock.
  556. * This is called as a scheduled work item (except for during initialization)
  557. */
  558. void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  559. {
  560. struct iwl_rx_queue *rxq = &priv->rxq;
  561. struct list_head *element;
  562. struct iwl_rx_mem_buffer *rxb;
  563. struct page *page;
  564. unsigned long flags;
  565. gfp_t gfp_mask = priority;
  566. while (1) {
  567. spin_lock_irqsave(&rxq->lock, flags);
  568. if (list_empty(&rxq->rx_used)) {
  569. spin_unlock_irqrestore(&rxq->lock, flags);
  570. return;
  571. }
  572. spin_unlock_irqrestore(&rxq->lock, flags);
  573. if (rxq->free_count > RX_LOW_WATERMARK)
  574. gfp_mask |= __GFP_NOWARN;
  575. if (priv->hw_params.rx_page_order > 0)
  576. gfp_mask |= __GFP_COMP;
  577. /* Alloc a new receive buffer */
  578. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  579. if (!page) {
  580. if (net_ratelimit())
  581. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  582. "order: %d\n",
  583. priv->hw_params.rx_page_order);
  584. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  585. net_ratelimit())
  586. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  587. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  588. rxq->free_count);
  589. /* We don't reschedule replenish work here -- we will
  590. * call the restock method and if it still needs
  591. * more buffers it will schedule replenish */
  592. return;
  593. }
  594. spin_lock_irqsave(&rxq->lock, flags);
  595. if (list_empty(&rxq->rx_used)) {
  596. spin_unlock_irqrestore(&rxq->lock, flags);
  597. __free_pages(page, priv->hw_params.rx_page_order);
  598. return;
  599. }
  600. element = rxq->rx_used.next;
  601. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  602. list_del(element);
  603. spin_unlock_irqrestore(&rxq->lock, flags);
  604. BUG_ON(rxb->page);
  605. rxb->page = page;
  606. /* Get physical address of the RB */
  607. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  608. PAGE_SIZE << priv->hw_params.rx_page_order,
  609. PCI_DMA_FROMDEVICE);
  610. /* dma address must be no more than 36 bits */
  611. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  612. /* and also 256 byte aligned! */
  613. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  614. spin_lock_irqsave(&rxq->lock, flags);
  615. list_add_tail(&rxb->list, &rxq->rx_free);
  616. rxq->free_count++;
  617. priv->alloc_rxb_page++;
  618. spin_unlock_irqrestore(&rxq->lock, flags);
  619. }
  620. }
  621. void iwlagn_rx_replenish(struct iwl_priv *priv)
  622. {
  623. unsigned long flags;
  624. iwlagn_rx_allocate(priv, GFP_KERNEL);
  625. spin_lock_irqsave(&priv->lock, flags);
  626. iwlagn_rx_queue_restock(priv);
  627. spin_unlock_irqrestore(&priv->lock, flags);
  628. }
  629. void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  630. {
  631. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  632. iwlagn_rx_queue_restock(priv);
  633. }
  634. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  635. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  636. * This free routine walks the list of POOL entries and if SKB is set to
  637. * non NULL it is unmapped and freed
  638. */
  639. void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  640. {
  641. int i;
  642. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  643. if (rxq->pool[i].page != NULL) {
  644. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  645. PAGE_SIZE << priv->hw_params.rx_page_order,
  646. PCI_DMA_FROMDEVICE);
  647. __iwl_free_pages(priv, rxq->pool[i].page);
  648. rxq->pool[i].page = NULL;
  649. }
  650. }
  651. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  652. rxq->bd_dma);
  653. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  654. rxq->rb_stts, rxq->rb_stts_dma);
  655. rxq->bd = NULL;
  656. rxq->rb_stts = NULL;
  657. }
  658. int iwlagn_rxq_stop(struct iwl_priv *priv)
  659. {
  660. /* stop Rx DMA */
  661. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  662. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  663. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  664. return 0;
  665. }
  666. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  667. {
  668. int idx = 0;
  669. int band_offset = 0;
  670. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  671. if (rate_n_flags & RATE_MCS_HT_MSK) {
  672. idx = (rate_n_flags & 0xff);
  673. return idx;
  674. /* Legacy rate format, search for match in table */
  675. } else {
  676. if (band == IEEE80211_BAND_5GHZ)
  677. band_offset = IWL_FIRST_OFDM_RATE;
  678. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  679. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  680. return idx - band_offset;
  681. }
  682. return -1;
  683. }
  684. /* Calc max signal level (dBm) among 3 possible receivers */
  685. static inline int iwlagn_calc_rssi(struct iwl_priv *priv,
  686. struct iwl_rx_phy_res *rx_resp)
  687. {
  688. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  689. }
  690. static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  691. {
  692. u32 decrypt_out = 0;
  693. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  694. RX_RES_STATUS_STATION_FOUND)
  695. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  696. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  697. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  698. /* packet was not encrypted */
  699. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  700. RX_RES_STATUS_SEC_TYPE_NONE)
  701. return decrypt_out;
  702. /* packet was encrypted with unknown alg */
  703. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  704. RX_RES_STATUS_SEC_TYPE_ERR)
  705. return decrypt_out;
  706. /* decryption was not done in HW */
  707. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  708. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  709. return decrypt_out;
  710. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  711. case RX_RES_STATUS_SEC_TYPE_CCMP:
  712. /* alg is CCM: check MIC only */
  713. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  714. /* Bad MIC */
  715. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  716. else
  717. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  718. break;
  719. case RX_RES_STATUS_SEC_TYPE_TKIP:
  720. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  721. /* Bad TTAK */
  722. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  723. break;
  724. }
  725. /* fall through if TTAK OK */
  726. default:
  727. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  728. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  729. else
  730. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  731. break;
  732. }
  733. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  734. decrypt_in, decrypt_out);
  735. return decrypt_out;
  736. }
  737. static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
  738. struct ieee80211_hdr *hdr,
  739. u16 len,
  740. u32 ampdu_status,
  741. struct iwl_rx_mem_buffer *rxb,
  742. struct ieee80211_rx_status *stats)
  743. {
  744. struct sk_buff *skb;
  745. __le16 fc = hdr->frame_control;
  746. /* We only process data packets if the interface is open */
  747. if (unlikely(!priv->is_open)) {
  748. IWL_DEBUG_DROP_LIMIT(priv,
  749. "Dropping packet while interface is not open.\n");
  750. return;
  751. }
  752. /* In case of HW accelerated crypto and bad decryption, drop */
  753. if (!priv->cfg->mod_params->sw_crypto &&
  754. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  755. return;
  756. skb = dev_alloc_skb(128);
  757. if (!skb) {
  758. IWL_ERR(priv, "dev_alloc_skb failed\n");
  759. return;
  760. }
  761. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
  762. iwl_update_stats(priv, false, fc, len);
  763. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  764. ieee80211_rx(priv->hw, skb);
  765. priv->alloc_rxb_page--;
  766. rxb->page = NULL;
  767. }
  768. /* Called for REPLY_RX (legacy ABG frames), or
  769. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  770. void iwlagn_rx_reply_rx(struct iwl_priv *priv,
  771. struct iwl_rx_mem_buffer *rxb)
  772. {
  773. struct ieee80211_hdr *header;
  774. struct ieee80211_rx_status rx_status;
  775. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  776. struct iwl_rx_phy_res *phy_res;
  777. __le32 rx_pkt_status;
  778. struct iwl_rx_mpdu_res_start *amsdu;
  779. u32 len;
  780. u32 ampdu_status;
  781. u32 rate_n_flags;
  782. /**
  783. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  784. * REPLY_RX: physical layer info is in this buffer
  785. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  786. * command and cached in priv->last_phy_res
  787. *
  788. * Here we set up local variables depending on which command is
  789. * received.
  790. */
  791. if (pkt->hdr.cmd == REPLY_RX) {
  792. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  793. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  794. + phy_res->cfg_phy_cnt);
  795. len = le16_to_cpu(phy_res->byte_count);
  796. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  797. phy_res->cfg_phy_cnt + len);
  798. ampdu_status = le32_to_cpu(rx_pkt_status);
  799. } else {
  800. if (!priv->_agn.last_phy_res_valid) {
  801. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  802. return;
  803. }
  804. phy_res = &priv->_agn.last_phy_res;
  805. amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
  806. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  807. len = le16_to_cpu(amsdu->byte_count);
  808. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  809. ampdu_status = iwlagn_translate_rx_status(priv,
  810. le32_to_cpu(rx_pkt_status));
  811. }
  812. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  813. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  814. phy_res->cfg_phy_cnt);
  815. return;
  816. }
  817. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  818. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  819. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  820. le32_to_cpu(rx_pkt_status));
  821. return;
  822. }
  823. /* This will be used in several places later */
  824. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  825. /* rx_status carries information about the packet to mac80211 */
  826. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  827. rx_status.freq =
  828. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
  829. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  830. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  831. rx_status.rate_idx =
  832. iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  833. rx_status.flag = 0;
  834. /* TSF isn't reliable. In order to allow smooth user experience,
  835. * this W/A doesn't propagate it to the mac80211 */
  836. /*rx_status.flag |= RX_FLAG_TSFT;*/
  837. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  838. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  839. rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
  840. iwl_dbg_log_rx_data_frame(priv, len, header);
  841. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
  842. rx_status.signal, (unsigned long long)rx_status.mactime);
  843. /*
  844. * "antenna number"
  845. *
  846. * It seems that the antenna field in the phy flags value
  847. * is actually a bit field. This is undefined by radiotap,
  848. * it wants an actual antenna number but I always get "7"
  849. * for most legacy frames I receive indicating that the
  850. * same frame was received on all three RX chains.
  851. *
  852. * I think this field should be removed in favor of a
  853. * new 802.11n radiotap field "RX chains" that is defined
  854. * as a bitmask.
  855. */
  856. rx_status.antenna =
  857. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  858. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  859. /* set the preamble flag if appropriate */
  860. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  861. rx_status.flag |= RX_FLAG_SHORTPRE;
  862. /* Set up the HT phy flags */
  863. if (rate_n_flags & RATE_MCS_HT_MSK)
  864. rx_status.flag |= RX_FLAG_HT;
  865. if (rate_n_flags & RATE_MCS_HT40_MSK)
  866. rx_status.flag |= RX_FLAG_40MHZ;
  867. if (rate_n_flags & RATE_MCS_SGI_MSK)
  868. rx_status.flag |= RX_FLAG_SHORT_GI;
  869. iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  870. rxb, &rx_status);
  871. }
  872. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  873. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  874. void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
  875. struct iwl_rx_mem_buffer *rxb)
  876. {
  877. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  878. priv->_agn.last_phy_res_valid = true;
  879. memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
  880. sizeof(struct iwl_rx_phy_res));
  881. }
  882. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  883. struct ieee80211_vif *vif,
  884. enum ieee80211_band band,
  885. struct iwl_scan_channel *scan_ch)
  886. {
  887. const struct ieee80211_supported_band *sband;
  888. u16 passive_dwell = 0;
  889. u16 active_dwell = 0;
  890. int added = 0;
  891. u16 channel = 0;
  892. sband = iwl_get_hw_mode(priv, band);
  893. if (!sband) {
  894. IWL_ERR(priv, "invalid band\n");
  895. return added;
  896. }
  897. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  898. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  899. if (passive_dwell <= active_dwell)
  900. passive_dwell = active_dwell + 1;
  901. channel = iwl_get_single_channel_number(priv, band);
  902. if (channel) {
  903. scan_ch->channel = cpu_to_le16(channel);
  904. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  905. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  906. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  907. /* Set txpower levels to defaults */
  908. scan_ch->dsp_atten = 110;
  909. if (band == IEEE80211_BAND_5GHZ)
  910. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  911. else
  912. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  913. added++;
  914. } else
  915. IWL_ERR(priv, "no valid channel found\n");
  916. return added;
  917. }
  918. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  919. struct ieee80211_vif *vif,
  920. enum ieee80211_band band,
  921. u8 is_active, u8 n_probes,
  922. struct iwl_scan_channel *scan_ch)
  923. {
  924. struct ieee80211_channel *chan;
  925. const struct ieee80211_supported_band *sband;
  926. const struct iwl_channel_info *ch_info;
  927. u16 passive_dwell = 0;
  928. u16 active_dwell = 0;
  929. int added, i;
  930. u16 channel;
  931. sband = iwl_get_hw_mode(priv, band);
  932. if (!sband)
  933. return 0;
  934. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  935. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  936. if (passive_dwell <= active_dwell)
  937. passive_dwell = active_dwell + 1;
  938. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  939. chan = priv->scan_request->channels[i];
  940. if (chan->band != band)
  941. continue;
  942. channel = chan->hw_value;
  943. scan_ch->channel = cpu_to_le16(channel);
  944. ch_info = iwl_get_channel_info(priv, band, channel);
  945. if (!is_channel_valid(ch_info)) {
  946. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  947. channel);
  948. continue;
  949. }
  950. if (!is_active || is_channel_passive(ch_info) ||
  951. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  952. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  953. else
  954. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  955. if (n_probes)
  956. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  957. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  958. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  959. /* Set txpower levels to defaults */
  960. scan_ch->dsp_atten = 110;
  961. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  962. * power level:
  963. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  964. */
  965. if (band == IEEE80211_BAND_5GHZ)
  966. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  967. else
  968. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  969. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  970. channel, le32_to_cpu(scan_ch->type),
  971. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  972. "ACTIVE" : "PASSIVE",
  973. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  974. active_dwell : passive_dwell);
  975. scan_ch++;
  976. added++;
  977. }
  978. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  979. return added;
  980. }
  981. void iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  982. {
  983. struct iwl_host_cmd cmd = {
  984. .id = REPLY_SCAN_CMD,
  985. .len = sizeof(struct iwl_scan_cmd),
  986. .flags = CMD_SIZE_HUGE,
  987. };
  988. struct iwl_scan_cmd *scan;
  989. struct ieee80211_conf *conf = NULL;
  990. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  991. u32 rate_flags = 0;
  992. u16 cmd_len;
  993. u16 rx_chain = 0;
  994. enum ieee80211_band band;
  995. u8 n_probes = 0;
  996. u8 rx_ant = priv->hw_params.valid_rx_ant;
  997. u8 rate;
  998. bool is_active = false;
  999. int chan_mod;
  1000. u8 active_chains;
  1001. u8 scan_tx_antennas = priv->hw_params.valid_tx_ant;
  1002. if (vif)
  1003. ctx = iwl_rxon_ctx_from_vif(vif);
  1004. conf = ieee80211_get_hw_conf(priv->hw);
  1005. cancel_delayed_work(&priv->scan_check);
  1006. if (!iwl_is_ready(priv)) {
  1007. IWL_WARN(priv, "request scan called when driver not ready.\n");
  1008. goto done;
  1009. }
  1010. /* Make sure the scan wasn't canceled before this queued work
  1011. * was given the chance to run... */
  1012. if (!test_bit(STATUS_SCANNING, &priv->status))
  1013. goto done;
  1014. /* This should never be called or scheduled if there is currently
  1015. * a scan active in the hardware. */
  1016. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  1017. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
  1018. "Ignoring second request.\n");
  1019. goto done;
  1020. }
  1021. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  1022. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  1023. goto done;
  1024. }
  1025. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1026. IWL_DEBUG_HC(priv, "Scan request while abort pending. Queuing.\n");
  1027. goto done;
  1028. }
  1029. if (iwl_is_rfkill(priv)) {
  1030. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  1031. goto done;
  1032. }
  1033. if (!test_bit(STATUS_READY, &priv->status)) {
  1034. IWL_DEBUG_HC(priv, "Scan request while uninitialized. Queuing.\n");
  1035. goto done;
  1036. }
  1037. if (!priv->scan_cmd) {
  1038. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  1039. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  1040. if (!priv->scan_cmd) {
  1041. IWL_DEBUG_SCAN(priv,
  1042. "fail to allocate memory for scan\n");
  1043. goto done;
  1044. }
  1045. }
  1046. scan = priv->scan_cmd;
  1047. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  1048. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  1049. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  1050. if (iwl_is_any_associated(priv)) {
  1051. u16 interval = 0;
  1052. u32 extra;
  1053. u32 suspend_time = 100;
  1054. u32 scan_suspend_time = 100;
  1055. unsigned long flags;
  1056. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  1057. spin_lock_irqsave(&priv->lock, flags);
  1058. if (priv->is_internal_short_scan)
  1059. interval = 0;
  1060. else
  1061. interval = vif->bss_conf.beacon_int;
  1062. spin_unlock_irqrestore(&priv->lock, flags);
  1063. scan->suspend_time = 0;
  1064. scan->max_out_time = cpu_to_le32(200 * 1024);
  1065. if (!interval)
  1066. interval = suspend_time;
  1067. extra = (suspend_time / interval) << 22;
  1068. scan_suspend_time = (extra |
  1069. ((suspend_time % interval) * 1024));
  1070. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  1071. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  1072. scan_suspend_time, interval);
  1073. }
  1074. if (priv->is_internal_short_scan) {
  1075. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  1076. } else if (priv->scan_request->n_ssids) {
  1077. int i, p = 0;
  1078. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  1079. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  1080. /* always does wildcard anyway */
  1081. if (!priv->scan_request->ssids[i].ssid_len)
  1082. continue;
  1083. scan->direct_scan[p].id = WLAN_EID_SSID;
  1084. scan->direct_scan[p].len =
  1085. priv->scan_request->ssids[i].ssid_len;
  1086. memcpy(scan->direct_scan[p].ssid,
  1087. priv->scan_request->ssids[i].ssid,
  1088. priv->scan_request->ssids[i].ssid_len);
  1089. n_probes++;
  1090. p++;
  1091. }
  1092. is_active = true;
  1093. } else
  1094. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  1095. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  1096. scan->tx_cmd.sta_id = ctx->bcast_sta_id;
  1097. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1098. switch (priv->scan_band) {
  1099. case IEEE80211_BAND_2GHZ:
  1100. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  1101. chan_mod = le32_to_cpu(
  1102. priv->contexts[IWL_RXON_CTX_BSS].active.flags &
  1103. RXON_FLG_CHANNEL_MODE_MSK)
  1104. >> RXON_FLG_CHANNEL_MODE_POS;
  1105. if (chan_mod == CHANNEL_MODE_PURE_40) {
  1106. rate = IWL_RATE_6M_PLCP;
  1107. } else {
  1108. rate = IWL_RATE_1M_PLCP;
  1109. rate_flags = RATE_MCS_CCK_MSK;
  1110. }
  1111. /*
  1112. * Internal scans are passive, so we can indiscriminately set
  1113. * the BT ignore flag on 2.4 GHz since it applies to TX only.
  1114. */
  1115. if (priv->cfg->advanced_bt_coexist)
  1116. scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
  1117. scan->good_CRC_th = IWL_GOOD_CRC_TH_DISABLED;
  1118. break;
  1119. case IEEE80211_BAND_5GHZ:
  1120. rate = IWL_RATE_6M_PLCP;
  1121. /*
  1122. * If active scanning is requested but a certain channel is
  1123. * marked passive, we can do active scanning if we detect
  1124. * transmissions.
  1125. *
  1126. * There is an issue with some firmware versions that triggers
  1127. * a sysassert on a "good CRC threshold" of zero (== disabled),
  1128. * on a radar channel even though this means that we should NOT
  1129. * send probes.
  1130. *
  1131. * The "good CRC threshold" is the number of frames that we
  1132. * need to receive during our dwell time on a channel before
  1133. * sending out probes -- setting this to a huge value will
  1134. * mean we never reach it, but at the same time work around
  1135. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  1136. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  1137. */
  1138. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  1139. IWL_GOOD_CRC_TH_NEVER;
  1140. break;
  1141. default:
  1142. IWL_WARN(priv, "Invalid scan band count\n");
  1143. goto done;
  1144. }
  1145. band = priv->scan_band;
  1146. if (priv->cfg->scan_rx_antennas[band])
  1147. rx_ant = priv->cfg->scan_rx_antennas[band];
  1148. if (priv->cfg->scan_tx_antennas[band])
  1149. scan_tx_antennas = priv->cfg->scan_tx_antennas[band];
  1150. if (priv->cfg->advanced_bt_coexist && priv->bt_full_concurrent) {
  1151. /* operated as 1x1 in full concurrency mode */
  1152. scan_tx_antennas =
  1153. first_antenna(priv->cfg->scan_tx_antennas[band]);
  1154. }
  1155. priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
  1156. scan_tx_antennas);
  1157. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  1158. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  1159. /* In power save mode use one chain, otherwise use all chains */
  1160. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  1161. /* rx_ant has been set to all valid chains previously */
  1162. active_chains = rx_ant &
  1163. ((u8)(priv->chain_noise_data.active_chains));
  1164. if (!active_chains)
  1165. active_chains = rx_ant;
  1166. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  1167. priv->chain_noise_data.active_chains);
  1168. rx_ant = first_antenna(active_chains);
  1169. }
  1170. if (priv->cfg->advanced_bt_coexist && priv->bt_full_concurrent) {
  1171. /* operated as 1x1 in full concurrency mode */
  1172. rx_ant = first_antenna(rx_ant);
  1173. }
  1174. /* MIMO is not used here, but value is required */
  1175. rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  1176. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  1177. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  1178. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  1179. scan->rx_chain = cpu_to_le16(rx_chain);
  1180. if (!priv->is_internal_short_scan) {
  1181. cmd_len = iwl_fill_probe_req(priv,
  1182. (struct ieee80211_mgmt *)scan->data,
  1183. vif->addr,
  1184. priv->scan_request->ie,
  1185. priv->scan_request->ie_len,
  1186. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1187. } else {
  1188. /* use bcast addr, will not be transmitted but must be valid */
  1189. cmd_len = iwl_fill_probe_req(priv,
  1190. (struct ieee80211_mgmt *)scan->data,
  1191. iwl_bcast_addr, NULL, 0,
  1192. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1193. }
  1194. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  1195. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  1196. RXON_FILTER_BCON_AWARE_MSK);
  1197. if (priv->is_internal_short_scan) {
  1198. scan->channel_count =
  1199. iwl_get_single_channel_for_scan(priv, vif, band,
  1200. (void *)&scan->data[le16_to_cpu(
  1201. scan->tx_cmd.len)]);
  1202. } else {
  1203. scan->channel_count =
  1204. iwl_get_channels_for_scan(priv, vif, band,
  1205. is_active, n_probes,
  1206. (void *)&scan->data[le16_to_cpu(
  1207. scan->tx_cmd.len)]);
  1208. }
  1209. if (scan->channel_count == 0) {
  1210. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  1211. goto done;
  1212. }
  1213. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  1214. scan->channel_count * sizeof(struct iwl_scan_channel);
  1215. cmd.data = scan;
  1216. scan->len = cpu_to_le16(cmd.len);
  1217. set_bit(STATUS_SCAN_HW, &priv->status);
  1218. if (priv->cfg->ops->hcmd->set_pan_params &&
  1219. priv->cfg->ops->hcmd->set_pan_params(priv))
  1220. goto done;
  1221. if (iwl_send_cmd_sync(priv, &cmd))
  1222. goto done;
  1223. queue_delayed_work(priv->workqueue, &priv->scan_check,
  1224. IWL_SCAN_CHECK_WATCHDOG);
  1225. return;
  1226. done:
  1227. /* Cannot perform scan. Make sure we clear scanning
  1228. * bits from status so next scan request can be performed.
  1229. * If we don't clear scanning status bit here all next scan
  1230. * will fail
  1231. */
  1232. clear_bit(STATUS_SCAN_HW, &priv->status);
  1233. clear_bit(STATUS_SCANNING, &priv->status);
  1234. /* inform mac80211 scan aborted */
  1235. queue_work(priv->workqueue, &priv->scan_completed);
  1236. }
  1237. int iwlagn_manage_ibss_station(struct iwl_priv *priv,
  1238. struct ieee80211_vif *vif, bool add)
  1239. {
  1240. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1241. if (add)
  1242. return iwl_add_bssid_station(priv, vif_priv->ctx,
  1243. vif->bss_conf.bssid, true,
  1244. &vif_priv->ibss_bssid_sta_id);
  1245. return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
  1246. vif->bss_conf.bssid);
  1247. }
  1248. void iwl_free_tfds_in_queue(struct iwl_priv *priv,
  1249. int sta_id, int tid, int freed)
  1250. {
  1251. lockdep_assert_held(&priv->sta_lock);
  1252. if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  1253. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  1254. else {
  1255. IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
  1256. priv->stations[sta_id].tid[tid].tfds_in_queue,
  1257. freed);
  1258. priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
  1259. }
  1260. }
  1261. #define IWL_FLUSH_WAIT_MS 2000
  1262. int iwlagn_wait_tx_queue_empty(struct iwl_priv *priv)
  1263. {
  1264. struct iwl_tx_queue *txq;
  1265. struct iwl_queue *q;
  1266. int cnt;
  1267. unsigned long now = jiffies;
  1268. int ret = 0;
  1269. /* waiting for all the tx frames complete might take a while */
  1270. for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
  1271. if (cnt == priv->cmd_queue)
  1272. continue;
  1273. txq = &priv->txq[cnt];
  1274. q = &txq->q;
  1275. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1276. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1277. msleep(1);
  1278. if (q->read_ptr != q->write_ptr) {
  1279. IWL_ERR(priv, "fail to flush all tx fifo queues\n");
  1280. ret = -ETIMEDOUT;
  1281. break;
  1282. }
  1283. }
  1284. return ret;
  1285. }
  1286. #define IWL_TX_QUEUE_MSK 0xfffff
  1287. /**
  1288. * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
  1289. *
  1290. * pre-requirements:
  1291. * 1. acquire mutex before calling
  1292. * 2. make sure rf is on and not in exit state
  1293. */
  1294. int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  1295. {
  1296. struct iwl_txfifo_flush_cmd flush_cmd;
  1297. struct iwl_host_cmd cmd = {
  1298. .id = REPLY_TXFIFO_FLUSH,
  1299. .len = sizeof(struct iwl_txfifo_flush_cmd),
  1300. .flags = CMD_SYNC,
  1301. .data = &flush_cmd,
  1302. };
  1303. might_sleep();
  1304. memset(&flush_cmd, 0, sizeof(flush_cmd));
  1305. flush_cmd.fifo_control = IWL_TX_FIFO_VO_MSK | IWL_TX_FIFO_VI_MSK |
  1306. IWL_TX_FIFO_BE_MSK | IWL_TX_FIFO_BK_MSK;
  1307. if (priv->cfg->sku & IWL_SKU_N)
  1308. flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
  1309. IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
  1310. flush_cmd.fifo_control);
  1311. flush_cmd.flush_control = cpu_to_le16(flush_control);
  1312. return iwl_send_cmd(priv, &cmd);
  1313. }
  1314. void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  1315. {
  1316. mutex_lock(&priv->mutex);
  1317. ieee80211_stop_queues(priv->hw);
  1318. if (priv->cfg->ops->lib->txfifo_flush(priv, IWL_DROP_ALL)) {
  1319. IWL_ERR(priv, "flush request fail\n");
  1320. goto done;
  1321. }
  1322. IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
  1323. iwlagn_wait_tx_queue_empty(priv);
  1324. done:
  1325. ieee80211_wake_queues(priv->hw);
  1326. mutex_unlock(&priv->mutex);
  1327. }
  1328. /*
  1329. * BT coex
  1330. */
  1331. /*
  1332. * Macros to access the lookup table.
  1333. *
  1334. * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
  1335. * wifi_prio, wifi_txrx and wifi_sh_ant_req.
  1336. *
  1337. * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
  1338. *
  1339. * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
  1340. * one after another in 32-bit registers, and "registers" 0 through 7 contain
  1341. * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
  1342. *
  1343. * These macros encode that format.
  1344. */
  1345. #define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
  1346. wifi_txrx, wifi_sh_ant_req) \
  1347. (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
  1348. (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
  1349. #define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
  1350. lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
  1351. #define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1352. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1353. (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
  1354. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1355. wifi_sh_ant_req))))
  1356. #define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1357. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1358. LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
  1359. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1360. wifi_sh_ant_req))
  1361. #define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
  1362. wifi_req, wifi_prio, wifi_txrx, \
  1363. wifi_sh_ant_req) \
  1364. LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
  1365. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1366. wifi_sh_ant_req))
  1367. #define LUT_WLAN_KILL_OP(lut, op, val) \
  1368. lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
  1369. #define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1370. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1371. (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1372. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
  1373. #define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1374. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1375. LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1376. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1377. #define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1378. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1379. LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1380. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1381. #define LUT_ANT_SWITCH_OP(lut, op, val) \
  1382. lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
  1383. #define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1384. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1385. (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1386. wifi_req, wifi_prio, wifi_txrx, \
  1387. wifi_sh_ant_req))))
  1388. #define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1389. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1390. LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1391. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1392. #define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1393. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1394. LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1395. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1396. static const __le32 iwlagn_def_3w_lookup[12] = {
  1397. cpu_to_le32(0xaaaaaaaa),
  1398. cpu_to_le32(0xaaaaaaaa),
  1399. cpu_to_le32(0xaeaaaaaa),
  1400. cpu_to_le32(0xaaaaaaaa),
  1401. cpu_to_le32(0xcc00ff28),
  1402. cpu_to_le32(0x0000aaaa),
  1403. cpu_to_le32(0xcc00aaaa),
  1404. cpu_to_le32(0x0000aaaa),
  1405. cpu_to_le32(0xc0004000),
  1406. cpu_to_le32(0x00004000),
  1407. cpu_to_le32(0xf0005000),
  1408. cpu_to_le32(0xf0004000),
  1409. };
  1410. static const __le32 iwlagn_concurrent_lookup[12] = {
  1411. cpu_to_le32(0xaaaaaaaa),
  1412. cpu_to_le32(0xaaaaaaaa),
  1413. cpu_to_le32(0xaaaaaaaa),
  1414. cpu_to_le32(0xaaaaaaaa),
  1415. cpu_to_le32(0xaaaaaaaa),
  1416. cpu_to_le32(0xaaaaaaaa),
  1417. cpu_to_le32(0xaaaaaaaa),
  1418. cpu_to_le32(0xaaaaaaaa),
  1419. cpu_to_le32(0x00000000),
  1420. cpu_to_le32(0x00000000),
  1421. cpu_to_le32(0x00000000),
  1422. cpu_to_le32(0x00000000),
  1423. };
  1424. void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
  1425. {
  1426. struct iwlagn_bt_cmd bt_cmd = {
  1427. .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
  1428. .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
  1429. .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
  1430. .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
  1431. };
  1432. BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
  1433. sizeof(bt_cmd.bt3_lookup_table));
  1434. bt_cmd.prio_boost = priv->cfg->bt_prio_boost;
  1435. bt_cmd.kill_ack_mask = priv->kill_ack_mask;
  1436. bt_cmd.kill_cts_mask = priv->kill_cts_mask;
  1437. bt_cmd.valid = priv->bt_valid;
  1438. /*
  1439. * Configure BT coex mode to "no coexistence" when the
  1440. * user disabled BT coexistence, we have no interface
  1441. * (might be in monitor mode), or the interface is in
  1442. * IBSS mode (no proper uCode support for coex then).
  1443. */
  1444. if (!bt_coex_active || priv->iw_mode == NL80211_IFTYPE_ADHOC) {
  1445. bt_cmd.flags = 0;
  1446. } else {
  1447. bt_cmd.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
  1448. IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
  1449. if (priv->bt_ch_announce)
  1450. bt_cmd.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
  1451. IWL_DEBUG_INFO(priv, "BT coex flag: 0X%x\n", bt_cmd.flags);
  1452. }
  1453. if (priv->bt_full_concurrent)
  1454. memcpy(bt_cmd.bt3_lookup_table, iwlagn_concurrent_lookup,
  1455. sizeof(iwlagn_concurrent_lookup));
  1456. else
  1457. memcpy(bt_cmd.bt3_lookup_table, iwlagn_def_3w_lookup,
  1458. sizeof(iwlagn_def_3w_lookup));
  1459. IWL_DEBUG_INFO(priv, "BT coex %s in %s mode\n",
  1460. bt_cmd.flags ? "active" : "disabled",
  1461. priv->bt_full_concurrent ?
  1462. "full concurrency" : "3-wire");
  1463. if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG, sizeof(bt_cmd), &bt_cmd))
  1464. IWL_ERR(priv, "failed to send BT Coex Config\n");
  1465. /*
  1466. * When we are doing a restart, need to also reconfigure BT
  1467. * SCO to the device. If not doing a restart, bt_sco_active
  1468. * will always be false, so there's no need to have an extra
  1469. * variable to check for it.
  1470. */
  1471. if (priv->bt_sco_active) {
  1472. struct iwlagn_bt_sco_cmd sco_cmd = { .flags = 0 };
  1473. if (priv->bt_sco_active)
  1474. sco_cmd.flags |= IWLAGN_BT_SCO_ACTIVE;
  1475. if (iwl_send_cmd_pdu(priv, REPLY_BT_COEX_SCO,
  1476. sizeof(sco_cmd), &sco_cmd))
  1477. IWL_ERR(priv, "failed to send BT SCO command\n");
  1478. }
  1479. }
  1480. static void iwlagn_bt_traffic_change_work(struct work_struct *work)
  1481. {
  1482. struct iwl_priv *priv =
  1483. container_of(work, struct iwl_priv, bt_traffic_change_work);
  1484. struct iwl_rxon_context *ctx;
  1485. int smps_request = -1;
  1486. IWL_DEBUG_INFO(priv, "BT traffic load changes: %d\n",
  1487. priv->bt_traffic_load);
  1488. switch (priv->bt_traffic_load) {
  1489. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1490. smps_request = IEEE80211_SMPS_AUTOMATIC;
  1491. break;
  1492. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1493. smps_request = IEEE80211_SMPS_DYNAMIC;
  1494. break;
  1495. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1496. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1497. smps_request = IEEE80211_SMPS_STATIC;
  1498. break;
  1499. default:
  1500. IWL_ERR(priv, "Invalid BT traffic load: %d\n",
  1501. priv->bt_traffic_load);
  1502. break;
  1503. }
  1504. mutex_lock(&priv->mutex);
  1505. if (priv->cfg->ops->lib->update_chain_flags)
  1506. priv->cfg->ops->lib->update_chain_flags(priv);
  1507. if (smps_request != -1) {
  1508. for_each_context(priv, ctx) {
  1509. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
  1510. ieee80211_request_smps(ctx->vif, smps_request);
  1511. }
  1512. }
  1513. mutex_unlock(&priv->mutex);
  1514. }
  1515. static void iwlagn_print_uartmsg(struct iwl_priv *priv,
  1516. struct iwl_bt_uart_msg *uart_msg)
  1517. {
  1518. IWL_DEBUG_NOTIF(priv, "Message Type = 0x%X, SSN = 0x%X, "
  1519. "Update Req = 0x%X",
  1520. (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
  1521. BT_UART_MSG_FRAME1MSGTYPE_POS,
  1522. (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
  1523. BT_UART_MSG_FRAME1SSN_POS,
  1524. (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
  1525. BT_UART_MSG_FRAME1UPDATEREQ_POS);
  1526. IWL_DEBUG_NOTIF(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
  1527. "Chl_SeqN = 0x%X, In band = 0x%X",
  1528. (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
  1529. BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
  1530. (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
  1531. BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
  1532. (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
  1533. BT_UART_MSG_FRAME2CHLSEQN_POS,
  1534. (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
  1535. BT_UART_MSG_FRAME2INBAND_POS);
  1536. IWL_DEBUG_NOTIF(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
  1537. "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
  1538. (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
  1539. BT_UART_MSG_FRAME3SCOESCO_POS,
  1540. (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
  1541. BT_UART_MSG_FRAME3SNIFF_POS,
  1542. (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
  1543. BT_UART_MSG_FRAME3A2DP_POS,
  1544. (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
  1545. BT_UART_MSG_FRAME3ACL_POS,
  1546. (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
  1547. BT_UART_MSG_FRAME3MASTER_POS,
  1548. (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
  1549. BT_UART_MSG_FRAME3OBEX_POS);
  1550. IWL_DEBUG_NOTIF(priv, "Idle duration = 0x%X",
  1551. (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
  1552. BT_UART_MSG_FRAME4IDLEDURATION_POS);
  1553. IWL_DEBUG_NOTIF(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
  1554. "eSCO Retransmissions = 0x%X",
  1555. (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
  1556. BT_UART_MSG_FRAME5TXACTIVITY_POS,
  1557. (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
  1558. BT_UART_MSG_FRAME5RXACTIVITY_POS,
  1559. (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
  1560. BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
  1561. IWL_DEBUG_NOTIF(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
  1562. (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
  1563. BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
  1564. (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
  1565. BT_UART_MSG_FRAME6DISCOVERABLE_POS);
  1566. IWL_DEBUG_NOTIF(priv, "Sniff Activity = 0x%X, Inquiry/Page SR Mode = "
  1567. "0x%X, Connectable = 0x%X",
  1568. (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
  1569. BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
  1570. (BT_UART_MSG_FRAME7INQUIRYPAGESRMODE_MSK & uart_msg->frame7) >>
  1571. BT_UART_MSG_FRAME7INQUIRYPAGESRMODE_POS,
  1572. (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
  1573. BT_UART_MSG_FRAME7CONNECTABLE_POS);
  1574. }
  1575. static void iwlagn_set_kill_ack_msk(struct iwl_priv *priv,
  1576. struct iwl_bt_uart_msg *uart_msg)
  1577. {
  1578. u8 kill_ack_msk;
  1579. __le32 bt_kill_ack_msg[2] = {
  1580. cpu_to_le32(0xFFFFFFF), cpu_to_le32(0xFFFFFC00) };
  1581. kill_ack_msk = (((BT_UART_MSG_FRAME3A2DP_MSK |
  1582. BT_UART_MSG_FRAME3SNIFF_MSK |
  1583. BT_UART_MSG_FRAME3SCOESCO_MSK) &
  1584. uart_msg->frame3) == 0) ? 1 : 0;
  1585. if (priv->kill_ack_mask != bt_kill_ack_msg[kill_ack_msk]) {
  1586. priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
  1587. priv->kill_ack_mask = bt_kill_ack_msg[kill_ack_msk];
  1588. /* schedule to send runtime bt_config */
  1589. queue_work(priv->workqueue, &priv->bt_runtime_config);
  1590. }
  1591. }
  1592. void iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
  1593. struct iwl_rx_mem_buffer *rxb)
  1594. {
  1595. unsigned long flags;
  1596. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1597. struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
  1598. struct iwlagn_bt_sco_cmd sco_cmd = { .flags = 0 };
  1599. struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
  1600. u8 last_traffic_load;
  1601. IWL_DEBUG_NOTIF(priv, "BT Coex notification:\n");
  1602. IWL_DEBUG_NOTIF(priv, " status: %d\n", coex->bt_status);
  1603. IWL_DEBUG_NOTIF(priv, " traffic load: %d\n", coex->bt_traffic_load);
  1604. IWL_DEBUG_NOTIF(priv, " CI compliance: %d\n",
  1605. coex->bt_ci_compliance);
  1606. iwlagn_print_uartmsg(priv, uart_msg);
  1607. last_traffic_load = priv->notif_bt_traffic_load;
  1608. priv->notif_bt_traffic_load = coex->bt_traffic_load;
  1609. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  1610. if (priv->bt_status != coex->bt_status ||
  1611. last_traffic_load != coex->bt_traffic_load) {
  1612. if (coex->bt_status) {
  1613. /* BT on */
  1614. if (!priv->bt_ch_announce)
  1615. priv->bt_traffic_load =
  1616. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1617. else
  1618. priv->bt_traffic_load =
  1619. coex->bt_traffic_load;
  1620. } else {
  1621. /* BT off */
  1622. priv->bt_traffic_load =
  1623. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  1624. }
  1625. priv->bt_status = coex->bt_status;
  1626. queue_work(priv->workqueue,
  1627. &priv->bt_traffic_change_work);
  1628. }
  1629. if (priv->bt_sco_active !=
  1630. (uart_msg->frame3 & BT_UART_MSG_FRAME3SCOESCO_MSK)) {
  1631. priv->bt_sco_active = uart_msg->frame3 &
  1632. BT_UART_MSG_FRAME3SCOESCO_MSK;
  1633. if (priv->bt_sco_active)
  1634. sco_cmd.flags |= IWLAGN_BT_SCO_ACTIVE;
  1635. iwl_send_cmd_pdu_async(priv, REPLY_BT_COEX_SCO,
  1636. sizeof(sco_cmd), &sco_cmd, NULL);
  1637. }
  1638. }
  1639. iwlagn_set_kill_ack_msk(priv, uart_msg);
  1640. /* FIXME: based on notification, adjust the prio_boost */
  1641. spin_lock_irqsave(&priv->lock, flags);
  1642. priv->bt_ci_compliance = coex->bt_ci_compliance;
  1643. spin_unlock_irqrestore(&priv->lock, flags);
  1644. }
  1645. void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
  1646. {
  1647. iwlagn_rx_handler_setup(priv);
  1648. priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
  1649. iwlagn_bt_coex_profile_notif;
  1650. }
  1651. void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
  1652. {
  1653. iwlagn_setup_deferred_work(priv);
  1654. INIT_WORK(&priv->bt_traffic_change_work,
  1655. iwlagn_bt_traffic_change_work);
  1656. }
  1657. void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
  1658. {
  1659. cancel_work_sync(&priv->bt_traffic_change_work);
  1660. }