iwl-rx.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  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 <net/mac80211.h>
  31. #include <asm/unaligned.h>
  32. #include "iwl-eeprom.h"
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-sta.h"
  36. #include "iwl-io.h"
  37. #include "iwl-calib.h"
  38. #include "iwl-helpers.h"
  39. /************************** RX-FUNCTIONS ****************************/
  40. /*
  41. * Rx theory of operation
  42. *
  43. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  44. * each of which point to Receive Buffers to be filled by the NIC. These get
  45. * used not only for Rx frames, but for any command response or notification
  46. * from the NIC. The driver and NIC manage the Rx buffers by means
  47. * of indexes into the circular buffer.
  48. *
  49. * Rx Queue Indexes
  50. * The host/firmware share two index registers for managing the Rx buffers.
  51. *
  52. * The READ index maps to the first position that the firmware may be writing
  53. * to -- the driver can read up to (but not including) this position and get
  54. * good data.
  55. * The READ index is managed by the firmware once the card is enabled.
  56. *
  57. * The WRITE index maps to the last position the driver has read from -- the
  58. * position preceding WRITE is the last slot the firmware can place a packet.
  59. *
  60. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  61. * WRITE = READ.
  62. *
  63. * During initialization, the host sets up the READ queue position to the first
  64. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  65. *
  66. * When the firmware places a packet in a buffer, it will advance the READ index
  67. * and fire the RX interrupt. The driver can then query the READ index and
  68. * process as many packets as possible, moving the WRITE index forward as it
  69. * resets the Rx queue buffers with new memory.
  70. *
  71. * The management in the driver is as follows:
  72. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  73. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  74. * to replenish the iwl->rxq->rx_free.
  75. * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
  76. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  77. * 'processed' and 'read' driver indexes as well)
  78. * + A received packet is processed and handed to the kernel network stack,
  79. * detached from the iwl->rxq. The driver 'processed' index is updated.
  80. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  81. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  82. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  83. * were enough free buffers and RX_STALLED is set it is cleared.
  84. *
  85. *
  86. * Driver sequence:
  87. *
  88. * iwl_rx_queue_alloc() Allocates rx_free
  89. * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
  90. * iwl_rx_queue_restock
  91. * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
  92. * queue, updates firmware pointers, and updates
  93. * the WRITE index. If insufficient rx_free buffers
  94. * are available, schedules iwl_rx_replenish
  95. *
  96. * -- enable interrupts --
  97. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  98. * READ INDEX, detaching the SKB from the pool.
  99. * Moves the packet buffer from queue to rx_used.
  100. * Calls iwl_rx_queue_restock to refill any empty
  101. * slots.
  102. * ...
  103. *
  104. */
  105. /**
  106. * iwl_rx_queue_space - Return number of free slots available in queue.
  107. */
  108. int iwl_rx_queue_space(const struct iwl_rx_queue *q)
  109. {
  110. int s = q->read - q->write;
  111. if (s <= 0)
  112. s += RX_QUEUE_SIZE;
  113. /* keep some buffer to not confuse full and empty queue */
  114. s -= 2;
  115. if (s < 0)
  116. s = 0;
  117. return s;
  118. }
  119. EXPORT_SYMBOL(iwl_rx_queue_space);
  120. /**
  121. * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  122. */
  123. int iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
  124. {
  125. unsigned long flags;
  126. u32 rx_wrt_ptr_reg = priv->hw_params.rx_wrt_ptr_reg;
  127. u32 reg;
  128. int ret = 0;
  129. spin_lock_irqsave(&q->lock, flags);
  130. if (q->need_update == 0)
  131. goto exit_unlock;
  132. /* If power-saving is in use, make sure device is awake */
  133. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  134. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  135. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  136. iwl_set_bit(priv, CSR_GP_CNTRL,
  137. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  138. goto exit_unlock;
  139. }
  140. ret = iwl_grab_nic_access(priv);
  141. if (ret)
  142. goto exit_unlock;
  143. /* Device expects a multiple of 8 */
  144. iwl_write_direct32(priv, rx_wrt_ptr_reg, q->write & ~0x7);
  145. iwl_release_nic_access(priv);
  146. /* Else device is assumed to be awake */
  147. } else {
  148. /* Device expects a multiple of 8 */
  149. iwl_write32(priv, rx_wrt_ptr_reg, q->write & ~0x7);
  150. }
  151. q->need_update = 0;
  152. exit_unlock:
  153. spin_unlock_irqrestore(&q->lock, flags);
  154. return ret;
  155. }
  156. EXPORT_SYMBOL(iwl_rx_queue_update_write_ptr);
  157. /**
  158. * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  159. */
  160. static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
  161. dma_addr_t dma_addr)
  162. {
  163. return cpu_to_le32((u32)(dma_addr >> 8));
  164. }
  165. /**
  166. * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
  167. *
  168. * If there are slots in the RX queue that need to be restocked,
  169. * and we have free pre-allocated buffers, fill the ranks as much
  170. * as we can, pulling from rx_free.
  171. *
  172. * This moves the 'write' index forward to catch up with 'processed', and
  173. * also updates the memory address in the firmware to reference the new
  174. * target buffer.
  175. */
  176. int iwl_rx_queue_restock(struct iwl_priv *priv)
  177. {
  178. struct iwl_rx_queue *rxq = &priv->rxq;
  179. struct list_head *element;
  180. struct iwl_rx_mem_buffer *rxb;
  181. unsigned long flags;
  182. int write;
  183. int ret = 0;
  184. spin_lock_irqsave(&rxq->lock, flags);
  185. write = rxq->write & ~0x7;
  186. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  187. /* Get next free Rx buffer, remove from free list */
  188. element = rxq->rx_free.next;
  189. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  190. list_del(element);
  191. /* Point to Rx buffer via next RBD in circular buffer */
  192. rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->aligned_dma_addr);
  193. rxq->queue[rxq->write] = rxb;
  194. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  195. rxq->free_count--;
  196. }
  197. spin_unlock_irqrestore(&rxq->lock, flags);
  198. /* If the pre-allocated buffer pool is dropping low, schedule to
  199. * refill it */
  200. if (rxq->free_count <= RX_LOW_WATERMARK)
  201. queue_work(priv->workqueue, &priv->rx_replenish);
  202. /* If we've added more space for the firmware to place data, tell it.
  203. * Increment device's write pointer in multiples of 8. */
  204. if (write != (rxq->write & ~0x7)) {
  205. spin_lock_irqsave(&rxq->lock, flags);
  206. rxq->need_update = 1;
  207. spin_unlock_irqrestore(&rxq->lock, flags);
  208. ret = iwl_rx_queue_update_write_ptr(priv, rxq);
  209. }
  210. return ret;
  211. }
  212. EXPORT_SYMBOL(iwl_rx_queue_restock);
  213. /**
  214. * iwl_rx_replenish - Move all used packet from rx_used to rx_free
  215. *
  216. * When moving to rx_free an SKB is allocated for the slot.
  217. *
  218. * Also restock the Rx queue via iwl_rx_queue_restock.
  219. * This is called as a scheduled work item (except for during initialization)
  220. */
  221. void iwl_rx_allocate(struct iwl_priv *priv)
  222. {
  223. struct iwl_rx_queue *rxq = &priv->rxq;
  224. struct list_head *element;
  225. struct iwl_rx_mem_buffer *rxb;
  226. unsigned long flags;
  227. while (1) {
  228. spin_lock_irqsave(&rxq->lock, flags);
  229. if (list_empty(&rxq->rx_used)) {
  230. spin_unlock_irqrestore(&rxq->lock, flags);
  231. return;
  232. }
  233. element = rxq->rx_used.next;
  234. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  235. list_del(element);
  236. spin_unlock_irqrestore(&rxq->lock, flags);
  237. /* Alloc a new receive buffer */
  238. rxb->skb = alloc_skb(priv->hw_params.rx_buf_size + 256,
  239. GFP_KERNEL);
  240. if (!rxb->skb) {
  241. IWL_CRIT(priv, "Can not allocate SKB buffers\n");
  242. /* We don't reschedule replenish work here -- we will
  243. * call the restock method and if it still needs
  244. * more buffers it will schedule replenish */
  245. break;
  246. }
  247. /* Get physical address of RB/SKB */
  248. rxb->real_dma_addr = pci_map_single(
  249. priv->pci_dev,
  250. rxb->skb->data,
  251. priv->hw_params.rx_buf_size + 256,
  252. PCI_DMA_FROMDEVICE);
  253. /* dma address must be no more than 36 bits */
  254. BUG_ON(rxb->real_dma_addr & ~DMA_BIT_MASK(36));
  255. /* and also 256 byte aligned! */
  256. rxb->aligned_dma_addr = ALIGN(rxb->real_dma_addr, 256);
  257. skb_reserve(rxb->skb, rxb->aligned_dma_addr - rxb->real_dma_addr);
  258. spin_lock_irqsave(&rxq->lock, flags);
  259. list_add_tail(&rxb->list, &rxq->rx_free);
  260. rxq->free_count++;
  261. priv->alloc_rxb_skb++;
  262. spin_unlock_irqrestore(&rxq->lock, flags);
  263. }
  264. }
  265. void iwl_rx_replenish(struct iwl_priv *priv)
  266. {
  267. unsigned long flags;
  268. iwl_rx_allocate(priv);
  269. spin_lock_irqsave(&priv->lock, flags);
  270. iwl_rx_queue_restock(priv);
  271. spin_unlock_irqrestore(&priv->lock, flags);
  272. }
  273. EXPORT_SYMBOL(iwl_rx_replenish);
  274. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  275. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  276. * This free routine walks the list of POOL entries and if SKB is set to
  277. * non NULL it is unmapped and freed
  278. */
  279. void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  280. {
  281. int i;
  282. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  283. if (rxq->pool[i].skb != NULL) {
  284. pci_unmap_single(priv->pci_dev,
  285. rxq->pool[i].real_dma_addr,
  286. priv->hw_params.rx_buf_size + 256,
  287. PCI_DMA_FROMDEVICE);
  288. dev_kfree_skb(rxq->pool[i].skb);
  289. }
  290. }
  291. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  292. rxq->dma_addr);
  293. pci_free_consistent(priv->pci_dev, sizeof(struct iwl_rb_status),
  294. rxq->rb_stts, rxq->rb_stts_dma);
  295. rxq->bd = NULL;
  296. rxq->rb_stts = NULL;
  297. }
  298. EXPORT_SYMBOL(iwl_rx_queue_free);
  299. int iwl_rx_queue_alloc(struct iwl_priv *priv)
  300. {
  301. struct iwl_rx_queue *rxq = &priv->rxq;
  302. struct pci_dev *dev = priv->pci_dev;
  303. int i;
  304. spin_lock_init(&rxq->lock);
  305. INIT_LIST_HEAD(&rxq->rx_free);
  306. INIT_LIST_HEAD(&rxq->rx_used);
  307. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  308. rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
  309. if (!rxq->bd)
  310. goto err_bd;
  311. rxq->rb_stts = pci_alloc_consistent(dev, sizeof(struct iwl_rb_status),
  312. &rxq->rb_stts_dma);
  313. if (!rxq->rb_stts)
  314. goto err_rb;
  315. /* Fill the rx_used queue with _all_ of the Rx buffers */
  316. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  317. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  318. /* Set us so that we have processed and used all buffers, but have
  319. * not restocked the Rx queue with fresh buffers */
  320. rxq->read = rxq->write = 0;
  321. rxq->free_count = 0;
  322. rxq->need_update = 0;
  323. return 0;
  324. err_rb:
  325. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  326. rxq->dma_addr);
  327. err_bd:
  328. return -ENOMEM;
  329. }
  330. EXPORT_SYMBOL(iwl_rx_queue_alloc);
  331. void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  332. {
  333. unsigned long flags;
  334. int i;
  335. spin_lock_irqsave(&rxq->lock, flags);
  336. INIT_LIST_HEAD(&rxq->rx_free);
  337. INIT_LIST_HEAD(&rxq->rx_used);
  338. /* Fill the rx_used queue with _all_ of the Rx buffers */
  339. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  340. /* In the reset function, these buffers may have been allocated
  341. * to an SKB, so we need to unmap and free potential storage */
  342. if (rxq->pool[i].skb != NULL) {
  343. pci_unmap_single(priv->pci_dev,
  344. rxq->pool[i].real_dma_addr,
  345. priv->hw_params.rx_buf_size + 256,
  346. PCI_DMA_FROMDEVICE);
  347. priv->alloc_rxb_skb--;
  348. dev_kfree_skb(rxq->pool[i].skb);
  349. rxq->pool[i].skb = NULL;
  350. }
  351. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  352. }
  353. /* Set us so that we have processed and used all buffers, but have
  354. * not restocked the Rx queue with fresh buffers */
  355. rxq->read = rxq->write = 0;
  356. rxq->free_count = 0;
  357. spin_unlock_irqrestore(&rxq->lock, flags);
  358. }
  359. EXPORT_SYMBOL(iwl_rx_queue_reset);
  360. int iwl_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  361. {
  362. int ret;
  363. unsigned long flags;
  364. u32 rb_size;
  365. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  366. const u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT why this stalls RX */
  367. spin_lock_irqsave(&priv->lock, flags);
  368. ret = iwl_grab_nic_access(priv);
  369. if (ret) {
  370. spin_unlock_irqrestore(&priv->lock, flags);
  371. return ret;
  372. }
  373. if (priv->cfg->mod_params->amsdu_size_8K)
  374. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  375. else
  376. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  377. /* Stop Rx DMA */
  378. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  379. /* Reset driver's Rx queue write index */
  380. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  381. /* Tell device where to find RBD circular buffer in DRAM */
  382. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  383. (u32)(rxq->dma_addr >> 8));
  384. /* Tell device where in DRAM to update its Rx status */
  385. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  386. rxq->rb_stts_dma >> 4);
  387. /* Enable Rx DMA
  388. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  389. * the credit mechanism in 5000 HW RX FIFO
  390. * Direct rx interrupts to hosts
  391. * Rx buffer size 4 or 8k
  392. * RB timeout 0x10
  393. * 256 RBDs
  394. */
  395. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  396. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  397. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  398. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  399. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  400. rb_size|
  401. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  402. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  403. iwl_release_nic_access(priv);
  404. iwl_write32(priv, CSR_INT_COALESCING, 0x40);
  405. spin_unlock_irqrestore(&priv->lock, flags);
  406. return 0;
  407. }
  408. int iwl_rxq_stop(struct iwl_priv *priv)
  409. {
  410. int ret;
  411. unsigned long flags;
  412. spin_lock_irqsave(&priv->lock, flags);
  413. ret = iwl_grab_nic_access(priv);
  414. if (unlikely(ret)) {
  415. spin_unlock_irqrestore(&priv->lock, flags);
  416. return ret;
  417. }
  418. /* stop Rx DMA */
  419. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  420. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  421. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  422. iwl_release_nic_access(priv);
  423. spin_unlock_irqrestore(&priv->lock, flags);
  424. return 0;
  425. }
  426. EXPORT_SYMBOL(iwl_rxq_stop);
  427. void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
  428. struct iwl_rx_mem_buffer *rxb)
  429. {
  430. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  431. struct iwl_missed_beacon_notif *missed_beacon;
  432. missed_beacon = &pkt->u.missed_beacon;
  433. if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
  434. IWL_DEBUG_CALIB(priv, "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  435. le32_to_cpu(missed_beacon->consequtive_missed_beacons),
  436. le32_to_cpu(missed_beacon->total_missed_becons),
  437. le32_to_cpu(missed_beacon->num_recvd_beacons),
  438. le32_to_cpu(missed_beacon->num_expected_beacons));
  439. if (!test_bit(STATUS_SCANNING, &priv->status))
  440. iwl_init_sensitivity(priv);
  441. }
  442. }
  443. EXPORT_SYMBOL(iwl_rx_missed_beacon_notif);
  444. /* Calculate noise level, based on measurements during network silence just
  445. * before arriving beacon. This measurement can be done only if we know
  446. * exactly when to expect beacons, therefore only when we're associated. */
  447. static void iwl_rx_calc_noise(struct iwl_priv *priv)
  448. {
  449. struct statistics_rx_non_phy *rx_info
  450. = &(priv->statistics.rx.general);
  451. int num_active_rx = 0;
  452. int total_silence = 0;
  453. int bcn_silence_a =
  454. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  455. int bcn_silence_b =
  456. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  457. int bcn_silence_c =
  458. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  459. if (bcn_silence_a) {
  460. total_silence += bcn_silence_a;
  461. num_active_rx++;
  462. }
  463. if (bcn_silence_b) {
  464. total_silence += bcn_silence_b;
  465. num_active_rx++;
  466. }
  467. if (bcn_silence_c) {
  468. total_silence += bcn_silence_c;
  469. num_active_rx++;
  470. }
  471. /* Average among active antennas */
  472. if (num_active_rx)
  473. priv->last_rx_noise = (total_silence / num_active_rx) - 107;
  474. else
  475. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  476. IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
  477. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  478. priv->last_rx_noise);
  479. }
  480. #define REG_RECALIB_PERIOD (60)
  481. void iwl_rx_statistics(struct iwl_priv *priv,
  482. struct iwl_rx_mem_buffer *rxb)
  483. {
  484. int change;
  485. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  486. IWL_DEBUG_RX(priv, "Statistics notification received (%d vs %d).\n",
  487. (int)sizeof(priv->statistics), pkt->len);
  488. change = ((priv->statistics.general.temperature !=
  489. pkt->u.stats.general.temperature) ||
  490. ((priv->statistics.flag &
  491. STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
  492. (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
  493. memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
  494. set_bit(STATUS_STATISTICS, &priv->status);
  495. /* Reschedule the statistics timer to occur in
  496. * REG_RECALIB_PERIOD seconds to ensure we get a
  497. * thermal update even if the uCode doesn't give
  498. * us one */
  499. mod_timer(&priv->statistics_periodic, jiffies +
  500. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  501. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  502. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  503. iwl_rx_calc_noise(priv);
  504. queue_work(priv->workqueue, &priv->run_time_calib_work);
  505. }
  506. iwl_leds_background(priv);
  507. if (priv->cfg->ops->lib->temperature && change)
  508. priv->cfg->ops->lib->temperature(priv);
  509. }
  510. EXPORT_SYMBOL(iwl_rx_statistics);
  511. #define PERFECT_RSSI (-20) /* dBm */
  512. #define WORST_RSSI (-95) /* dBm */
  513. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  514. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  515. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  516. * about formulas used below. */
  517. static int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
  518. {
  519. int sig_qual;
  520. int degradation = PERFECT_RSSI - rssi_dbm;
  521. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  522. * as indicator; formula is (signal dbm - noise dbm).
  523. * SNR at or above 40 is a great signal (100%).
  524. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  525. * Weakest usable signal is usually 10 - 15 dB SNR. */
  526. if (noise_dbm) {
  527. if (rssi_dbm - noise_dbm >= 40)
  528. return 100;
  529. else if (rssi_dbm < noise_dbm)
  530. return 0;
  531. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  532. /* Else use just the signal level.
  533. * This formula is a least squares fit of data points collected and
  534. * compared with a reference system that had a percentage (%) display
  535. * for signal quality. */
  536. } else
  537. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  538. (15 * RSSI_RANGE + 62 * degradation)) /
  539. (RSSI_RANGE * RSSI_RANGE);
  540. if (sig_qual > 100)
  541. sig_qual = 100;
  542. else if (sig_qual < 1)
  543. sig_qual = 0;
  544. return sig_qual;
  545. }
  546. /* Calc max signal level (dBm) among 3 possible receivers */
  547. static inline int iwl_calc_rssi(struct iwl_priv *priv,
  548. struct iwl_rx_phy_res *rx_resp)
  549. {
  550. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  551. }
  552. #ifdef CONFIG_IWLWIFI_DEBUG
  553. /**
  554. * iwl_dbg_report_frame - dump frame to syslog during debug sessions
  555. *
  556. * You may hack this function to show different aspects of received frames,
  557. * including selective frame dumps.
  558. * group100 parameter selects whether to show 1 out of 100 good data frames.
  559. * All beacon and probe response frames are printed.
  560. */
  561. static void iwl_dbg_report_frame(struct iwl_priv *priv,
  562. struct iwl_rx_phy_res *phy_res, u16 length,
  563. struct ieee80211_hdr *header, int group100)
  564. {
  565. u32 to_us;
  566. u32 print_summary = 0;
  567. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  568. u32 hundred = 0;
  569. u32 dataframe = 0;
  570. __le16 fc;
  571. u16 seq_ctl;
  572. u16 channel;
  573. u16 phy_flags;
  574. u32 rate_n_flags;
  575. u32 tsf_low;
  576. int rssi;
  577. if (likely(!(priv->debug_level & IWL_DL_RX)))
  578. return;
  579. /* MAC header */
  580. fc = header->frame_control;
  581. seq_ctl = le16_to_cpu(header->seq_ctrl);
  582. /* metadata */
  583. channel = le16_to_cpu(phy_res->channel);
  584. phy_flags = le16_to_cpu(phy_res->phy_flags);
  585. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  586. /* signal statistics */
  587. rssi = iwl_calc_rssi(priv, phy_res);
  588. tsf_low = le64_to_cpu(phy_res->timestamp) & 0x0ffffffff;
  589. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  590. /* if data frame is to us and all is good,
  591. * (optionally) print summary for only 1 out of every 100 */
  592. if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
  593. cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  594. dataframe = 1;
  595. if (!group100)
  596. print_summary = 1; /* print each frame */
  597. else if (priv->framecnt_to_us < 100) {
  598. priv->framecnt_to_us++;
  599. print_summary = 0;
  600. } else {
  601. priv->framecnt_to_us = 0;
  602. print_summary = 1;
  603. hundred = 1;
  604. }
  605. } else {
  606. /* print summary for all other frames */
  607. print_summary = 1;
  608. }
  609. if (print_summary) {
  610. char *title;
  611. int rate_idx;
  612. u32 bitrate;
  613. if (hundred)
  614. title = "100Frames";
  615. else if (ieee80211_has_retry(fc))
  616. title = "Retry";
  617. else if (ieee80211_is_assoc_resp(fc))
  618. title = "AscRsp";
  619. else if (ieee80211_is_reassoc_resp(fc))
  620. title = "RasRsp";
  621. else if (ieee80211_is_probe_resp(fc)) {
  622. title = "PrbRsp";
  623. print_dump = 1; /* dump frame contents */
  624. } else if (ieee80211_is_beacon(fc)) {
  625. title = "Beacon";
  626. print_dump = 1; /* dump frame contents */
  627. } else if (ieee80211_is_atim(fc))
  628. title = "ATIM";
  629. else if (ieee80211_is_auth(fc))
  630. title = "Auth";
  631. else if (ieee80211_is_deauth(fc))
  632. title = "DeAuth";
  633. else if (ieee80211_is_disassoc(fc))
  634. title = "DisAssoc";
  635. else
  636. title = "Frame";
  637. rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
  638. if (unlikely((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT))) {
  639. bitrate = 0;
  640. WARN_ON_ONCE(1);
  641. } else {
  642. bitrate = iwl_rates[rate_idx].ieee / 2;
  643. }
  644. /* print frame summary.
  645. * MAC addresses show just the last byte (for brevity),
  646. * but you can hack it to show more, if you'd like to. */
  647. if (dataframe)
  648. IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
  649. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  650. title, le16_to_cpu(fc), header->addr1[5],
  651. length, rssi, channel, bitrate);
  652. else {
  653. /* src/dst addresses assume managed mode */
  654. IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, src=0x%02x, "
  655. "len=%u, rssi=%d, tim=%lu usec, "
  656. "phy=0x%02x, chnl=%d\n",
  657. title, le16_to_cpu(fc), header->addr1[5],
  658. header->addr3[5], length, rssi,
  659. tsf_low - priv->scan_start_tsf,
  660. phy_flags, channel);
  661. }
  662. }
  663. if (print_dump)
  664. iwl_print_hex_dump(priv, IWL_DL_RX, header, length);
  665. }
  666. #endif
  667. static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  668. {
  669. /* 0 - mgmt, 1 - cnt, 2 - data */
  670. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  671. priv->rx_stats[idx].cnt++;
  672. priv->rx_stats[idx].bytes += len;
  673. }
  674. /*
  675. * returns non-zero if packet should be dropped
  676. */
  677. int iwl_set_decrypted_flag(struct iwl_priv *priv,
  678. struct ieee80211_hdr *hdr,
  679. u32 decrypt_res,
  680. struct ieee80211_rx_status *stats)
  681. {
  682. u16 fc = le16_to_cpu(hdr->frame_control);
  683. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  684. return 0;
  685. if (!(fc & IEEE80211_FCTL_PROTECTED))
  686. return 0;
  687. IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
  688. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  689. case RX_RES_STATUS_SEC_TYPE_TKIP:
  690. /* The uCode has got a bad phase 1 Key, pushes the packet.
  691. * Decryption will be done in SW. */
  692. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  693. RX_RES_STATUS_BAD_KEY_TTAK)
  694. break;
  695. case RX_RES_STATUS_SEC_TYPE_WEP:
  696. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  697. RX_RES_STATUS_BAD_ICV_MIC) {
  698. /* bad ICV, the packet is destroyed since the
  699. * decryption is inplace, drop it */
  700. IWL_DEBUG_RX(priv, "Packet destroyed\n");
  701. return -1;
  702. }
  703. case RX_RES_STATUS_SEC_TYPE_CCMP:
  704. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  705. RX_RES_STATUS_DECRYPT_OK) {
  706. IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
  707. stats->flag |= RX_FLAG_DECRYPTED;
  708. }
  709. break;
  710. default:
  711. break;
  712. }
  713. return 0;
  714. }
  715. EXPORT_SYMBOL(iwl_set_decrypted_flag);
  716. static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  717. {
  718. u32 decrypt_out = 0;
  719. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  720. RX_RES_STATUS_STATION_FOUND)
  721. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  722. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  723. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  724. /* packet was not encrypted */
  725. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  726. RX_RES_STATUS_SEC_TYPE_NONE)
  727. return decrypt_out;
  728. /* packet was encrypted with unknown alg */
  729. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  730. RX_RES_STATUS_SEC_TYPE_ERR)
  731. return decrypt_out;
  732. /* decryption was not done in HW */
  733. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  734. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  735. return decrypt_out;
  736. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  737. case RX_RES_STATUS_SEC_TYPE_CCMP:
  738. /* alg is CCM: check MIC only */
  739. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  740. /* Bad MIC */
  741. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  742. else
  743. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  744. break;
  745. case RX_RES_STATUS_SEC_TYPE_TKIP:
  746. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  747. /* Bad TTAK */
  748. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  749. break;
  750. }
  751. /* fall through if TTAK OK */
  752. default:
  753. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  754. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  755. else
  756. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  757. break;
  758. };
  759. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  760. decrypt_in, decrypt_out);
  761. return decrypt_out;
  762. }
  763. static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
  764. int include_phy,
  765. struct iwl_rx_mem_buffer *rxb,
  766. struct ieee80211_rx_status *stats)
  767. {
  768. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  769. struct iwl_rx_phy_res *rx_start = (include_phy) ?
  770. (struct iwl_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
  771. struct ieee80211_hdr *hdr;
  772. u16 len;
  773. __le32 *rx_end;
  774. unsigned int skblen;
  775. u32 ampdu_status;
  776. u32 ampdu_status_legacy;
  777. if (!include_phy && priv->last_phy_res[0])
  778. rx_start = (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  779. if (!rx_start) {
  780. IWL_ERR(priv, "MPDU frame without a PHY data\n");
  781. return;
  782. }
  783. if (include_phy) {
  784. hdr = (struct ieee80211_hdr *)((u8 *) &rx_start[1] +
  785. rx_start->cfg_phy_cnt);
  786. len = le16_to_cpu(rx_start->byte_count);
  787. rx_end = (__le32 *)((u8 *) &pkt->u.raw[0] +
  788. sizeof(struct iwl_rx_phy_res) +
  789. rx_start->cfg_phy_cnt + len);
  790. } else {
  791. struct iwl4965_rx_mpdu_res_start *amsdu =
  792. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  793. hdr = (struct ieee80211_hdr *)(pkt->u.raw +
  794. sizeof(struct iwl4965_rx_mpdu_res_start));
  795. len = le16_to_cpu(amsdu->byte_count);
  796. rx_start->byte_count = amsdu->byte_count;
  797. rx_end = (__le32 *) (((u8 *) hdr) + len);
  798. }
  799. ampdu_status = le32_to_cpu(*rx_end);
  800. skblen = ((u8 *) rx_end - (u8 *) &pkt->u.raw[0]) + sizeof(u32);
  801. if (!include_phy) {
  802. /* New status scheme, need to translate */
  803. ampdu_status_legacy = ampdu_status;
  804. ampdu_status = iwl_translate_rx_status(priv, ampdu_status);
  805. }
  806. /* start from MAC */
  807. skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
  808. skb_put(rxb->skb, len); /* end where data ends */
  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. hdr = (struct ieee80211_hdr *)rxb->skb->data;
  816. /* in case of HW accelerated crypto and bad decryption, drop */
  817. if (!priv->hw_params.sw_crypto &&
  818. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  819. return;
  820. iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
  821. ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
  822. priv->alloc_rxb_skb--;
  823. rxb->skb = NULL;
  824. }
  825. /* This is necessary only for a number of statistics, see the caller. */
  826. static int iwl_is_network_packet(struct iwl_priv *priv,
  827. struct ieee80211_hdr *header)
  828. {
  829. /* Filter incoming packets to determine if they are targeted toward
  830. * this network, discarding packets coming from ourselves */
  831. switch (priv->iw_mode) {
  832. case NL80211_IFTYPE_ADHOC: /* Header: Dest. | Source | BSSID */
  833. /* packets to our IBSS update information */
  834. return !compare_ether_addr(header->addr3, priv->bssid);
  835. case NL80211_IFTYPE_STATION: /* Header: Dest. | AP{BSSID} | Source */
  836. /* packets to our IBSS update information */
  837. return !compare_ether_addr(header->addr2, priv->bssid);
  838. default:
  839. return 1;
  840. }
  841. }
  842. /* Called for REPLY_RX (legacy ABG frames), or
  843. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  844. void iwl_rx_reply_rx(struct iwl_priv *priv,
  845. struct iwl_rx_mem_buffer *rxb)
  846. {
  847. struct ieee80211_hdr *header;
  848. struct ieee80211_rx_status rx_status;
  849. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  850. /* Use phy data (Rx signal strength, etc.) contained within
  851. * this rx packet for legacy frames,
  852. * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
  853. int include_phy = (pkt->hdr.cmd == REPLY_RX);
  854. struct iwl_rx_phy_res *rx_start = (include_phy) ?
  855. (struct iwl_rx_phy_res *)&(pkt->u.raw[0]) :
  856. (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  857. __le32 *rx_end;
  858. unsigned int len = 0;
  859. u16 fc;
  860. u8 network_packet;
  861. rx_status.mactime = le64_to_cpu(rx_start->timestamp);
  862. rx_status.freq =
  863. ieee80211_channel_to_frequency(le16_to_cpu(rx_start->channel));
  864. rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  865. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  866. rx_status.rate_idx =
  867. iwl_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
  868. if (rx_status.band == IEEE80211_BAND_5GHZ)
  869. rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
  870. rx_status.flag = 0;
  871. /* TSF isn't reliable. In order to allow smooth user experience,
  872. * this W/A doesn't propagate it to the mac80211 */
  873. /*rx_status.flag |= RX_FLAG_TSFT;*/
  874. if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
  875. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  876. rx_start->cfg_phy_cnt);
  877. return;
  878. }
  879. if (!include_phy) {
  880. if (priv->last_phy_res[0])
  881. rx_start = (struct iwl_rx_phy_res *)
  882. &priv->last_phy_res[1];
  883. else
  884. rx_start = NULL;
  885. }
  886. if (!rx_start) {
  887. IWL_ERR(priv, "MPDU frame without a PHY data\n");
  888. return;
  889. }
  890. if (include_phy) {
  891. header = (struct ieee80211_hdr *)((u8 *) &rx_start[1]
  892. + rx_start->cfg_phy_cnt);
  893. len = le16_to_cpu(rx_start->byte_count);
  894. rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
  895. sizeof(struct iwl_rx_phy_res) + len);
  896. } else {
  897. struct iwl4965_rx_mpdu_res_start *amsdu =
  898. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  899. header = (void *)(pkt->u.raw +
  900. sizeof(struct iwl4965_rx_mpdu_res_start));
  901. len = le16_to_cpu(amsdu->byte_count);
  902. rx_end = (__le32 *) (pkt->u.raw +
  903. sizeof(struct iwl4965_rx_mpdu_res_start) + len);
  904. }
  905. if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
  906. !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  907. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  908. le32_to_cpu(*rx_end));
  909. return;
  910. }
  911. priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
  912. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  913. rx_status.signal = iwl_calc_rssi(priv, rx_start);
  914. /* Meaningful noise values are available only from beacon statistics,
  915. * which are gathered only when associated, and indicate noise
  916. * only for the associated network channel ...
  917. * Ignore these noise values while scanning (other channels) */
  918. if (iwl_is_associated(priv) &&
  919. !test_bit(STATUS_SCANNING, &priv->status)) {
  920. rx_status.noise = priv->last_rx_noise;
  921. rx_status.qual = iwl_calc_sig_qual(rx_status.signal,
  922. rx_status.noise);
  923. } else {
  924. rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  925. rx_status.qual = iwl_calc_sig_qual(rx_status.signal, 0);
  926. }
  927. /* Reset beacon noise level if not associated. */
  928. if (!iwl_is_associated(priv))
  929. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  930. /* Set "1" to report good data frames in groups of 100 */
  931. #ifdef CONFIG_IWLWIFI_DEBUG
  932. if (unlikely(priv->debug_level & IWL_DL_RX))
  933. iwl_dbg_report_frame(priv, rx_start, len, header, 1);
  934. #endif
  935. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, noise %d, qual %d, TSF %llu\n",
  936. rx_status.signal, rx_status.noise, rx_status.signal,
  937. (unsigned long long)rx_status.mactime);
  938. /*
  939. * "antenna number"
  940. *
  941. * It seems that the antenna field in the phy flags value
  942. * is actually a bit field. This is undefined by radiotap,
  943. * it wants an actual antenna number but I always get "7"
  944. * for most legacy frames I receive indicating that the
  945. * same frame was received on all three RX chains.
  946. *
  947. * I think this field should be removed in favor of a
  948. * new 802.11n radiotap field "RX chains" that is defined
  949. * as a bitmask.
  950. */
  951. rx_status.antenna = le16_to_cpu(rx_start->phy_flags &
  952. RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
  953. /* set the preamble flag if appropriate */
  954. if (rx_start->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  955. rx_status.flag |= RX_FLAG_SHORTPRE;
  956. /* Take shortcut when only in monitor mode */
  957. if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
  958. iwl_pass_packet_to_mac80211(priv, include_phy,
  959. rxb, &rx_status);
  960. return;
  961. }
  962. network_packet = iwl_is_network_packet(priv, header);
  963. if (network_packet) {
  964. priv->last_rx_rssi = rx_status.signal;
  965. priv->last_beacon_time = priv->ucode_beacon_time;
  966. priv->last_tsf = le64_to_cpu(rx_start->timestamp);
  967. }
  968. fc = le16_to_cpu(header->frame_control);
  969. switch (fc & IEEE80211_FCTL_FTYPE) {
  970. case IEEE80211_FTYPE_MGMT:
  971. case IEEE80211_FTYPE_DATA:
  972. if (priv->iw_mode == NL80211_IFTYPE_AP)
  973. iwl_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  974. header->addr2);
  975. /* fall through */
  976. default:
  977. iwl_pass_packet_to_mac80211(priv, include_phy, rxb,
  978. &rx_status);
  979. break;
  980. }
  981. }
  982. EXPORT_SYMBOL(iwl_rx_reply_rx);
  983. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  984. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  985. void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
  986. struct iwl_rx_mem_buffer *rxb)
  987. {
  988. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  989. priv->last_phy_res[0] = 1;
  990. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  991. sizeof(struct iwl_rx_phy_res));
  992. }
  993. EXPORT_SYMBOL(iwl_rx_reply_rx_phy);