iwl-rx.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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_DEBUG_INFO(priv, "Rx queue requesting wakeup, GP1 = 0x%x\n",
  137. reg);
  138. iwl_set_bit(priv, CSR_GP_CNTRL,
  139. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  140. goto exit_unlock;
  141. }
  142. q->write_actual = (q->write & ~0x7);
  143. iwl_write_direct32(priv, rx_wrt_ptr_reg, q->write_actual);
  144. /* Else device is assumed to be awake */
  145. } else {
  146. /* Device expects a multiple of 8 */
  147. q->write_actual = (q->write & ~0x7);
  148. iwl_write_direct32(priv, rx_wrt_ptr_reg, q->write_actual);
  149. }
  150. q->need_update = 0;
  151. exit_unlock:
  152. spin_unlock_irqrestore(&q->lock, flags);
  153. return ret;
  154. }
  155. EXPORT_SYMBOL(iwl_rx_queue_update_write_ptr);
  156. /**
  157. * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  158. */
  159. static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
  160. dma_addr_t dma_addr)
  161. {
  162. return cpu_to_le32((u32)(dma_addr >> 8));
  163. }
  164. /**
  165. * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
  166. *
  167. * If there are slots in the RX queue that need to be restocked,
  168. * and we have free pre-allocated buffers, fill the ranks as much
  169. * as we can, pulling from rx_free.
  170. *
  171. * This moves the 'write' index forward to catch up with 'processed', and
  172. * also updates the memory address in the firmware to reference the new
  173. * target buffer.
  174. */
  175. int iwl_rx_queue_restock(struct iwl_priv *priv)
  176. {
  177. struct iwl_rx_queue *rxq = &priv->rxq;
  178. struct list_head *element;
  179. struct iwl_rx_mem_buffer *rxb;
  180. unsigned long flags;
  181. int write;
  182. int ret = 0;
  183. spin_lock_irqsave(&rxq->lock, flags);
  184. write = rxq->write & ~0x7;
  185. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  186. /* Get next free Rx buffer, remove from free list */
  187. element = rxq->rx_free.next;
  188. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  189. list_del(element);
  190. /* Point to Rx buffer via next RBD in circular buffer */
  191. rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->page_dma);
  192. rxq->queue[rxq->write] = rxb;
  193. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  194. rxq->free_count--;
  195. }
  196. spin_unlock_irqrestore(&rxq->lock, flags);
  197. /* If the pre-allocated buffer pool is dropping low, schedule to
  198. * refill it */
  199. if (rxq->free_count <= RX_LOW_WATERMARK)
  200. queue_work(priv->workqueue, &priv->rx_replenish);
  201. /* If we've added more space for the firmware to place data, tell it.
  202. * Increment device's write pointer in multiples of 8. */
  203. if (rxq->write_actual != (rxq->write & ~0x7)) {
  204. spin_lock_irqsave(&rxq->lock, flags);
  205. rxq->need_update = 1;
  206. spin_unlock_irqrestore(&rxq->lock, flags);
  207. ret = iwl_rx_queue_update_write_ptr(priv, rxq);
  208. }
  209. return ret;
  210. }
  211. EXPORT_SYMBOL(iwl_rx_queue_restock);
  212. /**
  213. * iwl_rx_replenish - Move all used packet from rx_used to rx_free
  214. *
  215. * When moving to rx_free an SKB is allocated for the slot.
  216. *
  217. * Also restock the Rx queue via iwl_rx_queue_restock.
  218. * This is called as a scheduled work item (except for during initialization)
  219. */
  220. void iwl_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  221. {
  222. struct iwl_rx_queue *rxq = &priv->rxq;
  223. struct list_head *element;
  224. struct iwl_rx_mem_buffer *rxb;
  225. struct page *page;
  226. unsigned long flags;
  227. gfp_t gfp_mask = priority;
  228. while (1) {
  229. spin_lock_irqsave(&rxq->lock, flags);
  230. if (list_empty(&rxq->rx_used)) {
  231. spin_unlock_irqrestore(&rxq->lock, flags);
  232. return;
  233. }
  234. spin_unlock_irqrestore(&rxq->lock, flags);
  235. if (rxq->free_count > RX_LOW_WATERMARK)
  236. gfp_mask |= __GFP_NOWARN;
  237. if (priv->hw_params.rx_page_order > 0)
  238. gfp_mask |= __GFP_COMP;
  239. /* Alloc a new receive buffer */
  240. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  241. if (!page) {
  242. if (net_ratelimit())
  243. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  244. "order: %d\n",
  245. priv->hw_params.rx_page_order);
  246. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  247. net_ratelimit())
  248. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  249. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  250. rxq->free_count);
  251. /* We don't reschedule replenish work here -- we will
  252. * call the restock method and if it still needs
  253. * more buffers it will schedule replenish */
  254. return;
  255. }
  256. spin_lock_irqsave(&rxq->lock, flags);
  257. if (list_empty(&rxq->rx_used)) {
  258. spin_unlock_irqrestore(&rxq->lock, flags);
  259. __free_pages(page, priv->hw_params.rx_page_order);
  260. return;
  261. }
  262. element = rxq->rx_used.next;
  263. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  264. list_del(element);
  265. spin_unlock_irqrestore(&rxq->lock, flags);
  266. rxb->page = page;
  267. /* Get physical address of the RB */
  268. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  269. PAGE_SIZE << priv->hw_params.rx_page_order,
  270. PCI_DMA_FROMDEVICE);
  271. /* dma address must be no more than 36 bits */
  272. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  273. /* and also 256 byte aligned! */
  274. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  275. spin_lock_irqsave(&rxq->lock, flags);
  276. list_add_tail(&rxb->list, &rxq->rx_free);
  277. rxq->free_count++;
  278. priv->alloc_rxb_page++;
  279. spin_unlock_irqrestore(&rxq->lock, flags);
  280. }
  281. }
  282. void iwl_rx_replenish(struct iwl_priv *priv)
  283. {
  284. unsigned long flags;
  285. iwl_rx_allocate(priv, GFP_KERNEL);
  286. spin_lock_irqsave(&priv->lock, flags);
  287. iwl_rx_queue_restock(priv);
  288. spin_unlock_irqrestore(&priv->lock, flags);
  289. }
  290. EXPORT_SYMBOL(iwl_rx_replenish);
  291. void iwl_rx_replenish_now(struct iwl_priv *priv)
  292. {
  293. iwl_rx_allocate(priv, GFP_ATOMIC);
  294. iwl_rx_queue_restock(priv);
  295. }
  296. EXPORT_SYMBOL(iwl_rx_replenish_now);
  297. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  298. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  299. * This free routine walks the list of POOL entries and if SKB is set to
  300. * non NULL it is unmapped and freed
  301. */
  302. void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  303. {
  304. int i;
  305. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  306. if (rxq->pool[i].page != NULL) {
  307. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  308. PAGE_SIZE << priv->hw_params.rx_page_order,
  309. PCI_DMA_FROMDEVICE);
  310. __iwl_free_pages(priv, rxq->pool[i].page);
  311. rxq->pool[i].page = NULL;
  312. }
  313. }
  314. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  315. rxq->dma_addr);
  316. pci_free_consistent(priv->pci_dev, sizeof(struct iwl_rb_status),
  317. rxq->rb_stts, rxq->rb_stts_dma);
  318. rxq->bd = NULL;
  319. rxq->rb_stts = NULL;
  320. }
  321. EXPORT_SYMBOL(iwl_rx_queue_free);
  322. int iwl_rx_queue_alloc(struct iwl_priv *priv)
  323. {
  324. struct iwl_rx_queue *rxq = &priv->rxq;
  325. struct pci_dev *dev = priv->pci_dev;
  326. int i;
  327. spin_lock_init(&rxq->lock);
  328. INIT_LIST_HEAD(&rxq->rx_free);
  329. INIT_LIST_HEAD(&rxq->rx_used);
  330. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  331. rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
  332. if (!rxq->bd)
  333. goto err_bd;
  334. rxq->rb_stts = pci_alloc_consistent(dev, sizeof(struct iwl_rb_status),
  335. &rxq->rb_stts_dma);
  336. if (!rxq->rb_stts)
  337. goto err_rb;
  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. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  341. /* Set us so that we have processed and used all buffers, but have
  342. * not restocked the Rx queue with fresh buffers */
  343. rxq->read = rxq->write = 0;
  344. rxq->write_actual = 0;
  345. rxq->free_count = 0;
  346. rxq->need_update = 0;
  347. return 0;
  348. err_rb:
  349. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  350. rxq->dma_addr);
  351. err_bd:
  352. return -ENOMEM;
  353. }
  354. EXPORT_SYMBOL(iwl_rx_queue_alloc);
  355. void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  356. {
  357. unsigned long flags;
  358. int i;
  359. spin_lock_irqsave(&rxq->lock, flags);
  360. INIT_LIST_HEAD(&rxq->rx_free);
  361. INIT_LIST_HEAD(&rxq->rx_used);
  362. /* Fill the rx_used queue with _all_ of the Rx buffers */
  363. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  364. /* In the reset function, these buffers may have been allocated
  365. * to an SKB, so we need to unmap and free potential storage */
  366. if (rxq->pool[i].page != NULL) {
  367. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  368. PAGE_SIZE << priv->hw_params.rx_page_order,
  369. PCI_DMA_FROMDEVICE);
  370. __iwl_free_pages(priv, rxq->pool[i].page);
  371. rxq->pool[i].page = NULL;
  372. }
  373. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  374. }
  375. /* Set us so that we have processed and used all buffers, but have
  376. * not restocked the Rx queue with fresh buffers */
  377. rxq->read = rxq->write = 0;
  378. rxq->write_actual = 0;
  379. rxq->free_count = 0;
  380. spin_unlock_irqrestore(&rxq->lock, flags);
  381. }
  382. int iwl_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  383. {
  384. u32 rb_size;
  385. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  386. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  387. if (!priv->cfg->use_isr_legacy)
  388. rb_timeout = RX_RB_TIMEOUT;
  389. if (priv->cfg->mod_params->amsdu_size_8K)
  390. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  391. else
  392. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  393. /* Stop Rx DMA */
  394. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  395. /* Reset driver's Rx queue write index */
  396. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  397. /* Tell device where to find RBD circular buffer in DRAM */
  398. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  399. (u32)(rxq->dma_addr >> 8));
  400. /* Tell device where in DRAM to update its Rx status */
  401. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  402. rxq->rb_stts_dma >> 4);
  403. /* Enable Rx DMA
  404. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  405. * the credit mechanism in 5000 HW RX FIFO
  406. * Direct rx interrupts to hosts
  407. * Rx buffer size 4 or 8k
  408. * RB timeout 0x10
  409. * 256 RBDs
  410. */
  411. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  412. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  413. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  414. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  415. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  416. rb_size|
  417. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  418. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  419. /* Set interrupt coalescing timer to 64 x 32 = 2048 usecs */
  420. iwl_write8(priv, CSR_INT_COALESCING, 0x40);
  421. return 0;
  422. }
  423. int iwl_rxq_stop(struct iwl_priv *priv)
  424. {
  425. /* stop Rx DMA */
  426. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  427. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  428. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  429. return 0;
  430. }
  431. EXPORT_SYMBOL(iwl_rxq_stop);
  432. void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
  433. struct iwl_rx_mem_buffer *rxb)
  434. {
  435. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  436. struct iwl_missed_beacon_notif *missed_beacon;
  437. missed_beacon = &pkt->u.missed_beacon;
  438. if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
  439. IWL_DEBUG_CALIB(priv, "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  440. le32_to_cpu(missed_beacon->consequtive_missed_beacons),
  441. le32_to_cpu(missed_beacon->total_missed_becons),
  442. le32_to_cpu(missed_beacon->num_recvd_beacons),
  443. le32_to_cpu(missed_beacon->num_expected_beacons));
  444. if (!test_bit(STATUS_SCANNING, &priv->status))
  445. iwl_init_sensitivity(priv);
  446. }
  447. }
  448. EXPORT_SYMBOL(iwl_rx_missed_beacon_notif);
  449. /* Calculate noise level, based on measurements during network silence just
  450. * before arriving beacon. This measurement can be done only if we know
  451. * exactly when to expect beacons, therefore only when we're associated. */
  452. static void iwl_rx_calc_noise(struct iwl_priv *priv)
  453. {
  454. struct statistics_rx_non_phy *rx_info
  455. = &(priv->statistics.rx.general);
  456. int num_active_rx = 0;
  457. int total_silence = 0;
  458. int bcn_silence_a =
  459. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  460. int bcn_silence_b =
  461. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  462. int bcn_silence_c =
  463. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  464. if (bcn_silence_a) {
  465. total_silence += bcn_silence_a;
  466. num_active_rx++;
  467. }
  468. if (bcn_silence_b) {
  469. total_silence += bcn_silence_b;
  470. num_active_rx++;
  471. }
  472. if (bcn_silence_c) {
  473. total_silence += bcn_silence_c;
  474. num_active_rx++;
  475. }
  476. /* Average among active antennas */
  477. if (num_active_rx)
  478. priv->last_rx_noise = (total_silence / num_active_rx) - 107;
  479. else
  480. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  481. IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
  482. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  483. priv->last_rx_noise);
  484. }
  485. #ifdef CONFIG_IWLWIFI_DEBUG
  486. /*
  487. * based on the assumption of all statistics counter are in DWORD
  488. * FIXME: This function is for debugging, do not deal with
  489. * the case of counters roll-over.
  490. */
  491. static void iwl_accumulative_statistics(struct iwl_priv *priv,
  492. __le32 *stats)
  493. {
  494. int i;
  495. __le32 *prev_stats;
  496. u32 *accum_stats;
  497. prev_stats = (__le32 *)&priv->statistics;
  498. accum_stats = (u32 *)&priv->accum_statistics;
  499. for (i = sizeof(__le32); i < sizeof(struct iwl_notif_statistics);
  500. i += sizeof(__le32), stats++, prev_stats++, accum_stats++)
  501. if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats))
  502. *accum_stats += (le32_to_cpu(*stats) -
  503. le32_to_cpu(*prev_stats));
  504. /* reset accumulative statistics for "no-counter" type statistics */
  505. priv->accum_statistics.general.temperature =
  506. priv->statistics.general.temperature;
  507. priv->accum_statistics.general.temperature_m =
  508. priv->statistics.general.temperature_m;
  509. priv->accum_statistics.general.ttl_timestamp =
  510. priv->statistics.general.ttl_timestamp;
  511. priv->accum_statistics.tx.tx_power.ant_a =
  512. priv->statistics.tx.tx_power.ant_a;
  513. priv->accum_statistics.tx.tx_power.ant_b =
  514. priv->statistics.tx.tx_power.ant_b;
  515. priv->accum_statistics.tx.tx_power.ant_c =
  516. priv->statistics.tx.tx_power.ant_c;
  517. }
  518. #endif
  519. #define REG_RECALIB_PERIOD (60)
  520. void iwl_rx_statistics(struct iwl_priv *priv,
  521. struct iwl_rx_mem_buffer *rxb)
  522. {
  523. int change;
  524. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  525. IWL_DEBUG_RX(priv, "Statistics notification received (%d vs %d).\n",
  526. (int)sizeof(priv->statistics),
  527. le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK);
  528. change = ((priv->statistics.general.temperature !=
  529. pkt->u.stats.general.temperature) ||
  530. ((priv->statistics.flag &
  531. STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
  532. (pkt->u.stats.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
  533. #ifdef CONFIG_IWLWIFI_DEBUG
  534. iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats);
  535. #endif
  536. memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
  537. set_bit(STATUS_STATISTICS, &priv->status);
  538. /* Reschedule the statistics timer to occur in
  539. * REG_RECALIB_PERIOD seconds to ensure we get a
  540. * thermal update even if the uCode doesn't give
  541. * us one */
  542. mod_timer(&priv->statistics_periodic, jiffies +
  543. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  544. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  545. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  546. iwl_rx_calc_noise(priv);
  547. queue_work(priv->workqueue, &priv->run_time_calib_work);
  548. }
  549. if (priv->cfg->ops->lib->temp_ops.temperature && change)
  550. priv->cfg->ops->lib->temp_ops.temperature(priv);
  551. }
  552. EXPORT_SYMBOL(iwl_rx_statistics);
  553. void iwl_reply_statistics(struct iwl_priv *priv,
  554. struct iwl_rx_mem_buffer *rxb)
  555. {
  556. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  557. if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
  558. #ifdef CONFIG_IWLWIFI_DEBUG
  559. memset(&priv->accum_statistics, 0,
  560. sizeof(struct iwl_notif_statistics));
  561. #endif
  562. IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
  563. }
  564. iwl_rx_statistics(priv, rxb);
  565. }
  566. EXPORT_SYMBOL(iwl_reply_statistics);
  567. /* Calc max signal level (dBm) among 3 possible receivers */
  568. static inline int iwl_calc_rssi(struct iwl_priv *priv,
  569. struct iwl_rx_phy_res *rx_resp)
  570. {
  571. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  572. }
  573. #ifdef CONFIG_IWLWIFI_DEBUG
  574. /**
  575. * iwl_dbg_report_frame - dump frame to syslog during debug sessions
  576. *
  577. * You may hack this function to show different aspects of received frames,
  578. * including selective frame dumps.
  579. * group100 parameter selects whether to show 1 out of 100 good data frames.
  580. * All beacon and probe response frames are printed.
  581. */
  582. static void iwl_dbg_report_frame(struct iwl_priv *priv,
  583. struct iwl_rx_phy_res *phy_res, u16 length,
  584. struct ieee80211_hdr *header, int group100)
  585. {
  586. u32 to_us;
  587. u32 print_summary = 0;
  588. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  589. u32 hundred = 0;
  590. u32 dataframe = 0;
  591. __le16 fc;
  592. u16 seq_ctl;
  593. u16 channel;
  594. u16 phy_flags;
  595. u32 rate_n_flags;
  596. u32 tsf_low;
  597. int rssi;
  598. if (likely(!(iwl_get_debug_level(priv) & IWL_DL_RX)))
  599. return;
  600. /* MAC header */
  601. fc = header->frame_control;
  602. seq_ctl = le16_to_cpu(header->seq_ctrl);
  603. /* metadata */
  604. channel = le16_to_cpu(phy_res->channel);
  605. phy_flags = le16_to_cpu(phy_res->phy_flags);
  606. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  607. /* signal statistics */
  608. rssi = iwl_calc_rssi(priv, phy_res);
  609. tsf_low = le64_to_cpu(phy_res->timestamp) & 0x0ffffffff;
  610. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  611. /* if data frame is to us and all is good,
  612. * (optionally) print summary for only 1 out of every 100 */
  613. if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
  614. cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  615. dataframe = 1;
  616. if (!group100)
  617. print_summary = 1; /* print each frame */
  618. else if (priv->framecnt_to_us < 100) {
  619. priv->framecnt_to_us++;
  620. print_summary = 0;
  621. } else {
  622. priv->framecnt_to_us = 0;
  623. print_summary = 1;
  624. hundred = 1;
  625. }
  626. } else {
  627. /* print summary for all other frames */
  628. print_summary = 1;
  629. }
  630. if (print_summary) {
  631. char *title;
  632. int rate_idx;
  633. u32 bitrate;
  634. if (hundred)
  635. title = "100Frames";
  636. else if (ieee80211_has_retry(fc))
  637. title = "Retry";
  638. else if (ieee80211_is_assoc_resp(fc))
  639. title = "AscRsp";
  640. else if (ieee80211_is_reassoc_resp(fc))
  641. title = "RasRsp";
  642. else if (ieee80211_is_probe_resp(fc)) {
  643. title = "PrbRsp";
  644. print_dump = 1; /* dump frame contents */
  645. } else if (ieee80211_is_beacon(fc)) {
  646. title = "Beacon";
  647. print_dump = 1; /* dump frame contents */
  648. } else if (ieee80211_is_atim(fc))
  649. title = "ATIM";
  650. else if (ieee80211_is_auth(fc))
  651. title = "Auth";
  652. else if (ieee80211_is_deauth(fc))
  653. title = "DeAuth";
  654. else if (ieee80211_is_disassoc(fc))
  655. title = "DisAssoc";
  656. else
  657. title = "Frame";
  658. rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
  659. if (unlikely((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT))) {
  660. bitrate = 0;
  661. WARN_ON_ONCE(1);
  662. } else {
  663. bitrate = iwl_rates[rate_idx].ieee / 2;
  664. }
  665. /* print frame summary.
  666. * MAC addresses show just the last byte (for brevity),
  667. * but you can hack it to show more, if you'd like to. */
  668. if (dataframe)
  669. IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
  670. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  671. title, le16_to_cpu(fc), header->addr1[5],
  672. length, rssi, channel, bitrate);
  673. else {
  674. /* src/dst addresses assume managed mode */
  675. IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, src=0x%02x, "
  676. "len=%u, rssi=%d, tim=%lu usec, "
  677. "phy=0x%02x, chnl=%d\n",
  678. title, le16_to_cpu(fc), header->addr1[5],
  679. header->addr3[5], length, rssi,
  680. tsf_low - priv->scan_start_tsf,
  681. phy_flags, channel);
  682. }
  683. }
  684. if (print_dump)
  685. iwl_print_hex_dump(priv, IWL_DL_RX, header, length);
  686. }
  687. #endif
  688. /*
  689. * returns non-zero if packet should be dropped
  690. */
  691. int iwl_set_decrypted_flag(struct iwl_priv *priv,
  692. struct ieee80211_hdr *hdr,
  693. u32 decrypt_res,
  694. struct ieee80211_rx_status *stats)
  695. {
  696. u16 fc = le16_to_cpu(hdr->frame_control);
  697. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  698. return 0;
  699. if (!(fc & IEEE80211_FCTL_PROTECTED))
  700. return 0;
  701. IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
  702. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  703. case RX_RES_STATUS_SEC_TYPE_TKIP:
  704. /* The uCode has got a bad phase 1 Key, pushes the packet.
  705. * Decryption will be done in SW. */
  706. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  707. RX_RES_STATUS_BAD_KEY_TTAK)
  708. break;
  709. case RX_RES_STATUS_SEC_TYPE_WEP:
  710. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  711. RX_RES_STATUS_BAD_ICV_MIC) {
  712. /* bad ICV, the packet is destroyed since the
  713. * decryption is inplace, drop it */
  714. IWL_DEBUG_RX(priv, "Packet destroyed\n");
  715. return -1;
  716. }
  717. case RX_RES_STATUS_SEC_TYPE_CCMP:
  718. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  719. RX_RES_STATUS_DECRYPT_OK) {
  720. IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
  721. stats->flag |= RX_FLAG_DECRYPTED;
  722. }
  723. break;
  724. default:
  725. break;
  726. }
  727. return 0;
  728. }
  729. EXPORT_SYMBOL(iwl_set_decrypted_flag);
  730. static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  731. {
  732. u32 decrypt_out = 0;
  733. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  734. RX_RES_STATUS_STATION_FOUND)
  735. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  736. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  737. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  738. /* packet was not encrypted */
  739. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  740. RX_RES_STATUS_SEC_TYPE_NONE)
  741. return decrypt_out;
  742. /* packet was encrypted with unknown alg */
  743. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  744. RX_RES_STATUS_SEC_TYPE_ERR)
  745. return decrypt_out;
  746. /* decryption was not done in HW */
  747. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  748. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  749. return decrypt_out;
  750. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  751. case RX_RES_STATUS_SEC_TYPE_CCMP:
  752. /* alg is CCM: check MIC only */
  753. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  754. /* Bad MIC */
  755. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  756. else
  757. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  758. break;
  759. case RX_RES_STATUS_SEC_TYPE_TKIP:
  760. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  761. /* Bad TTAK */
  762. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  763. break;
  764. }
  765. /* fall through if TTAK OK */
  766. default:
  767. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  768. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  769. else
  770. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  771. break;
  772. };
  773. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  774. decrypt_in, decrypt_out);
  775. return decrypt_out;
  776. }
  777. static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
  778. struct ieee80211_hdr *hdr,
  779. u16 len,
  780. u32 ampdu_status,
  781. struct iwl_rx_mem_buffer *rxb,
  782. struct ieee80211_rx_status *stats)
  783. {
  784. struct sk_buff *skb;
  785. int ret = 0;
  786. __le16 fc = hdr->frame_control;
  787. /* We only process data packets if the interface is open */
  788. if (unlikely(!priv->is_open)) {
  789. IWL_DEBUG_DROP_LIMIT(priv,
  790. "Dropping packet while interface is not open.\n");
  791. return;
  792. }
  793. /* In case of HW accelerated crypto and bad decryption, drop */
  794. if (!priv->cfg->mod_params->sw_crypto &&
  795. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  796. return;
  797. skb = alloc_skb(IWL_LINK_HDR_MAX * 2, GFP_ATOMIC);
  798. if (!skb) {
  799. IWL_ERR(priv, "alloc_skb failed\n");
  800. return;
  801. }
  802. skb_reserve(skb, IWL_LINK_HDR_MAX);
  803. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
  804. /* mac80211 currently doesn't support paged SKB. Convert it to
  805. * linear SKB for management frame and data frame requires
  806. * software decryption or software defragementation. */
  807. if (ieee80211_is_mgmt(fc) ||
  808. ieee80211_has_protected(fc) ||
  809. ieee80211_has_morefrags(fc) ||
  810. le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)
  811. ret = skb_linearize(skb);
  812. else
  813. ret = __pskb_pull_tail(skb, min_t(u16, IWL_LINK_HDR_MAX, len)) ?
  814. 0 : -ENOMEM;
  815. if (ret) {
  816. kfree_skb(skb);
  817. goto out;
  818. }
  819. /*
  820. * XXX: We cannot touch the page and its virtual memory (hdr) after
  821. * here. It might have already been freed by the above skb change.
  822. */
  823. iwl_update_stats(priv, false, fc, len);
  824. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  825. ieee80211_rx(priv->hw, skb);
  826. out:
  827. priv->alloc_rxb_page--;
  828. rxb->page = NULL;
  829. }
  830. /* This is necessary only for a number of statistics, see the caller. */
  831. static int iwl_is_network_packet(struct iwl_priv *priv,
  832. struct ieee80211_hdr *header)
  833. {
  834. /* Filter incoming packets to determine if they are targeted toward
  835. * this network, discarding packets coming from ourselves */
  836. switch (priv->iw_mode) {
  837. case NL80211_IFTYPE_ADHOC: /* Header: Dest. | Source | BSSID */
  838. /* packets to our IBSS update information */
  839. return !compare_ether_addr(header->addr3, priv->bssid);
  840. case NL80211_IFTYPE_STATION: /* Header: Dest. | AP{BSSID} | Source */
  841. /* packets to our IBSS update information */
  842. return !compare_ether_addr(header->addr2, priv->bssid);
  843. default:
  844. return 1;
  845. }
  846. }
  847. /* Called for REPLY_RX (legacy ABG frames), or
  848. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  849. void iwl_rx_reply_rx(struct iwl_priv *priv,
  850. struct iwl_rx_mem_buffer *rxb)
  851. {
  852. struct ieee80211_hdr *header;
  853. struct ieee80211_rx_status rx_status;
  854. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  855. struct iwl_rx_phy_res *phy_res;
  856. __le32 rx_pkt_status;
  857. struct iwl4965_rx_mpdu_res_start *amsdu;
  858. u32 len;
  859. u32 ampdu_status;
  860. u32 rate_n_flags;
  861. /**
  862. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  863. * REPLY_RX: physical layer info is in this buffer
  864. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  865. * command and cached in priv->last_phy_res
  866. *
  867. * Here we set up local variables depending on which command is
  868. * received.
  869. */
  870. if (pkt->hdr.cmd == REPLY_RX) {
  871. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  872. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  873. + phy_res->cfg_phy_cnt);
  874. len = le16_to_cpu(phy_res->byte_count);
  875. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  876. phy_res->cfg_phy_cnt + len);
  877. ampdu_status = le32_to_cpu(rx_pkt_status);
  878. } else {
  879. if (!priv->last_phy_res[0]) {
  880. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  881. return;
  882. }
  883. phy_res = (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  884. amsdu = (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  885. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  886. len = le16_to_cpu(amsdu->byte_count);
  887. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  888. ampdu_status = iwl_translate_rx_status(priv,
  889. le32_to_cpu(rx_pkt_status));
  890. }
  891. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  892. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  893. phy_res->cfg_phy_cnt);
  894. return;
  895. }
  896. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  897. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  898. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  899. le32_to_cpu(rx_pkt_status));
  900. return;
  901. }
  902. /* This will be used in several places later */
  903. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  904. /* rx_status carries information about the packet to mac80211 */
  905. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  906. rx_status.freq =
  907. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
  908. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  909. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  910. rx_status.rate_idx =
  911. iwl_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  912. rx_status.flag = 0;
  913. /* TSF isn't reliable. In order to allow smooth user experience,
  914. * this W/A doesn't propagate it to the mac80211 */
  915. /*rx_status.flag |= RX_FLAG_TSFT;*/
  916. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  917. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  918. rx_status.signal = iwl_calc_rssi(priv, phy_res);
  919. /* Meaningful noise values are available only from beacon statistics,
  920. * which are gathered only when associated, and indicate noise
  921. * only for the associated network channel ...
  922. * Ignore these noise values while scanning (other channels) */
  923. if (iwl_is_associated(priv) &&
  924. !test_bit(STATUS_SCANNING, &priv->status)) {
  925. rx_status.noise = priv->last_rx_noise;
  926. } else {
  927. rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  928. }
  929. /* Reset beacon noise level if not associated. */
  930. if (!iwl_is_associated(priv))
  931. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  932. #ifdef CONFIG_IWLWIFI_DEBUG
  933. /* Set "1" to report good data frames in groups of 100 */
  934. if (unlikely(iwl_get_debug_level(priv) & IWL_DL_RX))
  935. iwl_dbg_report_frame(priv, phy_res, len, header, 1);
  936. #endif
  937. iwl_dbg_log_rx_data_frame(priv, len, header);
  938. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, noise %d, TSF %llu\n",
  939. rx_status.signal, rx_status.noise,
  940. (unsigned long long)rx_status.mactime);
  941. /*
  942. * "antenna number"
  943. *
  944. * It seems that the antenna field in the phy flags value
  945. * is actually a bit field. This is undefined by radiotap,
  946. * it wants an actual antenna number but I always get "7"
  947. * for most legacy frames I receive indicating that the
  948. * same frame was received on all three RX chains.
  949. *
  950. * I think this field should be removed in favor of a
  951. * new 802.11n radiotap field "RX chains" that is defined
  952. * as a bitmask.
  953. */
  954. rx_status.antenna =
  955. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  956. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  957. /* set the preamble flag if appropriate */
  958. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  959. rx_status.flag |= RX_FLAG_SHORTPRE;
  960. /* Set up the HT phy flags */
  961. if (rate_n_flags & RATE_MCS_HT_MSK)
  962. rx_status.flag |= RX_FLAG_HT;
  963. if (rate_n_flags & RATE_MCS_HT40_MSK)
  964. rx_status.flag |= RX_FLAG_40MHZ;
  965. if (rate_n_flags & RATE_MCS_SGI_MSK)
  966. rx_status.flag |= RX_FLAG_SHORT_GI;
  967. if (iwl_is_network_packet(priv, header)) {
  968. priv->last_rx_rssi = rx_status.signal;
  969. priv->last_beacon_time = priv->ucode_beacon_time;
  970. priv->last_tsf = le64_to_cpu(phy_res->timestamp);
  971. }
  972. iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  973. rxb, &rx_status);
  974. }
  975. EXPORT_SYMBOL(iwl_rx_reply_rx);
  976. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  977. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  978. void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
  979. struct iwl_rx_mem_buffer *rxb)
  980. {
  981. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  982. priv->last_phy_res[0] = 1;
  983. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  984. sizeof(struct iwl_rx_phy_res));
  985. }
  986. EXPORT_SYMBOL(iwl_rx_reply_rx_phy);