iwl-agn-lib.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. static inline u32 iwlagn_get_scd_ssn(struct iwl5000_tx_resp *tx_resp)
  41. {
  42. return le32_to_cpup((__le32 *)&tx_resp->status +
  43. tx_resp->frame_count) & MAX_SN;
  44. }
  45. static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
  46. struct iwl_ht_agg *agg,
  47. struct iwl5000_tx_resp *tx_resp,
  48. int txq_id, u16 start_idx)
  49. {
  50. u16 status;
  51. struct agg_tx_status *frame_status = &tx_resp->status;
  52. struct ieee80211_tx_info *info = NULL;
  53. struct ieee80211_hdr *hdr = NULL;
  54. u32 rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  55. int i, sh, idx;
  56. u16 seq;
  57. if (agg->wait_for_ba)
  58. IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
  59. agg->frame_count = tx_resp->frame_count;
  60. agg->start_idx = start_idx;
  61. agg->rate_n_flags = rate_n_flags;
  62. agg->bitmap = 0;
  63. /* # frames attempted by Tx command */
  64. if (agg->frame_count == 1) {
  65. /* Only one frame was attempted; no block-ack will arrive */
  66. status = le16_to_cpu(frame_status[0].status);
  67. idx = start_idx;
  68. /* FIXME: code repetition */
  69. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
  70. agg->frame_count, agg->start_idx, idx);
  71. info = IEEE80211_SKB_CB(priv->txq[txq_id].txb[idx].skb[0]);
  72. info->status.rates[0].count = tx_resp->failure_frame + 1;
  73. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  74. info->flags |= iwl_tx_status_to_mac80211(status);
  75. iwlagn_hwrate_to_tx_control(priv, rate_n_flags, info);
  76. /* FIXME: code repetition end */
  77. IWL_DEBUG_TX_REPLY(priv, "1 Frame 0x%x failure :%d\n",
  78. status & 0xff, tx_resp->failure_frame);
  79. IWL_DEBUG_TX_REPLY(priv, "Rate Info rate_n_flags=%x\n", rate_n_flags);
  80. agg->wait_for_ba = 0;
  81. } else {
  82. /* Two or more frames were attempted; expect block-ack */
  83. u64 bitmap = 0;
  84. int start = agg->start_idx;
  85. /* Construct bit-map of pending frames within Tx window */
  86. for (i = 0; i < agg->frame_count; i++) {
  87. u16 sc;
  88. status = le16_to_cpu(frame_status[i].status);
  89. seq = le16_to_cpu(frame_status[i].sequence);
  90. idx = SEQ_TO_INDEX(seq);
  91. txq_id = SEQ_TO_QUEUE(seq);
  92. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  93. AGG_TX_STATE_ABORT_MSK))
  94. continue;
  95. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
  96. agg->frame_count, txq_id, idx);
  97. hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
  98. if (!hdr) {
  99. IWL_ERR(priv,
  100. "BUG_ON idx doesn't point to valid skb"
  101. " idx=%d, txq_id=%d\n", idx, txq_id);
  102. return -1;
  103. }
  104. sc = le16_to_cpu(hdr->seq_ctrl);
  105. if (idx != (SEQ_TO_SN(sc) & 0xff)) {
  106. IWL_ERR(priv,
  107. "BUG_ON idx doesn't match seq control"
  108. " idx=%d, seq_idx=%d, seq=%d\n",
  109. idx, SEQ_TO_SN(sc),
  110. hdr->seq_ctrl);
  111. return -1;
  112. }
  113. IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
  114. i, idx, SEQ_TO_SN(sc));
  115. sh = idx - start;
  116. if (sh > 64) {
  117. sh = (start - idx) + 0xff;
  118. bitmap = bitmap << sh;
  119. sh = 0;
  120. start = idx;
  121. } else if (sh < -64)
  122. sh = 0xff - (start - idx);
  123. else if (sh < 0) {
  124. sh = start - idx;
  125. start = idx;
  126. bitmap = bitmap << sh;
  127. sh = 0;
  128. }
  129. bitmap |= 1ULL << sh;
  130. IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
  131. start, (unsigned long long)bitmap);
  132. }
  133. agg->bitmap = bitmap;
  134. agg->start_idx = start;
  135. IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
  136. agg->frame_count, agg->start_idx,
  137. (unsigned long long)agg->bitmap);
  138. if (bitmap)
  139. agg->wait_for_ba = 1;
  140. }
  141. return 0;
  142. }
  143. void iwl_check_abort_status(struct iwl_priv *priv,
  144. u8 frame_count, u32 status)
  145. {
  146. if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
  147. IWL_ERR(priv, "TODO: Implement Tx flush command!!!\n");
  148. }
  149. }
  150. static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
  151. struct iwl_rx_mem_buffer *rxb)
  152. {
  153. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  154. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  155. int txq_id = SEQ_TO_QUEUE(sequence);
  156. int index = SEQ_TO_INDEX(sequence);
  157. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  158. struct ieee80211_tx_info *info;
  159. struct iwl5000_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  160. u32 status = le16_to_cpu(tx_resp->status.status);
  161. int tid;
  162. int sta_id;
  163. int freed;
  164. if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
  165. IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
  166. "is out of range [0-%d] %d %d\n", txq_id,
  167. index, txq->q.n_bd, txq->q.write_ptr,
  168. txq->q.read_ptr);
  169. return;
  170. }
  171. info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
  172. memset(&info->status, 0, sizeof(info->status));
  173. tid = (tx_resp->ra_tid & IWL50_TX_RES_TID_MSK) >> IWL50_TX_RES_TID_POS;
  174. sta_id = (tx_resp->ra_tid & IWL50_TX_RES_RA_MSK) >> IWL50_TX_RES_RA_POS;
  175. if (txq->sched_retry) {
  176. const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
  177. struct iwl_ht_agg *agg = NULL;
  178. agg = &priv->stations[sta_id].tid[tid].agg;
  179. iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  180. /* check if BAR is needed */
  181. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
  182. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  183. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  184. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  185. IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
  186. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  187. scd_ssn , index, txq_id, txq->swq_id);
  188. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  189. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  190. if (priv->mac80211_registered &&
  191. (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  192. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
  193. if (agg->state == IWL_AGG_OFF)
  194. iwl_wake_queue(priv, txq_id);
  195. else
  196. iwl_wake_queue(priv, txq->swq_id);
  197. }
  198. }
  199. } else {
  200. BUG_ON(txq_id != txq->swq_id);
  201. info->status.rates[0].count = tx_resp->failure_frame + 1;
  202. info->flags |= iwl_tx_status_to_mac80211(status);
  203. iwlagn_hwrate_to_tx_control(priv,
  204. le32_to_cpu(tx_resp->rate_n_flags),
  205. info);
  206. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
  207. "0x%x retries %d\n",
  208. txq_id,
  209. iwl_get_tx_fail_reason(status), status,
  210. le32_to_cpu(tx_resp->rate_n_flags),
  211. tx_resp->failure_frame);
  212. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  213. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  214. if (priv->mac80211_registered &&
  215. (iwl_queue_space(&txq->q) > txq->q.low_mark))
  216. iwl_wake_queue(priv, txq_id);
  217. }
  218. iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
  219. iwl_check_abort_status(priv, tx_resp->frame_count, status);
  220. }
  221. void iwlagn_rx_handler_setup(struct iwl_priv *priv)
  222. {
  223. /* init calibration handlers */
  224. priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
  225. iwlagn_rx_calib_result;
  226. priv->rx_handlers[CALIBRATION_COMPLETE_NOTIFICATION] =
  227. iwlagn_rx_calib_complete;
  228. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  229. }
  230. void iwlagn_setup_deferred_work(struct iwl_priv *priv)
  231. {
  232. /* in agn, the tx power calibration is done in uCode */
  233. priv->disable_tx_power_cal = 1;
  234. }
  235. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  236. {
  237. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  238. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  239. }
  240. int iwlagn_send_tx_power(struct iwl_priv *priv)
  241. {
  242. struct iwl5000_tx_power_dbm_cmd tx_power_cmd;
  243. u8 tx_ant_cfg_cmd;
  244. /* half dBm need to multiply */
  245. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  246. if (priv->tx_power_lmt_in_half_dbm &&
  247. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  248. /*
  249. * For the newer devices which using enhanced/extend tx power
  250. * table in EEPROM, the format is in half dBm. driver need to
  251. * convert to dBm format before report to mac80211.
  252. * By doing so, there is a possibility of 1/2 dBm resolution
  253. * lost. driver will perform "round-up" operation before
  254. * reporting, but it will cause 1/2 dBm tx power over the
  255. * regulatory limit. Perform the checking here, if the
  256. * "tx_power_user_lmt" is higher than EEPROM value (in
  257. * half-dBm format), lower the tx power based on EEPROM
  258. */
  259. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  260. }
  261. tx_power_cmd.flags = IWL50_TX_POWER_NO_CLOSED;
  262. tx_power_cmd.srv_chan_lmt = IWL50_TX_POWER_AUTO;
  263. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  264. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  265. else
  266. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  267. return iwl_send_cmd_pdu_async(priv, tx_ant_cfg_cmd,
  268. sizeof(tx_power_cmd), &tx_power_cmd,
  269. NULL);
  270. }
  271. void iwlagn_temperature(struct iwl_priv *priv)
  272. {
  273. /* store temperature from statistics (in Celsius) */
  274. priv->temperature = le32_to_cpu(priv->statistics.general.temperature);
  275. iwl_tt_handler(priv);
  276. }
  277. u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
  278. {
  279. struct iwl_eeprom_calib_hdr {
  280. u8 version;
  281. u8 pa_type;
  282. u16 voltage;
  283. } *hdr;
  284. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  285. EEPROM_CALIB_ALL);
  286. return hdr->version;
  287. }
  288. /*
  289. * EEPROM
  290. */
  291. static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
  292. {
  293. u16 offset = 0;
  294. if ((address & INDIRECT_ADDRESS) == 0)
  295. return address;
  296. switch (address & INDIRECT_TYPE_MSK) {
  297. case INDIRECT_HOST:
  298. offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
  299. break;
  300. case INDIRECT_GENERAL:
  301. offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
  302. break;
  303. case INDIRECT_REGULATORY:
  304. offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
  305. break;
  306. case INDIRECT_CALIBRATION:
  307. offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
  308. break;
  309. case INDIRECT_PROCESS_ADJST:
  310. offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
  311. break;
  312. case INDIRECT_OTHERS:
  313. offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
  314. break;
  315. default:
  316. IWL_ERR(priv, "illegal indirect type: 0x%X\n",
  317. address & INDIRECT_TYPE_MSK);
  318. break;
  319. }
  320. /* translate the offset from words to byte */
  321. return (address & ADDRESS_MSK) + (offset << 1);
  322. }
  323. const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
  324. size_t offset)
  325. {
  326. u32 address = eeprom_indirect_address(priv, offset);
  327. BUG_ON(address >= priv->cfg->eeprom_size);
  328. return &priv->eeprom[address];
  329. }
  330. struct iwl_mod_params iwlagn_mod_params = {
  331. .amsdu_size_8K = 1,
  332. .restart_fw = 1,
  333. /* the rest are 0 by default */
  334. };
  335. void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  336. {
  337. unsigned long flags;
  338. int i;
  339. spin_lock_irqsave(&rxq->lock, flags);
  340. INIT_LIST_HEAD(&rxq->rx_free);
  341. INIT_LIST_HEAD(&rxq->rx_used);
  342. /* Fill the rx_used queue with _all_ of the Rx buffers */
  343. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  344. /* In the reset function, these buffers may have been allocated
  345. * to an SKB, so we need to unmap and free potential storage */
  346. if (rxq->pool[i].page != NULL) {
  347. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  348. PAGE_SIZE << priv->hw_params.rx_page_order,
  349. PCI_DMA_FROMDEVICE);
  350. __iwl_free_pages(priv, rxq->pool[i].page);
  351. rxq->pool[i].page = NULL;
  352. }
  353. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  354. }
  355. for (i = 0; i < RX_QUEUE_SIZE; i++)
  356. rxq->queue[i] = NULL;
  357. /* Set us so that we have processed and used all buffers, but have
  358. * not restocked the Rx queue with fresh buffers */
  359. rxq->read = rxq->write = 0;
  360. rxq->write_actual = 0;
  361. rxq->free_count = 0;
  362. spin_unlock_irqrestore(&rxq->lock, flags);
  363. }
  364. int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  365. {
  366. u32 rb_size;
  367. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  368. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  369. if (!priv->cfg->use_isr_legacy)
  370. rb_timeout = RX_RB_TIMEOUT;
  371. if (priv->cfg->mod_params->amsdu_size_8K)
  372. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  373. else
  374. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  375. /* Stop Rx DMA */
  376. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  377. /* Reset driver's Rx queue write index */
  378. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  379. /* Tell device where to find RBD circular buffer in DRAM */
  380. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  381. (u32)(rxq->dma_addr >> 8));
  382. /* Tell device where in DRAM to update its Rx status */
  383. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  384. rxq->rb_stts_dma >> 4);
  385. /* Enable Rx DMA
  386. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  387. * the credit mechanism in 5000 HW RX FIFO
  388. * Direct rx interrupts to hosts
  389. * Rx buffer size 4 or 8k
  390. * RB timeout 0x10
  391. * 256 RBDs
  392. */
  393. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  394. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  395. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  396. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  397. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  398. rb_size|
  399. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  400. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  401. /* Set interrupt coalescing timer to default (2048 usecs) */
  402. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  403. return 0;
  404. }
  405. int iwlagn_hw_nic_init(struct iwl_priv *priv)
  406. {
  407. unsigned long flags;
  408. struct iwl_rx_queue *rxq = &priv->rxq;
  409. int ret;
  410. /* nic_init */
  411. spin_lock_irqsave(&priv->lock, flags);
  412. priv->cfg->ops->lib->apm_ops.init(priv);
  413. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  414. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  415. spin_unlock_irqrestore(&priv->lock, flags);
  416. ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
  417. priv->cfg->ops->lib->apm_ops.config(priv);
  418. /* Allocate the RX queue, or reset if it is already allocated */
  419. if (!rxq->bd) {
  420. ret = iwl_rx_queue_alloc(priv);
  421. if (ret) {
  422. IWL_ERR(priv, "Unable to initialize Rx queue\n");
  423. return -ENOMEM;
  424. }
  425. } else
  426. iwlagn_rx_queue_reset(priv, rxq);
  427. iwlagn_rx_replenish(priv);
  428. iwlagn_rx_init(priv, rxq);
  429. spin_lock_irqsave(&priv->lock, flags);
  430. rxq->need_update = 1;
  431. iwl_rx_queue_update_write_ptr(priv, rxq);
  432. spin_unlock_irqrestore(&priv->lock, flags);
  433. /* Allocate or reset and init all Tx and Command queues */
  434. if (!priv->txq) {
  435. ret = iwlagn_txq_ctx_alloc(priv);
  436. if (ret)
  437. return ret;
  438. } else
  439. iwlagn_txq_ctx_reset(priv);
  440. set_bit(STATUS_INIT, &priv->status);
  441. return 0;
  442. }
  443. /**
  444. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  445. */
  446. static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
  447. dma_addr_t dma_addr)
  448. {
  449. return cpu_to_le32((u32)(dma_addr >> 8));
  450. }
  451. /**
  452. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  453. *
  454. * If there are slots in the RX queue that need to be restocked,
  455. * and we have free pre-allocated buffers, fill the ranks as much
  456. * as we can, pulling from rx_free.
  457. *
  458. * This moves the 'write' index forward to catch up with 'processed', and
  459. * also updates the memory address in the firmware to reference the new
  460. * target buffer.
  461. */
  462. void iwlagn_rx_queue_restock(struct iwl_priv *priv)
  463. {
  464. struct iwl_rx_queue *rxq = &priv->rxq;
  465. struct list_head *element;
  466. struct iwl_rx_mem_buffer *rxb;
  467. unsigned long flags;
  468. spin_lock_irqsave(&rxq->lock, flags);
  469. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  470. /* The overwritten rxb must be a used one */
  471. rxb = rxq->queue[rxq->write];
  472. BUG_ON(rxb && rxb->page);
  473. /* Get next free Rx buffer, remove from free list */
  474. element = rxq->rx_free.next;
  475. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  476. list_del(element);
  477. /* Point to Rx buffer via next RBD in circular buffer */
  478. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
  479. rxb->page_dma);
  480. rxq->queue[rxq->write] = rxb;
  481. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  482. rxq->free_count--;
  483. }
  484. spin_unlock_irqrestore(&rxq->lock, flags);
  485. /* If the pre-allocated buffer pool is dropping low, schedule to
  486. * refill it */
  487. if (rxq->free_count <= RX_LOW_WATERMARK)
  488. queue_work(priv->workqueue, &priv->rx_replenish);
  489. /* If we've added more space for the firmware to place data, tell it.
  490. * Increment device's write pointer in multiples of 8. */
  491. if (rxq->write_actual != (rxq->write & ~0x7)) {
  492. spin_lock_irqsave(&rxq->lock, flags);
  493. rxq->need_update = 1;
  494. spin_unlock_irqrestore(&rxq->lock, flags);
  495. iwl_rx_queue_update_write_ptr(priv, rxq);
  496. }
  497. }
  498. /**
  499. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  500. *
  501. * When moving to rx_free an SKB is allocated for the slot.
  502. *
  503. * Also restock the Rx queue via iwl_rx_queue_restock.
  504. * This is called as a scheduled work item (except for during initialization)
  505. */
  506. void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  507. {
  508. struct iwl_rx_queue *rxq = &priv->rxq;
  509. struct list_head *element;
  510. struct iwl_rx_mem_buffer *rxb;
  511. struct page *page;
  512. unsigned long flags;
  513. gfp_t gfp_mask = priority;
  514. while (1) {
  515. spin_lock_irqsave(&rxq->lock, flags);
  516. if (list_empty(&rxq->rx_used)) {
  517. spin_unlock_irqrestore(&rxq->lock, flags);
  518. return;
  519. }
  520. spin_unlock_irqrestore(&rxq->lock, flags);
  521. if (rxq->free_count > RX_LOW_WATERMARK)
  522. gfp_mask |= __GFP_NOWARN;
  523. if (priv->hw_params.rx_page_order > 0)
  524. gfp_mask |= __GFP_COMP;
  525. /* Alloc a new receive buffer */
  526. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  527. if (!page) {
  528. if (net_ratelimit())
  529. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  530. "order: %d\n",
  531. priv->hw_params.rx_page_order);
  532. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  533. net_ratelimit())
  534. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  535. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  536. rxq->free_count);
  537. /* We don't reschedule replenish work here -- we will
  538. * call the restock method and if it still needs
  539. * more buffers it will schedule replenish */
  540. return;
  541. }
  542. spin_lock_irqsave(&rxq->lock, flags);
  543. if (list_empty(&rxq->rx_used)) {
  544. spin_unlock_irqrestore(&rxq->lock, flags);
  545. __free_pages(page, priv->hw_params.rx_page_order);
  546. return;
  547. }
  548. element = rxq->rx_used.next;
  549. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  550. list_del(element);
  551. spin_unlock_irqrestore(&rxq->lock, flags);
  552. BUG_ON(rxb->page);
  553. rxb->page = page;
  554. /* Get physical address of the RB */
  555. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  556. PAGE_SIZE << priv->hw_params.rx_page_order,
  557. PCI_DMA_FROMDEVICE);
  558. /* dma address must be no more than 36 bits */
  559. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  560. /* and also 256 byte aligned! */
  561. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  562. spin_lock_irqsave(&rxq->lock, flags);
  563. list_add_tail(&rxb->list, &rxq->rx_free);
  564. rxq->free_count++;
  565. priv->alloc_rxb_page++;
  566. spin_unlock_irqrestore(&rxq->lock, flags);
  567. }
  568. }
  569. void iwlagn_rx_replenish(struct iwl_priv *priv)
  570. {
  571. unsigned long flags;
  572. iwlagn_rx_allocate(priv, GFP_KERNEL);
  573. spin_lock_irqsave(&priv->lock, flags);
  574. iwlagn_rx_queue_restock(priv);
  575. spin_unlock_irqrestore(&priv->lock, flags);
  576. }
  577. void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  578. {
  579. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  580. iwlagn_rx_queue_restock(priv);
  581. }
  582. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  583. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  584. * This free routine walks the list of POOL entries and if SKB is set to
  585. * non NULL it is unmapped and freed
  586. */
  587. void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  588. {
  589. int i;
  590. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  591. if (rxq->pool[i].page != NULL) {
  592. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  593. PAGE_SIZE << priv->hw_params.rx_page_order,
  594. PCI_DMA_FROMDEVICE);
  595. __iwl_free_pages(priv, rxq->pool[i].page);
  596. rxq->pool[i].page = NULL;
  597. }
  598. }
  599. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  600. rxq->dma_addr);
  601. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  602. rxq->rb_stts, rxq->rb_stts_dma);
  603. rxq->bd = NULL;
  604. rxq->rb_stts = NULL;
  605. }
  606. int iwlagn_rxq_stop(struct iwl_priv *priv)
  607. {
  608. /* stop Rx DMA */
  609. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  610. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  611. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  612. return 0;
  613. }
  614. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  615. {
  616. int idx = 0;
  617. int band_offset = 0;
  618. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  619. if (rate_n_flags & RATE_MCS_HT_MSK) {
  620. idx = (rate_n_flags & 0xff);
  621. return idx;
  622. /* Legacy rate format, search for match in table */
  623. } else {
  624. if (band == IEEE80211_BAND_5GHZ)
  625. band_offset = IWL_FIRST_OFDM_RATE;
  626. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  627. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  628. return idx - band_offset;
  629. }
  630. return -1;
  631. }
  632. /* Calc max signal level (dBm) among 3 possible receivers */
  633. static inline int iwlagn_calc_rssi(struct iwl_priv *priv,
  634. struct iwl_rx_phy_res *rx_resp)
  635. {
  636. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  637. }
  638. #ifdef CONFIG_IWLWIFI_DEBUG
  639. /**
  640. * iwlagn_dbg_report_frame - dump frame to syslog during debug sessions
  641. *
  642. * You may hack this function to show different aspects of received frames,
  643. * including selective frame dumps.
  644. * group100 parameter selects whether to show 1 out of 100 good data frames.
  645. * All beacon and probe response frames are printed.
  646. */
  647. static void iwlagn_dbg_report_frame(struct iwl_priv *priv,
  648. struct iwl_rx_phy_res *phy_res, u16 length,
  649. struct ieee80211_hdr *header, int group100)
  650. {
  651. u32 to_us;
  652. u32 print_summary = 0;
  653. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  654. u32 hundred = 0;
  655. u32 dataframe = 0;
  656. __le16 fc;
  657. u16 seq_ctl;
  658. u16 channel;
  659. u16 phy_flags;
  660. u32 rate_n_flags;
  661. u32 tsf_low;
  662. int rssi;
  663. if (likely(!(iwl_get_debug_level(priv) & IWL_DL_RX)))
  664. return;
  665. /* MAC header */
  666. fc = header->frame_control;
  667. seq_ctl = le16_to_cpu(header->seq_ctrl);
  668. /* metadata */
  669. channel = le16_to_cpu(phy_res->channel);
  670. phy_flags = le16_to_cpu(phy_res->phy_flags);
  671. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  672. /* signal statistics */
  673. rssi = iwlagn_calc_rssi(priv, phy_res);
  674. tsf_low = le64_to_cpu(phy_res->timestamp) & 0x0ffffffff;
  675. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  676. /* if data frame is to us and all is good,
  677. * (optionally) print summary for only 1 out of every 100 */
  678. if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
  679. cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  680. dataframe = 1;
  681. if (!group100)
  682. print_summary = 1; /* print each frame */
  683. else if (priv->framecnt_to_us < 100) {
  684. priv->framecnt_to_us++;
  685. print_summary = 0;
  686. } else {
  687. priv->framecnt_to_us = 0;
  688. print_summary = 1;
  689. hundred = 1;
  690. }
  691. } else {
  692. /* print summary for all other frames */
  693. print_summary = 1;
  694. }
  695. if (print_summary) {
  696. char *title;
  697. int rate_idx;
  698. u32 bitrate;
  699. if (hundred)
  700. title = "100Frames";
  701. else if (ieee80211_has_retry(fc))
  702. title = "Retry";
  703. else if (ieee80211_is_assoc_resp(fc))
  704. title = "AscRsp";
  705. else if (ieee80211_is_reassoc_resp(fc))
  706. title = "RasRsp";
  707. else if (ieee80211_is_probe_resp(fc)) {
  708. title = "PrbRsp";
  709. print_dump = 1; /* dump frame contents */
  710. } else if (ieee80211_is_beacon(fc)) {
  711. title = "Beacon";
  712. print_dump = 1; /* dump frame contents */
  713. } else if (ieee80211_is_atim(fc))
  714. title = "ATIM";
  715. else if (ieee80211_is_auth(fc))
  716. title = "Auth";
  717. else if (ieee80211_is_deauth(fc))
  718. title = "DeAuth";
  719. else if (ieee80211_is_disassoc(fc))
  720. title = "DisAssoc";
  721. else
  722. title = "Frame";
  723. rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
  724. if (unlikely((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT))) {
  725. bitrate = 0;
  726. WARN_ON_ONCE(1);
  727. } else {
  728. bitrate = iwl_rates[rate_idx].ieee / 2;
  729. }
  730. /* print frame summary.
  731. * MAC addresses show just the last byte (for brevity),
  732. * but you can hack it to show more, if you'd like to. */
  733. if (dataframe)
  734. IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
  735. "len=%u, rssi=%d, chnl=%d, rate=%u,\n",
  736. title, le16_to_cpu(fc), header->addr1[5],
  737. length, rssi, channel, bitrate);
  738. else {
  739. /* src/dst addresses assume managed mode */
  740. IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, src=0x%02x, "
  741. "len=%u, rssi=%d, tim=%lu usec, "
  742. "phy=0x%02x, chnl=%d\n",
  743. title, le16_to_cpu(fc), header->addr1[5],
  744. header->addr3[5], length, rssi,
  745. tsf_low - priv->scan_start_tsf,
  746. phy_flags, channel);
  747. }
  748. }
  749. if (print_dump)
  750. iwl_print_hex_dump(priv, IWL_DL_RX, header, length);
  751. }
  752. #endif
  753. static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  754. {
  755. u32 decrypt_out = 0;
  756. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  757. RX_RES_STATUS_STATION_FOUND)
  758. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  759. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  760. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  761. /* packet was not encrypted */
  762. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  763. RX_RES_STATUS_SEC_TYPE_NONE)
  764. return decrypt_out;
  765. /* packet was encrypted with unknown alg */
  766. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  767. RX_RES_STATUS_SEC_TYPE_ERR)
  768. return decrypt_out;
  769. /* decryption was not done in HW */
  770. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  771. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  772. return decrypt_out;
  773. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  774. case RX_RES_STATUS_SEC_TYPE_CCMP:
  775. /* alg is CCM: check MIC only */
  776. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  777. /* Bad MIC */
  778. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  779. else
  780. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  781. break;
  782. case RX_RES_STATUS_SEC_TYPE_TKIP:
  783. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  784. /* Bad TTAK */
  785. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  786. break;
  787. }
  788. /* fall through if TTAK OK */
  789. default:
  790. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  791. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  792. else
  793. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  794. break;
  795. };
  796. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  797. decrypt_in, decrypt_out);
  798. return decrypt_out;
  799. }
  800. static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
  801. struct ieee80211_hdr *hdr,
  802. u16 len,
  803. u32 ampdu_status,
  804. struct iwl_rx_mem_buffer *rxb,
  805. struct ieee80211_rx_status *stats)
  806. {
  807. struct sk_buff *skb;
  808. __le16 fc = hdr->frame_control;
  809. /* We only process data packets if the interface is open */
  810. if (unlikely(!priv->is_open)) {
  811. IWL_DEBUG_DROP_LIMIT(priv,
  812. "Dropping packet while interface is not open.\n");
  813. return;
  814. }
  815. /* In case of HW accelerated crypto and bad decryption, drop */
  816. if (!priv->cfg->mod_params->sw_crypto &&
  817. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  818. return;
  819. skb = dev_alloc_skb(128);
  820. if (!skb) {
  821. IWL_ERR(priv, "dev_alloc_skb failed\n");
  822. return;
  823. }
  824. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
  825. iwl_update_stats(priv, false, fc, len);
  826. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  827. ieee80211_rx(priv->hw, skb);
  828. priv->alloc_rxb_page--;
  829. rxb->page = NULL;
  830. }
  831. /* Called for REPLY_RX (legacy ABG frames), or
  832. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  833. void iwlagn_rx_reply_rx(struct iwl_priv *priv,
  834. struct iwl_rx_mem_buffer *rxb)
  835. {
  836. struct ieee80211_hdr *header;
  837. struct ieee80211_rx_status rx_status;
  838. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  839. struct iwl_rx_phy_res *phy_res;
  840. __le32 rx_pkt_status;
  841. struct iwl4965_rx_mpdu_res_start *amsdu;
  842. u32 len;
  843. u32 ampdu_status;
  844. u32 rate_n_flags;
  845. /**
  846. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  847. * REPLY_RX: physical layer info is in this buffer
  848. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  849. * command and cached in priv->last_phy_res
  850. *
  851. * Here we set up local variables depending on which command is
  852. * received.
  853. */
  854. if (pkt->hdr.cmd == REPLY_RX) {
  855. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  856. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  857. + phy_res->cfg_phy_cnt);
  858. len = le16_to_cpu(phy_res->byte_count);
  859. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  860. phy_res->cfg_phy_cnt + len);
  861. ampdu_status = le32_to_cpu(rx_pkt_status);
  862. } else {
  863. if (!priv->_agn.last_phy_res_valid) {
  864. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  865. return;
  866. }
  867. phy_res = &priv->_agn.last_phy_res;
  868. amsdu = (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  869. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  870. len = le16_to_cpu(amsdu->byte_count);
  871. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  872. ampdu_status = iwlagn_translate_rx_status(priv,
  873. le32_to_cpu(rx_pkt_status));
  874. }
  875. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  876. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  877. phy_res->cfg_phy_cnt);
  878. return;
  879. }
  880. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  881. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  882. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  883. le32_to_cpu(rx_pkt_status));
  884. return;
  885. }
  886. /* This will be used in several places later */
  887. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  888. /* rx_status carries information about the packet to mac80211 */
  889. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  890. rx_status.freq =
  891. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
  892. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  893. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  894. rx_status.rate_idx =
  895. iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  896. rx_status.flag = 0;
  897. /* TSF isn't reliable. In order to allow smooth user experience,
  898. * this W/A doesn't propagate it to the mac80211 */
  899. /*rx_status.flag |= RX_FLAG_TSFT;*/
  900. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  901. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  902. rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
  903. #ifdef CONFIG_IWLWIFI_DEBUG
  904. /* Set "1" to report good data frames in groups of 100 */
  905. if (unlikely(iwl_get_debug_level(priv) & IWL_DL_RX))
  906. iwlagn_dbg_report_frame(priv, phy_res, len, header, 1);
  907. #endif
  908. iwl_dbg_log_rx_data_frame(priv, len, header);
  909. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
  910. rx_status.signal, (unsigned long long)rx_status.mactime);
  911. /*
  912. * "antenna number"
  913. *
  914. * It seems that the antenna field in the phy flags value
  915. * is actually a bit field. This is undefined by radiotap,
  916. * it wants an actual antenna number but I always get "7"
  917. * for most legacy frames I receive indicating that the
  918. * same frame was received on all three RX chains.
  919. *
  920. * I think this field should be removed in favor of a
  921. * new 802.11n radiotap field "RX chains" that is defined
  922. * as a bitmask.
  923. */
  924. rx_status.antenna =
  925. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  926. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  927. /* set the preamble flag if appropriate */
  928. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  929. rx_status.flag |= RX_FLAG_SHORTPRE;
  930. /* Set up the HT phy flags */
  931. if (rate_n_flags & RATE_MCS_HT_MSK)
  932. rx_status.flag |= RX_FLAG_HT;
  933. if (rate_n_flags & RATE_MCS_HT40_MSK)
  934. rx_status.flag |= RX_FLAG_40MHZ;
  935. if (rate_n_flags & RATE_MCS_SGI_MSK)
  936. rx_status.flag |= RX_FLAG_SHORT_GI;
  937. iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  938. rxb, &rx_status);
  939. }
  940. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  941. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  942. void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
  943. struct iwl_rx_mem_buffer *rxb)
  944. {
  945. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  946. priv->_agn.last_phy_res_valid = true;
  947. memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
  948. sizeof(struct iwl_rx_phy_res));
  949. }
  950. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  951. enum ieee80211_band band,
  952. struct iwl_scan_channel *scan_ch)
  953. {
  954. const struct ieee80211_supported_band *sband;
  955. const struct iwl_channel_info *ch_info;
  956. u16 passive_dwell = 0;
  957. u16 active_dwell = 0;
  958. int i, added = 0;
  959. u16 channel = 0;
  960. sband = iwl_get_hw_mode(priv, band);
  961. if (!sband) {
  962. IWL_ERR(priv, "invalid band\n");
  963. return added;
  964. }
  965. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  966. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  967. if (passive_dwell <= active_dwell)
  968. passive_dwell = active_dwell + 1;
  969. /* only scan single channel, good enough to reset the RF */
  970. /* pick the first valid not in-use channel */
  971. if (band == IEEE80211_BAND_5GHZ) {
  972. for (i = 14; i < priv->channel_count; i++) {
  973. if (priv->channel_info[i].channel !=
  974. le16_to_cpu(priv->staging_rxon.channel)) {
  975. channel = priv->channel_info[i].channel;
  976. ch_info = iwl_get_channel_info(priv,
  977. band, channel);
  978. if (is_channel_valid(ch_info))
  979. break;
  980. }
  981. }
  982. } else {
  983. for (i = 0; i < 14; i++) {
  984. if (priv->channel_info[i].channel !=
  985. le16_to_cpu(priv->staging_rxon.channel)) {
  986. channel =
  987. priv->channel_info[i].channel;
  988. ch_info = iwl_get_channel_info(priv,
  989. band, channel);
  990. if (is_channel_valid(ch_info))
  991. break;
  992. }
  993. }
  994. }
  995. if (channel) {
  996. scan_ch->channel = cpu_to_le16(channel);
  997. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  998. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  999. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1000. /* Set txpower levels to defaults */
  1001. scan_ch->dsp_atten = 110;
  1002. if (band == IEEE80211_BAND_5GHZ)
  1003. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1004. else
  1005. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  1006. added++;
  1007. } else
  1008. IWL_ERR(priv, "no valid channel found\n");
  1009. return added;
  1010. }
  1011. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  1012. enum ieee80211_band band,
  1013. u8 is_active, u8 n_probes,
  1014. struct iwl_scan_channel *scan_ch)
  1015. {
  1016. struct ieee80211_channel *chan;
  1017. const struct ieee80211_supported_band *sband;
  1018. const struct iwl_channel_info *ch_info;
  1019. u16 passive_dwell = 0;
  1020. u16 active_dwell = 0;
  1021. int added, i;
  1022. u16 channel;
  1023. sband = iwl_get_hw_mode(priv, band);
  1024. if (!sband)
  1025. return 0;
  1026. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  1027. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  1028. if (passive_dwell <= active_dwell)
  1029. passive_dwell = active_dwell + 1;
  1030. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  1031. chan = priv->scan_request->channels[i];
  1032. if (chan->band != band)
  1033. continue;
  1034. channel = ieee80211_frequency_to_channel(chan->center_freq);
  1035. scan_ch->channel = cpu_to_le16(channel);
  1036. ch_info = iwl_get_channel_info(priv, band, channel);
  1037. if (!is_channel_valid(ch_info)) {
  1038. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  1039. channel);
  1040. continue;
  1041. }
  1042. if (!is_active || is_channel_passive(ch_info) ||
  1043. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  1044. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  1045. else
  1046. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  1047. if (n_probes)
  1048. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  1049. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  1050. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1051. /* Set txpower levels to defaults */
  1052. scan_ch->dsp_atten = 110;
  1053. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  1054. * power level:
  1055. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  1056. */
  1057. if (band == IEEE80211_BAND_5GHZ)
  1058. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1059. else
  1060. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  1061. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  1062. channel, le32_to_cpu(scan_ch->type),
  1063. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  1064. "ACTIVE" : "PASSIVE",
  1065. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  1066. active_dwell : passive_dwell);
  1067. scan_ch++;
  1068. added++;
  1069. }
  1070. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  1071. return added;
  1072. }
  1073. void iwlagn_request_scan(struct iwl_priv *priv)
  1074. {
  1075. struct iwl_host_cmd cmd = {
  1076. .id = REPLY_SCAN_CMD,
  1077. .len = sizeof(struct iwl_scan_cmd),
  1078. .flags = CMD_SIZE_HUGE,
  1079. };
  1080. struct iwl_scan_cmd *scan;
  1081. struct ieee80211_conf *conf = NULL;
  1082. u32 rate_flags = 0;
  1083. u16 cmd_len;
  1084. u16 rx_chain = 0;
  1085. enum ieee80211_band band;
  1086. u8 n_probes = 0;
  1087. u8 rx_ant = priv->hw_params.valid_rx_ant;
  1088. u8 rate;
  1089. bool is_active = false;
  1090. int chan_mod;
  1091. u8 active_chains;
  1092. conf = ieee80211_get_hw_conf(priv->hw);
  1093. cancel_delayed_work(&priv->scan_check);
  1094. if (!iwl_is_ready(priv)) {
  1095. IWL_WARN(priv, "request scan called when driver not ready.\n");
  1096. goto done;
  1097. }
  1098. /* Make sure the scan wasn't canceled before this queued work
  1099. * was given the chance to run... */
  1100. if (!test_bit(STATUS_SCANNING, &priv->status))
  1101. goto done;
  1102. /* This should never be called or scheduled if there is currently
  1103. * a scan active in the hardware. */
  1104. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  1105. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
  1106. "Ignoring second request.\n");
  1107. goto done;
  1108. }
  1109. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  1110. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  1111. goto done;
  1112. }
  1113. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1114. IWL_DEBUG_HC(priv, "Scan request while abort pending. Queuing.\n");
  1115. goto done;
  1116. }
  1117. if (iwl_is_rfkill(priv)) {
  1118. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  1119. goto done;
  1120. }
  1121. if (!test_bit(STATUS_READY, &priv->status)) {
  1122. IWL_DEBUG_HC(priv, "Scan request while uninitialized. Queuing.\n");
  1123. goto done;
  1124. }
  1125. if (!priv->scan_cmd) {
  1126. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  1127. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  1128. if (!priv->scan_cmd) {
  1129. IWL_DEBUG_SCAN(priv,
  1130. "fail to allocate memory for scan\n");
  1131. goto done;
  1132. }
  1133. }
  1134. scan = priv->scan_cmd;
  1135. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  1136. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  1137. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  1138. if (iwl_is_associated(priv)) {
  1139. u16 interval = 0;
  1140. u32 extra;
  1141. u32 suspend_time = 100;
  1142. u32 scan_suspend_time = 100;
  1143. unsigned long flags;
  1144. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  1145. spin_lock_irqsave(&priv->lock, flags);
  1146. interval = priv->beacon_int;
  1147. spin_unlock_irqrestore(&priv->lock, flags);
  1148. scan->suspend_time = 0;
  1149. scan->max_out_time = cpu_to_le32(200 * 1024);
  1150. if (!interval)
  1151. interval = suspend_time;
  1152. extra = (suspend_time / interval) << 22;
  1153. scan_suspend_time = (extra |
  1154. ((suspend_time % interval) * 1024));
  1155. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  1156. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  1157. scan_suspend_time, interval);
  1158. }
  1159. if (priv->is_internal_short_scan) {
  1160. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  1161. } else if (priv->scan_request->n_ssids) {
  1162. int i, p = 0;
  1163. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  1164. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  1165. /* always does wildcard anyway */
  1166. if (!priv->scan_request->ssids[i].ssid_len)
  1167. continue;
  1168. scan->direct_scan[p].id = WLAN_EID_SSID;
  1169. scan->direct_scan[p].len =
  1170. priv->scan_request->ssids[i].ssid_len;
  1171. memcpy(scan->direct_scan[p].ssid,
  1172. priv->scan_request->ssids[i].ssid,
  1173. priv->scan_request->ssids[i].ssid_len);
  1174. n_probes++;
  1175. p++;
  1176. }
  1177. is_active = true;
  1178. } else
  1179. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  1180. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  1181. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  1182. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1183. switch (priv->scan_band) {
  1184. case IEEE80211_BAND_2GHZ:
  1185. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  1186. chan_mod = le32_to_cpu(priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_MSK)
  1187. >> RXON_FLG_CHANNEL_MODE_POS;
  1188. if (chan_mod == CHANNEL_MODE_PURE_40) {
  1189. rate = IWL_RATE_6M_PLCP;
  1190. } else {
  1191. rate = IWL_RATE_1M_PLCP;
  1192. rate_flags = RATE_MCS_CCK_MSK;
  1193. }
  1194. scan->good_CRC_th = IWL_GOOD_CRC_TH_DISABLED;
  1195. break;
  1196. case IEEE80211_BAND_5GHZ:
  1197. rate = IWL_RATE_6M_PLCP;
  1198. /*
  1199. * If active scanning is requested but a certain channel is
  1200. * marked passive, we can do active scanning if we detect
  1201. * transmissions.
  1202. *
  1203. * There is an issue with some firmware versions that triggers
  1204. * a sysassert on a "good CRC threshold" of zero (== disabled),
  1205. * on a radar channel even though this means that we should NOT
  1206. * send probes.
  1207. *
  1208. * The "good CRC threshold" is the number of frames that we
  1209. * need to receive during our dwell time on a channel before
  1210. * sending out probes -- setting this to a huge value will
  1211. * mean we never reach it, but at the same time work around
  1212. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  1213. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  1214. */
  1215. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  1216. IWL_GOOD_CRC_TH_NEVER;
  1217. break;
  1218. default:
  1219. IWL_WARN(priv, "Invalid scan band count\n");
  1220. goto done;
  1221. }
  1222. band = priv->scan_band;
  1223. if (priv->cfg->scan_antennas[band])
  1224. rx_ant = priv->cfg->scan_antennas[band];
  1225. priv->scan_tx_ant[band] =
  1226. iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]);
  1227. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  1228. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  1229. /* In power save mode use one chain, otherwise use all chains */
  1230. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  1231. /* rx_ant has been set to all valid chains previously */
  1232. active_chains = rx_ant &
  1233. ((u8)(priv->chain_noise_data.active_chains));
  1234. if (!active_chains)
  1235. active_chains = rx_ant;
  1236. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  1237. priv->chain_noise_data.active_chains);
  1238. rx_ant = first_antenna(active_chains);
  1239. }
  1240. /* MIMO is not used here, but value is required */
  1241. rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  1242. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  1243. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  1244. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  1245. scan->rx_chain = cpu_to_le16(rx_chain);
  1246. if (!priv->is_internal_short_scan) {
  1247. cmd_len = iwl_fill_probe_req(priv,
  1248. (struct ieee80211_mgmt *)scan->data,
  1249. priv->scan_request->ie,
  1250. priv->scan_request->ie_len,
  1251. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1252. } else {
  1253. cmd_len = iwl_fill_probe_req(priv,
  1254. (struct ieee80211_mgmt *)scan->data,
  1255. NULL, 0,
  1256. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1257. }
  1258. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  1259. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  1260. RXON_FILTER_BCON_AWARE_MSK);
  1261. if (priv->is_internal_short_scan) {
  1262. scan->channel_count =
  1263. iwl_get_single_channel_for_scan(priv, band,
  1264. (void *)&scan->data[le16_to_cpu(
  1265. scan->tx_cmd.len)]);
  1266. } else {
  1267. scan->channel_count =
  1268. iwl_get_channels_for_scan(priv, band,
  1269. is_active, n_probes,
  1270. (void *)&scan->data[le16_to_cpu(
  1271. scan->tx_cmd.len)]);
  1272. }
  1273. if (scan->channel_count == 0) {
  1274. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  1275. goto done;
  1276. }
  1277. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  1278. scan->channel_count * sizeof(struct iwl_scan_channel);
  1279. cmd.data = scan;
  1280. scan->len = cpu_to_le16(cmd.len);
  1281. set_bit(STATUS_SCAN_HW, &priv->status);
  1282. if (iwl_send_cmd_sync(priv, &cmd))
  1283. goto done;
  1284. queue_delayed_work(priv->workqueue, &priv->scan_check,
  1285. IWL_SCAN_CHECK_WATCHDOG);
  1286. return;
  1287. done:
  1288. /* Cannot perform scan. Make sure we clear scanning
  1289. * bits from status so next scan request can be performed.
  1290. * If we don't clear scanning status bit here all next scan
  1291. * will fail
  1292. */
  1293. clear_bit(STATUS_SCAN_HW, &priv->status);
  1294. clear_bit(STATUS_SCANNING, &priv->status);
  1295. /* inform mac80211 scan aborted */
  1296. queue_work(priv->workqueue, &priv->scan_completed);
  1297. }