iwl-agn-lib.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
  144. struct iwl_rx_mem_buffer *rxb)
  145. {
  146. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  147. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  148. int txq_id = SEQ_TO_QUEUE(sequence);
  149. int index = SEQ_TO_INDEX(sequence);
  150. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  151. struct ieee80211_tx_info *info;
  152. struct iwl5000_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  153. u32 status = le16_to_cpu(tx_resp->status.status);
  154. int tid;
  155. int sta_id;
  156. int freed;
  157. if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
  158. IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
  159. "is out of range [0-%d] %d %d\n", txq_id,
  160. index, txq->q.n_bd, txq->q.write_ptr,
  161. txq->q.read_ptr);
  162. return;
  163. }
  164. info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
  165. memset(&info->status, 0, sizeof(info->status));
  166. tid = (tx_resp->ra_tid & IWL50_TX_RES_TID_MSK) >> IWL50_TX_RES_TID_POS;
  167. sta_id = (tx_resp->ra_tid & IWL50_TX_RES_RA_MSK) >> IWL50_TX_RES_RA_POS;
  168. if (txq->sched_retry) {
  169. const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
  170. struct iwl_ht_agg *agg = NULL;
  171. agg = &priv->stations[sta_id].tid[tid].agg;
  172. iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  173. /* check if BAR is needed */
  174. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
  175. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  176. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  177. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  178. IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
  179. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  180. scd_ssn , index, txq_id, txq->swq_id);
  181. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  182. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  183. if (priv->mac80211_registered &&
  184. (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  185. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
  186. if (agg->state == IWL_AGG_OFF)
  187. iwl_wake_queue(priv, txq_id);
  188. else
  189. iwl_wake_queue(priv, txq->swq_id);
  190. }
  191. }
  192. } else {
  193. BUG_ON(txq_id != txq->swq_id);
  194. info->status.rates[0].count = tx_resp->failure_frame + 1;
  195. info->flags |= iwl_tx_status_to_mac80211(status);
  196. iwlagn_hwrate_to_tx_control(priv,
  197. le32_to_cpu(tx_resp->rate_n_flags),
  198. info);
  199. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
  200. "0x%x retries %d\n",
  201. txq_id,
  202. iwl_get_tx_fail_reason(status), status,
  203. le32_to_cpu(tx_resp->rate_n_flags),
  204. tx_resp->failure_frame);
  205. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  206. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  207. if (priv->mac80211_registered &&
  208. (iwl_queue_space(&txq->q) > txq->q.low_mark))
  209. iwl_wake_queue(priv, txq_id);
  210. }
  211. iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
  212. if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
  213. IWL_ERR(priv, "TODO: Implement Tx ABORT REQUIRED!!!\n");
  214. }
  215. void iwlagn_rx_handler_setup(struct iwl_priv *priv)
  216. {
  217. /* init calibration handlers */
  218. priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
  219. iwlagn_rx_calib_result;
  220. priv->rx_handlers[CALIBRATION_COMPLETE_NOTIFICATION] =
  221. iwlagn_rx_calib_complete;
  222. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  223. }
  224. void iwlagn_setup_deferred_work(struct iwl_priv *priv)
  225. {
  226. /* in agn, the tx power calibration is done in uCode */
  227. priv->disable_tx_power_cal = 1;
  228. }
  229. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  230. {
  231. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  232. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  233. }
  234. int iwlagn_send_tx_power(struct iwl_priv *priv)
  235. {
  236. struct iwl5000_tx_power_dbm_cmd tx_power_cmd;
  237. u8 tx_ant_cfg_cmd;
  238. /* half dBm need to multiply */
  239. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  240. if (priv->tx_power_lmt_in_half_dbm &&
  241. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  242. /*
  243. * For the newer devices which using enhanced/extend tx power
  244. * table in EEPROM, the format is in half dBm. driver need to
  245. * convert to dBm format before report to mac80211.
  246. * By doing so, there is a possibility of 1/2 dBm resolution
  247. * lost. driver will perform "round-up" operation before
  248. * reporting, but it will cause 1/2 dBm tx power over the
  249. * regulatory limit. Perform the checking here, if the
  250. * "tx_power_user_lmt" is higher than EEPROM value (in
  251. * half-dBm format), lower the tx power based on EEPROM
  252. */
  253. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  254. }
  255. tx_power_cmd.flags = IWL50_TX_POWER_NO_CLOSED;
  256. tx_power_cmd.srv_chan_lmt = IWL50_TX_POWER_AUTO;
  257. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  258. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  259. else
  260. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  261. return iwl_send_cmd_pdu_async(priv, tx_ant_cfg_cmd,
  262. sizeof(tx_power_cmd), &tx_power_cmd,
  263. NULL);
  264. }
  265. void iwlagn_temperature(struct iwl_priv *priv)
  266. {
  267. /* store temperature from statistics (in Celsius) */
  268. priv->temperature = le32_to_cpu(priv->statistics.general.temperature);
  269. iwl_tt_handler(priv);
  270. }
  271. u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
  272. {
  273. struct iwl_eeprom_calib_hdr {
  274. u8 version;
  275. u8 pa_type;
  276. u16 voltage;
  277. } *hdr;
  278. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  279. EEPROM_5000_CALIB_ALL);
  280. return hdr->version;
  281. }
  282. /*
  283. * EEPROM
  284. */
  285. static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
  286. {
  287. u16 offset = 0;
  288. if ((address & INDIRECT_ADDRESS) == 0)
  289. return address;
  290. switch (address & INDIRECT_TYPE_MSK) {
  291. case INDIRECT_HOST:
  292. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_HOST);
  293. break;
  294. case INDIRECT_GENERAL:
  295. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_GENERAL);
  296. break;
  297. case INDIRECT_REGULATORY:
  298. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_REGULATORY);
  299. break;
  300. case INDIRECT_CALIBRATION:
  301. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_CALIBRATION);
  302. break;
  303. case INDIRECT_PROCESS_ADJST:
  304. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_PROCESS_ADJST);
  305. break;
  306. case INDIRECT_OTHERS:
  307. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_OTHERS);
  308. break;
  309. default:
  310. IWL_ERR(priv, "illegal indirect type: 0x%X\n",
  311. address & INDIRECT_TYPE_MSK);
  312. break;
  313. }
  314. /* translate the offset from words to byte */
  315. return (address & ADDRESS_MSK) + (offset << 1);
  316. }
  317. const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
  318. size_t offset)
  319. {
  320. u32 address = eeprom_indirect_address(priv, offset);
  321. BUG_ON(address >= priv->cfg->eeprom_size);
  322. return &priv->eeprom[address];
  323. }
  324. struct iwl_mod_params iwlagn_mod_params = {
  325. .amsdu_size_8K = 1,
  326. .restart_fw = 1,
  327. /* the rest are 0 by default */
  328. };
  329. void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  330. {
  331. unsigned long flags;
  332. int i;
  333. spin_lock_irqsave(&rxq->lock, flags);
  334. INIT_LIST_HEAD(&rxq->rx_free);
  335. INIT_LIST_HEAD(&rxq->rx_used);
  336. /* Fill the rx_used queue with _all_ of the Rx buffers */
  337. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  338. /* In the reset function, these buffers may have been allocated
  339. * to an SKB, so we need to unmap and free potential storage */
  340. if (rxq->pool[i].page != NULL) {
  341. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  342. PAGE_SIZE << priv->hw_params.rx_page_order,
  343. PCI_DMA_FROMDEVICE);
  344. __iwl_free_pages(priv, rxq->pool[i].page);
  345. rxq->pool[i].page = NULL;
  346. }
  347. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  348. }
  349. /* Set us so that we have processed and used all buffers, but have
  350. * not restocked the Rx queue with fresh buffers */
  351. rxq->read = rxq->write = 0;
  352. rxq->write_actual = 0;
  353. rxq->free_count = 0;
  354. spin_unlock_irqrestore(&rxq->lock, flags);
  355. }
  356. int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  357. {
  358. u32 rb_size;
  359. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  360. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  361. if (!priv->cfg->use_isr_legacy)
  362. rb_timeout = RX_RB_TIMEOUT;
  363. if (priv->cfg->mod_params->amsdu_size_8K)
  364. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  365. else
  366. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  367. /* Stop Rx DMA */
  368. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  369. /* Reset driver's Rx queue write index */
  370. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  371. /* Tell device where to find RBD circular buffer in DRAM */
  372. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  373. (u32)(rxq->dma_addr >> 8));
  374. /* Tell device where in DRAM to update its Rx status */
  375. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  376. rxq->rb_stts_dma >> 4);
  377. /* Enable Rx DMA
  378. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  379. * the credit mechanism in 5000 HW RX FIFO
  380. * Direct rx interrupts to hosts
  381. * Rx buffer size 4 or 8k
  382. * RB timeout 0x10
  383. * 256 RBDs
  384. */
  385. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  386. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  387. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  388. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  389. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  390. rb_size|
  391. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  392. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  393. /* Set interrupt coalescing timer to default (2048 usecs) */
  394. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  395. return 0;
  396. }
  397. int iwlagn_hw_nic_init(struct iwl_priv *priv)
  398. {
  399. unsigned long flags;
  400. struct iwl_rx_queue *rxq = &priv->rxq;
  401. int ret;
  402. /* nic_init */
  403. spin_lock_irqsave(&priv->lock, flags);
  404. priv->cfg->ops->lib->apm_ops.init(priv);
  405. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  406. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  407. spin_unlock_irqrestore(&priv->lock, flags);
  408. ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
  409. priv->cfg->ops->lib->apm_ops.config(priv);
  410. /* Allocate the RX queue, or reset if it is already allocated */
  411. if (!rxq->bd) {
  412. ret = iwl_rx_queue_alloc(priv);
  413. if (ret) {
  414. IWL_ERR(priv, "Unable to initialize Rx queue\n");
  415. return -ENOMEM;
  416. }
  417. } else
  418. iwlagn_rx_queue_reset(priv, rxq);
  419. iwlagn_rx_replenish(priv);
  420. iwlagn_rx_init(priv, rxq);
  421. spin_lock_irqsave(&priv->lock, flags);
  422. rxq->need_update = 1;
  423. iwl_rx_queue_update_write_ptr(priv, rxq);
  424. spin_unlock_irqrestore(&priv->lock, flags);
  425. /* Allocate and init all Tx and Command queues */
  426. ret = iwlagn_txq_ctx_reset(priv);
  427. if (ret)
  428. return ret;
  429. set_bit(STATUS_INIT, &priv->status);
  430. return 0;
  431. }
  432. /**
  433. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  434. */
  435. static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
  436. dma_addr_t dma_addr)
  437. {
  438. return cpu_to_le32((u32)(dma_addr >> 8));
  439. }
  440. /**
  441. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  442. *
  443. * If there are slots in the RX queue that need to be restocked,
  444. * and we have free pre-allocated buffers, fill the ranks as much
  445. * as we can, pulling from rx_free.
  446. *
  447. * This moves the 'write' index forward to catch up with 'processed', and
  448. * also updates the memory address in the firmware to reference the new
  449. * target buffer.
  450. */
  451. void iwlagn_rx_queue_restock(struct iwl_priv *priv)
  452. {
  453. struct iwl_rx_queue *rxq = &priv->rxq;
  454. struct list_head *element;
  455. struct iwl_rx_mem_buffer *rxb;
  456. unsigned long flags;
  457. int write;
  458. spin_lock_irqsave(&rxq->lock, flags);
  459. write = rxq->write & ~0x7;
  460. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  461. /* Get next free Rx buffer, remove from free list */
  462. element = rxq->rx_free.next;
  463. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  464. list_del(element);
  465. /* Point to Rx buffer via next RBD in circular buffer */
  466. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
  467. rxb->page_dma);
  468. rxq->queue[rxq->write] = rxb;
  469. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  470. rxq->free_count--;
  471. }
  472. spin_unlock_irqrestore(&rxq->lock, flags);
  473. /* If the pre-allocated buffer pool is dropping low, schedule to
  474. * refill it */
  475. if (rxq->free_count <= RX_LOW_WATERMARK)
  476. queue_work(priv->workqueue, &priv->rx_replenish);
  477. /* If we've added more space for the firmware to place data, tell it.
  478. * Increment device's write pointer in multiples of 8. */
  479. if (rxq->write_actual != (rxq->write & ~0x7)) {
  480. spin_lock_irqsave(&rxq->lock, flags);
  481. rxq->need_update = 1;
  482. spin_unlock_irqrestore(&rxq->lock, flags);
  483. iwl_rx_queue_update_write_ptr(priv, rxq);
  484. }
  485. }
  486. /**
  487. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  488. *
  489. * When moving to rx_free an SKB is allocated for the slot.
  490. *
  491. * Also restock the Rx queue via iwl_rx_queue_restock.
  492. * This is called as a scheduled work item (except for during initialization)
  493. */
  494. void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  495. {
  496. struct iwl_rx_queue *rxq = &priv->rxq;
  497. struct list_head *element;
  498. struct iwl_rx_mem_buffer *rxb;
  499. struct page *page;
  500. unsigned long flags;
  501. gfp_t gfp_mask = priority;
  502. while (1) {
  503. spin_lock_irqsave(&rxq->lock, flags);
  504. if (list_empty(&rxq->rx_used)) {
  505. spin_unlock_irqrestore(&rxq->lock, flags);
  506. return;
  507. }
  508. spin_unlock_irqrestore(&rxq->lock, flags);
  509. if (rxq->free_count > RX_LOW_WATERMARK)
  510. gfp_mask |= __GFP_NOWARN;
  511. if (priv->hw_params.rx_page_order > 0)
  512. gfp_mask |= __GFP_COMP;
  513. /* Alloc a new receive buffer */
  514. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  515. if (!page) {
  516. if (net_ratelimit())
  517. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  518. "order: %d\n",
  519. priv->hw_params.rx_page_order);
  520. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  521. net_ratelimit())
  522. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  523. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  524. rxq->free_count);
  525. /* We don't reschedule replenish work here -- we will
  526. * call the restock method and if it still needs
  527. * more buffers it will schedule replenish */
  528. return;
  529. }
  530. spin_lock_irqsave(&rxq->lock, flags);
  531. if (list_empty(&rxq->rx_used)) {
  532. spin_unlock_irqrestore(&rxq->lock, flags);
  533. __free_pages(page, priv->hw_params.rx_page_order);
  534. return;
  535. }
  536. element = rxq->rx_used.next;
  537. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  538. list_del(element);
  539. spin_unlock_irqrestore(&rxq->lock, flags);
  540. rxb->page = page;
  541. /* Get physical address of the RB */
  542. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  543. PAGE_SIZE << priv->hw_params.rx_page_order,
  544. PCI_DMA_FROMDEVICE);
  545. /* dma address must be no more than 36 bits */
  546. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  547. /* and also 256 byte aligned! */
  548. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  549. spin_lock_irqsave(&rxq->lock, flags);
  550. list_add_tail(&rxb->list, &rxq->rx_free);
  551. rxq->free_count++;
  552. priv->alloc_rxb_page++;
  553. spin_unlock_irqrestore(&rxq->lock, flags);
  554. }
  555. }
  556. void iwlagn_rx_replenish(struct iwl_priv *priv)
  557. {
  558. unsigned long flags;
  559. iwlagn_rx_allocate(priv, GFP_KERNEL);
  560. spin_lock_irqsave(&priv->lock, flags);
  561. iwlagn_rx_queue_restock(priv);
  562. spin_unlock_irqrestore(&priv->lock, flags);
  563. }
  564. void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  565. {
  566. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  567. iwlagn_rx_queue_restock(priv);
  568. }
  569. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  570. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  571. * This free routine walks the list of POOL entries and if SKB is set to
  572. * non NULL it is unmapped and freed
  573. */
  574. void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  575. {
  576. int i;
  577. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  578. if (rxq->pool[i].page != NULL) {
  579. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  580. PAGE_SIZE << priv->hw_params.rx_page_order,
  581. PCI_DMA_FROMDEVICE);
  582. __iwl_free_pages(priv, rxq->pool[i].page);
  583. rxq->pool[i].page = NULL;
  584. }
  585. }
  586. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  587. rxq->dma_addr);
  588. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  589. rxq->rb_stts, rxq->rb_stts_dma);
  590. rxq->bd = NULL;
  591. rxq->rb_stts = NULL;
  592. }
  593. int iwlagn_rxq_stop(struct iwl_priv *priv)
  594. {
  595. /* stop Rx DMA */
  596. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  597. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  598. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  599. return 0;
  600. }
  601. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  602. {
  603. int idx = 0;
  604. int band_offset = 0;
  605. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  606. if (rate_n_flags & RATE_MCS_HT_MSK) {
  607. idx = (rate_n_flags & 0xff);
  608. return idx;
  609. /* Legacy rate format, search for match in table */
  610. } else {
  611. if (band == IEEE80211_BAND_5GHZ)
  612. band_offset = IWL_FIRST_OFDM_RATE;
  613. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  614. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  615. return idx - band_offset;
  616. }
  617. return -1;
  618. }
  619. /* Calc max signal level (dBm) among 3 possible receivers */
  620. static inline int iwlagn_calc_rssi(struct iwl_priv *priv,
  621. struct iwl_rx_phy_res *rx_resp)
  622. {
  623. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  624. }
  625. #ifdef CONFIG_IWLWIFI_DEBUG
  626. /**
  627. * iwlagn_dbg_report_frame - dump frame to syslog during debug sessions
  628. *
  629. * You may hack this function to show different aspects of received frames,
  630. * including selective frame dumps.
  631. * group100 parameter selects whether to show 1 out of 100 good data frames.
  632. * All beacon and probe response frames are printed.
  633. */
  634. static void iwlagn_dbg_report_frame(struct iwl_priv *priv,
  635. struct iwl_rx_phy_res *phy_res, u16 length,
  636. struct ieee80211_hdr *header, int group100)
  637. {
  638. u32 to_us;
  639. u32 print_summary = 0;
  640. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  641. u32 hundred = 0;
  642. u32 dataframe = 0;
  643. __le16 fc;
  644. u16 seq_ctl;
  645. u16 channel;
  646. u16 phy_flags;
  647. u32 rate_n_flags;
  648. u32 tsf_low;
  649. int rssi;
  650. if (likely(!(iwl_get_debug_level(priv) & IWL_DL_RX)))
  651. return;
  652. /* MAC header */
  653. fc = header->frame_control;
  654. seq_ctl = le16_to_cpu(header->seq_ctrl);
  655. /* metadata */
  656. channel = le16_to_cpu(phy_res->channel);
  657. phy_flags = le16_to_cpu(phy_res->phy_flags);
  658. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  659. /* signal statistics */
  660. rssi = iwlagn_calc_rssi(priv, phy_res);
  661. tsf_low = le64_to_cpu(phy_res->timestamp) & 0x0ffffffff;
  662. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  663. /* if data frame is to us and all is good,
  664. * (optionally) print summary for only 1 out of every 100 */
  665. if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
  666. cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  667. dataframe = 1;
  668. if (!group100)
  669. print_summary = 1; /* print each frame */
  670. else if (priv->framecnt_to_us < 100) {
  671. priv->framecnt_to_us++;
  672. print_summary = 0;
  673. } else {
  674. priv->framecnt_to_us = 0;
  675. print_summary = 1;
  676. hundred = 1;
  677. }
  678. } else {
  679. /* print summary for all other frames */
  680. print_summary = 1;
  681. }
  682. if (print_summary) {
  683. char *title;
  684. int rate_idx;
  685. u32 bitrate;
  686. if (hundred)
  687. title = "100Frames";
  688. else if (ieee80211_has_retry(fc))
  689. title = "Retry";
  690. else if (ieee80211_is_assoc_resp(fc))
  691. title = "AscRsp";
  692. else if (ieee80211_is_reassoc_resp(fc))
  693. title = "RasRsp";
  694. else if (ieee80211_is_probe_resp(fc)) {
  695. title = "PrbRsp";
  696. print_dump = 1; /* dump frame contents */
  697. } else if (ieee80211_is_beacon(fc)) {
  698. title = "Beacon";
  699. print_dump = 1; /* dump frame contents */
  700. } else if (ieee80211_is_atim(fc))
  701. title = "ATIM";
  702. else if (ieee80211_is_auth(fc))
  703. title = "Auth";
  704. else if (ieee80211_is_deauth(fc))
  705. title = "DeAuth";
  706. else if (ieee80211_is_disassoc(fc))
  707. title = "DisAssoc";
  708. else
  709. title = "Frame";
  710. rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
  711. if (unlikely((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT))) {
  712. bitrate = 0;
  713. WARN_ON_ONCE(1);
  714. } else {
  715. bitrate = iwl_rates[rate_idx].ieee / 2;
  716. }
  717. /* print frame summary.
  718. * MAC addresses show just the last byte (for brevity),
  719. * but you can hack it to show more, if you'd like to. */
  720. if (dataframe)
  721. IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
  722. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  723. title, le16_to_cpu(fc), header->addr1[5],
  724. length, rssi, channel, bitrate);
  725. else {
  726. /* src/dst addresses assume managed mode */
  727. IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, src=0x%02x, "
  728. "len=%u, rssi=%d, tim=%lu usec, "
  729. "phy=0x%02x, chnl=%d\n",
  730. title, le16_to_cpu(fc), header->addr1[5],
  731. header->addr3[5], length, rssi,
  732. tsf_low - priv->scan_start_tsf,
  733. phy_flags, channel);
  734. }
  735. }
  736. if (print_dump)
  737. iwl_print_hex_dump(priv, IWL_DL_RX, header, length);
  738. }
  739. #endif
  740. static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  741. {
  742. u32 decrypt_out = 0;
  743. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  744. RX_RES_STATUS_STATION_FOUND)
  745. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  746. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  747. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  748. /* packet was not encrypted */
  749. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  750. RX_RES_STATUS_SEC_TYPE_NONE)
  751. return decrypt_out;
  752. /* packet was encrypted with unknown alg */
  753. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  754. RX_RES_STATUS_SEC_TYPE_ERR)
  755. return decrypt_out;
  756. /* decryption was not done in HW */
  757. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  758. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  759. return decrypt_out;
  760. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  761. case RX_RES_STATUS_SEC_TYPE_CCMP:
  762. /* alg is CCM: check MIC only */
  763. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  764. /* Bad MIC */
  765. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  766. else
  767. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  768. break;
  769. case RX_RES_STATUS_SEC_TYPE_TKIP:
  770. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  771. /* Bad TTAK */
  772. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  773. break;
  774. }
  775. /* fall through if TTAK OK */
  776. default:
  777. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  778. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  779. else
  780. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  781. break;
  782. };
  783. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  784. decrypt_in, decrypt_out);
  785. return decrypt_out;
  786. }
  787. static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
  788. struct ieee80211_hdr *hdr,
  789. u16 len,
  790. u32 ampdu_status,
  791. struct iwl_rx_mem_buffer *rxb,
  792. struct ieee80211_rx_status *stats)
  793. {
  794. struct sk_buff *skb;
  795. int ret = 0;
  796. __le16 fc = hdr->frame_control;
  797. /* We only process data packets if the interface is open */
  798. if (unlikely(!priv->is_open)) {
  799. IWL_DEBUG_DROP_LIMIT(priv,
  800. "Dropping packet while interface is not open.\n");
  801. return;
  802. }
  803. /* In case of HW accelerated crypto and bad decryption, drop */
  804. if (!priv->cfg->mod_params->sw_crypto &&
  805. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  806. return;
  807. skb = alloc_skb(IWL_LINK_HDR_MAX * 2, GFP_ATOMIC);
  808. if (!skb) {
  809. IWL_ERR(priv, "alloc_skb failed\n");
  810. return;
  811. }
  812. skb_reserve(skb, IWL_LINK_HDR_MAX);
  813. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
  814. /* mac80211 currently doesn't support paged SKB. Convert it to
  815. * linear SKB for management frame and data frame requires
  816. * software decryption or software defragementation. */
  817. if (ieee80211_is_mgmt(fc) ||
  818. ieee80211_has_protected(fc) ||
  819. ieee80211_has_morefrags(fc) ||
  820. le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG ||
  821. (ieee80211_is_data_qos(fc) &&
  822. *ieee80211_get_qos_ctl(hdr) &
  823. IEEE80211_QOS_CONTROL_A_MSDU_PRESENT))
  824. ret = skb_linearize(skb);
  825. else
  826. ret = __pskb_pull_tail(skb, min_t(u16, IWL_LINK_HDR_MAX, len)) ?
  827. 0 : -ENOMEM;
  828. if (ret) {
  829. kfree_skb(skb);
  830. goto out;
  831. }
  832. /*
  833. * XXX: We cannot touch the page and its virtual memory (hdr) after
  834. * here. It might have already been freed by the above skb change.
  835. */
  836. iwl_update_stats(priv, false, fc, len);
  837. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  838. ieee80211_rx(priv->hw, skb);
  839. out:
  840. priv->alloc_rxb_page--;
  841. rxb->page = NULL;
  842. }
  843. /* Called for REPLY_RX (legacy ABG frames), or
  844. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  845. void iwlagn_rx_reply_rx(struct iwl_priv *priv,
  846. struct iwl_rx_mem_buffer *rxb)
  847. {
  848. struct ieee80211_hdr *header;
  849. struct ieee80211_rx_status rx_status;
  850. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  851. struct iwl_rx_phy_res *phy_res;
  852. __le32 rx_pkt_status;
  853. struct iwl4965_rx_mpdu_res_start *amsdu;
  854. u32 len;
  855. u32 ampdu_status;
  856. u32 rate_n_flags;
  857. /**
  858. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  859. * REPLY_RX: physical layer info is in this buffer
  860. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  861. * command and cached in priv->last_phy_res
  862. *
  863. * Here we set up local variables depending on which command is
  864. * received.
  865. */
  866. if (pkt->hdr.cmd == REPLY_RX) {
  867. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  868. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  869. + phy_res->cfg_phy_cnt);
  870. len = le16_to_cpu(phy_res->byte_count);
  871. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  872. phy_res->cfg_phy_cnt + len);
  873. ampdu_status = le32_to_cpu(rx_pkt_status);
  874. } else {
  875. if (!priv->last_phy_res[0]) {
  876. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  877. return;
  878. }
  879. phy_res = (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  880. amsdu = (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  881. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  882. len = le16_to_cpu(amsdu->byte_count);
  883. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  884. ampdu_status = iwlagn_translate_rx_status(priv,
  885. le32_to_cpu(rx_pkt_status));
  886. }
  887. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  888. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  889. phy_res->cfg_phy_cnt);
  890. return;
  891. }
  892. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  893. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  894. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  895. le32_to_cpu(rx_pkt_status));
  896. return;
  897. }
  898. /* This will be used in several places later */
  899. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  900. /* rx_status carries information about the packet to mac80211 */
  901. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  902. rx_status.freq =
  903. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
  904. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  905. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  906. rx_status.rate_idx =
  907. iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  908. rx_status.flag = 0;
  909. /* TSF isn't reliable. In order to allow smooth user experience,
  910. * this W/A doesn't propagate it to the mac80211 */
  911. /*rx_status.flag |= RX_FLAG_TSFT;*/
  912. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  913. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  914. rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
  915. /* Meaningful noise values are available only from beacon statistics,
  916. * which are gathered only when associated, and indicate noise
  917. * only for the associated network channel ...
  918. * Ignore these noise values while scanning (other channels) */
  919. if (iwl_is_associated(priv) &&
  920. !test_bit(STATUS_SCANNING, &priv->status)) {
  921. rx_status.noise = priv->last_rx_noise;
  922. } else {
  923. rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  924. }
  925. /* Reset beacon noise level if not associated. */
  926. if (!iwl_is_associated(priv))
  927. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  928. #ifdef CONFIG_IWLWIFI_DEBUG
  929. /* Set "1" to report good data frames in groups of 100 */
  930. if (unlikely(iwl_get_debug_level(priv) & IWL_DL_RX))
  931. iwlagn_dbg_report_frame(priv, phy_res, len, header, 1);
  932. #endif
  933. iwl_dbg_log_rx_data_frame(priv, len, header);
  934. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, noise %d, TSF %llu\n",
  935. rx_status.signal, rx_status.noise,
  936. (unsigned long long)rx_status.mactime);
  937. /*
  938. * "antenna number"
  939. *
  940. * It seems that the antenna field in the phy flags value
  941. * is actually a bit field. This is undefined by radiotap,
  942. * it wants an actual antenna number but I always get "7"
  943. * for most legacy frames I receive indicating that the
  944. * same frame was received on all three RX chains.
  945. *
  946. * I think this field should be removed in favor of a
  947. * new 802.11n radiotap field "RX chains" that is defined
  948. * as a bitmask.
  949. */
  950. rx_status.antenna =
  951. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  952. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  953. /* set the preamble flag if appropriate */
  954. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  955. rx_status.flag |= RX_FLAG_SHORTPRE;
  956. /* Set up the HT phy flags */
  957. if (rate_n_flags & RATE_MCS_HT_MSK)
  958. rx_status.flag |= RX_FLAG_HT;
  959. if (rate_n_flags & RATE_MCS_HT40_MSK)
  960. rx_status.flag |= RX_FLAG_40MHZ;
  961. if (rate_n_flags & RATE_MCS_SGI_MSK)
  962. rx_status.flag |= RX_FLAG_SHORT_GI;
  963. iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  964. rxb, &rx_status);
  965. }
  966. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  967. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  968. void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
  969. struct iwl_rx_mem_buffer *rxb)
  970. {
  971. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  972. priv->last_phy_res[0] = 1;
  973. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  974. sizeof(struct iwl_rx_phy_res));
  975. }