iwl-rx.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 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. * James P. Ketrenos <ipw2100-admin@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. u32 reg = 0;
  126. int ret = 0;
  127. unsigned long flags;
  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_set_bit(priv, CSR_GP_CNTRL,
  136. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  137. goto exit_unlock;
  138. }
  139. ret = iwl_grab_nic_access(priv);
  140. if (ret)
  141. goto exit_unlock;
  142. /* Device expects a multiple of 8 */
  143. iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
  144. 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, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
  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->aligned_dma_addr);
  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 ((write != (rxq->write & ~0x7))
  204. || (abs(rxq->write - rxq->read) > 7)) {
  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. spin_lock_irqsave(&rxq->lock, flags);
  228. while (!list_empty(&rxq->rx_used)) {
  229. element = rxq->rx_used.next;
  230. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  231. /* Alloc a new receive buffer */
  232. rxb->skb = alloc_skb(priv->hw_params.rx_buf_size + 256,
  233. __GFP_NOWARN | GFP_ATOMIC);
  234. if (!rxb->skb) {
  235. if (net_ratelimit())
  236. printk(KERN_CRIT DRV_NAME
  237. ": Can not allocate SKB buffers\n");
  238. /* We don't reschedule replenish work here -- we will
  239. * call the restock method and if it still needs
  240. * more buffers it will schedule replenish */
  241. break;
  242. }
  243. priv->alloc_rxb_skb++;
  244. list_del(element);
  245. /* Get physical address of RB/SKB */
  246. rxb->real_dma_addr = pci_map_single(
  247. priv->pci_dev,
  248. rxb->skb->data,
  249. priv->hw_params.rx_buf_size + 256,
  250. PCI_DMA_FROMDEVICE);
  251. /* dma address must be no more than 36 bits */
  252. BUG_ON(rxb->real_dma_addr & ~DMA_BIT_MASK(36));
  253. /* and also 256 byte aligned! */
  254. rxb->aligned_dma_addr = ALIGN(rxb->real_dma_addr, 256);
  255. skb_reserve(rxb->skb, rxb->aligned_dma_addr - rxb->real_dma_addr);
  256. list_add_tail(&rxb->list, &rxq->rx_free);
  257. rxq->free_count++;
  258. }
  259. spin_unlock_irqrestore(&rxq->lock, flags);
  260. }
  261. EXPORT_SYMBOL(iwl_rx_allocate);
  262. void iwl_rx_replenish(struct iwl_priv *priv)
  263. {
  264. unsigned long flags;
  265. iwl_rx_allocate(priv);
  266. spin_lock_irqsave(&priv->lock, flags);
  267. iwl_rx_queue_restock(priv);
  268. spin_unlock_irqrestore(&priv->lock, flags);
  269. }
  270. EXPORT_SYMBOL(iwl_rx_replenish);
  271. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  272. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  273. * This free routine walks the list of POOL entries and if SKB is set to
  274. * non NULL it is unmapped and freed
  275. */
  276. void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  277. {
  278. int i;
  279. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  280. if (rxq->pool[i].skb != NULL) {
  281. pci_unmap_single(priv->pci_dev,
  282. rxq->pool[i].real_dma_addr,
  283. priv->hw_params.rx_buf_size + 256,
  284. PCI_DMA_FROMDEVICE);
  285. dev_kfree_skb(rxq->pool[i].skb);
  286. }
  287. }
  288. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  289. rxq->dma_addr);
  290. rxq->bd = NULL;
  291. }
  292. EXPORT_SYMBOL(iwl_rx_queue_free);
  293. int iwl_rx_queue_alloc(struct iwl_priv *priv)
  294. {
  295. struct iwl_rx_queue *rxq = &priv->rxq;
  296. struct pci_dev *dev = priv->pci_dev;
  297. int i;
  298. spin_lock_init(&rxq->lock);
  299. INIT_LIST_HEAD(&rxq->rx_free);
  300. INIT_LIST_HEAD(&rxq->rx_used);
  301. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  302. rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
  303. if (!rxq->bd)
  304. return -ENOMEM;
  305. /* Fill the rx_used queue with _all_ of the Rx buffers */
  306. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  307. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  308. /* Set us so that we have processed and used all buffers, but have
  309. * not restocked the Rx queue with fresh buffers */
  310. rxq->read = rxq->write = 0;
  311. rxq->free_count = 0;
  312. rxq->need_update = 0;
  313. return 0;
  314. }
  315. EXPORT_SYMBOL(iwl_rx_queue_alloc);
  316. void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  317. {
  318. unsigned long flags;
  319. int i;
  320. spin_lock_irqsave(&rxq->lock, flags);
  321. INIT_LIST_HEAD(&rxq->rx_free);
  322. INIT_LIST_HEAD(&rxq->rx_used);
  323. /* Fill the rx_used queue with _all_ of the Rx buffers */
  324. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  325. /* In the reset function, these buffers may have been allocated
  326. * to an SKB, so we need to unmap and free potential storage */
  327. if (rxq->pool[i].skb != NULL) {
  328. pci_unmap_single(priv->pci_dev,
  329. rxq->pool[i].real_dma_addr,
  330. priv->hw_params.rx_buf_size + 256,
  331. PCI_DMA_FROMDEVICE);
  332. priv->alloc_rxb_skb--;
  333. dev_kfree_skb(rxq->pool[i].skb);
  334. rxq->pool[i].skb = NULL;
  335. }
  336. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  337. }
  338. /* Set us so that we have processed and used all buffers, but have
  339. * not restocked the Rx queue with fresh buffers */
  340. rxq->read = rxq->write = 0;
  341. rxq->free_count = 0;
  342. spin_unlock_irqrestore(&rxq->lock, flags);
  343. }
  344. EXPORT_SYMBOL(iwl_rx_queue_reset);
  345. int iwl_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  346. {
  347. int ret;
  348. unsigned long flags;
  349. u32 rb_size;
  350. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  351. const u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT why this stalls RX */
  352. spin_lock_irqsave(&priv->lock, flags);
  353. ret = iwl_grab_nic_access(priv);
  354. if (ret) {
  355. spin_unlock_irqrestore(&priv->lock, flags);
  356. return ret;
  357. }
  358. if (priv->cfg->mod_params->amsdu_size_8K)
  359. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  360. else
  361. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  362. /* Stop Rx DMA */
  363. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  364. /* Reset driver's Rx queue write index */
  365. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  366. /* Tell device where to find RBD circular buffer in DRAM */
  367. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  368. (u32)(rxq->dma_addr >> 8));
  369. /* Tell device where in DRAM to update its Rx status */
  370. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  371. (priv->shared_phys + priv->rb_closed_offset) >> 4);
  372. /* Enable Rx DMA
  373. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set becuase of HW bug in
  374. * the credit mechanism in 5000 HW RX FIFO
  375. * Direct rx interrupts to hosts
  376. * Rx buffer size 4 or 8k
  377. * RB timeout 0x10
  378. * 256 RBDs
  379. */
  380. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  381. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  382. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  383. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  384. rb_size|
  385. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  386. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  387. iwl_release_nic_access(priv);
  388. iwl_write32(priv, CSR_INT_COALESCING, 0x40);
  389. spin_unlock_irqrestore(&priv->lock, flags);
  390. return 0;
  391. }
  392. int iwl_rxq_stop(struct iwl_priv *priv)
  393. {
  394. int ret;
  395. unsigned long flags;
  396. spin_lock_irqsave(&priv->lock, flags);
  397. ret = iwl_grab_nic_access(priv);
  398. if (unlikely(ret)) {
  399. spin_unlock_irqrestore(&priv->lock, flags);
  400. return ret;
  401. }
  402. /* stop Rx DMA */
  403. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  404. ret = iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  405. (1 << 24), 1000);
  406. if (ret < 0)
  407. IWL_ERROR("Can't stop Rx DMA.\n");
  408. iwl_release_nic_access(priv);
  409. spin_unlock_irqrestore(&priv->lock, flags);
  410. return 0;
  411. }
  412. EXPORT_SYMBOL(iwl_rxq_stop);
  413. void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
  414. struct iwl_rx_mem_buffer *rxb)
  415. {
  416. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  417. struct iwl4965_missed_beacon_notif *missed_beacon;
  418. missed_beacon = &pkt->u.missed_beacon;
  419. if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
  420. IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  421. le32_to_cpu(missed_beacon->consequtive_missed_beacons),
  422. le32_to_cpu(missed_beacon->total_missed_becons),
  423. le32_to_cpu(missed_beacon->num_recvd_beacons),
  424. le32_to_cpu(missed_beacon->num_expected_beacons));
  425. if (!test_bit(STATUS_SCANNING, &priv->status))
  426. iwl_init_sensitivity(priv);
  427. }
  428. }
  429. EXPORT_SYMBOL(iwl_rx_missed_beacon_notif);
  430. int iwl_rx_agg_start(struct iwl_priv *priv, const u8 *addr, int tid, u16 ssn)
  431. {
  432. unsigned long flags;
  433. int sta_id;
  434. sta_id = iwl_find_station(priv, addr);
  435. if (sta_id == IWL_INVALID_STATION)
  436. return -ENXIO;
  437. spin_lock_irqsave(&priv->sta_lock, flags);
  438. priv->stations[sta_id].sta.station_flags_msk = 0;
  439. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  440. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  441. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  442. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  443. spin_unlock_irqrestore(&priv->sta_lock, flags);
  444. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  445. CMD_ASYNC);
  446. }
  447. EXPORT_SYMBOL(iwl_rx_agg_start);
  448. int iwl_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
  449. {
  450. unsigned long flags;
  451. int sta_id;
  452. sta_id = iwl_find_station(priv, addr);
  453. if (sta_id == IWL_INVALID_STATION)
  454. return -ENXIO;
  455. spin_lock_irqsave(&priv->sta_lock, flags);
  456. priv->stations[sta_id].sta.station_flags_msk = 0;
  457. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  458. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  459. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  460. spin_unlock_irqrestore(&priv->sta_lock, flags);
  461. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  462. CMD_ASYNC);
  463. }
  464. EXPORT_SYMBOL(iwl_rx_agg_stop);
  465. /* Calculate noise level, based on measurements during network silence just
  466. * before arriving beacon. This measurement can be done only if we know
  467. * exactly when to expect beacons, therefore only when we're associated. */
  468. static void iwl_rx_calc_noise(struct iwl_priv *priv)
  469. {
  470. struct statistics_rx_non_phy *rx_info
  471. = &(priv->statistics.rx.general);
  472. int num_active_rx = 0;
  473. int total_silence = 0;
  474. int bcn_silence_a =
  475. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  476. int bcn_silence_b =
  477. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  478. int bcn_silence_c =
  479. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  480. if (bcn_silence_a) {
  481. total_silence += bcn_silence_a;
  482. num_active_rx++;
  483. }
  484. if (bcn_silence_b) {
  485. total_silence += bcn_silence_b;
  486. num_active_rx++;
  487. }
  488. if (bcn_silence_c) {
  489. total_silence += bcn_silence_c;
  490. num_active_rx++;
  491. }
  492. /* Average among active antennas */
  493. if (num_active_rx)
  494. priv->last_rx_noise = (total_silence / num_active_rx) - 107;
  495. else
  496. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  497. IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
  498. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  499. priv->last_rx_noise);
  500. }
  501. #define REG_RECALIB_PERIOD (60)
  502. void iwl_rx_statistics(struct iwl_priv *priv,
  503. struct iwl_rx_mem_buffer *rxb)
  504. {
  505. int change;
  506. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  507. IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
  508. (int)sizeof(priv->statistics), pkt->len);
  509. change = ((priv->statistics.general.temperature !=
  510. pkt->u.stats.general.temperature) ||
  511. ((priv->statistics.flag &
  512. STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
  513. (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
  514. memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
  515. set_bit(STATUS_STATISTICS, &priv->status);
  516. /* Reschedule the statistics timer to occur in
  517. * REG_RECALIB_PERIOD seconds to ensure we get a
  518. * thermal update even if the uCode doesn't give
  519. * us one */
  520. mod_timer(&priv->statistics_periodic, jiffies +
  521. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  522. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  523. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  524. iwl_rx_calc_noise(priv);
  525. queue_work(priv->workqueue, &priv->run_time_calib_work);
  526. }
  527. iwl_leds_background(priv);
  528. if (priv->cfg->ops->lib->temperature && change)
  529. priv->cfg->ops->lib->temperature(priv);
  530. }
  531. EXPORT_SYMBOL(iwl_rx_statistics);
  532. #define PERFECT_RSSI (-20) /* dBm */
  533. #define WORST_RSSI (-95) /* dBm */
  534. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  535. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  536. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  537. * about formulas used below. */
  538. static int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
  539. {
  540. int sig_qual;
  541. int degradation = PERFECT_RSSI - rssi_dbm;
  542. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  543. * as indicator; formula is (signal dbm - noise dbm).
  544. * SNR at or above 40 is a great signal (100%).
  545. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  546. * Weakest usable signal is usually 10 - 15 dB SNR. */
  547. if (noise_dbm) {
  548. if (rssi_dbm - noise_dbm >= 40)
  549. return 100;
  550. else if (rssi_dbm < noise_dbm)
  551. return 0;
  552. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  553. /* Else use just the signal level.
  554. * This formula is a least squares fit of data points collected and
  555. * compared with a reference system that had a percentage (%) display
  556. * for signal quality. */
  557. } else
  558. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  559. (15 * RSSI_RANGE + 62 * degradation)) /
  560. (RSSI_RANGE * RSSI_RANGE);
  561. if (sig_qual > 100)
  562. sig_qual = 100;
  563. else if (sig_qual < 1)
  564. sig_qual = 0;
  565. return sig_qual;
  566. }
  567. #ifdef CONFIG_IWLWIFI_DEBUG
  568. /**
  569. * iwl_dbg_report_frame - dump frame to syslog during debug sessions
  570. *
  571. * You may hack this function to show different aspects of received frames,
  572. * including selective frame dumps.
  573. * group100 parameter selects whether to show 1 out of 100 good frames.
  574. *
  575. * TODO: This was originally written for 3945, need to audit for
  576. * proper operation with 4965.
  577. */
  578. static void iwl_dbg_report_frame(struct iwl_priv *priv,
  579. struct iwl_rx_packet *pkt,
  580. struct ieee80211_hdr *header, int group100)
  581. {
  582. u32 to_us;
  583. u32 print_summary = 0;
  584. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  585. u32 hundred = 0;
  586. u32 dataframe = 0;
  587. __le16 fc;
  588. u16 seq_ctl;
  589. u16 channel;
  590. u16 phy_flags;
  591. int rate_sym;
  592. u16 length;
  593. u16 status;
  594. u16 bcn_tmr;
  595. u32 tsf_low;
  596. u64 tsf;
  597. u8 rssi;
  598. u8 agc;
  599. u16 sig_avg;
  600. u16 noise_diff;
  601. struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
  602. struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
  603. struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
  604. u8 *data = IWL_RX_DATA(pkt);
  605. if (likely(!(priv->debug_level & IWL_DL_RX)))
  606. return;
  607. /* MAC header */
  608. fc = header->frame_control;
  609. seq_ctl = le16_to_cpu(header->seq_ctrl);
  610. /* metadata */
  611. channel = le16_to_cpu(rx_hdr->channel);
  612. phy_flags = le16_to_cpu(rx_hdr->phy_flags);
  613. rate_sym = rx_hdr->rate;
  614. length = le16_to_cpu(rx_hdr->len);
  615. /* end-of-frame status and timestamp */
  616. status = le32_to_cpu(rx_end->status);
  617. bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
  618. tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
  619. tsf = le64_to_cpu(rx_end->timestamp);
  620. /* signal statistics */
  621. rssi = rx_stats->rssi;
  622. agc = rx_stats->agc;
  623. sig_avg = le16_to_cpu(rx_stats->sig_avg);
  624. noise_diff = le16_to_cpu(rx_stats->noise_diff);
  625. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  626. /* if data frame is to us and all is good,
  627. * (optionally) print summary for only 1 out of every 100 */
  628. if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
  629. cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  630. dataframe = 1;
  631. if (!group100)
  632. print_summary = 1; /* print each frame */
  633. else if (priv->framecnt_to_us < 100) {
  634. priv->framecnt_to_us++;
  635. print_summary = 0;
  636. } else {
  637. priv->framecnt_to_us = 0;
  638. print_summary = 1;
  639. hundred = 1;
  640. }
  641. } else {
  642. /* print summary for all other frames */
  643. print_summary = 1;
  644. }
  645. if (print_summary) {
  646. char *title;
  647. int rate_idx;
  648. u32 bitrate;
  649. if (hundred)
  650. title = "100Frames";
  651. else if (ieee80211_has_retry(fc))
  652. title = "Retry";
  653. else if (ieee80211_is_assoc_resp(fc))
  654. title = "AscRsp";
  655. else if (ieee80211_is_reassoc_resp(fc))
  656. title = "RasRsp";
  657. else if (ieee80211_is_probe_resp(fc)) {
  658. title = "PrbRsp";
  659. print_dump = 1; /* dump frame contents */
  660. } else if (ieee80211_is_beacon(fc)) {
  661. title = "Beacon";
  662. print_dump = 1; /* dump frame contents */
  663. } else if (ieee80211_is_atim(fc))
  664. title = "ATIM";
  665. else if (ieee80211_is_auth(fc))
  666. title = "Auth";
  667. else if (ieee80211_is_deauth(fc))
  668. title = "DeAuth";
  669. else if (ieee80211_is_disassoc(fc))
  670. title = "DisAssoc";
  671. else
  672. title = "Frame";
  673. rate_idx = iwl_hwrate_to_plcp_idx(rate_sym);
  674. if (unlikely(rate_idx == -1))
  675. bitrate = 0;
  676. else
  677. bitrate = iwl_rates[rate_idx].ieee / 2;
  678. /* print frame summary.
  679. * MAC addresses show just the last byte (for brevity),
  680. * but you can hack it to show more, if you'd like to. */
  681. if (dataframe)
  682. IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
  683. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  684. title, le16_to_cpu(fc), header->addr1[5],
  685. length, rssi, channel, bitrate);
  686. else {
  687. /* src/dst addresses assume managed mode */
  688. IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
  689. "src=0x%02x, rssi=%u, tim=%lu usec, "
  690. "phy=0x%02x, chnl=%d\n",
  691. title, le16_to_cpu(fc), header->addr1[5],
  692. header->addr3[5], rssi,
  693. tsf_low - priv->scan_start_tsf,
  694. phy_flags, channel);
  695. }
  696. }
  697. if (print_dump)
  698. iwl_print_hex_dump(priv, IWL_DL_RX, data, length);
  699. }
  700. #else
  701. static inline void iwl_dbg_report_frame(struct iwl_priv *priv,
  702. struct iwl_rx_packet *pkt,
  703. struct ieee80211_hdr *header,
  704. int group100)
  705. {
  706. }
  707. #endif
  708. static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  709. {
  710. /* 0 - mgmt, 1 - cnt, 2 - data */
  711. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  712. priv->rx_stats[idx].cnt++;
  713. priv->rx_stats[idx].bytes += len;
  714. }
  715. /*
  716. * returns non-zero if packet should be dropped
  717. */
  718. static int iwl_set_decrypted_flag(struct iwl_priv *priv,
  719. struct ieee80211_hdr *hdr,
  720. u32 decrypt_res,
  721. struct ieee80211_rx_status *stats)
  722. {
  723. u16 fc = le16_to_cpu(hdr->frame_control);
  724. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  725. return 0;
  726. if (!(fc & IEEE80211_FCTL_PROTECTED))
  727. return 0;
  728. IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
  729. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  730. case RX_RES_STATUS_SEC_TYPE_TKIP:
  731. /* The uCode has got a bad phase 1 Key, pushes the packet.
  732. * Decryption will be done in SW. */
  733. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  734. RX_RES_STATUS_BAD_KEY_TTAK)
  735. break;
  736. case RX_RES_STATUS_SEC_TYPE_WEP:
  737. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  738. RX_RES_STATUS_BAD_ICV_MIC) {
  739. /* bad ICV, the packet is destroyed since the
  740. * decryption is inplace, drop it */
  741. IWL_DEBUG_RX("Packet destroyed\n");
  742. return -1;
  743. }
  744. case RX_RES_STATUS_SEC_TYPE_CCMP:
  745. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  746. RX_RES_STATUS_DECRYPT_OK) {
  747. IWL_DEBUG_RX("hw decrypt successfully!!!\n");
  748. stats->flag |= RX_FLAG_DECRYPTED;
  749. }
  750. break;
  751. default:
  752. break;
  753. }
  754. return 0;
  755. }
  756. static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  757. {
  758. u32 decrypt_out = 0;
  759. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  760. RX_RES_STATUS_STATION_FOUND)
  761. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  762. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  763. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  764. /* packet was not encrypted */
  765. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  766. RX_RES_STATUS_SEC_TYPE_NONE)
  767. return decrypt_out;
  768. /* packet was encrypted with unknown alg */
  769. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  770. RX_RES_STATUS_SEC_TYPE_ERR)
  771. return decrypt_out;
  772. /* decryption was not done in HW */
  773. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  774. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  775. return decrypt_out;
  776. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  777. case RX_RES_STATUS_SEC_TYPE_CCMP:
  778. /* alg is CCM: check MIC only */
  779. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  780. /* Bad MIC */
  781. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  782. else
  783. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  784. break;
  785. case RX_RES_STATUS_SEC_TYPE_TKIP:
  786. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  787. /* Bad TTAK */
  788. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  789. break;
  790. }
  791. /* fall through if TTAK OK */
  792. default:
  793. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  794. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  795. else
  796. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  797. break;
  798. };
  799. IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
  800. decrypt_in, decrypt_out);
  801. return decrypt_out;
  802. }
  803. static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
  804. int include_phy,
  805. struct iwl_rx_mem_buffer *rxb,
  806. struct ieee80211_rx_status *stats)
  807. {
  808. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  809. struct iwl_rx_phy_res *rx_start = (include_phy) ?
  810. (struct iwl_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
  811. struct ieee80211_hdr *hdr;
  812. u16 len;
  813. __le32 *rx_end;
  814. unsigned int skblen;
  815. u32 ampdu_status;
  816. u32 ampdu_status_legacy;
  817. if (!include_phy && priv->last_phy_res[0])
  818. rx_start = (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  819. if (!rx_start) {
  820. IWL_ERROR("MPDU frame without a PHY data\n");
  821. return;
  822. }
  823. if (include_phy) {
  824. hdr = (struct ieee80211_hdr *)((u8 *) &rx_start[1] +
  825. rx_start->cfg_phy_cnt);
  826. len = le16_to_cpu(rx_start->byte_count);
  827. rx_end = (__le32 *)((u8 *) &pkt->u.raw[0] +
  828. sizeof(struct iwl_rx_phy_res) +
  829. rx_start->cfg_phy_cnt + len);
  830. } else {
  831. struct iwl4965_rx_mpdu_res_start *amsdu =
  832. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  833. hdr = (struct ieee80211_hdr *)(pkt->u.raw +
  834. sizeof(struct iwl4965_rx_mpdu_res_start));
  835. len = le16_to_cpu(amsdu->byte_count);
  836. rx_start->byte_count = amsdu->byte_count;
  837. rx_end = (__le32 *) (((u8 *) hdr) + len);
  838. }
  839. ampdu_status = le32_to_cpu(*rx_end);
  840. skblen = ((u8 *) rx_end - (u8 *) &pkt->u.raw[0]) + sizeof(u32);
  841. if (!include_phy) {
  842. /* New status scheme, need to translate */
  843. ampdu_status_legacy = ampdu_status;
  844. ampdu_status = iwl_translate_rx_status(priv, ampdu_status);
  845. }
  846. /* start from MAC */
  847. skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
  848. skb_put(rxb->skb, len); /* end where data ends */
  849. /* We only process data packets if the interface is open */
  850. if (unlikely(!priv->is_open)) {
  851. IWL_DEBUG_DROP_LIMIT
  852. ("Dropping packet while interface is not open.\n");
  853. return;
  854. }
  855. hdr = (struct ieee80211_hdr *)rxb->skb->data;
  856. /* in case of HW accelerated crypto and bad decryption, drop */
  857. if (!priv->hw_params.sw_crypto &&
  858. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  859. return;
  860. iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
  861. ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
  862. priv->alloc_rxb_skb--;
  863. rxb->skb = NULL;
  864. }
  865. /* Calc max signal level (dBm) among 3 possible receivers */
  866. static inline int iwl_calc_rssi(struct iwl_priv *priv,
  867. struct iwl_rx_phy_res *rx_resp)
  868. {
  869. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  870. }
  871. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  872. {
  873. unsigned long flags;
  874. spin_lock_irqsave(&priv->sta_lock, flags);
  875. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  876. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  877. priv->stations[sta_id].sta.sta.modify_mask = 0;
  878. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  879. spin_unlock_irqrestore(&priv->sta_lock, flags);
  880. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  881. }
  882. static void iwl_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
  883. {
  884. /* FIXME: need locking over ps_status ??? */
  885. u8 sta_id = iwl_find_station(priv, addr);
  886. if (sta_id != IWL_INVALID_STATION) {
  887. u8 sta_awake = priv->stations[sta_id].
  888. ps_status == STA_PS_STATUS_WAKE;
  889. if (sta_awake && ps_bit)
  890. priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
  891. else if (!sta_awake && !ps_bit) {
  892. iwl_sta_modify_ps_wake(priv, sta_id);
  893. priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
  894. }
  895. }
  896. }
  897. /* This is necessary only for a number of statistics, see the caller. */
  898. static int iwl_is_network_packet(struct iwl_priv *priv,
  899. struct ieee80211_hdr *header)
  900. {
  901. /* Filter incoming packets to determine if they are targeted toward
  902. * this network, discarding packets coming from ourselves */
  903. switch (priv->iw_mode) {
  904. case NL80211_IFTYPE_ADHOC: /* Header: Dest. | Source | BSSID */
  905. /* packets to our IBSS update information */
  906. return !compare_ether_addr(header->addr3, priv->bssid);
  907. case NL80211_IFTYPE_STATION: /* Header: Dest. | AP{BSSID} | Source */
  908. /* packets to our IBSS update information */
  909. return !compare_ether_addr(header->addr2, priv->bssid);
  910. default:
  911. return 1;
  912. }
  913. }
  914. /* Called for REPLY_RX (legacy ABG frames), or
  915. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  916. void iwl_rx_reply_rx(struct iwl_priv *priv,
  917. struct iwl_rx_mem_buffer *rxb)
  918. {
  919. struct ieee80211_hdr *header;
  920. struct ieee80211_rx_status rx_status;
  921. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  922. /* Use phy data (Rx signal strength, etc.) contained within
  923. * this rx packet for legacy frames,
  924. * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
  925. int include_phy = (pkt->hdr.cmd == REPLY_RX);
  926. struct iwl_rx_phy_res *rx_start = (include_phy) ?
  927. (struct iwl_rx_phy_res *)&(pkt->u.raw[0]) :
  928. (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  929. __le32 *rx_end;
  930. unsigned int len = 0;
  931. u16 fc;
  932. u8 network_packet;
  933. rx_status.mactime = le64_to_cpu(rx_start->timestamp);
  934. rx_status.freq =
  935. ieee80211_channel_to_frequency(le16_to_cpu(rx_start->channel));
  936. rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  937. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  938. rx_status.rate_idx =
  939. iwl_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
  940. if (rx_status.band == IEEE80211_BAND_5GHZ)
  941. rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
  942. rx_status.flag = 0;
  943. /* TSF isn't reliable. In order to allow smooth user experience,
  944. * this W/A doesn't propagate it to the mac80211 */
  945. /*rx_status.flag |= RX_FLAG_TSFT;*/
  946. if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
  947. IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
  948. rx_start->cfg_phy_cnt);
  949. return;
  950. }
  951. if (!include_phy) {
  952. if (priv->last_phy_res[0])
  953. rx_start = (struct iwl_rx_phy_res *)
  954. &priv->last_phy_res[1];
  955. else
  956. rx_start = NULL;
  957. }
  958. if (!rx_start) {
  959. IWL_ERROR("MPDU frame without a PHY data\n");
  960. return;
  961. }
  962. if (include_phy) {
  963. header = (struct ieee80211_hdr *)((u8 *) &rx_start[1]
  964. + rx_start->cfg_phy_cnt);
  965. len = le16_to_cpu(rx_start->byte_count);
  966. rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
  967. sizeof(struct iwl_rx_phy_res) + len);
  968. } else {
  969. struct iwl4965_rx_mpdu_res_start *amsdu =
  970. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  971. header = (void *)(pkt->u.raw +
  972. sizeof(struct iwl4965_rx_mpdu_res_start));
  973. len = le16_to_cpu(amsdu->byte_count);
  974. rx_end = (__le32 *) (pkt->u.raw +
  975. sizeof(struct iwl4965_rx_mpdu_res_start) + len);
  976. }
  977. if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
  978. !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  979. IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
  980. le32_to_cpu(*rx_end));
  981. return;
  982. }
  983. priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
  984. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  985. rx_status.signal = iwl_calc_rssi(priv, rx_start);
  986. /* Meaningful noise values are available only from beacon statistics,
  987. * which are gathered only when associated, and indicate noise
  988. * only for the associated network channel ...
  989. * Ignore these noise values while scanning (other channels) */
  990. if (iwl_is_associated(priv) &&
  991. !test_bit(STATUS_SCANNING, &priv->status)) {
  992. rx_status.noise = priv->last_rx_noise;
  993. rx_status.qual = iwl_calc_sig_qual(rx_status.signal,
  994. rx_status.noise);
  995. } else {
  996. rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  997. rx_status.qual = iwl_calc_sig_qual(rx_status.signal, 0);
  998. }
  999. /* Reset beacon noise level if not associated. */
  1000. if (!iwl_is_associated(priv))
  1001. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  1002. /* Set "1" to report good data frames in groups of 100 */
  1003. /* FIXME: need to optimze the call: */
  1004. iwl_dbg_report_frame(priv, pkt, header, 1);
  1005. IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
  1006. rx_status.signal, rx_status.noise, rx_status.signal,
  1007. (unsigned long long)rx_status.mactime);
  1008. /*
  1009. * "antenna number"
  1010. *
  1011. * It seems that the antenna field in the phy flags value
  1012. * is actually a bitfield. This is undefined by radiotap,
  1013. * it wants an actual antenna number but I always get "7"
  1014. * for most legacy frames I receive indicating that the
  1015. * same frame was received on all three RX chains.
  1016. *
  1017. * I think this field should be removed in favour of a
  1018. * new 802.11n radiotap field "RX chains" that is defined
  1019. * as a bitmask.
  1020. */
  1021. rx_status.antenna = le16_to_cpu(rx_start->phy_flags &
  1022. RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
  1023. /* set the preamble flag if appropriate */
  1024. if (rx_start->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  1025. rx_status.flag |= RX_FLAG_SHORTPRE;
  1026. /* Take shortcut when only in monitor mode */
  1027. if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
  1028. iwl_pass_packet_to_mac80211(priv, include_phy,
  1029. rxb, &rx_status);
  1030. return;
  1031. }
  1032. network_packet = iwl_is_network_packet(priv, header);
  1033. if (network_packet) {
  1034. priv->last_rx_rssi = rx_status.signal;
  1035. priv->last_beacon_time = priv->ucode_beacon_time;
  1036. priv->last_tsf = le64_to_cpu(rx_start->timestamp);
  1037. }
  1038. fc = le16_to_cpu(header->frame_control);
  1039. switch (fc & IEEE80211_FCTL_FTYPE) {
  1040. case IEEE80211_FTYPE_MGMT:
  1041. case IEEE80211_FTYPE_DATA:
  1042. if (priv->iw_mode == NL80211_IFTYPE_AP)
  1043. iwl_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  1044. header->addr2);
  1045. /* fall through */
  1046. default:
  1047. iwl_pass_packet_to_mac80211(priv, include_phy, rxb,
  1048. &rx_status);
  1049. break;
  1050. }
  1051. }
  1052. EXPORT_SYMBOL(iwl_rx_reply_rx);
  1053. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  1054. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  1055. void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
  1056. struct iwl_rx_mem_buffer *rxb)
  1057. {
  1058. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1059. priv->last_phy_res[0] = 1;
  1060. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  1061. sizeof(struct iwl_rx_phy_res));
  1062. }
  1063. EXPORT_SYMBOL(iwl_rx_reply_rx_phy);