iwl-rx.c 37 KB

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